Просмотр исходного кода

Added a RegionEditHelper that ensures that subdiagrams of a state that is deleted are not opened in another editor

Andreas Mülder 13 лет назад
Родитель
Сommit
f0ebd33162

+ 1 - 0
plugins/org.yakindu.sct.ui.editor/plugin.xml

@@ -41,6 +41,7 @@
          </metamodelType>
          <metamodelType
                eclass="Region"
+                edithelper="org.yakindu.sct.ui.editor.edithelper.RegionEditHelper"
                icon="icons/obj16/Region-16.png"
                id="org.yakindu.sct.ui.editor.Region"
                kind="org.eclipse.gmf.runtime.emf.type.core.IHintedType"

+ 30 - 0
plugins/org.yakindu.sct.ui.editor/src/org/yakindu/sct/ui/editor/breadcrumb/DiagramPartitioningUtil.java

@@ -21,10 +21,14 @@ import org.eclipse.emf.ecore.EObject;
 import org.eclipse.emf.ecore.resource.Resource;
 import org.eclipse.emf.ecore.util.EcoreUtil;
 import org.eclipse.emf.workspace.util.WorkspaceSynchronizer;
+import org.eclipse.gmf.runtime.diagram.ui.parts.IDiagramEditorInput;
 import org.eclipse.gmf.runtime.notation.Diagram;
 import org.eclipse.gmf.runtime.notation.NotationPackage;
 import org.eclipse.gmf.runtime.notation.View;
+import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.swt.widgets.Shell;
 import org.eclipse.ui.IEditorDescriptor;
+import org.eclipse.ui.IEditorReference;
 import org.eclipse.ui.IWorkbenchPage;
 import org.eclipse.ui.PartInitException;
 import org.eclipse.ui.PlatformUI;
@@ -94,6 +98,32 @@ public class DiagramPartitioningUtil {
 		}
 	}
 
+	public static boolean closeSubdiagramEditors(State state) {
+		Diagram diagram = DiagramPartitioningUtil.getSubDiagram(state);
+		if (diagram == null)
+			return true;
+		IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
+		IEditorReference[] refs = activePage.getEditorReferences();
+		for (IEditorReference ref : refs) {
+			try {
+				if (ref.getEditorInput() instanceof IDiagramEditorInput) {
+					IDiagramEditorInput diagramInput = (IDiagramEditorInput) ref.getEditorInput();
+					if (diagramInput.getDiagram().equals(diagram)) {
+						boolean close = MessageDialog.openQuestion(new Shell(), "Close subdiagram editor?",
+								"The subdiagram is still open in another editor. Do you want to close it?");
+						if (close) {
+							activePage.closeEditor(ref.getEditor(false), false);
+						}
+						return close;
+					}
+				}
+			} catch (PartInitException e) {
+				e.printStackTrace();
+			}
+		}
+		return true;
+	}
+
 	public static List<Diagram> getDiagramContainerHierachy(Diagram diagram) {
 		List<Diagram> result = new ArrayList<Diagram>();
 		result.add(diagram);

+ 49 - 0
plugins/org.yakindu.sct.ui.editor/src/org/yakindu/sct/ui/editor/edithelper/RegionEditHelper.java

@@ -0,0 +1,49 @@
+/**
+ * Copyright (c) 2013 committers of YAKINDU and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ * Contributors:
+ * 	committers of YAKINDU - initial API and implementation
+ * 
+ */
+package org.yakindu.sct.ui.editor.edithelper;
+
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.core.runtime.IAdaptable;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.gmf.runtime.common.core.command.ICommand;
+import org.eclipse.gmf.runtime.emf.type.core.commands.DestroyElementCommand;
+import org.eclipse.gmf.runtime.emf.type.core.edithelper.AbstractEditHelper;
+import org.eclipse.gmf.runtime.emf.type.core.requests.DestroyElementRequest;
+import org.yakindu.sct.model.sgraph.State;
+import org.yakindu.sct.ui.editor.breadcrumb.DiagramPartitioningUtil;
+
+/**
+ * 
+ * @author andreas muelder - Initial contribution and API
+ * 
+ */
+public class RegionEditHelper extends AbstractEditHelper {
+	@Override
+	protected ICommand getBasicDestroyElementCommand(final DestroyElementRequest req) {
+		// Deletion is only allowed if subdiagrams are not opened in another
+		// editor
+		return new DestroyElementCommand(req) {
+			@Override
+			protected IStatus doExecute(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
+				if (getElementToDestroy() instanceof State) {
+					boolean success = DiagramPartitioningUtil.closeSubdiagramEditors((State) getElementToDestroy());
+					if (success)
+						return super.doExecute(monitor, info);
+					else
+						return Status.CANCEL_STATUS;
+				}
+				return super.doExecute(monitor, info);
+			}
+		};
+	}
+}