private void ContextMenuNewPageClick(object sender, EventArgs e) { ToolStripMenuItem menuItem = sender as ToolStripMenuItem; if (menuItem != null) { Store store = base.DocData.Store; using (Transaction t = store.TransactionManager.BeginTransaction(menuItem.Text.Replace("&", string.Empty))) { object[] itemInfo; if (null != (itemInfo = menuItem.Tag as object[])) { DomainClassInfo diagramInfo = (DomainClassInfo)itemInfo[1]; DiagramMenuDisplayAttribute attribute = (DiagramMenuDisplayAttribute)itemInfo[0]; IDiagramInitialization initializer = attribute.CreateInitializer(diagramInfo.ImplementationClass); Diagram newDiagram = (Diagram)store.ElementFactory.CreateElement(diagramInfo); if (initializer != null) { initializer.InitializeDiagram(newDiagram); } Diagram selectedDiagram; DiagramDisplay displayContainer; LinkedElementCollection <Diagram> diagramOrder; int selectedDiagramIndex; if (null != (selectedDiagram = ContextMenuStrip.SelectedDiagram) && null != store.FindDomainModel(DiagramDisplayDomainModel.DomainModelId) && null != (displayContainer = DiagramDisplayHasDiagramOrder.GetDiagramDisplay(selectedDiagram)) && (selectedDiagramIndex = (diagramOrder = displayContainer.OrderedDiagramCollection).IndexOf(selectedDiagram)) < (diagramOrder.Count - 1)) { // Add the page immediately after the selected diagram, unless we're already at the end, in which // case we leave it to the rules to add in the correct place diagramOrder.Insert(selectedDiagramIndex + 1, newDiagram); } t.Commit(); } } } }
/// <summary> /// See <see cref="ModelingDocView.LoadView"/>. /// </summary> protected override bool LoadView() { if (base.LoadView()) { ORMDesignerDocData document = (ORMDesignerDocData)this.DocData; #region Setup context menu ContextMenuStrip contextMenu = base.ContextMenuStrip = new MultiDiagramContextMenuStrip(); contextMenu.ShowImageMargin = false; contextMenu.Opening += ContextMenuOpening; ToolStripMenuItem newPageMenuItem = new ToolStripMenuItem(ResourceStrings.DiagramCommandNewPage); ToolStripMenuItem deletePageMenuItem = new ToolStripMenuItem(ResourceStrings.DiagramCommandDeletePage); ToolStripMenuItem renamePageMenuItem = new ToolStripMenuItem(ResourceStrings.DiagramCommandRenamePage); newPageMenuItem.DropDown = new ToolStripDropDown(); ToolStripItemCollection items = newPageMenuItem.DropDownItems; ReadOnlyCollection <DomainClassInfo> diagrams = document.Store.DomainDataDirectory.FindDomainClass(Diagram.DomainClassId).AllDescendants; int diagramCount = diagrams.Count; for (int i = 0; i < diagramCount; ++i) { DomainClassInfo diagramInfo = diagrams[i]; object[] attributes = diagramInfo.ImplementationClass.GetCustomAttributes(typeof(DiagramMenuDisplayAttribute), false); if (attributes.Length > 0) { DiagramMenuDisplayAttribute attribute = (DiagramMenuDisplayAttribute)attributes[0]; Image image = attribute.TabImage; string name = attribute.DisplayName; if (string.IsNullOrEmpty(name)) { name = diagramInfo.DisplayName; } if (image != null) { base.RegisterImageForDiagramType(diagramInfo.ImplementationClass, image); } ToolStripMenuItem newDiagramMenuItem = new ToolStripMenuItem(name, image, ContextMenuNewPageClick); newDiagramMenuItem.Tag = new object[] { attribute, diagramInfo }; items.Add(newDiagramMenuItem); } } newPageMenuItem.Name = ResourceStrings.DiagramCommandNewPage; newPageMenuItem.DropDown.ImageScalingSize = DiagramImageSize; deletePageMenuItem.Name = ResourceStrings.DiagramCommandDeletePage; deletePageMenuItem.Click += ContextMenuDeletePageClick; deletePageMenuItem.Tag = ContextMenuItemNeedsSelectedTab; renamePageMenuItem.Name = ResourceStrings.DiagramCommandRenamePage; renamePageMenuItem.Click += ContextMenuRenamePageClick; renamePageMenuItem.Tag = ContextMenuItemNeedsSelectedTab; if (null != Store.FindDomainModel(DiagramDisplayDomainModel.DomainModelId)) { ToolStripMenuItem reorderPagesMenuItem = new ToolStripMenuItem(ResourceStrings.DiagramCommandReorderPages); reorderPagesMenuItem.Name = ResourceStrings.DiagramCommandReorderPages; reorderPagesMenuItem.Click += ContextMenuPageOrderClick; contextMenu.Items.AddRange(new ToolStripItem[] { newPageMenuItem, reorderPagesMenuItem, new ToolStripSeparator(), deletePageMenuItem, renamePageMenuItem }); } else { contextMenu.Items.AddRange(new ToolStripItem[] { newPageMenuItem, new ToolStripSeparator(), deletePageMenuItem, renamePageMenuItem }); } #endregion // Setup context menu Store store = document.Store; // Add our existing diagrams. If we have the diagram display model extension turned on // for this designer, then we will always have an ordered set of diagrams at this point. if (null != (store.FindDomainModel(DiagramDisplayDomainModel.DomainModelId))) { ReadOnlyCollection <DiagramDisplay> diagramDisplayElements = store.ElementDirectory.FindElements <DiagramDisplay>(false); if (diagramDisplayElements.Count != 0) { IList <DiagramDisplayHasDiagramOrder> existingDiagramLinks = DiagramDisplayHasDiagramOrder.GetLinksToOrderedDiagramCollection(diagramDisplayElements[0]); int existingDiagramsCount = existingDiagramLinks.Count; if (existingDiagramsCount != 0) { Partition defaultPartition = store.DefaultPartition; for (int i = 0; i < existingDiagramsCount; ++i) { DiagramDisplayHasDiagramOrder link = existingDiagramLinks[i]; Diagram existingDiagram = link.Diagram; if (existingDiagram.Partition == defaultPartition) { // The fixup listeners guarantee that there is exactly one active diagram, // so we can safely trust the property to get an active diagram base.AddDiagram(existingDiagram, link.IsActiveDiagram); } } } } } else { // Add our existing diagrams ReadOnlyCollection <Diagram> existingDiagrams = store.ElementDirectory.FindElements <Diagram>(true); int existingDiagramsCount = existingDiagrams.Count; if (existingDiagramsCount != 0) { bool seenDiagram = false; Partition defaultPartition = store.DefaultPartition; for (int i = 0; i < existingDiagramsCount; ++i) { Diagram existingDiagram = existingDiagrams[i]; if (existingDiagram.Partition == defaultPartition) { // Make the first diagram be selected base.AddDiagram(existingDiagram, !seenDiagram); seenDiagram = true; } } } } // Make sure we get a closing notification so we can clear the // selected components document.DocumentClosing += new EventHandler(DocumentClosing); return(true); } return(false); }