using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Collections; using System.Text; using System.Windows.Forms; using GZTW.AyaNova.BLL; using Infragistics.Win; namespace AyaNovaOL { public partial class ScheduleMarkerForm : Form { public ScheduleMarkerForm() { InitializeComponent(); } #region Form Load / Close //variable to hold user form settings //UIUserFormSetting mFormSetting; public bool bHideFollowObjectButton = false; /// /// Prepare the form /// /// /// private void ScheduleMarkerForm_Load(object sender, System.EventArgs e) { //case 1039 //log.Debug("ScheduleMarkerForm_Load"); Cursor.Current = Cursors.WaitCursor; this.Visible = false; this.Icon = Resource1.ScheduleMarker16icon; //See if user has any rights to be here... //(this is done here to ensure that no matter what happens if a user has no rights they can't get here //useful in case we miss a way of getting to a Region edit screen from elsewhere in the program) if (AyaBizUtils.Right("Object.ScheduleMarker") < (int)SecurityLevelTypes.ReadOnly)//Less than read only instead of NoAccess to catch records where it's zero instead of 1 { //Inform them of their wicked ways and boot them out of here... MessageBox.Show(string.Format( Util.LocaleText.GetLocalizedText("Error.Security.NotAuthorizedToRetrieve"), Util.LocaleText.GetLocalizedText("O.ScheduleMarker"))); this.Close(); return; } //Set locale input format for dates dtStartDate.MaskInput = Util.LocaleDateTimeMask(); dtStopDate.MaskInput = Util.LocaleDateTimeMask(); InitializeComboBoxes(); DataBind(); Util.Localize(this); Cursor.Current = Cursors.Default; //Case 152 (new TabOrderManager(this)).SetTabOrder(TabOrderManager.TabScheme.AcrossFirst); //restrict UI based on security settings RestrictUI(); if (mScheduleMarker.FollowID != Guid.Empty) this.Text = Util.LocaleText.GetLocalizedText("ScheduleMarker.Label.FollowUp"); this.Visible = true; } /// /// Used to track whether the close was done by the program /// or done by the user clicking on the upper left corner X control /// bool bClosingHandled = false; /// /// Save form layout and grid layout for next time /// /// /// private void ScheduleMarkerForm_Closing(object sender, System.ComponentModel.CancelEventArgs e) { if (!bClosingHandled && (AyaBizUtils.Right("Object.ScheduleMarker") > (int)SecurityLevelTypes.ReadOnly)) { //Save record if necessary //User may opt to not cancel exit if (!RecordUpdate(RecordActionType.PromptToSave)) { e.Cancel = true; return; } } } #endregion #region Form UI / Business object synchronization /// /// Adapt UI to accomodate users security access level /// by default form is set with full rights in mind /// so this code just needs to restrict them as required /// private void RestrictUI() { SecurityLevelTypes right = (SecurityLevelTypes)AyaBizUtils.Right("Object.ScheduleMarker"); bool bReadOnly = right < SecurityLevelTypes.ReadWrite; edName.Enabled = !bReadOnly; edNotes.Enabled = !bReadOnly; cbScheduleMarkerSourceID.Enabled = !bReadOnly; cpColor.Enabled = !bReadOnly; dtStartDate.Enabled = !bReadOnly; dtStopDate.Enabled = !bReadOnly; ckCompleted.Enabled = !bReadOnly; bool bshow = !bHideFollowObjectButton; bshow = bshow && mScheduleMarker.FollowID != Guid.Empty; btnOpenFollowObject.Visible = bshow; if (bshow) btnOpenFollowObject.Text = NameFetcher.GetItem(new TypeAndID(mScheduleMarker.FollowType, mScheduleMarker.FollowID)).RecordName; //case 1208 if (AyaBizUtils.Lite) cbScheduleMarkerSourceID.Enabled = false; } #endregion #region Form DataBinding and BrokenRules handling /// /// Bind controls to business objects /// private void DataBind() { //case 1039 //log.Debug("DataBind"); //Subscribe to broken rules changed event OnBrokenRulesChanged mScheduleMarker.GetBrokenRulesCollection().ListChanged += new System.ComponentModel.ListChangedEventHandler(OnBrokenRulesChanged); //=-=-=-=-=-=- Form Field bindings =-=-=-=-=-=-=-=-=-=-=-= //Text editors Util.BindField(edName, "Text", mScheduleMarker, "Name"); Util.BindField(edNotes, "Text", mScheduleMarker, "Notes"); //Date and time Util.BindField(dtStartDate, "Value", mScheduleMarker, "StartDate"); Util.BindField(dtStopDate, "Value", mScheduleMarker, "StopDate"); //Color which can't be bound but can be set here and in //the save function cpColor.Color = Color.FromArgb(mScheduleMarker.ARGB); //Check boxes Util.BindField(ckCompleted, "Checked", mScheduleMarker, "Completed"); //=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //Bind IsDirty property to hidden checkbox to //ensure isdirtychanged event is available //to data binding infrastructure Util.BindField(ckIsDirty, "Checked", mScheduleMarker, "IsDirty"); //Setup initial link to broken rules OnBrokenRulesChanged(null, null); } /// /// Handle a change in broken rules /// /// /// private void OnBrokenRulesChanged(object sender, System.ComponentModel.ListChangedEventArgs e) { Util.BrokenRuleFeedback(this, mScheduleMarker, epSchedMarker); } #endregion #region Business object editing / adding / deletion related code private GZTW.AyaNova.BLL.ScheduleMarker mScheduleMarker; /// /// ScheduleMarker record to display /// set by caller /// public GZTW.AyaNova.BLL.ScheduleMarker ScheduleMarkerToEdit { get { return mScheduleMarker; } set { mScheduleMarker = value; //mScheduleMarker.BeginEdit(); } } //Used to signal to main form that there are changes //which could affect it if true private bool mbChangesMade = false; public bool ChangesMade { get { return mbChangesMade; } } /// /// Update record and quit if requested /// /// /// True if handled, false if not handled private bool RecordUpdate(RecordActionType SaveType) { //case 1039 //if(log.IsDebugEnabled) //case 1039 //log.Debug("SaveHandler(Action="+SaveType.ToString()+")"); //switch the focus to a non editable control //so that any changes in the current editable control //will get updated before attempting to save lblScheduleMarkerSourceID.Focus(); //Bind data from unbindable controls cbScheduleMarkerSourceID_ValueChanged(null, null); mScheduleMarker.ARGB = cpColor.Color.ToArgb(); switch (SaveType) { case RecordActionType.SaveAndExit: //Save if necessary and exit if (mScheduleMarker.IsSavable) { mScheduleMarker.ApplyEdit(); mScheduleMarker.Save(); bClosingHandled = true; mbChangesMade = true; this.Close(); return true; } if (mScheduleMarker.IsDirty)//dirty and unsaveable due to broken rules { if (Util.PromptForBrokenRulesCancelSave() == DialogResult.Yes) { bClosingHandled = true; this.Close(); return true; } else return false; } //not dirty so just exit bClosingHandled = true; this.Close(); return true; case RecordActionType.SaveAndNew: if (mScheduleMarker.IsSavable) { mScheduleMarker.ApplyEdit(); mScheduleMarker.Save(); AddNewScheduleMarker(); mbChangesMade = true; return true; } if (mScheduleMarker.IsDirty)//dirty and unsaveable due to broken rules { //User says that's ok, continue on? if (Util.PromptForBrokenRulesCancelSave() == DialogResult.Yes) { //Ok, add new Region and don't save old record AddNewScheduleMarker(); return true; } else return false; } //Current record isn't dirty so just add a new one AddNewScheduleMarker(); return true; case RecordActionType.SaveOnly: if (mScheduleMarker.IsSavable) { mScheduleMarker.ApplyEdit(); mScheduleMarker = (GZTW.AyaNova.BLL.ScheduleMarker)mScheduleMarker.Save(); DataBind(); mbChangesMade = true; return true; } return true; case RecordActionType.PromptToSave: //Prompt to save and save if //required if (mScheduleMarker.IsDirty) { DialogResult dr = Util.PromptForSave(); if (dr == DialogResult.Cancel) { //Cancel return false; } if (dr == DialogResult.Yes) { //Save before exit if (mScheduleMarker.IsSavable) { mScheduleMarker.ApplyEdit(); mScheduleMarker.Save(); mbChangesMade = true; return true; } if (mScheduleMarker.IsDirty)//dirty and unsaveable due to broken rules { if (Util.PromptForBrokenRulesCancelSave() == DialogResult.Yes) { return true; } else return false; } } } return true; } return false; } /// /// ///Creates a new ScheduleMarker record and opens it for editing /// private void AddNewScheduleMarker() { //case 1039 //log.Debug("AddNewScheduleMarker"); try { mScheduleMarker = GZTW.AyaNova.BLL.ScheduleMarker.NewItem(); //mScheduleMarker.BeginEdit(); DataBind(); } catch (Exception ex) { //log.Error("AddNewScheduleMarker",ex); MessageBox.Show(ex.Message + "\r\n" + ex.Source); } } #endregion #region Form ComboBoxes (Setup / Events) //This combo can't be bound because it's got more than just the ID in it's value column private void cbScheduleMarkerSourceID_ValueChanged(object sender, System.EventArgs e) { if (cbScheduleMarkerSourceID.SelectedItem == null) return; TypeAndID ti = (TypeAndID)cbScheduleMarkerSourceID.SelectedItem.DataValue; if (mScheduleMarker.SourceID != ti.ID) { mScheduleMarker.SourceID = ti.ID; switch (ti.RootObjectType) { case RootObjectTypes.Global: mScheduleMarker.ScheduleMarkerSourceType = ScheduleMarkerSourceTypes.Global; break; case RootObjectTypes.Region: mScheduleMarker.ScheduleMarkerSourceType = ScheduleMarkerSourceTypes.Regional; break; case RootObjectTypes.User: mScheduleMarker.ScheduleMarkerSourceType = ScheduleMarkerSourceTypes.User; break; case RootObjectTypes.DispatchZone: mScheduleMarker.ScheduleMarkerSourceType = ScheduleMarkerSourceTypes.DispatchZone; break; case RootObjectTypes.ScheduleableUserGroup: mScheduleMarker.ScheduleMarkerSourceType = ScheduleMarkerSourceTypes.ScheduleableUserGroup; break; } } } private void InitializeComboBoxes() { if (mScheduleMarker.FollowID == Guid.Empty) { //Load combo boxes with initial values //Global object cbScheduleMarkerSourceID.Items.Add(new TypeAndID(RootObjectTypes.Global, ScheduleMarker.ScheduleMarkerGlobalSourceID), Util.LocaleText.GetLocalizedText("O.Global")); //Regions... //NVCHANGED GenericNVList l = GenericNVList.GetList("aRegion", "aID", "aName", true, false, true); //Loop through the items in the list and put them //into the valuelist for (int x = 0; x < l.Count; x++) { cbScheduleMarkerSourceID.Items.Add(new TypeAndID(RootObjectTypes.Region, new Guid(((DictionaryEntry)l.BindableList[x]).Key.ToString())), Util.LocaleText.GetLocalizedText("O.Region") + " - " + ((DictionaryEntry)l.BindableList[x]).Value.ToString()); } } //*************************************************************** //Scheduleable users.... UserListScheduleable mUserList = UserListScheduleable.GetList(); foreach (UserListScheduleable.UserListScheduleableInfo ui in mUserList) { if (ui.Active == true) { cbScheduleMarkerSourceID.Items.Add(new TypeAndID(RootObjectTypes.User, ui.ID), Util.LocaleText.GetLocalizedText("O.User") + " - " + ui.Name(Util.GlobalSettings.DefaultScheduleableUserNameDisplayFormat)); } } //INACTIVE USER? (Only a user can be inactive) //Is it a user? if (mScheduleMarker.ScheduleMarkerSourceType == ScheduleMarkerSourceTypes.User) { //Get that user record from the list we already have foreach (UserListScheduleable.UserListScheduleableInfo ui in mUserList) { if (ui.ID == mScheduleMarker.SourceID) { if (ui.Active == true) break; //if active then it's already in the list else { //found but not active so add to list as In-Active item ValueListItem vi = cbScheduleMarkerSourceID.Items.Add(new TypeAndID(RootObjectTypes.User, ui.ID), Util.LocaleText.GetLocalizedText("O.User") + " - " + ui.Name(Util.GlobalSettings.DefaultScheduleableUserNameDisplayFormat)); vi.Appearance.ForeColor = System.Drawing.SystemColors.GrayText; } } } } //***************************************************************************** if (mScheduleMarker.FollowID == Guid.Empty) { //*************************************************************** //Dispatch zones DispatchZonePickList DispatchZoneList = DispatchZonePickList.GetList(true); foreach (DispatchZonePickList.DispatchZonePickListInfo di in DispatchZoneList) { if (di.Active == true) { cbScheduleMarkerSourceID.Items.Add(new TypeAndID(RootObjectTypes.DispatchZone, di.ID), Util.LocaleText.GetLocalizedText("O.DispatchZone") + " - " + di.Name); } } //INACTIVE ZONE? //Is it a Zone? if (mScheduleMarker.ScheduleMarkerSourceType == ScheduleMarkerSourceTypes.DispatchZone) { //Get that zone record from the list we already have foreach (DispatchZonePickList.DispatchZonePickListInfo di in DispatchZoneList) { if (di.ID == mScheduleMarker.SourceID) { if (di.Active == true) break; //if active then it's already in the list else { //found but not active so add to list as In-Active item ValueListItem vi = cbScheduleMarkerSourceID.Items.Add(new TypeAndID(RootObjectTypes.DispatchZone, di.ID), Util.LocaleText.GetLocalizedText("O.DispatchZone") + " - " + di.Name); vi.Appearance.ForeColor = System.Drawing.SystemColors.GrayText; } } } } //***************************************************************************** //*************************************************************** //Scheduleable user GROUPS.... ScheduleableUserGroupPickList GroupList = ScheduleableUserGroupPickList.GetList(); foreach (ScheduleableUserGroupPickList.ScheduleableUserGroupPickListInfo gi in GroupList) { if (gi.Active == true) { cbScheduleMarkerSourceID.Items.Add(new TypeAndID(RootObjectTypes.ScheduleableUserGroup, gi.ID), Util.LocaleText.GetLocalizedText("O.ScheduleableUserGroup") + " - " + gi.Name); } } //INACTIVE ScheduleableUserGroup? //Is it a user? if (mScheduleMarker.ScheduleMarkerSourceType == ScheduleMarkerSourceTypes.ScheduleableUserGroup) { //Get that ScheduleableUserGroup record from the list we already have foreach (ScheduleableUserGroupPickList.ScheduleableUserGroupPickListInfo gi in GroupList) { if (gi.ID == mScheduleMarker.SourceID) { if (gi.Active == true) break; //if active then it's already in the list else { //found but not active so add to list as In-Active item ValueListItem vi = cbScheduleMarkerSourceID.Items.Add(new TypeAndID(RootObjectTypes.ScheduleableUserGroup, gi.ID), Util.LocaleText.GetLocalizedText("O.ScheduleableUserGroup") + " - " + gi.Name); vi.Appearance.ForeColor = System.Drawing.SystemColors.GrayText; } } } } //***************************************************************************** } //Select initial value because combo box can't be bound as normal foreach (ValueListItem vi in cbScheduleMarkerSourceID.Items) { if (((TypeAndID)vi.DataValue).ID == mScheduleMarker.SourceID) { cbScheduleMarkerSourceID.SelectedItem = vi; break; } } } #endregion private void btnOK_Click(object sender, EventArgs e) { RecordUpdate(RecordActionType.SaveAndExit); } //----------------- }//class }//ns