public void CreateControls(FileDatabase database, DataEntryHandler dataEntryPropagator) { // Depending on how the user interacts with the file import process image set loading can be aborted after controls are generated and then // another image set loaded. Any existing controls therefore need to be cleared. this.ControlGrid.Children.Clear(); this.Controls.Clear(); this.ControlsByDataLabel.Clear(); DataEntryDateTime dateTimeControl = null; DataEntryUtcOffset utcOffsetControl = null; List<DataEntryControl> visibleControls = new List<DataEntryControl>(); foreach (ControlRow control in database.Controls) { // no point in generating a control if it doesn't render in the UX if (control.Visible == false) { continue; } if (control.Type == Constant.DatabaseColumn.DateTime) { dateTimeControl = new DataEntryDateTime(control, this); visibleControls.Add(dateTimeControl); } else if (control.Type == Constant.DatabaseColumn.File || control.Type == Constant.DatabaseColumn.RelativePath || control.Type == Constant.Control.Note) { // standard controls rendering as notes aren't editable by the user List<string> autocompletions = null; bool readOnly = control.Type != Constant.Control.Note; if (readOnly == false) { autocompletions = new List<string>(database.GetDistinctValuesInFileDataColumn(control.DataLabel)); } DataEntryNote noteControl = new DataEntryNote(control, autocompletions, this); noteControl.ContentReadOnly = readOnly; visibleControls.Add(noteControl); } else if (control.Type == Constant.Control.Flag || control.Type == Constant.DatabaseColumn.DeleteFlag) { DataEntryFlag flagControl = new DataEntryFlag(control, this); visibleControls.Add(flagControl); } else if (control.Type == Constant.Control.Counter) { DataEntryCounter counterControl = new DataEntryCounter(control, this); visibleControls.Add(counterControl); } else if (control.Type == Constant.Control.FixedChoice || control.Type == Constant.DatabaseColumn.ImageQuality) { DataEntryChoice choiceControl = new DataEntryChoice(control, this); visibleControls.Add(choiceControl); } else if (control.Type == Constant.DatabaseColumn.UtcOffset) { utcOffsetControl = new DataEntryUtcOffset(control, this); visibleControls.Add(utcOffsetControl); } else { Debug.Fail(String.Format("Unhandled control type {0}.", control.Type)); continue; } } if ((dateTimeControl != null) && (utcOffsetControl != null)) { dateTimeControl.ShowUtcOffset(); visibleControls.Remove(utcOffsetControl); } foreach (DataEntryControl control in visibleControls) { this.ControlGrid.Children.Add(control.Container); this.Controls.Add(control); this.ControlsByDataLabel.Add(control.DataLabel, control); } dataEntryPropagator.SetDataEntryCallbacks(this.ControlsByDataLabel); }
/// <summary> /// Add user interface event handler callbacks for (possibly invisible) controls /// </summary> private void SetUserInterfaceCallbacks() { // Add data entry callbacks to all editable controls. When the user changes an image's attribute using a particular control, // the callback updates the matching field for that image in the database. DataEntryNote date = null; DataEntryDateTime dateTime = null; DataEntryNote time = null; foreach (KeyValuePair <string, DataEntryControl> pair in this.DataEntryControls.ControlsByDataLabel) { string controlType = this.DataHandler.FileDatabase.FileTableColumnsByDataLabel[pair.Key].ControlType; switch (controlType) { case Constant.Control.Counter: DataEntryCounter counter = (DataEntryCounter)pair.Value; counter.ContentControl.PreviewMouseDown += this.ContentControl_MouseDown; counter.ContentControl.PreviewKeyDown += this.ContentCtl_PreviewKeyDown; counter.Container.MouseEnter += this.CounterControl_MouseEnter; counter.Container.MouseLeave += this.CounterControl_MouseLeave; counter.LabelControl.Click += this.CounterControl_Click; break; case Constant.Control.Flag: case Constant.DatabaseColumn.DeleteFlag: DataEntryFlag flag = (DataEntryFlag)pair.Value; flag.ContentControl.PreviewMouseDown += this.ContentControl_MouseDown; flag.ContentControl.PreviewKeyDown += this.ContentCtl_PreviewKeyDown; break; case Constant.Control.FixedChoice: case Constant.DatabaseColumn.ImageQuality: DataEntryChoice choice = (DataEntryChoice)pair.Value; choice.ContentControl.PreviewMouseDown += this.ContentControl_MouseDown; choice.ContentControl.PreviewKeyDown += this.ContentCtl_PreviewKeyDown; break; case Constant.Control.Note: case Constant.DatabaseColumn.Date: case Constant.DatabaseColumn.File: case Constant.DatabaseColumn.Folder: case Constant.DatabaseColumn.RelativePath: case Constant.DatabaseColumn.Time: DataEntryNote note = (DataEntryNote)pair.Value; note.ContentControl.PreviewMouseDown += this.ContentControl_MouseDown; note.ContentControl.PreviewKeyDown += this.ContentCtl_PreviewKeyDown; if (controlType == Constant.DatabaseColumn.Date) { date = note; } if (controlType == Constant.DatabaseColumn.Time) { time = note; } break; case Constant.DatabaseColumn.DateTime: dateTime = (DataEntryDateTime)pair.Value; dateTime.ContentControl.PreviewMouseDown += this.ContentControl_MouseDown; dateTime.ContentControl.PreviewKeyDown += this.ContentCtl_PreviewKeyDown; break; case Constant.DatabaseColumn.UtcOffset: DataEntryUtcOffset utcOffset = (DataEntryUtcOffset)pair.Value; utcOffset.ContentControl.PreviewMouseDown += this.ContentControl_MouseDown; utcOffset.ContentControl.PreviewKeyDown += this.ContentCtl_PreviewKeyDown; break; default: TracePrint.PrintMessage(String.Format("Unhandled control type '{0}' in SetUserInterfaceCallbacks.", controlType)); break; } } // if needed, link date and time controls to datetime control if (dateTime != null && date != null) { dateTime.DateControl = date; } if (dateTime != null && time != null) { dateTime.TimeControl = time; } }