/// <summary> /// Populates mySelectedNoteOwners with all currently selected note /// owners in the current selection container. /// </summary> private void PopulateSelectedNoteOwners() { List <INoteOwner <NoteType> > selectedTypes = mySelectedNoteOwners; if (selectedTypes == null) { selectedTypes = new List <INoteOwner <NoteType> >(); mySelectedNoteOwners = selectedTypes; } selectedTypes.Clear(); // Clear the list of selected root types. IORMSelectionContainer selectionContainer = CurrentORMSelectionContainer; if (selectionContainer != null) { ICollection objects = base.GetSelectedComponents(); if (objects != null) { foreach (object o in objects) { INoteOwner <NoteType> owner = EditorUtility.ResolveContextInstance(o, false) as INoteOwner <NoteType>; // and if they are an INoteOwner, if (owner != null) { selectedTypes.Add(owner); // add them to the list of selected owners. } } } } DisplayNotes(); // Display the notes for all selected owners. }
/// <summary> /// Handles note change. /// </summary> private void NoteAlteredEventHandler(object sender, ElementPropertyChangedEventArgs e) { ModelElement element = e.ModelElement; Store store = element.Store; if (e.DomainProperty.Id == GetNoteTextPropertyId(store)) { // First, see if the note element implements INoteOwner directly INoteOwner <NoteType> noteOwner = element as INoteOwner <NoteType>; NoteRoleAndHandler[] ownerLinks; if (noteOwner != null) { NoteAlteredEventHandler(noteOwner); } else if (null != (ownerLinks = GetOwningRelationshipHandlers(store, false))) { // If note, find the owning element for (int i = 0; i < ownerLinks.Length; ++i) { if (null != (noteOwner = ownerLinks[i].DomainRole.GetLinkedElement(element) as INoteOwner <NoteType>)) { NoteAlteredEventHandler(noteOwner); break; } } } } }
/// <summary> /// Displays the notes for all selected note owners in the Notes Window. /// </summary> private void DisplayNotes() { List <INoteOwner <NoteType> > selectedNoteOwners = mySelectedNoteOwners; // Cache the list of selected note owners. int selectedNoteOwnersCount = selectedNoteOwners.Count; // Cache the count of selected note owners. TextBox textBox = myTextBox; // Cache the text box. if (textBox != null) // If the text box is not null, { textBox.Clear(); // clear it's text if (selectedNoteOwnersCount == 0) // If there aren't any selected note owners, { textBox.ReadOnly = true; textBox.Enabled = false; textBox.AppendText(EmptyDisplayText); } else if (selectedNoteOwnersCount == 1) // If there is only one selected note owner, { textBox.ReadOnly = false; textBox.Enabled = true; textBox.AppendText(selectedNoteOwners[0].NoteText); // add its note text to the text box. } else { textBox.ReadOnly = true; // Otherwise, the textbox should be read-only because multiple elements can't be edited together textBox.Enabled = true; StringBuilder sb = new StringBuilder(); string formatString = ResourceStrings.ModelNotesWindowRootTypeNameNotesSeparatorFormatString; // root types are selected. bool first = true; for (int i = 0; i < selectedNoteOwnersCount; ++i) // Loop through the selected root types, { INoteOwner <NoteType> noteOwner = selectedNoteOwners[i]; // cache them locally, string noteText = noteOwner.NoteText; // and cache their note text. if (!string.IsNullOrEmpty(noteText)) // If there is note text on the note owner, { if (!first) { sb.AppendLine(); } else { first = false; } sb.AppendFormat(CultureInfo.InvariantCulture, formatString, noteOwner.Name, noteText); } } textBox.Text = sb.ToString(); } } }
/// <summary> /// Sets the NoteText property on an INoteOwner instance. /// </summary> /// <param name="owner">The INoteOwner on which the note should be set.</param> /// <param name="text">The text of the note.</param> private void SetNote(INoteOwner <NoteType> owner, string text) { PropertyDescriptor descriptor; ORMDesignerDocData currentDoc; IORMToolServices currentStore; if (null != owner && null != (currentDoc = CurrentDocument) && null != (currentStore = currentDoc.Store as IORMToolServices) && currentStore.CanAddTransaction && null != (descriptor = owner.NoteTextPropertyDescriptor)) // Be really defensive. { if (0 != string.CompareOrdinal((string)descriptor.GetValue(owner), text)) { descriptor.SetValue(owner, text); } } }
/// <summary> /// Helper function to handler a note change event /// </summary> private void NoteAlteredEventHandler(INoteOwner <NoteType> noteOwner) { List <INoteOwner <NoteType> > currentOwners; int ownerCount; if (null != noteOwner && null != (currentOwners = mySelectedNoteOwners) && 0 != (ownerCount = currentOwners.Count)) { bool matchedOwner = false; for (int i = 0; i < ownerCount; ++i) { INoteOwner <NoteType> currentOwner = currentOwners[i]; if (currentOwner == noteOwner) { matchedOwner = true; break; } IRedirectedNoteOwner <NoteType> redirect = currentOwner as IRedirectedNoteOwner <NoteType>; while (redirect != null) { INoteOwner <NoteType> directOwner; if (null == (directOwner = redirect.DirectNoteOwner) || directOwner == currentOwner) { break; } else if (directOwner == noteOwner) { matchedOwner = true; break; } currentOwner = directOwner; redirect = currentOwner as IRedirectedNoteOwner <NoteType>; } } if (matchedOwner) { DisplayNotes(); } } }