Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormgolubev2012-05-15 08:40:35 -0400
committermgolubev2012-05-15 08:40:35 -0400
commit60398929970657f97a12a823a7bcbec8ef729e09 (patch)
treec60311ff422c5f82a45fe199aca80f521ff807a8 /examples/org.eclipse.gmf.examples.layers.subdiagrams/src
parentdcf75b44092b5f6dad20cd7030fe116519e234b9 (diff)
downloadorg.eclipse.gmf-tooling-60398929970657f97a12a823a7bcbec8ef729e09.tar.gz
org.eclipse.gmf-tooling-60398929970657f97a12a823a7bcbec8ef729e09.tar.xz
org.eclipse.gmf-tooling-60398929970657f97a12a823a7bcbec8ef729e09.zip
Subsetting / layering diagram support for GMFT separated from Taipan(samples)-layers-support-taipan
sample to be reusable.
Diffstat (limited to 'examples/org.eclipse.gmf.examples.layers.subdiagrams/src')
-rw-r--r--examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/DefaultSaveOptions.java29
-rw-r--r--examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/SubDiagramManager.java10
-rw-r--r--examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/SubDiagramManagerImpl.java85
-rw-r--r--examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/popup/AssignToLayerAction.java107
-rw-r--r--examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/popup/AssignToLayerItemProvider.java93
-rw-r--r--examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/popup/AssignToLayerItemProviderPolicy.java35
-rw-r--r--examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/popup/CreateNewLayerAction.java109
-rw-r--r--examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/popup/CreateSubDiagramAction.java115
-rw-r--r--examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/popup/DomainOnlyEditRequest.java21
-rw-r--r--examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/popup/OpenSubDiagramAction.java47
-rw-r--r--examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/popup/SubDiagramActionBase.java195
-rw-r--r--examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/popup/SubDiagramsItemProvider.java85
-rw-r--r--examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/popup/SwitchLayerVisibilityAction.java210
-rw-r--r--examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/popup/SwitchLayerVisibilityItemProvider.java76
-rw-r--r--examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/popup/UniqueNameValidator.java65
15 files changed, 1282 insertions, 0 deletions
diff --git a/examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/DefaultSaveOptions.java b/examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/DefaultSaveOptions.java
new file mode 100644
index 000000000..8099bf148
--- /dev/null
+++ b/examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/DefaultSaveOptions.java
@@ -0,0 +1,29 @@
+package org.eclipse.gmf.examples.layers.subdiagrams;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.eclipse.emf.ecore.resource.Resource;
+import org.eclipse.emf.ecore.xmi.XMLResource;
+
+/**
+ * @deprecated find a way to plug the real per-diagram save options (XXXDiagramEditorUtil.getSaveOptions) here.
+ */
+@Deprecated
+public class DefaultSaveOptions {
+
+ /**
+ * XXX: We don't have a way to plug the actual diagram save options (XXXDiagramEditorUtil.getSaveOptions) here.
+ * For now we will use the default ones, but it may lead to a problem if actual diagram has this method generated NOT.
+ *
+ * @deprecated find a way to plug the real per-diagram save options (XXXDiagramEditorUtil.getSaveOptions) here.
+ */
+ @Deprecated
+ public static Map<?, ?> getSaveOptions() {
+ HashMap<String, Object> saveOptions = new HashMap<String, Object>();
+ saveOptions.put(XMLResource.OPTION_ENCODING, "UTF-8"); //$NON-NLS-1$
+ saveOptions.put(Resource.OPTION_SAVE_ONLY_IF_CHANGED, Resource.OPTION_SAVE_ONLY_IF_CHANGED_MEMORY_BUFFER);
+ return saveOptions;
+ }
+
+}
diff --git a/examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/SubDiagramManager.java b/examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/SubDiagramManager.java
new file mode 100644
index 000000000..53eea5cc5
--- /dev/null
+++ b/examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/SubDiagramManager.java
@@ -0,0 +1,10 @@
+package org.eclipse.gmf.examples.layers.subdiagrams;
+
+import org.eclipse.gmf.examples.layers.SubDiagramSupport;
+
+
+public interface SubDiagramManager {
+
+ public SubDiagramSupport getSubDiagramSupport();
+
+}
diff --git a/examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/SubDiagramManagerImpl.java b/examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/SubDiagramManagerImpl.java
new file mode 100644
index 000000000..fb16f70a6
--- /dev/null
+++ b/examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/SubDiagramManagerImpl.java
@@ -0,0 +1,85 @@
+package org.eclipse.gmf.examples.layers.subdiagrams;
+
+import org.eclipse.emf.common.notify.Adapter;
+import org.eclipse.emf.common.notify.impl.AdapterImpl;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.resource.Resource;
+import org.eclipse.gmf.examples.layers.LayersFactory;
+import org.eclipse.gmf.examples.layers.SubDiagramSpec;
+import org.eclipse.gmf.examples.layers.SubDiagramSupport;
+import org.eclipse.gmf.runtime.notation.Diagram;
+
+public class SubDiagramManagerImpl implements SubDiagramManager {
+
+ private static class ManagerHolderAdapter extends AdapterImpl {
+
+ private final SubDiagramManagerImpl myManager;
+
+ public ManagerHolderAdapter(SubDiagramManagerImpl manager) {
+ myManager = manager;
+ }
+
+ public SubDiagramManagerImpl getManager() {
+ return myManager;
+ }
+ }
+
+ public static SubDiagramManager findOrCreateSubsetManager(Diagram diagram) {
+ for (Adapter next : diagram.eAdapters()) {
+ if (next instanceof ManagerHolderAdapter) {
+ return ((ManagerHolderAdapter) next).getManager();
+ }
+ }
+ SubDiagramManagerImpl result = new SubDiagramManagerImpl(diagram.eResource());
+ diagram.eAdapters().add(new ManagerHolderAdapter(result));
+ return result;
+ }
+
+ public static void setupSubsetsSupport(Diagram diagram, Resource diagramResource) {
+ SubDiagramSupport support = LayersFactory.eINSTANCE.createSubDiagramSupport();
+ diagramResource.getContents().add(support);
+
+ SubDiagramSpec mainDiagramSpec = LayersFactory.eINSTANCE.createSubDiagramSpec();
+ mainDiagramSpec.setName("Main");
+ mainDiagramSpec.setShowing(true);
+ mainDiagramSpec.setDiagram(diagram);
+ support.setMainDiagram(mainDiagramSpec);
+
+ //fake layers
+ // Layer layerA = LayersFactory.eINSTANCE.createLayer();
+ // layerA.setName("Layer A");
+ //
+ // Layer layerB = LayersFactory.eINSTANCE.createLayer();
+ // layerB.setName("Layer B");
+ //
+ // support.getLayers().add(layerA);
+ // support.getLayers().add(layerB);
+ //
+
+ support.ensureAllSubDiagramsHaveEnablementForAllLayers();
+ }
+
+ private final Resource myDiagramResource;
+
+ private final SubDiagramSupport mySubDiagramSupport;
+
+ public SubDiagramManagerImpl(Resource diagramResource) {
+ myDiagramResource = diagramResource;
+ mySubDiagramSupport = findSubDiagramSupport(myDiagramResource);
+ }
+
+ private static SubDiagramSupport findSubDiagramSupport(Resource resource) {
+ for (EObject next : resource.getContents()) {
+ if (next instanceof SubDiagramSupport) {
+ return (SubDiagramSupport) next;
+ }
+ }
+ return null;
+ }
+
+ @Override
+ public SubDiagramSupport getSubDiagramSupport() {
+ return mySubDiagramSupport;
+ }
+
+}
diff --git a/examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/popup/AssignToLayerAction.java b/examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/popup/AssignToLayerAction.java
new file mode 100644
index 000000000..9c73bc9cf
--- /dev/null
+++ b/examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/popup/AssignToLayerAction.java
@@ -0,0 +1,107 @@
+package org.eclipse.gmf.examples.layers.subdiagrams.popup;
+
+import java.util.List;
+
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.core.runtime.IAdaptable;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.gef.Request;
+import org.eclipse.gef.commands.Command;
+import org.eclipse.gef.commands.UnexecutableCommand;
+import org.eclipse.gmf.examples.layers.Layer;
+import org.eclipse.gmf.runtime.common.core.command.CommandResult;
+import org.eclipse.gmf.runtime.diagram.ui.actions.DiagramAction;
+import org.eclipse.gmf.runtime.diagram.ui.commands.ICommandProxy;
+import org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart;
+import org.eclipse.gmf.runtime.emf.type.core.commands.EditElementCommand;
+import org.eclipse.gmf.runtime.emf.type.core.requests.IEditCommandRequest;
+import org.eclipse.ui.IWorkbenchPage;
+
+public class AssignToLayerAction extends DiagramAction {
+
+ protected static final String EMPTY_LAYER_NAME = "<empty-name>";
+
+ private final Layer myLayerToAssign;
+
+ private final List<EObject> mySemanticElements;
+
+ public AssignToLayerAction(IWorkbenchPage workbenchPage, List<EObject> semanticElements, Layer layer) {
+ super(workbenchPage);
+ mySemanticElements = semanticElements;
+ myLayerToAssign = layer;
+ }
+
+ @Override
+ protected Request createTargetRequest() {
+ return null;
+ }
+
+ @Override
+ protected boolean isSelectionListener() {
+ return true;
+ }
+
+ @Override
+ protected Command getCommand() {
+ DiagramEditPart diagramEditPart = getDiagramEditPart();
+ if (diagramEditPart == null) {
+ return UnexecutableCommand.INSTANCE;
+ }
+ IEditCommandRequest request = new DomainOnlyEditRequest(diagramEditPart);
+ boolean addNotRemove = !isAssignedToLayer(mySemanticElements, myLayerToAssign);
+ return new ICommandProxy(new AssignToLayer("Changing Layer Assignments", myLayerToAssign, mySemanticElements, addNotRemove, request));
+ }
+
+ @Override
+ public boolean isEnabled() {
+ return getDiagramEditPart() != null;
+ }
+
+ @Override
+ public void refresh() {
+ super.refresh();
+ setText(calculateText());
+ setChecked(calculateChecked());
+ }
+
+ protected String calculateText() {
+ String name = myLayerToAssign.getName();
+ return name != null ? name : EMPTY_LAYER_NAME;
+ }
+
+ protected boolean calculateChecked() {
+ return isAssignedToLayer(mySemanticElements, myLayerToAssign);
+ }
+
+ private boolean isAssignedToLayer(List<EObject> semanticElements, Layer layer) {
+ return layer.getParticipants().containsAll(semanticElements);
+ }
+
+ public static class AssignToLayer extends EditElementCommand {
+
+ private final Layer myLayer;
+
+ private final List<EObject> myAssignees;
+
+ private final boolean myAddNotRemove;
+
+ protected AssignToLayer(String label, Layer layer, List<EObject> assignees, boolean addNotRemove, IEditCommandRequest request) {
+ super(label, layer, request);
+ myAssignees = assignees;
+ myLayer = layer;
+ myAddNotRemove = addNotRemove;
+ }
+
+ @Override
+ protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
+ if (myAddNotRemove) {
+ myLayer.getParticipants().addAll(myAssignees);
+ } else {
+ myLayer.getParticipants().removeAll(myAssignees);
+ }
+ return CommandResult.newOKCommandResult(myLayer);
+ }
+
+ }
+}
diff --git a/examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/popup/AssignToLayerItemProvider.java b/examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/popup/AssignToLayerItemProvider.java
new file mode 100644
index 000000000..7651bd7d8
--- /dev/null
+++ b/examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/popup/AssignToLayerItemProvider.java
@@ -0,0 +1,93 @@
+package org.eclipse.gmf.examples.layers.subdiagrams.popup;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.LinkedHashSet;
+import java.util.List;
+
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.gmf.examples.layers.Layer;
+import org.eclipse.gmf.examples.layers.SubDiagramSpec;
+import org.eclipse.gmf.examples.layers.SubDiagramSupport;
+import org.eclipse.gmf.examples.layers.subdiagrams.SubDiagramManager;
+import org.eclipse.gmf.examples.layers.subdiagrams.SubDiagramManagerImpl;
+import org.eclipse.gmf.runtime.common.core.service.IProvider;
+import org.eclipse.gmf.runtime.common.ui.services.action.contributionitem.AbstractContributionItemProvider;
+import org.eclipse.gmf.runtime.common.ui.util.IWorkbenchPartDescriptor;
+import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
+import org.eclipse.gmf.runtime.notation.Diagram;
+import org.eclipse.jface.action.IMenuListener;
+import org.eclipse.jface.action.IMenuManager;
+import org.eclipse.jface.action.MenuManager;
+import org.eclipse.jface.action.Separator;
+import org.eclipse.ui.IWorkbenchPage;
+
+public class AssignToLayerItemProvider extends AbstractContributionItemProvider implements IProvider {
+
+ public static final String MENU_ASSIGN_TO_LAYER = "menu_assign_to_layer"; //$NON-NLS-1$
+
+ @Override
+ protected IMenuManager createMenuManager(String menuId, IWorkbenchPartDescriptor partDescriptor) {
+ if (!MENU_ASSIGN_TO_LAYER.equals(menuId)) {
+ return super.createMenuManager(menuId, partDescriptor);
+ }
+ MenuManager menuManager = new MenuManager("Assign To Layer");
+ MenuBuilder builder = new MenuBuilder(partDescriptor);
+ // XXX: build initial content -- otherwise menu is never shown
+ builder.buildMenu(menuManager);
+
+ menuManager.addMenuListener(builder);
+ return menuManager;
+ }
+
+ private class MenuBuilder implements IMenuListener {
+
+ private final IWorkbenchPartDescriptor myWorkbenchPart;
+
+ public MenuBuilder(IWorkbenchPartDescriptor workbenchPart) {
+ myWorkbenchPart = workbenchPart;
+ }
+
+ public void menuAboutToShow(IMenuManager manager) {
+ buildMenu(manager);
+ }
+
+ public void buildMenu(IMenuManager manager) {
+ manager.removeAll();
+ IGraphicalEditPart firstSelected = (IGraphicalEditPart) getSelectedObject(myWorkbenchPart);
+ Diagram activeDiagram = firstSelected.getNotationView().getDiagram();
+
+ SubDiagramManager subsetManager = SubDiagramManagerImpl.findOrCreateSubsetManager(activeDiagram);
+ SubDiagramSupport support = subsetManager.getSubDiagramSupport();
+
+ SubDiagramSpec activeSpec = support.findDiagramSpec(activeDiagram);
+
+ List<EObject> allSelectedEObjects = Collections.unmodifiableList(resolveSelectedSemanticElements());
+ for (Layer next : support.getLayers()) {
+ AssignToLayerAction action = new AssignToLayerAction(getWorkbenchPage(), allSelectedEObjects, next);
+ action.init();
+ manager.add(action);
+ }
+
+ manager.add(new Separator());
+ CreateNewLayerAction createNewAction = new CreateNewLayerAction(getWorkbenchPage(), allSelectedEObjects, support, activeSpec);
+ createNewAction.init();
+ manager.add(createNewAction);
+ }
+
+ private List<EObject> resolveSelectedSemanticElements() {
+ LinkedHashSet<EObject> result = new LinkedHashSet<EObject>();
+ for (Object next : getStructuredSelection(myWorkbenchPart).toArray()) {
+ if (next instanceof IGraphicalEditPart) {
+ result.add(((IGraphicalEditPart) next).resolveSemanticElement());
+ }
+ }
+ return new ArrayList<EObject>(result);
+ }
+
+ private IWorkbenchPage getWorkbenchPage() {
+ return myWorkbenchPart.getPartPage();
+ }
+ }
+
+}
diff --git a/examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/popup/AssignToLayerItemProviderPolicy.java b/examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/popup/AssignToLayerItemProviderPolicy.java
new file mode 100644
index 000000000..f0397c695
--- /dev/null
+++ b/examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/popup/AssignToLayerItemProviderPolicy.java
@@ -0,0 +1,35 @@
+package org.eclipse.gmf.examples.layers.subdiagrams.popup;
+
+import java.util.List;
+
+import org.eclipse.core.runtime.IConfigurationElement;
+import org.eclipse.gmf.runtime.common.ui.services.action.contributionitem.IPopupMenuContributionPolicy;
+import org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart;
+import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.IStructuredSelection;
+
+public class AssignToLayerItemProviderPolicy implements IPopupMenuContributionPolicy {
+
+ @Override
+ public boolean appliesTo(ISelection selection, IConfigurationElement configuration) {
+ if (false == selection instanceof IStructuredSelection) {
+ return false;
+ }
+ List<?> allSelected = ((IStructuredSelection) selection).toList();
+ if (allSelected.isEmpty()) {
+ return false;
+ }
+ for (Object next : allSelected) {
+ if (!checkSelectedElement(next)) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ private boolean checkSelectedElement(Object selected) {
+ return selected instanceof IGraphicalEditPart && !(selected instanceof DiagramEditPart);
+ }
+
+}
diff --git a/examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/popup/CreateNewLayerAction.java b/examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/popup/CreateNewLayerAction.java
new file mode 100644
index 000000000..9e020c9ad
--- /dev/null
+++ b/examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/popup/CreateNewLayerAction.java
@@ -0,0 +1,109 @@
+package org.eclipse.gmf.examples.layers.subdiagrams.popup;
+
+import java.util.List;
+
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.core.runtime.IAdaptable;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.gef.Request;
+import org.eclipse.gef.commands.Command;
+import org.eclipse.gef.commands.UnexecutableCommand;
+import org.eclipse.gmf.examples.layers.Layer;
+import org.eclipse.gmf.examples.layers.LayersFactory;
+import org.eclipse.gmf.examples.layers.LayersPackage;
+import org.eclipse.gmf.examples.layers.SubDiagramSpec;
+import org.eclipse.gmf.examples.layers.SubDiagramSupport;
+import org.eclipse.gmf.runtime.common.core.command.CommandResult;
+import org.eclipse.gmf.runtime.diagram.ui.actions.DiagramAction;
+import org.eclipse.gmf.runtime.diagram.ui.commands.ICommandProxy;
+import org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart;
+import org.eclipse.gmf.runtime.emf.type.core.commands.EditElementCommand;
+import org.eclipse.gmf.runtime.emf.type.core.requests.IEditCommandRequest;
+import org.eclipse.jface.dialogs.InputDialog;
+import org.eclipse.jface.window.Window;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.ui.IWorkbenchPage;
+
+public class CreateNewLayerAction extends DiagramAction {
+
+ private final List<EObject> mySemanticElements;
+
+ private final SubDiagramSupport mySupport;
+
+ private final SubDiagramSpec myDiagram;
+
+ public CreateNewLayerAction(IWorkbenchPage workbenchPage, List<EObject> semanticElements, SubDiagramSupport support, SubDiagramSpec diagram) {
+ super(workbenchPage);
+ mySemanticElements = semanticElements;
+ mySupport = support;
+ myDiagram = diagram;
+ setText("Add To New Layer...");
+ setChecked(false);
+ }
+
+ @Override
+ protected Request createTargetRequest() {
+ return null;
+ }
+
+ @Override
+ protected boolean isSelectionListener() {
+ return true;
+ }
+
+ @Override
+ protected Command getCommand() {
+ DiagramEditPart diagramEditPart = getDiagramEditPart();
+ if (diagramEditPart == null) {
+ return UnexecutableCommand.INSTANCE;
+ }
+ IEditCommandRequest request = new DomainOnlyEditRequest(diagramEditPart);
+ return new ICommandProxy(new CreateNewLayer("Creating New Layer", mySupport, mySemanticElements, myDiagram, request, getWorkbenchPart().getSite().getShell()));
+ }
+
+ @Override
+ public boolean isEnabled() {
+ return getDiagramEditPart() != null && !mySemanticElements.isEmpty();
+ }
+
+ public static class CreateNewLayer extends EditElementCommand {
+
+ private final SubDiagramSupport mySupport;
+
+ private final List<EObject> myAssignees;
+
+ private final Shell myShell;
+
+ private final SubDiagramSpec myDiagram;
+
+ protected CreateNewLayer(String label, SubDiagramSupport support, List<EObject> assignees, SubDiagramSpec diagram, IEditCommandRequest request, Shell shell) {
+ super(label, support, request);
+ myAssignees = assignees;
+ mySupport = support;
+ myDiagram = diagram;
+ myShell = shell;
+ }
+
+ @Override
+ protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
+ UniqueNameValidator validator = new UniqueNameValidator(mySupport.getLayers(), LayersPackage.eINSTANCE.getLayer_Name());
+ InputDialog dialog = new InputDialog(myShell, //
+ "Enter The Name", "New Layer's Name:", validator.guessNewName("Layer "), validator);
+
+ if (dialog.open() != Window.OK) {
+ return CommandResult.newCancelledCommandResult();
+ }
+
+ Layer layer = LayersFactory.eINSTANCE.createLayer();
+ layer.setName(dialog.getValue());
+ mySupport.getLayers().add(layer);
+ mySupport.ensureAllSubDiagramsHaveEnablementForAllLayers();
+ layer.getParticipants().addAll(myAssignees);
+
+ myDiagram.findLayerEnablement(layer).setVisible(true);
+
+ return CommandResult.newOKCommandResult(layer);
+ }
+ }
+}
diff --git a/examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/popup/CreateSubDiagramAction.java b/examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/popup/CreateSubDiagramAction.java
new file mode 100644
index 000000000..9004c22ee
--- /dev/null
+++ b/examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/popup/CreateSubDiagramAction.java
@@ -0,0 +1,115 @@
+package org.eclipse.gmf.examples.layers.subdiagrams.popup;
+
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.core.runtime.IAdaptable;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.emf.ecore.util.EcoreUtil;
+import org.eclipse.gef.commands.Command;
+import org.eclipse.gef.commands.UnexecutableCommand;
+import org.eclipse.gmf.examples.layers.LayersPackage;
+import org.eclipse.gmf.examples.layers.SubDiagramSpec;
+import org.eclipse.gmf.examples.layers.SubDiagramSupport;
+import org.eclipse.gmf.runtime.common.core.command.CommandResult;
+import org.eclipse.gmf.runtime.diagram.ui.commands.ICommandProxy;
+import org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart;
+import org.eclipse.gmf.runtime.emf.commands.core.command.CompositeTransactionalCommand;
+import org.eclipse.gmf.runtime.emf.type.core.commands.EditElementCommand;
+import org.eclipse.gmf.runtime.emf.type.core.requests.IEditCommandRequest;
+import org.eclipse.gmf.runtime.notation.Diagram;
+import org.eclipse.jface.dialogs.InputDialog;
+import org.eclipse.jface.window.Window;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.ui.IWorkbenchPage;
+
+public class CreateSubDiagramAction extends SubDiagramActionBase {
+
+ private final SubDiagramSpec mySpec;
+
+ private final SubDiagramSupport mySupport;
+
+ public CreateSubDiagramAction(IWorkbenchPage workbenchPage, SubDiagramSpec spec, SubDiagramSupport support) {
+ super(workbenchPage);
+ mySpec = spec;
+ mySupport = support;
+ }
+
+ @Override
+ protected Command getCommand() {
+ DiagramEditPart diagramEditPart = getDiagramEditPart();
+ if (diagramEditPart == null) {
+ return UnexecutableCommand.INSTANCE;
+ }
+ if (mySpec == null) {
+ return UnexecutableCommand.INSTANCE;
+ }
+
+ String label = "Creating New Sub Diagram";
+ Ref<SubDiagramSpec> gateBetweenCommands = new Ref<SubDiagramSpec>();
+ CompositeTransactionalCommand composite = new CompositeTransactionalCommand(getDiagramEditPart().getEditingDomain(), label);
+ composite.add(new CreateNewSubDiagram(label, mySupport, mySpec, gateBetweenCommands, new DomainOnlyEditRequest(diagramEditPart), getShell()));
+ composite.add(createOpenCommand("Opening New Diagram", gateBetweenCommands));
+
+ return new ICommandProxy(composite);
+ }
+
+ private Shell getShell() {
+ return getWorkbenchPart().getSite().getShell();
+ }
+
+ @Override
+ public void refresh() {
+ super.refresh();
+ setText(calculateText());
+ setChecked(calculateChecked());
+ }
+
+ protected String calculateText() {
+ return "New...";
+ }
+
+ protected boolean calculateChecked() {
+ return false;
+ }
+
+ private static class CreateNewSubDiagram extends EditElementCommand {
+
+ private final SubDiagramSupport mySupport;
+
+ private final SubDiagramSpec myTemplate;
+
+ private final Ref<SubDiagramSpec> myResultRef;
+
+ private final Shell myShell;
+
+ protected CreateNewSubDiagram(String label, SubDiagramSupport support, SubDiagramSpec template, Ref<SubDiagramSpec> result, IEditCommandRequest request, Shell shell) {
+ super(label, support, request);
+ mySupport = support;
+ myTemplate = template;
+ myResultRef = result;
+ myShell = shell;
+ }
+
+ @Override
+ protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
+ UniqueNameValidator validator = new UniqueNameValidator(mySupport.getAllDiagrams(), LayersPackage.eINSTANCE.getSubDiagramSpec_Name());
+ InputDialog dialog = new InputDialog(myShell, //
+ "Enter The Name", "New Sub Diagram' Name:", validator.guessNewName("Sub Diagram "), validator);
+
+ if (dialog.open() != Window.OK) {
+ return CommandResult.newCancelledCommandResult();
+ }
+
+ Diagram clone = EcoreUtil.copy(myTemplate.getDiagram());
+ SubDiagramSpec result = EcoreUtil.copy(myTemplate);
+ result.setDiagram(clone);
+ result.setName(dialog.getValue());
+
+ mySupport.getSubDiagrams().add(result);
+ mySupport.eResource().getContents().add(clone);
+
+ myResultRef.set(result);
+
+ return CommandResult.newOKCommandResult(result);
+ }
+ }
+}
diff --git a/examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/popup/DomainOnlyEditRequest.java b/examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/popup/DomainOnlyEditRequest.java
new file mode 100644
index 000000000..818d5b8df
--- /dev/null
+++ b/examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/popup/DomainOnlyEditRequest.java
@@ -0,0 +1,21 @@
+package org.eclipse.gmf.examples.layers.subdiagrams.popup;
+
+import org.eclipse.emf.transaction.TransactionalEditingDomain;
+import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
+import org.eclipse.gmf.runtime.emf.type.core.requests.AbstractEditCommandRequest;
+
+class DomainOnlyEditRequest extends AbstractEditCommandRequest {
+
+ public DomainOnlyEditRequest(TransactionalEditingDomain editingDomain) {
+ super(editingDomain);
+ }
+
+ public DomainOnlyEditRequest(IGraphicalEditPart editPart) {
+ this(editPart.getEditingDomain());
+ }
+
+ @Override
+ public Object getEditHelperContext() {
+ return null;
+ }
+} \ No newline at end of file
diff --git a/examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/popup/OpenSubDiagramAction.java b/examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/popup/OpenSubDiagramAction.java
new file mode 100644
index 000000000..6f174ac11
--- /dev/null
+++ b/examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/popup/OpenSubDiagramAction.java
@@ -0,0 +1,47 @@
+package org.eclipse.gmf.examples.layers.subdiagrams.popup;
+
+import org.eclipse.gef.commands.Command;
+import org.eclipse.gef.commands.UnexecutableCommand;
+import org.eclipse.gmf.examples.layers.SubDiagramSpec;
+import org.eclipse.gmf.runtime.diagram.ui.commands.ICommandProxy;
+import org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart;
+import org.eclipse.ui.IWorkbenchPage;
+
+public class OpenSubDiagramAction extends SubDiagramActionBase {
+
+ private final SubDiagramSpec mySpec;
+
+ public OpenSubDiagramAction(IWorkbenchPage workbenchPage, SubDiagramSpec spec) {
+ super(workbenchPage);
+ mySpec = spec;
+ }
+
+ @Override
+ protected Command getCommand() {
+ DiagramEditPart diagramEditPart = getDiagramEditPart();
+ if (diagramEditPart == null) {
+ return UnexecutableCommand.INSTANCE;
+ }
+ if (mySpec == null) {
+ return UnexecutableCommand.INSTANCE;
+ }
+
+ return new ICommandProxy(createOpenCommand("Opening Diagram", mySpec));
+ }
+
+ @Override
+ public void refresh() {
+ super.refresh();
+ setText(calculateText());
+ setChecked(calculateChecked());
+ }
+
+ protected String calculateText() {
+ return safeGetSubDiagramName(mySpec);
+ }
+
+ protected boolean calculateChecked() {
+ return false;
+ }
+
+}
diff --git a/examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/popup/SubDiagramActionBase.java b/examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/popup/SubDiagramActionBase.java
new file mode 100644
index 000000000..46e231c10
--- /dev/null
+++ b/examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/popup/SubDiagramActionBase.java
@@ -0,0 +1,195 @@
+package org.eclipse.gmf.examples.layers.subdiagrams.popup;
+
+import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IAdaptable;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.emf.common.ui.URIEditorInput;
+import org.eclipse.emf.common.util.URI;
+import org.eclipse.emf.ecore.resource.Resource;
+import org.eclipse.emf.ecore.util.EcoreUtil;
+import org.eclipse.emf.ecore.xmi.XMLResource;
+import org.eclipse.emf.workspace.util.WorkspaceSynchronizer;
+import org.eclipse.gef.Request;
+import org.eclipse.gmf.examples.layers.SubDiagramSpec;
+import org.eclipse.gmf.examples.layers.SubDiagramSupport;
+import org.eclipse.gmf.examples.layers.subdiagrams.DefaultSaveOptions;
+import org.eclipse.gmf.runtime.common.core.command.CommandResult;
+import org.eclipse.gmf.runtime.diagram.ui.actions.DiagramAction;
+import org.eclipse.gmf.runtime.emf.type.core.commands.EditElementCommand;
+import org.eclipse.gmf.runtime.emf.type.core.requests.IEditCommandRequest;
+import org.eclipse.gmf.runtime.notation.Diagram;
+import org.eclipse.ui.IEditorInput;
+import org.eclipse.ui.IWorkbenchPage;
+import org.eclipse.ui.PartInitException;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.actions.WorkspaceModifyOperation;
+import org.eclipse.ui.part.FileEditorInput;
+
+public abstract class SubDiagramActionBase extends DiagramAction {
+
+ protected static final String EMPTY_DIAGRAM_NAME = "<empty-name>";
+
+ public SubDiagramActionBase(IWorkbenchPage workbenchPage) {
+ super(workbenchPage);
+ }
+
+ @Override
+ protected Request createTargetRequest() {
+ return null;
+ }
+
+ @Override
+ protected boolean isSelectionListener() {
+ return true;
+ }
+
+ protected String findDiagramEditorId() {
+ return getDiagramWorkbenchPart().getSite().getId();
+ }
+
+ @Override
+ public boolean isEnabled() {
+ return getDiagramEditPart() != null;
+ }
+
+ protected static String safeGetSubDiagramName(SubDiagramSpec spec) {
+ String name = spec.getName();
+ return name != null ? name : EMPTY_DIAGRAM_NAME;
+
+ }
+
+ protected OpenSubDiagramCommand createOpenCommand(String label, SubDiagramSpec spec) {
+ return createOpenCommand(label, Ref.create(spec));
+ }
+
+ protected OpenSubDiagramCommand createOpenCommand(String label, Ref<SubDiagramSpec> spec) {
+ IEditCommandRequest request = new DomainOnlyEditRequest(getDiagramEditPart());
+ return new OpenSubDiagramCommand(label, spec, findDiagramEditorId(), request);
+ }
+
+ protected static class OpenSubDiagramCommand extends EditElementCommand {
+
+ private final Ref<SubDiagramSpec> mySpecRef;
+
+ private final String myEditorId;
+
+ public OpenSubDiagramCommand(String label, Ref<SubDiagramSpec> spec, String editorId, IEditCommandRequest request) {
+ super(label, spec.get(), request);
+ mySpecRef = spec;
+ myEditorId = editorId;
+ }
+
+ @Override
+ protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
+ SubDiagramSpec spec = mySpecRef.get();
+ if (spec == null) {
+ throw new ExecutionException("Can't find diagram specification");
+ }
+
+ Diagram diagram = spec.getDiagram();
+ SubDiagramSupport support = (SubDiagramSupport) spec.eContainer();
+
+ saveResourceSet(spec);
+
+ IEditorInput editorInput = null;
+ if (spec == support.getMainDiagram()) {
+ //to switch to the main diagram if already open
+ IFile file = WorkspaceSynchronizer.getFile(diagram.eResource());
+ if (file != null) {
+ editorInput = new FileEditorInput(file);
+ }
+ }
+ if (editorInput == null) {
+ URI uri = EcoreUtil.getURI(diagram);
+ String editorName = uri.lastSegment() + '#' + OpenSubDiagramAction.safeGetSubDiagramName(spec);
+ editorInput = new URIEditorInput(uri, editorName);
+ }
+
+ IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
+ try {
+ page.openEditor(editorInput, myEditorId);
+ } catch (PartInitException e) {
+ throw new ExecutionException("Can't Open Sub Diagram", e);
+ }
+
+ return CommandResult.newOKCommandResult();
+ }
+
+ protected void saveResourceSet(final SubDiagramSpec diagramSpec) throws ExecutionException {
+ try {
+ new WorkspaceModifyOperation() {
+
+ @Override
+ protected void execute(IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException {
+ try {
+ for (Resource nextResource : diagramSpec.eResource().getResourceSet().getResources()) {
+ if (nextResource.isLoaded() && !getEditingDomain().isReadOnly(nextResource)) {
+ nextResource.save(DefaultSaveOptions.getSaveOptions());
+ }
+ }
+ } catch (IOException ex) {
+ throw new InvocationTargetException(ex, "Save operation failed");
+ }
+ }
+ }.run(null);
+ } catch (InvocationTargetException e) {
+ throw new ExecutionException("Can't save diagram file", e);
+ } catch (InterruptedException e) {
+ throw new ExecutionException("Can't save diagram file", e);
+ }
+
+ }
+
+ /**
+ * XXX: this is copied from default diagram save options.
+ * It may be a problem if the particular diagram has a custom changed ones
+ */
+ protected static Map<?, ?> getSaveOptions() {
+
+ HashMap<String, Object> saveOptions = new HashMap<String, Object>();
+ saveOptions.put(XMLResource.OPTION_ENCODING, "UTF-8"); //$NON-NLS-1$
+ saveOptions.put(Resource.OPTION_SAVE_ONLY_IF_CHANGED, Resource.OPTION_SAVE_ONLY_IF_CHANGED_MEMORY_BUFFER);
+ return saveOptions;
+ }
+
+ }
+
+ protected static class Ref<T> {
+
+ private T myValue;
+
+ public Ref() {
+ }
+
+ public Ref(T value) {
+ myValue = value;
+ }
+
+ public boolean isNull() {
+ return myValue == null;
+ }
+
+ public T get() {
+ return myValue;
+ }
+
+ public void set(T value) {
+ myValue = value;
+ }
+
+ public static <T> Ref<T> create(T value) {
+ return new Ref<T>(value);
+ }
+
+ public String toString() {
+ return String.valueOf(myValue);
+ }
+ }
+}
diff --git a/examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/popup/SubDiagramsItemProvider.java b/examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/popup/SubDiagramsItemProvider.java
new file mode 100644
index 000000000..052131c60
--- /dev/null
+++ b/examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/popup/SubDiagramsItemProvider.java
@@ -0,0 +1,85 @@
+package org.eclipse.gmf.examples.layers.subdiagrams.popup;
+
+import org.eclipse.gmf.examples.layers.SubDiagramSpec;
+import org.eclipse.gmf.examples.layers.SubDiagramSupport;
+import org.eclipse.gmf.examples.layers.subdiagrams.SubDiagramManager;
+import org.eclipse.gmf.examples.layers.subdiagrams.SubDiagramManagerImpl;
+import org.eclipse.gmf.runtime.common.core.service.IProvider;
+import org.eclipse.gmf.runtime.common.ui.services.action.contributionitem.AbstractContributionItemProvider;
+import org.eclipse.gmf.runtime.common.ui.util.IWorkbenchPartDescriptor;
+import org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart;
+import org.eclipse.gmf.runtime.notation.Diagram;
+import org.eclipse.jface.action.IMenuListener;
+import org.eclipse.jface.action.IMenuManager;
+import org.eclipse.jface.action.MenuManager;
+import org.eclipse.jface.action.Separator;
+import org.eclipse.ui.IWorkbenchPage;
+
+public class SubDiagramsItemProvider extends AbstractContributionItemProvider implements IProvider {
+
+ public static final String MENU_MANAGE_SUB_DIAGRAMS = "menu_manage_subdiagrams"; //$NON-NLS-1$
+
+ @Override
+ protected IMenuManager createMenuManager(String menuId, IWorkbenchPartDescriptor partDescriptor) {
+ if (!MENU_MANAGE_SUB_DIAGRAMS.equals(menuId)) {
+ return super.createMenuManager(menuId, partDescriptor);
+ }
+ MenuManager menuManager = new MenuManager("Open Sub Diagram");
+ MenuBuilder builder = new MenuBuilder(partDescriptor);
+ // XXX: build initial content -- otherwise menu is never shown
+ builder.buildMenu(menuManager);
+
+ menuManager.addMenuListener(builder);
+ return menuManager;
+ }
+
+ private class MenuBuilder implements IMenuListener {
+
+ private final IWorkbenchPartDescriptor myWorkbenchPart;
+
+ public MenuBuilder(IWorkbenchPartDescriptor workbenchPart) {
+ myWorkbenchPart = workbenchPart;
+ }
+
+ public void menuAboutToShow(IMenuManager manager) {
+ buildMenu(manager);
+ }
+
+ public void buildMenu(IMenuManager manager) {
+ manager.removeAll();
+ DiagramEditPart selected = (DiagramEditPart) getSelectedObject(myWorkbenchPart);
+ Diagram diagram = selected.getDiagramView();
+
+ SubDiagramManager subsetManager = SubDiagramManagerImpl.findOrCreateSubsetManager(diagram);
+ SubDiagramSupport support = subsetManager.getSubDiagramSupport();
+ if (support == null) {
+ return;
+ }
+
+ SubDiagramSpec thisSpec = support.findDiagramSpec(diagram);
+ if (thisSpec == null) {
+ return;
+ }
+
+ for (SubDiagramSpec nextSpec : support.getAllDiagrams()) {
+ if (nextSpec == thisSpec) {
+ continue;
+ }
+ OpenSubDiagramAction action = new OpenSubDiagramAction(getWorkbenchPage(), nextSpec);
+ action.init();
+ manager.add(action);
+ }
+
+ manager.add(new Separator());
+
+ CreateSubDiagramAction createNew = new CreateSubDiagramAction(getWorkbenchPage(), thisSpec, support);
+ createNew.init();
+ manager.add(createNew);
+ }
+
+ private IWorkbenchPage getWorkbenchPage() {
+ return myWorkbenchPart.getPartPage();
+ }
+ }
+
+}
diff --git a/examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/popup/SwitchLayerVisibilityAction.java b/examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/popup/SwitchLayerVisibilityAction.java
new file mode 100644
index 000000000..c56d74249
--- /dev/null
+++ b/examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/popup/SwitchLayerVisibilityAction.java
@@ -0,0 +1,210 @@
+package org.eclipse.gmf.examples.layers.subdiagrams.popup;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.core.runtime.IAdaptable;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.gef.Request;
+import org.eclipse.gef.commands.Command;
+import org.eclipse.gef.commands.UnexecutableCommand;
+import org.eclipse.gmf.examples.layers.Layer;
+import org.eclipse.gmf.examples.layers.LayerEnablement;
+import org.eclipse.gmf.examples.layers.LayersFactory;
+import org.eclipse.gmf.examples.layers.SubDiagramSpec;
+import org.eclipse.gmf.runtime.common.core.command.CommandResult;
+import org.eclipse.gmf.runtime.diagram.ui.actions.DiagramAction;
+import org.eclipse.gmf.runtime.diagram.ui.commands.ICommandProxy;
+import org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart;
+import org.eclipse.gmf.runtime.emf.type.core.commands.EditElementCommand;
+import org.eclipse.gmf.runtime.emf.type.core.requests.IEditCommandRequest;
+import org.eclipse.gmf.runtime.notation.Node;
+import org.eclipse.gmf.runtime.notation.View;
+import org.eclipse.ui.IWorkbenchPage;
+
+public class SwitchLayerVisibilityAction extends DiagramAction {
+
+ protected static final String EMPTY_LAYER_NAME = "<empty-name>";
+
+ private final Layer myLayer;
+
+ private final SubDiagramSpec mySpec;
+
+ public SwitchLayerVisibilityAction(IWorkbenchPage workbenchPage, Layer layer, SubDiagramSpec spec) {
+ super(workbenchPage);
+ myLayer = layer;
+ mySpec = spec;
+ }
+
+ @Override
+ protected Request createTargetRequest() {
+ return null;
+ }
+
+ @Override
+ protected boolean isSelectionListener() {
+ return true;
+ }
+
+ @Override
+ protected Command getCommand() {
+ DiagramEditPart diagramEditPart = getDiagramEditPart();
+ if (diagramEditPart == null) {
+ return UnexecutableCommand.INSTANCE;
+ }
+ if (mySpec == null) {
+ return UnexecutableCommand.INSTANCE;
+ }
+
+ IEditCommandRequest request = new DomainOnlyEditRequest(diagramEditPart);
+ return new ICommandProxy(new SwitchLayerVisibility("Switching Layer Visibility", myLayer, mySpec, request));
+ }
+
+ @Override
+ public boolean isEnabled() {
+ return getDiagramEditPart() != null;
+ }
+
+ @Override
+ public void refresh() {
+ super.refresh();
+ setText(calculateText());
+ setChecked(calculateChecked());
+ }
+
+ protected String calculateText() {
+ String name = myLayer.getName();
+ return name != null ? name : EMPTY_LAYER_NAME;
+ }
+
+ protected boolean calculateChecked() {
+ LayerEnablement layerEnablement = mySpec.findLayerEnablement(myLayer);
+ //may be null, but only if disabled
+ return layerEnablement != null && layerEnablement.isVisible();
+ }
+
+ protected static class SwitchLayerVisibility extends EditElementCommand {
+
+ private final Layer myLayer;
+
+ private final SubDiagramSpec myDiagramSpec;
+
+ protected SwitchLayerVisibility(String label, Layer layer, SubDiagramSpec spec, IEditCommandRequest request) {
+ super(label, layer, request);
+ myLayer = layer;
+ myDiagramSpec = spec;
+ }
+
+ @Override
+ protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
+ final NodesBag mappedNodes = mapDiagramNodes();
+ LayerEnablement enablement = myDiagramSpec.findLayerEnablement(myLayer);
+ if (enablement != null && enablement.isVisible()) {
+ enablement.setVisible(false);
+
+ final Map<EObject, List<LayerEnablement>> mappedLayers = mapLayers();
+ for (EObject nextParticipant : enablement.getLayer().getParticipants()) {
+ List<LayerEnablement> allLayers = mappedLayers.get(nextParticipant);
+ if (!hasVisibleLayerOtherThan(allLayers, enablement.getLayer())) {
+ mappedNodes.setAllNodesVisibleFor(nextParticipant, false);
+ }
+ }
+ } else {
+ if (enablement == null) {
+ enablement = LayersFactory.eINSTANCE.createLayerEnablement();
+ enablement.setLayer(myLayer);
+ myDiagramSpec.getDiagramLayers().add(enablement);
+ }
+ enablement.setVisible(true);
+ //if element is in at least one visible layer, it is visible for sure
+ for (EObject nextParticiant : myLayer.getParticipants()) {
+ mappedNodes.setAllNodesVisibleFor(nextParticiant, true);
+ }
+ }
+ return CommandResult.newOKCommandResult(enablement);
+ }
+
+ private static boolean hasVisibleLayerOtherThan(List<LayerEnablement> allLayers, Layer layer) {
+ if (allLayers == null) {
+ return false;
+ }
+ for (LayerEnablement next : allLayers) {
+ if (next.getLayer() == layer) {
+ continue;
+ }
+ if (next.isVisible()) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private NodesBag mapDiagramNodes() {
+ NodesBag result = new NodesBag();
+ LinkedList<View> queue = new LinkedList<View>();
+ queue.add(myDiagramSpec.getDiagram());
+
+ while (!queue.isEmpty()) {
+ View next = queue.removeFirst();
+ if (next.isSetElement() && next instanceof Node) {
+ EObject element = next.getElement();
+ if (element != null) {
+ result.putOne(element, (Node) next);
+ }
+ }
+ @SuppressWarnings("unchecked")
+ List<View> children = next.getChildren();
+ queue.addAll(children);
+ }
+
+ return result;
+ }
+
+ private BagByEObject<LayerEnablement> mapLayers() {
+ BagByEObject<LayerEnablement> result = new BagByEObject<LayerEnablement>();
+ for (LayerEnablement next : myDiagramSpec.getDiagramLayers()) {
+ for (EObject participant : next.getLayer().getParticipants()) {
+ result.putOne(participant, next);
+ }
+ }
+ return result;
+ }
+
+ }
+
+ @SuppressWarnings("serial")
+ private static class BagByEObject<V> extends HashMap<EObject, List<V>> {
+
+ public void putOne(EObject key, V value) {
+ if (key == null) {
+ return;
+ }
+ List<V> values = get(key);
+ if (values == null) {
+ values = new ArrayList<V>();
+ put(key, values);
+ }
+ values.add(value);
+ }
+ }
+
+ @SuppressWarnings("serial")
+ private static class NodesBag extends BagByEObject<Node> {
+
+ public void setAllNodesVisibleFor(EObject object, boolean visible) {
+ List<Node> nodes = this.get(object);
+ if (nodes != null) {
+ for (Node nextNode : nodes) {
+ nextNode.setVisible(visible);
+ }
+ }
+ }
+
+ }
+
+}
diff --git a/examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/popup/SwitchLayerVisibilityItemProvider.java b/examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/popup/SwitchLayerVisibilityItemProvider.java
new file mode 100644
index 000000000..206de8774
--- /dev/null
+++ b/examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/popup/SwitchLayerVisibilityItemProvider.java
@@ -0,0 +1,76 @@
+package org.eclipse.gmf.examples.layers.subdiagrams.popup;
+
+import org.eclipse.gmf.examples.layers.Layer;
+import org.eclipse.gmf.examples.layers.SubDiagramSpec;
+import org.eclipse.gmf.examples.layers.SubDiagramSupport;
+import org.eclipse.gmf.examples.layers.subdiagrams.SubDiagramManager;
+import org.eclipse.gmf.examples.layers.subdiagrams.SubDiagramManagerImpl;
+import org.eclipse.gmf.runtime.common.core.service.IProvider;
+import org.eclipse.gmf.runtime.common.ui.services.action.contributionitem.AbstractContributionItemProvider;
+import org.eclipse.gmf.runtime.common.ui.util.IWorkbenchPartDescriptor;
+import org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart;
+import org.eclipse.gmf.runtime.notation.Diagram;
+import org.eclipse.jface.action.IMenuListener;
+import org.eclipse.jface.action.IMenuManager;
+import org.eclipse.jface.action.MenuManager;
+import org.eclipse.ui.IWorkbenchPage;
+
+public class SwitchLayerVisibilityItemProvider extends AbstractContributionItemProvider implements IProvider {
+
+ public static final String MENU_SET_LAYER_VISIBILITY = "menu_switch_layer_visibility"; //$NON-NLS-1$
+
+ @Override
+ protected IMenuManager createMenuManager(String menuId, IWorkbenchPartDescriptor partDescriptor) {
+ if (!MENU_SET_LAYER_VISIBILITY.equals(menuId)) {
+ return super.createMenuManager(menuId, partDescriptor);
+ }
+ MenuManager menuManager = new MenuManager("Show Layers");
+ MenuBuilder builder = new MenuBuilder(partDescriptor);
+ // XXX: build initial content -- otherwise menu is never shown
+ builder.buildMenu(menuManager);
+
+ menuManager.addMenuListener(builder);
+ return menuManager;
+ }
+
+ private class MenuBuilder implements IMenuListener {
+
+ private final IWorkbenchPartDescriptor myWorkbenchPart;
+
+ public MenuBuilder(IWorkbenchPartDescriptor workbenchPart) {
+ myWorkbenchPart = workbenchPart;
+ }
+
+ public void menuAboutToShow(IMenuManager manager) {
+ buildMenu(manager);
+ }
+
+ public void buildMenu(IMenuManager manager) {
+ manager.removeAll();
+ DiagramEditPart selected = (DiagramEditPart) getSelectedObject(myWorkbenchPart);
+ Diagram diagram = selected.getDiagramView();
+
+ SubDiagramManager subsetManager = SubDiagramManagerImpl.findOrCreateSubsetManager(diagram);
+ SubDiagramSupport support = subsetManager.getSubDiagramSupport();
+ if (support == null) {
+ return;
+ }
+
+ SubDiagramSpec diagramSpec = support.findDiagramSpec(diagram);
+ if (diagramSpec == null) {
+ return;
+ }
+
+ for (Layer next : support.getLayers()) {
+ SwitchLayerVisibilityAction action = new SwitchLayerVisibilityAction(getWorkbenchPage(), next, diagramSpec);
+ action.init();
+ manager.add(action);
+ }
+ }
+
+ private IWorkbenchPage getWorkbenchPage() {
+ return myWorkbenchPart.getPartPage();
+ }
+ }
+
+}
diff --git a/examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/popup/UniqueNameValidator.java b/examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/popup/UniqueNameValidator.java
new file mode 100644
index 000000000..5fdaafd22
--- /dev/null
+++ b/examples/org.eclipse.gmf.examples.layers.subdiagrams/src/org/eclipse/gmf/examples/layers/subdiagrams/popup/UniqueNameValidator.java
@@ -0,0 +1,65 @@
+package org.eclipse.gmf.examples.layers.subdiagrams.popup;
+
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.EStructuralFeature;
+import org.eclipse.jface.dialogs.IInputValidator;
+
+class UniqueNameValidator implements IInputValidator {
+
+ private final Set<String> myAllNames = new HashSet<String>();
+
+ private boolean myAllowEmptyName;
+
+ public UniqueNameValidator(List<? extends EObject> objects, EStructuralFeature nameFeature) {
+ for (EObject next : objects) {
+ Object nextValue = next.eGet(nameFeature);
+ if (nextValue != null) {
+ myAllNames.add(String.valueOf(nextValue));
+ }
+ }
+ }
+
+ public void setAllowEmptyName(boolean allowEmptyName) {
+ myAllowEmptyName = allowEmptyName;
+ }
+
+ @Override
+ public String isValid(String newText) {
+ if (newText == null) {
+ return myAllowEmptyName ? null : "Empty Name Is Not Allowed";
+ }
+ newText = newText.trim();
+ if (newText.length() == 0 && !myAllowEmptyName) {
+ return "Empty Name Is Not Allowed";
+ }
+ if (myAllNames.contains(newText)) {
+ return "This Name Already Exists";
+ }
+ return null;
+ }
+
+ public String guessNewName(String prefix) {
+ for (int i = 0; i < 26; i++) {
+ String nextCandidate = prefix + (char) ('A' + i);
+ if (isValid(nextCandidate) == null) {
+ return nextCandidate;
+ }
+ }
+
+ //wow
+ int i = 0;
+ while (++i < 1000) {
+ String nextCandidate = prefix + i;
+ if (isValid(nextCandidate) == null) {
+ return nextCandidate;
+ }
+ }
+
+ return "<can't guess -- too many names already>";
+ }
+
+} \ No newline at end of file

Back to the top