From f515b310417b386c79d9fbd2500c5373a7179aa7 Mon Sep 17 00:00:00 2001 From: Laurent Wouters Date: Fri, 14 Mar 2014 14:51:50 +0100 Subject: 399860: Papyrus shall enable to customize the creation policy of views https://bugs.eclipse.org/bugs/show_bug.cgi?id=399860 Signed-off-by: Laurent Wouters --- .../META-INF/MANIFEST.MF | 1 + .../infra/gmfdiag/css/notation/CSSDiagramImpl.java | 27 + .../META-INF/MANIFEST.MF | 1 + .../eclipse/papyrus/commands/ICreationCommand.java | 74 +- .../META-INF/MANIFEST.MF | 2 + .../plugin.xml | 95 ++- ...tractPapyrusGmfCreateDiagramCommandHandler.java | 793 +++++++++++---------- .../infra/gmfdiag/common/GmfEditorFactory.java | 3 +- .../handler/DynamicDiagramsMenuContribution.java | 101 +++ .../gmfdiag/common/helper/DiagramEditHelper.java | 178 +++++ .../gmfdiag/common/helper/DiagramPrototype.java | 120 ++++ .../common/helper/GMFDiagramViewTypeHelper.java | 165 +++++ .../gmfdiag/common/helper/NotationHelper.java | 14 +- .../infra/gmfdiag/common/model/NotationUtils.java | 482 ++++++------- .../common/providers/NotationLabelProvider.java | 34 +- .../infra/gmfdiag/common/utils/DiagramUtils.java | 124 ++++ .../controlmode/ControlDiagramsCommand.java | 2 +- .../META-INF/MANIFEST.MF | 4 +- .../plugin.xml | 29 +- .../queries/GetContainedDiagrams.java | 3 +- .../modelexplorer/queries/GetDiagramIcon.java | 5 +- .../modelexplorer/queries/IsDiagramContainer.java | 3 +- .../CreateDiagramWithNavigationHandler.java | 173 ----- .../META-INF/MANIFEST.MF | 3 +- .../model/notation/notation.ctx | 110 +-- .../model/notation/ui/SingleDiagram.xwt | 4 +- .../properties/modelelement/GMFModelElement.java | 88 +++ .../properties/provider/ModelContentProvider.java | 78 ++ .../properties/util/LegacyOwnerObservable.java | 158 ++++ 29 files changed, 1907 insertions(+), 967 deletions(-) create mode 100755 plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/handler/DynamicDiagramsMenuContribution.java create mode 100755 plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/helper/DiagramEditHelper.java create mode 100755 plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/helper/DiagramPrototype.java create mode 100755 plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/helper/GMFDiagramViewTypeHelper.java create mode 100755 plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/utils/DiagramUtils.java delete mode 100644 plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.navigation/src/org/eclipse/papyrus/infra/gmfdiag/navigation/CreateDiagramWithNavigationHandler.java create mode 100755 plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.properties/src/org/eclipse/papyrus/infra/gmfdiag/properties/provider/ModelContentProvider.java create mode 100755 plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.properties/src/org/eclipse/papyrus/infra/gmfdiag/properties/util/LegacyOwnerObservable.java (limited to 'plugins/infra/gmfdiag') diff --git a/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css/META-INF/MANIFEST.MF b/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css/META-INF/MANIFEST.MF index c8f2154ede7..385a581b8bf 100644 --- a/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css/META-INF/MANIFEST.MF +++ b/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css/META-INF/MANIFEST.MF @@ -15,6 +15,7 @@ Require-Bundle: org.eclipse.ui, org.eclipse.papyrus.infra.gmfdiag.common;bundle-version="1.0.0", org.eclipse.papyrus.infra.core;bundle-version="1.0.0", org.eclipse.papyrus.infra.services.markerlistener;bundle-version="1.0.0", + org.eclipse.papyrus.infra.viewpoints.policy;bundle-version="1.0.0", org.eclipse.gmf.runtime.notation.edit;bundle-version="1.7.0", org.w3c.dom.svg;bundle-version="1.1.0" Export-Package: org.eclipse.papyrus.infra.gmfdiag.css, diff --git a/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css/src/org/eclipse/papyrus/infra/gmfdiag/css/notation/CSSDiagramImpl.java b/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css/src/org/eclipse/papyrus/infra/gmfdiag/css/notation/CSSDiagramImpl.java index 28183752898..62f2a628285 100644 --- a/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css/src/org/eclipse/papyrus/infra/gmfdiag/css/notation/CSSDiagramImpl.java +++ b/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css/src/org/eclipse/papyrus/infra/gmfdiag/css/notation/CSSDiagramImpl.java @@ -28,6 +28,11 @@ import org.eclipse.papyrus.infra.gmfdiag.css.engine.DiagramCSSEngine; import org.eclipse.papyrus.infra.gmfdiag.css.engine.ExtendedCSSEngine; import org.eclipse.papyrus.infra.gmfdiag.css.resource.CSSNotationResource; import org.eclipse.papyrus.infra.gmfdiag.css.stylesheets.StyleSheet; +import org.eclipse.papyrus.infra.gmfdiag.css.stylesheets.StyleSheetReference; +import org.eclipse.papyrus.infra.gmfdiag.css.stylesheets.StylesheetsFactory; +import org.eclipse.papyrus.infra.viewpoints.configuration.PapyrusDiagram; +import org.eclipse.papyrus.infra.viewpoints.configuration.PapyrusView; +import org.eclipse.papyrus.infra.viewpoints.policy.ViewPrototype; /** * Default implementation for CSSDiagram @@ -84,9 +89,31 @@ public class CSSDiagramImpl extends DiagramImpl implements CSSDiagram { } } + StyleSheet s = getViewpointDefinedStylesheet(); + if (s != null) + result.add(s); + return result; } + private StyleSheet getViewpointDefinedStylesheet() { + ViewPrototype proto = ViewPrototype.get(this); + if (proto == null) { + return null; + } + PapyrusView conf = proto.getConfiguration(); + if (conf == null || !(conf instanceof PapyrusDiagram)) { + return null; + } + String path = ((PapyrusDiagram) conf).getCustomStyle(); + if (path == null || path.isEmpty()) { + return null; + } + StyleSheetReference ref = StylesheetsFactory.eINSTANCE.createStyleSheetReference(); + ref.setPath(path); + return ref; + } + private class DiagramDisposeListener extends AdapterImpl { @Override diff --git a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.commands/META-INF/MANIFEST.MF b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.commands/META-INF/MANIFEST.MF index 8870a72c112..493e7348a61 100644 --- a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.commands/META-INF/MANIFEST.MF +++ b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.commands/META-INF/MANIFEST.MF @@ -13,6 +13,7 @@ Require-Bundle: org.eclipse.ui.workbench, org.eclipse.papyrus.infra.core;bundle-version="1.0.0", org.eclipse.papyrus.infra.emf;bundle-version="1.0.0", org.eclipse.papyrus.infra.core.sasheditor;bundle-version="1.0.0", + org.eclipse.papyrus.infra.viewpoints.policy;bundle-version="1.0.0", org.eclipse.core.expressions;bundle-version="3.4.500", org.eclipse.gmf.runtime.diagram.ui.resources.editor Bundle-Vendor: %providerName diff --git a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.commands/src/org/eclipse/papyrus/commands/ICreationCommand.java b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.commands/src/org/eclipse/papyrus/commands/ICreationCommand.java index cf4e726d3f5..532f4ebe38b 100644 --- a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.commands/src/org/eclipse/papyrus/commands/ICreationCommand.java +++ b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.commands/src/org/eclipse/papyrus/commands/ICreationCommand.java @@ -12,7 +12,10 @@ package org.eclipse.papyrus.commands; import org.eclipse.emf.ecore.EObject; import org.eclipse.gmf.runtime.common.core.command.ICommand; +import org.eclipse.gmf.runtime.notation.Diagram; import org.eclipse.papyrus.infra.core.resource.ModelSet; +import org.eclipse.papyrus.infra.viewpoints.configuration.PapyrusDiagram; +import org.eclipse.papyrus.infra.viewpoints.policy.ViewPrototype; /** * Define a command use to create new diagram. It use to provide Eclipse @@ -24,33 +27,66 @@ import org.eclipse.papyrus.infra.core.resource.ModelSet; public interface ICreationCommand { /** - * Create a new diagram in diResourceSet with the given name and open it. + * Create a diagram. + * This method will try to find an adequate view for the passed arguments * * @param modelSet - * the resourceSet containing all diagrams - * @param container - * of the diagram, if container is null, the diagram is contained - * by the top level container - * @param diagramName - * the name of the new diagram or null to use a dialog to choose - * the name + * the current model set + * @param owner + * the diagram's owner and root element + * @param name + * the diagram's name + * @return the created diagram, or null if the creation failed */ - public void createDiagram(ModelSet modelSet, EObject container, String diagramName); + public Diagram createDiagram(ModelSet modelSet, EObject owner, String name); /** - * Get a command to create a new diagram in diResourceSet with the given - * name. + * Create a diagram. * * @param modelSet - * the resourceSet containing all diagrams - * @param container - * of the diagram, if container is null, the diagram is contained - * by the top level container - * @param diagramName - * the name of the new diagram or null to use a dialog to choose - * the name + * the current model set + * @param owner + * the diagram's owner + * @param element + * the diagram's model element + * @param prototype + * the diagram's prototype + * @param name + * the diagram's name + * @return the created diagram, or null if the creation failed */ - public ICommand getCreateDiagramCommand(final ModelSet modelSet, final EObject container, final String diagramName); + public Diagram createDiagram(ModelSet modelSet, EObject owner, EObject element, ViewPrototype prototype, String name); + + /** + * Gets the GMF command for the diagram creation + * This method will try to find an adequate view for the passed arguments + * + * @param modelSet + * the current model set + * @param owner + * the diagram's owner and root element + * @param name + * the diagram's name + * @return the created diagram, or null if the creation failed + */ + public ICommand getCreateDiagramCommand(ModelSet modelSet, EObject owner, String name); + + /** + * Gets the GMF command for the diagram creation + * + * @param modelSet + * the current model set + * @param owner + * the diagram's owner + * @param element + * the diagram's model element + * @param prototype + * the diagram's prototype + * @param name + * the diagram's name + * @return the created diagram, or null if the creation failed + */ + public ICommand getCreateDiagramCommand(ModelSet modelSet, EObject owner, EObject element, ViewPrototype prototype, String name); /** * Get the type of the diagram to create. diff --git a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/META-INF/MANIFEST.MF b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/META-INF/MANIFEST.MF index 2df85a9ff59..5d782327186 100644 --- a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/META-INF/MANIFEST.MF +++ b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/META-INF/MANIFEST.MF @@ -55,6 +55,8 @@ Require-Bundle: org.eclipse.emf.edit.ui;bundle-version="2.5.0", org.eclipse.papyrus.emf.facet.custom.ui;bundle-version="0.4.0", org.eclipse.papyrus.infra.services.decoration;bundle-version="1.0.0", org.eclipse.papyrus.infra.services.markerlistener;bundle-version="1.0.0", + org.eclipse.papyrus.infra.viewpoints.configuration;bundle-version="1.0.0", + org.eclipse.papyrus.infra.viewpoints.policy;bundle-version="1.0.0", org.eclipse.papyrus.uml.tools Bundle-Vendor: %providerName Bundle-ActivationPolicy: lazy diff --git a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/plugin.xml b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/plugin.xml index 2d220dd6389..9ed75968eea 100644 --- a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/plugin.xml +++ b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/plugin.xml @@ -7,8 +7,8 @@ - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/AbstractPapyrusGmfCreateDiagramCommandHandler.java b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/AbstractPapyrusGmfCreateDiagramCommandHandler.java index 103b1eb5ee7..6ebafce763c 100644 --- a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/AbstractPapyrusGmfCreateDiagramCommandHandler.java +++ b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/AbstractPapyrusGmfCreateDiagramCommandHandler.java @@ -1,382 +1,411 @@ -/***************************************************************************** - * Copyright (c) 2008, 2013 CEA LIST. - * - * - * 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: - * Cedric Dumoulin Cedric.dumoulin@lifl.fr - Initial API and implementation - * Christian W. Damus (CEA) - only calculate affected files for workspace resources (CDO) - * - *****************************************************************************/ -package org.eclipse.papyrus.infra.gmfdiag.common; - -import java.util.ArrayList; - -import org.eclipse.core.commands.AbstractHandler; -import org.eclipse.core.commands.ExecutionEvent; -import org.eclipse.core.commands.ExecutionException; -import org.eclipse.core.commands.IHandler; -import org.eclipse.core.resources.IFile; -import org.eclipse.core.resources.ResourcesPlugin; -import org.eclipse.core.runtime.IAdaptable; -import org.eclipse.core.runtime.IProgressMonitor; -import org.eclipse.core.runtime.IStatus; -import org.eclipse.core.runtime.Path; -import org.eclipse.emf.ecore.EObject; -import org.eclipse.emf.ecore.resource.Resource; -import org.eclipse.emf.transaction.TransactionalEditingDomain; -import org.eclipse.gmf.runtime.common.core.command.CommandResult; -import org.eclipse.gmf.runtime.common.core.command.CompositeCommand; -import org.eclipse.gmf.runtime.common.core.command.ICommand; -import org.eclipse.gmf.runtime.diagram.core.preferences.PreferencesHint; -import org.eclipse.gmf.runtime.diagram.core.services.ViewService; -import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand; -import org.eclipse.gmf.runtime.notation.Diagram; -import org.eclipse.jface.dialogs.InputDialog; -import org.eclipse.jface.viewers.ISelection; -import org.eclipse.jface.viewers.IStructuredSelection; -import org.eclipse.jface.window.Window; -import org.eclipse.papyrus.commands.ICreationCommand; -import org.eclipse.papyrus.commands.OpenDiagramCommand; -import org.eclipse.papyrus.commands.wrappers.GMFtoEMFCommandWrapper; -import org.eclipse.papyrus.infra.core.editor.IMultiDiagramEditor; -import org.eclipse.papyrus.infra.core.resource.ModelSet; -import org.eclipse.papyrus.infra.core.resource.sasheditor.DiModelUtils; -import org.eclipse.papyrus.infra.core.sasheditor.contentprovider.IPageManager; -import org.eclipse.papyrus.infra.core.sasheditor.contentprovider.ISashWindowsContentProvider; -import org.eclipse.papyrus.infra.core.services.ServiceException; -import org.eclipse.papyrus.infra.core.services.ServicesRegistry; -import org.eclipse.papyrus.infra.emf.utils.EMFHelper; -import org.eclipse.papyrus.infra.emf.utils.ServiceUtilsForHandlers; -import org.eclipse.papyrus.infra.emf.utils.ServiceUtilsForResource; -import org.eclipse.papyrus.infra.gmfdiag.common.model.NotationUtils; -import org.eclipse.papyrus.uml.tools.model.UmlUtils; -import org.eclipse.swt.widgets.Display; -import org.eclipse.ui.IEditorPart; -import org.eclipse.ui.IWorkbenchPage; -import org.eclipse.ui.PlatformUI; - -/** - * Command creating a new GMF diagram in Papyrus. This command is intended to be used in eclipse - * extensions. - * - * Commands to create a GMF Diagram can subclass this class. There is two kinds of commands: - - * Eclipse handlers issuing commands (toolbar, menu, ...). This commands can find the active editor - * by using the Worbench.getActivePArt(). The entry point is {@link #execute(ExecutionEvent)}. - - * Commands called during editor initializing (like wizard). This commands require the diResourceSet - * to work. The entry point is {@link #createDiagram(DiResourceSet, EObject, String)} - * - * @author cedric dumoulin - * @author Jerome Benois - */ -public abstract class AbstractPapyrusGmfCreateDiagramCommandHandler extends AbstractHandler implements IHandler, ICreationCommand { - - /** - * Method called when the command is invoked. - */ - public Object execute(ExecutionEvent event) throws ExecutionException { - - ServicesRegistry registry; - try { - registry = ServiceUtilsForHandlers.getInstance().getServiceRegistry(event); - } catch (ServiceException ex) { - throw new ExecutionException("Cannot retrieve the ServicesRegistry", ex); - } - - EObject container = null; - // if editor is open and active - container = getSelectedElement(); - runAsTransaction(container, registry); - return null; - } - - /** - * Create a new class diagram - * - * @param sharedObjects - * @param container - * The uml element to which the diagram should be attached, if possible. - * @throws ExecutionException - */ - protected void runAsTransaction(EObject container, ServicesRegistry registry) throws ExecutionException { - - ModelSet modelSet; - try { - modelSet = registry.getService(ModelSet.class); - } catch (ServiceException e) { - throw new ExecutionException("Can't get ModelSet", e); - } - - runAsTransaction(modelSet, container, null); - } - - /** - * Create a new gmf diagram - * - * @param sharedObjects - * @param container - * The eObject to which the diagram should be attached, if possible. - */ - protected void runAsTransaction(final ModelSet modelSet, final EObject container, String name) { - TransactionalEditingDomain dom = modelSet.getTransactionalEditingDomain(); - CompositeCommand cmd = new CompositeCommand("Create diagram"); - ICommand createCmd = getCreateDiagramCommand(modelSet, container, name); - cmd.add(createCmd); - cmd.add(new OpenDiagramCommand(dom, createCmd)); - - dom.getCommandStack().execute(new GMFtoEMFCommandWrapper(cmd)); - } - - /** - * Get the root element associated with canvas. - */ - protected EObject getRootElement(Resource modelResource) { - EObject rootElement = null; - if(modelResource != null && modelResource.getContents() != null && modelResource.getContents().size() > 0) { - Object root = modelResource.getContents().get(0); - if(root instanceof EObject) { - rootElement = (EObject)root; - } - } - - return rootElement; - } - - /** - * Store model element in the resource. - */ - protected void attachModelToResource(EObject root, Resource resource) { - resource.getContents().add(root); - } - - /** - * Get the type of the diagram to create. - * - * @return diagram type - */ - public String getCreatedDiagramType() { - return getDiagramNotationID(); - } - - /** - * @return - */ - abstract protected String getDiagramNotationID(); - - /** - * @return - */ - abstract protected PreferencesHint getPreferenceHint(); - - /** - * Get the name used for diagram. - * - * @return - */ - abstract protected String getDefaultDiagramName(); - - /** - * Get currently selected element. - * - * @return The currently selected element, or null if any. - */ - protected EObject getSelectedElement() { - EObject eObject = null; - Object selection = getCurrentSelection(); - if(selection != null) { - eObject = EMFHelper.getEObject(selection); - } - return eObject; - } - - /** - * Get current selection first element. - * - * @return the selected element or null. - */ - private Object getCurrentSelection() { - ISelection selection = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection(); - if(selection instanceof IStructuredSelection) { - IStructuredSelection structuredSelection = (IStructuredSelection)selection; - return structuredSelection.getFirstElement(); - } - - return null; - } - - /** - * Create a diagram. - * - * @param diagramResource - * the diagram resource - * @param owner - * the diagram container - * @param name - * the diagram name - * @return - */ - protected Diagram createDiagram(Resource diagramResource, EObject owner, String name) { - // create diagram - Diagram diagram = ViewService.createDiagram(owner, getDiagramNotationID(), getPreferenceHint()); - if(diagram != null) { - diagram.setName(name); - diagram.setElement(owner); - initializeDiagram(diagram); - diagramResource.getContents().add(diagram); - } - return diagram; - } - - protected void initializeDiagram(EObject diagram) { - }; - - /** - * Get the current MultiDiagramEditor. - * - * @return - */ - protected IMultiDiagramEditor getMultiDiagramEditor() { - IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); - IEditorPart editorPart = page.getActiveEditor(); - return (IMultiDiagramEditor)editorPart; - } - - /** - * Open popup to enter the new diagram name - * - * @param defaultValue - * the default value - * @return the entered diagram name - */ - protected String openDiagramNameDialog(String defaultValue) { - if(defaultValue == null) { - defaultValue = ""; - } - - InputDialog inputDialog = new InputDialog(Display.getCurrent().getActiveShell(), Messages.AbstractPapyrusGmfCreateDiagramCommandHandler_SelectNewDiagramName, Messages.AbstractPapyrusGmfCreateDiagramCommandHandler_NewDiagramName, defaultValue, null); - int result = inputDialog.open(); - - if(result == Window.OK) { - String name = inputDialog.getValue(); - if(name == null || name.length() == 0) { - name = defaultValue; - } - return name; - } - return null; - } - - /** - * {@inheritDoc} - */ - public void createDiagram(final ModelSet modelSet, final EObject container, final String diagramName) { - runAsTransaction(modelSet, container, diagramName); - } - - /** - * {@inheritDoc} - */ - public ICommand getCreateDiagramCommand(final ModelSet modelSet, final EObject container, final String diagramName) { - //Diagram creation should not change the semantic resource - final Resource modelResource = UmlUtils.getUmlResource(modelSet); - final Resource notationResource = NotationUtils.getNotationResource(modelSet); - final Resource diResource = DiModelUtils.getDiResource(modelSet); - - ArrayList modifiedFiles = new ArrayList(); - if (notationResource.getURI().isPlatformResource()) { - modifiedFiles.add(ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(notationResource.getURI().toPlatformString(true)))); - } - if (diResource.getURI().isPlatformResource()) { - modifiedFiles.add(ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(diResource.getURI().toPlatformString(true)))); - } - - return new AbstractTransactionalCommand(modelSet.getTransactionalEditingDomain(), Messages.AbstractPapyrusGmfCreateDiagramCommandHandler_CreateDiagramCommandLabel, modifiedFiles) { - - protected Diagram diagram; - - protected EObject modelElement; - - @Override - protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException { - - String name = diagramName; - if(name == null) { - name = openDiagramNameDialog(getDefaultDiagramName()); - } - // canceled - if(name == null) { - return CommandResult.newCancelledCommandResult(); - } - - EObject model = container; - if(model == null) { - model = getRootElement(modelResource); - attachModelToResource(model, modelResource); - } - - diagram = createDiagram(notationResource, model, name); - - if(diagram != null) { - try { - IPageManager pageManager = ServiceUtilsForResource.getInstance().getIPageManager(diResource); - pageManager.addPage(diagram); - return CommandResult.newOKCommandResult(diagram); - } catch (ServiceException ex) { - Activator.log.error(ex); - return CommandResult.newErrorCommandResult(ex); - } - } - return CommandResult.newErrorCommandResult("Error during diagram creation"); - } - - @Override - protected IStatus doUndo(IProgressMonitor monitor, IAdaptable info) throws ExecutionException { - // the undo corresponds to a destroy diagram command - // during diagram creation no adapters are set to the diagram so the setElement is not registered - // to remove the cross reference using the element reference it is better to use the destroy element command - // DestroyElementPapyrusCommand depc = (diagram != null) ? new DestroyElementPapyrusCommand(new DestroyElementRequest(diagram, false)) : null; - IStatus status = super.doUndo(monitor, info); - - modelElement = diagram.getElement(); - diagram.setElement(null); - return status; - } - - @Override - protected IStatus doRedo(IProgressMonitor monitor, IAdaptable info) throws ExecutionException { - diagram.setElement(modelElement); - IStatus status = super.doRedo(monitor, info); - return status; - } - }; - } - - /** - * Get the ISashWindowsContentProvider from the main editor. - * - * @return - */ - protected ISashWindowsContentProvider getISashWindowsContentProvider(ServicesRegistry registry) { - try { - return registry.getService(ISashWindowsContentProvider.class); - } catch (ServiceException ex) { - Activator.log.error(ex); - return null; - } - } - - /** - * Check if the creation of this diagram is strongly attached to its parent or if it can be reassigned after creation. - * - * @return true if parent can be reassigned - */ - public boolean isParentReassignable() { - // yes by default - return true; - } - -} +/***************************************************************************** + * Copyright (c) 2008, 2013 CEA LIST. + * + * + * 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: + * Cedric Dumoulin Cedric.dumoulin@lifl.fr - Initial API and implementation + * Christian W. Damus (CEA) - only calculate affected files for workspace resources (CDO) + * Laurent Wouters (CEA) - laurent.wouters@cea.fr - Refactoring for viewpoints + * + *****************************************************************************/ +package org.eclipse.papyrus.infra.gmfdiag.common; + +import java.util.ArrayList; + +import org.eclipse.core.commands.AbstractHandler; +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.core.commands.ExecutionException; +import org.eclipse.core.commands.IHandler; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.runtime.IAdaptable; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Path; +import org.eclipse.emf.ecore.EClass; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.emf.transaction.TransactionalEditingDomain; +import org.eclipse.gmf.runtime.common.core.command.CommandResult; +import org.eclipse.gmf.runtime.common.core.command.CompositeCommand; +import org.eclipse.gmf.runtime.common.core.command.ICommand; +import org.eclipse.gmf.runtime.diagram.core.preferences.PreferencesHint; +import org.eclipse.gmf.runtime.diagram.core.services.ViewService; +import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand; +import org.eclipse.gmf.runtime.notation.Diagram; +import org.eclipse.jface.dialogs.InputDialog; +import org.eclipse.jface.window.Window; +import org.eclipse.papyrus.commands.ICreationCommand; +import org.eclipse.papyrus.commands.OpenDiagramCommand; +import org.eclipse.papyrus.commands.wrappers.GMFtoEMFCommandWrapper; +import org.eclipse.papyrus.infra.core.editor.IMultiDiagramEditor; +import org.eclipse.papyrus.infra.core.resource.ModelSet; +import org.eclipse.papyrus.infra.core.resource.sasheditor.DiModelUtils; +import org.eclipse.papyrus.infra.core.sasheditor.contentprovider.IPageManager; +import org.eclipse.papyrus.infra.core.services.ServiceException; +import org.eclipse.papyrus.infra.emf.utils.ServiceUtilsForResource; +import org.eclipse.papyrus.infra.gmfdiag.common.model.NotationUtils; +import org.eclipse.papyrus.infra.gmfdiag.common.utils.DiagramUtils; +import org.eclipse.papyrus.infra.viewpoints.configuration.ModelRule; +import org.eclipse.papyrus.infra.viewpoints.configuration.ui.IModelElementValidator; +import org.eclipse.papyrus.infra.viewpoints.configuration.ui.ModelElementSelectionDialog; +import org.eclipse.papyrus.infra.viewpoints.policy.PolicyChecker; +import org.eclipse.papyrus.infra.viewpoints.policy.ViewPrototype; +import org.eclipse.papyrus.uml.tools.model.UmlUtils; +import org.eclipse.swt.widgets.Display; +import org.eclipse.ui.IEditorPart; +import org.eclipse.ui.IWorkbenchPage; +import org.eclipse.ui.PlatformUI; + +/** + * Command creating a new GMF diagram in Papyrus. This command is intended to be used in eclipse + * extensions. + * + * Commands to create a GMF Diagram can subclass this class. There is two kinds of commands: - + * Eclipse handlers issuing commands (toolbar, menu, ...). This commands can find the active editor + * by using the Worbench.getActivePArt(). The entry point is {@link #execute(ExecutionEvent)}. - + * Commands called during editor initializing (like wizard). This commands require the diResourceSet + * to work. The entry point is {@link #createDiagram(Resource, EObject, String)} + * + * @author cedric dumoulin + * @author Jerome Benois + */ +public abstract class AbstractPapyrusGmfCreateDiagramCommandHandler extends AbstractHandler implements IHandler, ICreationCommand { + + /** + * Inner class for the creation of diagrams + * + * @author Laurent Wouters + * + */ + private class Creator { + private ModelSet modelSet; + private EObject owner; + private EObject element; + private ViewPrototype prototype; + private String name; + + public Creator(ModelSet modelSet, EObject owner, EObject element, ViewPrototype prototype, String name) { + this.modelSet = modelSet; + this.owner = owner; + this.element = element; + this.prototype = prototype; + this.name = name; + } + + private Diagram createDiagram() throws ServiceException { + Resource modelResource = UmlUtils.getUmlResource(modelSet); + Resource notationResource = NotationUtils.getNotationResource(modelSet); + Resource diResource = DiModelUtils.getDiResource(modelSet); + + if (owner == null) { + owner = getRootElement(modelResource); + attachModelToResource(owner, modelResource); + } + + if (!PolicyChecker.getCurrent().canOwnNewView(owner, prototype)) { + ModelElementSelectionDialog dialog = new ModelElementSelectionDialog(Display.getCurrent().getActiveShell(), getMultiDiagramEditor().getServicesRegistry(), "Select an appropriate owner for the diagram:", getRootElement(modelResource), owner, + new IModelElementValidator() { + public String isSelectable(EObject element) { + if (PolicyChecker.getCurrent().canOwnNewView(owner, prototype)) + return null; + return "This element cannot own the diagram."; + } + }); + int result = dialog.open(); + if (result != Window.OK) + return null; + owner = dialog.getSelection(); + } + + element = prototype.getRootFor(owner); + if (!PolicyChecker.getCurrent().canHaveNewView(element, owner, prototype)) { + StringBuilder builder = new StringBuilder("Select a root element for the diagram. Allowed types: "); + if (prototype.getConfiguration() == null) { + builder.append(""); + } else if (prototype.getConfiguration().getModelRules().size() == 0) { + builder.append(""); + } else { + boolean first = true; + for (ModelRule rule : prototype.getConfiguration().getModelRules()) { + EClass model = rule.getElement(); + if (model != null) { + if (!first) { + builder.append(", "); + } + first = false; + builder.append(model.getName()); + } + } + } + builder.append("."); + ModelElementSelectionDialog dialog = new ModelElementSelectionDialog(Display.getCurrent().getActiveShell(), getMultiDiagramEditor().getServicesRegistry(), builder.toString(), getRootElement(modelResource), element, + new IModelElementValidator() { + public String isSelectable(EObject element) { + if (PolicyChecker.getCurrent().canHaveNewView(element, owner, prototype)) + return null; + return "This element cannot be the root element of the diagram."; + } + }); + int result = dialog.open(); + if (result != Window.OK) + return null; + element = dialog.getSelection(); + } + + if (name == null) { + name = openDiagramNameDialog(prototype.isNatural() ? getDefaultDiagramName() : "New" + prototype.getLabel().replace(" ", "")); + } + // canceled + if (name == null) { + return null; + } + + Diagram diagram = doCreateDiagram(notationResource, owner, element, prototype, name); + + if (diagram != null) { + IPageManager pageManager = ServiceUtilsForResource.getInstance().getIPageManager(diResource); + pageManager.addPage(diagram); + + } + return diagram; + } + } + + + /** + * Get the root element associated with canvas. + */ + private EObject getRootElement(Resource modelResource) { + EObject rootElement = null; + if (modelResource != null && modelResource.getContents() != null && modelResource.getContents().size() > 0) { + Object root = modelResource.getContents().get(0); + if (root instanceof EObject) { + rootElement = (EObject) root; + } + } + + return rootElement; + } + + /** + * Store model element in the resource. + */ + private void attachModelToResource(EObject root, Resource resource) { + resource.getContents().add(root); + } + + /** + * Get the current MultiDiagramEditor. + */ + private IMultiDiagramEditor getMultiDiagramEditor() { + IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); + IEditorPart editorPart = page.getActiveEditor(); + return (IMultiDiagramEditor) editorPart; + } + + /** + * Open popup to enter the new diagram name + * + * @param defaultValue + * the default value + * @return the entered diagram name + */ + private String openDiagramNameDialog(String defaultValue) { + if (defaultValue == null) { + defaultValue = ""; + } + + InputDialog inputDialog = new InputDialog(Display.getCurrent().getActiveShell(), Messages.AbstractPapyrusGmfCreateDiagramCommandHandler_SelectNewDiagramName, Messages.AbstractPapyrusGmfCreateDiagramCommandHandler_NewDiagramName, defaultValue, null); + int result = inputDialog.open(); + + if (result == Window.OK) { + String name = inputDialog.getValue(); + if (name == null || name.length() == 0) { + name = defaultValue; + } + return name; + } + return null; + } + + + /* + * (non-Javadoc) + * + * @see org.eclipse.papyrus.commands.ICreationCommand#createDiagram(org.eclipse.emf.ecore.resource.Resource, org.eclipse.emf.ecore.EObject, java.lang.String) + */ + public final Diagram createDiagram(ModelSet modelSet, EObject owner, String name) { + ViewPrototype proto = ViewPrototype.get(getCreatedDiagramType(), owner, owner); + if (proto == null) + return null; + return createDiagram(modelSet, owner, owner, proto, name); + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.papyrus.commands.ICreationCommand#createDiagram(org.eclipse.emf.ecore.resource.Resource, org.eclipse.emf.ecore.EObject, org.eclipse.emf.ecore.EObject, org.eclipse.papyrus.infra.viewpoints.policy.ViewPrototype, java.lang.String) + */ + public final Diagram createDiagram(ModelSet modelSet, EObject owner, EObject element, ViewPrototype prototype, String name) { + ICommand createCmd = getCreateDiagramCommand(modelSet, owner, element, prototype, name); + TransactionalEditingDomain dom = modelSet.getTransactionalEditingDomain(); + CompositeCommand cmd = new CompositeCommand("Create diagram"); + cmd.add(createCmd); + cmd.add(new OpenDiagramCommand(dom, createCmd)); + dom.getCommandStack().execute(new GMFtoEMFCommandWrapper(cmd)); + return null; + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.papyrus.commands.ICreationCommand#getCreateDiagramCommand(org.eclipse.papyrus.infra.core.resource.ModelSet, org.eclipse.emf.ecore.EObject, java.lang.String) + */ + public final ICommand getCreateDiagramCommand(ModelSet modelSet, EObject owner, String name) { + ViewPrototype proto = ViewPrototype.get(getCreatedDiagramType(), owner, owner); + if (proto == null) + return null; + return getCreateDiagramCommand(modelSet, owner, owner, proto, name); + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.papyrus.commands.ICreationCommand#getCreateDiagramCommand(org.eclipse.papyrus.infra.core.resource.ModelSet, org.eclipse.emf.ecore.EObject, org.eclipse.emf.ecore.EObject, org.eclipse.papyrus.infra.viewpoints.policy.ViewPrototype, + * java.lang.String) + */ + public final ICommand getCreateDiagramCommand(final ModelSet modelSet, final EObject owner, final EObject element, final ViewPrototype prototype, final String name) { + // Diagram creation should not change the semantic resource + final Resource notationResource = NotationUtils.getNotationResource(modelSet); + final Resource diResource = DiModelUtils.getDiResource(modelSet); + + ArrayList modifiedFiles = new ArrayList(); + if (notationResource.getURI().isPlatformResource()) { + modifiedFiles.add(ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(notationResource.getURI().toPlatformString(true)))); + } + if (diResource.getURI().isPlatformResource()) { + modifiedFiles.add(ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(diResource.getURI().toPlatformString(true)))); + } + + return new AbstractTransactionalCommand(modelSet.getTransactionalEditingDomain(), Messages.AbstractPapyrusGmfCreateDiagramCommandHandler_CreateDiagramCommandLabel, modifiedFiles) { + private Diagram diagram = null; + private EObject diagramElement = null; + private EObject diagramOwner = null; + + @Override + protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException { + Creator creator = new Creator(modelSet, owner, element, prototype, name); + try { + diagram = creator.createDiagram(); + if (diagram == null) + return CommandResult.newCancelledCommandResult(); + diagramElement = diagram.getElement(); + diagramOwner = DiagramUtils.getOwner(diagram); + return CommandResult.newOKCommandResult(diagram); + } catch (ServiceException e) { + Activator.log.error(e); + } + return CommandResult.newErrorCommandResult("Error during diagram creation"); + } + + @Override + protected IStatus doUndo(IProgressMonitor monitor, IAdaptable info) throws ExecutionException { + // the undo corresponds to a destroy diagram command + // during diagram creation no adapters are set to the diagram so the setElement is not registered + // to remove the cross reference using the element reference it is better to use the destroy element command + // DestroyElementPapyrusCommand depc = (diagram != null) ? new DestroyElementPapyrusCommand(new DestroyElementRequest(diagram, false)) : null; + IStatus status = super.doUndo(monitor, info); + diagram.setElement(null); + return status; + } + + @Override + protected IStatus doRedo(IProgressMonitor monitor, IAdaptable info) throws ExecutionException { + diagram.setElement(diagramElement); + DiagramUtils.setOwner(diagram, diagramOwner); + IStatus status = super.doRedo(monitor, info); + return status; + } + }; + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.papyrus.commands.ICreationCommand#getCreatedDiagramType() + */ + public String getCreatedDiagramType() { + return getDiagramNotationID(); + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.papyrus.commands.ICreationCommand#isParentReassignable() + */ + public boolean isParentReassignable() { + // yes by default + return true; + } + + + + public Object execute(ExecutionEvent event) throws ExecutionException { + // This method should not be called, use the execute(ExecutionEvent, ViewPrototype, String) method. + throw new UnsupportedOperationException(); + } + + + protected abstract String getDiagramNotationID(); + + protected abstract PreferencesHint getPreferenceHint(); + + protected abstract String getDefaultDiagramName(); + + /** + * Overridable method that effectively create the diagram with the given validated parameters + * + * @param diagramResource + * the diagram resource + * @param owner + * the diagram's owner + * @param element + * the diagram's model element + * @param prototype + * the diagram's prototype + * @param name + * the diagram's name + * @return the created diagram, or null if the creation failed + */ + protected Diagram doCreateDiagram(Resource diagramResource, EObject owner, EObject element, ViewPrototype prototype, String name) { + // create diagram + Diagram diagram = ViewService.createDiagram(element, getDiagramNotationID(), getPreferenceHint()); + if (diagram != null) { + diagram.setName(name); + diagram.setElement(element); + DiagramUtils.setOwner(diagram, owner); + if (!prototype.isNatural()) + DiagramUtils.setPrototype(diagram, prototype); + initializeDiagram(diagram); + diagramResource.getContents().add(diagram); + } + return diagram; + } + + /** + * Overridable method for the initialization of create diagrams + * + * @param diagram + * the created diagram + */ + protected void initializeDiagram(EObject diagram) { + + }; +} + diff --git a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/GmfEditorFactory.java b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/GmfEditorFactory.java index 0ade2777252..5c7b9e57d0e 100644 --- a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/GmfEditorFactory.java +++ b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/GmfEditorFactory.java @@ -26,6 +26,7 @@ import org.eclipse.papyrus.infra.core.sasheditor.contentprovider.IEditorModel; import org.eclipse.papyrus.infra.core.sasheditor.contentprovider.IPageModel; import org.eclipse.papyrus.infra.core.services.ServiceException; import org.eclipse.papyrus.infra.core.services.ServicesRegistry; +import org.eclipse.papyrus.infra.gmfdiag.common.utils.DiagramUtils; import org.eclipse.swt.graphics.Image; import org.eclipse.ui.IEditorPart; import org.eclipse.ui.PartInitException; @@ -201,7 +202,7 @@ public class GmfEditorFactory extends AbstractEditorFactory { * */ public Image getTabIcon() { - ImageDescriptor imageDescriptor = getEditorDescriptor().getIcon(); + ImageDescriptor imageDescriptor = DiagramUtils.getPrototype(diagram).getIconDescriptor(); if(imageDescriptor == null) return null; diff --git a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/handler/DynamicDiagramsMenuContribution.java b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/handler/DynamicDiagramsMenuContribution.java new file mode 100755 index 00000000000..cd6e89e6823 --- /dev/null +++ b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/handler/DynamicDiagramsMenuContribution.java @@ -0,0 +1,101 @@ +/***************************************************************************** + * Copyright (c) 2013 CEA LIST. + * + * + * 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: + * Laurent Wouters laurent.wouters@cea.fr - Initial API and implementation + * + *****************************************************************************/ +package org.eclipse.papyrus.infra.gmfdiag.common.handler; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import org.eclipse.emf.ecore.EObject; +import org.eclipse.jface.action.Action; +import org.eclipse.jface.action.ActionContributionItem; +import org.eclipse.jface.action.IContributionItem; +import org.eclipse.papyrus.infra.viewpoints.configuration.PapyrusDiagram; +import org.eclipse.papyrus.infra.viewpoints.policy.DynamicContribution; +import org.eclipse.papyrus.infra.viewpoints.policy.PolicyChecker; +import org.eclipse.papyrus.infra.viewpoints.policy.ViewPrototype; + +/** + * Represents the dynamic contribution of a policy to menus + * @author Laurent Wouters + */ +public class DynamicDiagramsMenuContribution extends DynamicContribution { + /** + * Constructor. + */ + public DynamicDiagramsMenuContribution() { } + + /** + * Constructor. + * @param id + */ + public DynamicDiagramsMenuContribution(String id) { + super(id); + } + + /** + * @see org.eclipse.ui.actions.CompoundContributionItem#getContributionItems() + */ + @Override + protected IContributionItem[] getContributionItems() { + final EObject selection = getSelection(); + if (selection == null) + return new IContributionItem[0]; + + // build a list of all the available prototypes + List data = new ArrayList(); + for (final ViewPrototype proto : PolicyChecker.getCurrent().getPrototypesFor(selection)) { + if (!(proto.getConfiguration() instanceof PapyrusDiagram)) + continue; + data.add(proto); + } + + // sort them + Collections.sort(data, new ViewPrototype.Comp()); + + // build the full labels + List labels = new ArrayList(data.size()); + String last = null; + boolean first = true; + for (ViewPrototype item : data) { + String label = item.getLabel(); + if (last != null && last.equals(label)) { + // name collision + if (first) { + labels.set(labels.size() - 1, data.get(labels.size() - 1).getFullLabel()); + first = false; + } + labels.add(item.getFullLabel()); + } else { + labels.add(label); + last = label; + first = true; + } + } + + // build the menu + List items = new ArrayList(data.size()); + for (int i = 0; i != data.size(); i++) { + final ViewPrototype proto = data.get(i); + String label = labels.get(i); + items.add(new ActionContributionItem(new Action(CREATE_MESSAGE + label, proto.getIconDescriptor()) { + @Override + public void run() { + proto.instantiateOn(selection); + } + })); + } + return items.toArray(new IContributionItem[items.size()]); + } +} diff --git a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/helper/DiagramEditHelper.java b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/helper/DiagramEditHelper.java new file mode 100755 index 00000000000..96a332820f5 --- /dev/null +++ b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/helper/DiagramEditHelper.java @@ -0,0 +1,178 @@ +/***************************************************************************** + * Copyright (c) 2013 CEA LIST. + * + * + * 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: + * Laurent Wouters laurent.wouters@cea.fr - Initial API and implementation + * + *****************************************************************************/ +package org.eclipse.papyrus.infra.gmfdiag.common.helper; + +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.emf.ecore.EReference; +import org.eclipse.emf.ecore.EStructuralFeature; +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.gmf.runtime.common.core.command.AbstractCommand; +import org.eclipse.gmf.runtime.common.core.command.CommandResult; +import org.eclipse.gmf.runtime.common.core.command.ICommand; +import org.eclipse.gmf.runtime.emf.type.core.IElementType; +import org.eclipse.gmf.runtime.emf.type.core.edithelper.IEditHelper; +import org.eclipse.gmf.runtime.emf.type.core.requests.CreateElementRequest; +import org.eclipse.gmf.runtime.emf.type.core.requests.DestroyElementRequest; +import org.eclipse.gmf.runtime.emf.type.core.requests.IEditCommandRequest; +import org.eclipse.gmf.runtime.emf.type.core.requests.SetRequest; +import org.eclipse.gmf.runtime.notation.Diagram; +import org.eclipse.papyrus.infra.viewpoints.policy.ModelAddData; +import org.eclipse.papyrus.infra.viewpoints.policy.PolicyChecker; + +/** + * Represents an Edit Helper for GMF Diagrams + * @author Laurent Wouters + */ +public class DiagramEditHelper implements IEditHelper { + + /* (non-Javadoc) + * @see org.eclipse.gmf.runtime.emf.type.core.edithelper.IEditHelper#canEdit(org.eclipse.gmf.runtime.emf.type.core.requests.IEditCommandRequest) + */ + public boolean canEdit(IEditCommandRequest req) { + return false; + } + + /* (non-Javadoc) + * @see org.eclipse.gmf.runtime.emf.type.core.edithelper.IEditHelper#getEditCommand(org.eclipse.gmf.runtime.emf.type.core.requests.IEditCommandRequest) + */ + public ICommand getEditCommand(final IEditCommandRequest req) { + if (req instanceof DestroyElementRequest) { + DestroyElementRequest r = (DestroyElementRequest)req; + return getDestroyDiagramCommand((Diagram)r.getElementToDestroy()); + } else if (req instanceof SetRequest) { + SetRequest r = (SetRequest)req; + return GetSimpleSetCommand(r.getElementToEdit(), r.getFeature(), r.getValue()); + } else if (req instanceof CreateElementRequest) { + CreateElementRequest r = (CreateElementRequest)req; + EObject container = r.getContainer(); + if (container instanceof Diagram) + return getCreateCommand((Diagram)container, r.getContainmentFeature(), r.getElementType()); + } + return null; + } + + /* (non-Javadoc) + * @see org.eclipse.gmf.runtime.emf.type.core.edithelper.IEditHelper#getContainedValues(org.eclipse.emf.ecore.EObject, org.eclipse.emf.ecore.EReference) + */ + public List getContainedValues(EObject eContainer, EReference feature) { + throw new UnsupportedOperationException(); + } + + /** + * Gets the command for destroying the given diagram + * @param diagram The diagram to destroy + * @return The appropriate command + */ + private ICommand getDestroyDiagramCommand(final Diagram diagram) { + final Resource resource = diagram.eResource(); + return new AbstractCommand("Destroy diagram") { + @Override + protected CommandResult doExecuteWithResult(IProgressMonitor progressMonitor, IAdaptable info) throws ExecutionException { + resource.getContents().remove(diagram); + return CommandResult.newOKCommandResult(); + } + + @Override + protected CommandResult doRedoWithResult(IProgressMonitor progressMonitor, IAdaptable info) throws ExecutionException { + resource.getContents().remove(diagram); + return CommandResult.newOKCommandResult(); + } + + @Override + protected CommandResult doUndoWithResult(IProgressMonitor progressMonitor, IAdaptable info) throws ExecutionException { + resource.getContents().add(diagram); + return CommandResult.newOKCommandResult(); + } + }; + } + + /** + * Gets the command for setting a property of a view + * @param object The object to modify + * @param feature The feature to modify + * @param newValue The new value for the feature + * @return The appropriate command + */ + private ICommand GetSimpleSetCommand(final EObject object, final EStructuralFeature feature, final Object newValue) { + return new AbstractCommand("Edit property") { + private Object oldValue; + + @Override + protected CommandResult doExecuteWithResult(IProgressMonitor progressMonitor, IAdaptable info) throws ExecutionException { + oldValue = object.eGet(feature); + object.eSet(feature, newValue); + return CommandResult.newOKCommandResult(); + } + + @Override + protected CommandResult doRedoWithResult(IProgressMonitor progressMonitor, IAdaptable info) throws ExecutionException { + object.eSet(feature, newValue); + return CommandResult.newOKCommandResult(); + } + + @Override + protected CommandResult doUndoWithResult(IProgressMonitor progressMonitor, IAdaptable info) throws ExecutionException { + object.eSet(feature, oldValue); + return CommandResult.newOKCommandResult(); + } + }; + } + + + /** + * Gets the command for the creation of an element in a diagram + * @param diagram The diagram to change + * @param reference The containment feature + * @param type The type of the element to be created + * @return The appropriate command + */ + private ICommand getCreateCommand(final Diagram diagram, final EReference reference, final IElementType type) { + return new AbstractCommand("Create element") { + private EObject newElement; + private ModelAddData data; + + @Override + protected CommandResult doExecuteWithResult(IProgressMonitor progressMonitor, IAdaptable info) throws ExecutionException { + data = PolicyChecker.getCurrent().getChildAddData(diagram, diagram.getElement().eClass(), type.getEClass()); + if (!data.isPermitted()) + return CommandResult.newErrorCommandResult("The current active viewpoint policy prevents the addition of this element to the view"); + newElement = type.getEClass().getEPackage().getEFactoryInstance().create(type.getEClass()); + if (data.isPathDefined()) { + if (!data.execute(diagram.getElement(), newElement)) + return CommandResult.newErrorCommandResult("Failed to follow the path"); + } else { + diagram.getElement().eSet(reference, newElement); + } + return CommandResult.newOKCommandResult(); + } + + @Override + protected CommandResult doRedoWithResult(IProgressMonitor progressMonitor, IAdaptable info) throws ExecutionException { + data.redoExecute(); + return CommandResult.newOKCommandResult(); + } + + @Override + protected CommandResult doUndoWithResult(IProgressMonitor progressMonitor, IAdaptable info) throws ExecutionException { + data.undoExecute(); + return CommandResult.newOKCommandResult(); + } + }; + } +} diff --git a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/helper/DiagramPrototype.java b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/helper/DiagramPrototype.java new file mode 100755 index 00000000000..e28a4eaf4ec --- /dev/null +++ b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/helper/DiagramPrototype.java @@ -0,0 +1,120 @@ +/***************************************************************************** + * Copyright (c) 2013 CEA LIST. + * + * + * 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: + * Laurent Wouters laurent.wouters@cea.fr - Initial API and implementation + * + *****************************************************************************/ +package org.eclipse.papyrus.infra.gmfdiag.common.helper; + +import org.eclipse.emf.common.command.AbstractCommand; +import org.eclipse.emf.common.command.Command; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.gmf.runtime.notation.Diagram; +import org.eclipse.papyrus.commands.Activator; +import org.eclipse.papyrus.infra.core.resource.ModelSet; +import org.eclipse.papyrus.infra.core.services.ServiceException; +import org.eclipse.papyrus.infra.core.services.ServicesRegistry; +import org.eclipse.papyrus.infra.emf.utils.ServiceUtilsForEObject; +import org.eclipse.papyrus.infra.gmfdiag.common.AbstractPapyrusGmfCreateDiagramCommandHandler; +import org.eclipse.papyrus.infra.gmfdiag.common.utils.DiagramUtils; +import org.eclipse.papyrus.infra.viewpoints.configuration.PapyrusView; +import org.eclipse.papyrus.infra.viewpoints.policy.ViewPrototype; + +/** + * Represents a prototype of GMF diagram for the viewpoints infrastructure + * @author Laurent Wouters + */ +public class DiagramPrototype extends ViewPrototype { + protected final String category; + protected final AbstractPapyrusGmfCreateDiagramCommandHandler command; + + public DiagramPrototype(PapyrusView configuration, String category, AbstractPapyrusGmfCreateDiagramCommandHandler command) { + super(configuration); + this.category = category; + this.command = command; + } + + @Override + public boolean instantiateOn(EObject owner) { + return instantiateOn(owner, null); + } + + @Override + public boolean instantiateOn(EObject owner, String name) { + ServicesRegistry registry; + try { + registry = ServiceUtilsForEObject.getInstance().getServiceRegistry(owner); + } catch (ServiceException ex) { + Activator.log.error(ex); + return false; + } + ModelSet modelSet; + try { + modelSet = registry.getService(ModelSet.class); + } catch (ServiceException ex) { + Activator.log.error(ex); + return false; + } + Object result = command.createDiagram(modelSet, owner, owner, this, name); + return (result != null); + } + + @Override + public boolean isOwnerReassignable() { + // Users can always move diagrams that are part of their viewpoint + return true; + } + + @Override + public Command getCommandChangeOwner(EObject view, final EObject target) { + final Diagram diagram = (Diagram)view; + final EObject previous = DiagramUtils.getOwner(diagram); + return new AbstractCommand("Change diagram owner") { + @Override + public void execute() { + DiagramUtils.setOwner(diagram, target); + } + @Override + public void redo() { + DiagramUtils.setOwner(diagram, previous); + } + @Override + protected boolean prepare() { return true; } + }; + } + + @Override + public Command getCommandChangeRoot(EObject view, final EObject target) { + final Diagram diagram = (Diagram)view; + final EObject previous = diagram.getElement(); + return new AbstractCommand("Change diagram root element") { + @Override + public void execute() { + diagram.setElement(target); + } + @Override + public void redo() { + diagram.setElement(previous); + } + @Override + protected boolean prepare() { return true; } + }; + } + + @Override + public EObject getOwnerOf(EObject view) { + return DiagramUtils.getOwner((Diagram)view); + } + + @Override + public EObject getRootOf(EObject view) { + return ((Diagram)view).getElement(); + } +} diff --git a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/helper/GMFDiagramViewTypeHelper.java b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/helper/GMFDiagramViewTypeHelper.java new file mode 100755 index 00000000000..19544a0c3ea --- /dev/null +++ b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/helper/GMFDiagramViewTypeHelper.java @@ -0,0 +1,165 @@ +/***************************************************************************** + * Copyright (c) 2013 CEA LIST. + * + * + * 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: + * Laurent Wouters laurent.wouters@cea.fr - Initial API and implementation + * + *****************************************************************************/ +package org.eclipse.papyrus.infra.gmfdiag.common.helper; + +import java.util.HashMap; +import java.util.Map; + +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IConfigurationElement; +import org.eclipse.core.runtime.IExtension; +import org.eclipse.core.runtime.IExtensionPoint; +import org.eclipse.core.runtime.IExtensionRegistry; +import org.eclipse.core.runtime.Platform; +import org.eclipse.emf.ecore.EClass; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.util.EcoreUtil; +import org.eclipse.gmf.runtime.notation.Diagram; +import org.eclipse.papyrus.infra.gmfdiag.common.AbstractPapyrusGmfCreateDiagramCommandHandler; +import org.eclipse.papyrus.infra.gmfdiag.common.utils.DiagramUtils; +import org.eclipse.papyrus.infra.viewpoints.configuration.ConfigurationPackage; +import org.eclipse.papyrus.infra.viewpoints.configuration.PapyrusDiagram; +import org.eclipse.papyrus.infra.viewpoints.configuration.PapyrusView; +import org.eclipse.papyrus.infra.viewpoints.policy.IViewTypeHelper; +import org.eclipse.papyrus.infra.viewpoints.policy.ViewPrototype; + +/** + * Represents the dynamic contribution of a policy to menus + * @author Laurent Wouters + */ +public class GMFDiagramViewTypeHelper implements IViewTypeHelper { + /** + * ID of the extension point for Papyrus diagrams + */ + private static final String EXTENSION_PAPYRUS_DIAGRAM = "org.eclipse.papyrus.infra.core.papyrusDiagram"; + + private static class NakedViewPrototype extends DiagramPrototype { + private final String label; + private final String icon; + public NakedViewPrototype(PapyrusView configuration, String category, AbstractPapyrusGmfCreateDiagramCommandHandler command, String label, String icon) { + super(configuration, category, command); + this.label = label; + this.icon = icon; + } + @Override + public String getLabel() { return label; } + @Override + public String getIconURI() { return icon; } + } + + private static class Implementation { + public final String name; + public final String icon; + public final String language; + public final AbstractPapyrusGmfCreateDiagramCommandHandler command; + public Implementation(String name, String icon, String language, AbstractPapyrusGmfCreateDiagramCommandHandler command) { + this.name = name; + this.icon = icon; + this.language = language; + this.command = command; + } + } + + + /** + * The cache of prototypes + */ + private Map cache; + + /** + * The cache of implementations + */ + private Map implementations; + + /** + * @see org.eclipse.papyrus.infra.viewpoints.policy.IViewTypeHelper#isSupported(org.eclipse.emf.ecore.EClass) + */ + public boolean isSupported(EClass type) { + return EcoreUtil.equals(type, ConfigurationPackage.eINSTANCE.getPapyrusDiagram()); + } + + /** + * @see org.eclipse.papyrus.infra.viewpoints.policy.IViewTypeHelper#isSupported(org.eclipse.emf.ecore.EObject) + */ + public boolean isSupported(EObject view) { + return (view instanceof Diagram); + } + + /** + * @see org.eclipse.papyrus.infra.viewpoints.policy.IViewTypeHelper#getPrototypeFor(org.eclipse.papyrus.infra.viewpoints.configuration.PapyrusView) + */ + public ViewPrototype getPrototypeFor(PapyrusView configuration) { + if (!(configuration instanceof PapyrusDiagram)) + return null; + if (implementations == null) + buildImplementationCache(); + if (cache == null) + cache = new HashMap(); + if (cache.containsKey(configuration)) + return cache.get(configuration); + if (!implementations.containsKey(configuration.getImplementationID())) + return null; + Implementation impl = implementations.get(configuration.getImplementationID()); + if (ViewPrototype.isNatural(configuration)) { + NakedViewPrototype proto = new NakedViewPrototype(configuration, impl.language, impl.command, impl.name, impl.icon); + cache.put(configuration, proto); + return proto; + } else { + DiagramPrototype proto = new DiagramPrototype(configuration, impl.language, impl.command); + cache.put(configuration, proto); + return proto; + } + } + + /** + * @see org.eclipse.papyrus.infra.viewpoints.policy.IViewTypeHelper#getPrototypeOf(org.eclipse.emf.ecore.EObject) + */ + public ViewPrototype getPrototypeOf(EObject view) { + if (!(view instanceof Diagram)) + return null; + return DiagramUtils.getPrototype((Diagram)view); + } + + /** + * Builds the cache of metadata + */ + private void buildImplementationCache() { + implementations = new HashMap(); + IExtensionRegistry registry = Platform.getExtensionRegistry(); + IExtensionPoint point = registry.getExtensionPoint(EXTENSION_PAPYRUS_DIAGRAM); + IExtension[] extensions = point.getExtensions(); + for (int i=0; i!=extensions.length; i++) { + IConfigurationElement[] elements = extensions[i].getConfigurationElements(); + for (int j=0; j!=elements.length; j++) { + if (elements[j].getName().equals("creationCommand")) { + try { + Object obj = elements[j].createExecutableExtension("creationCommandClass"); + if (obj instanceof AbstractPapyrusGmfCreateDiagramCommandHandler) { + AbstractPapyrusGmfCreateDiagramCommandHandler command = (AbstractPapyrusGmfCreateDiagramCommandHandler)obj; + String label = elements[j].getAttribute("label"); + String icon = elements[j].getAttribute("icon"); + String language = elements[j].getAttribute("language"); + String implID = command.getCreatedDiagramType(); + if (!icon.startsWith("platform:/")) + icon = "platform:/plugin/" + extensions[i].getContributor().getName() + "/" + icon; + if (!implementations.containsKey(implID)) + implementations.put(implID, new Implementation(label, icon, language, command)); + } + } + catch (CoreException e) { } + } + } + } + } +} diff --git a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/helper/NotationHelper.java b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/helper/NotationHelper.java index 7d783690bec..7c43a346c1a 100644 --- a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/helper/NotationHelper.java +++ b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/helper/NotationHelper.java @@ -11,7 +11,6 @@ *****************************************************************************/ package org.eclipse.papyrus.infra.gmfdiag.common.helper; -import org.eclipse.core.runtime.IAdaptable; import org.eclipse.emf.ecore.EObject; import org.eclipse.gmf.runtime.notation.View; import org.eclipse.papyrus.infra.emf.utils.EMFHelper; @@ -34,18 +33,7 @@ public class NotationHelper { if(source instanceof View) { return (View)source; } - if(source instanceof IAdaptable) { - IAdaptable adaptable = (IAdaptable)source; - Object adapter = adaptable.getAdapter(View.class); - if(adapter != null) { - return (View)adapter; - } - adapter = EMFHelper.getEObject(adaptable); - if(adapter instanceof View) { - return (View)adapter; - } - } - return null; + return (View) EMFHelper.getEObject(source); } /** diff --git a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/model/NotationUtils.java b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/model/NotationUtils.java index 9ad8ee049be..606fa01febd 100644 --- a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/model/NotationUtils.java +++ b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/model/NotationUtils.java @@ -1,272 +1,210 @@ -/***************************************************************************** - * Copyright (c) 2009 Atos Origin. - * - * - * 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: - * Emilien Perico (Atos Origin) emilien.perico@atosorigin.com - Initial API and implementation - * - *****************************************************************************/ -package org.eclipse.papyrus.infra.gmfdiag.common.model; - -import java.util.ArrayList; -import java.util.LinkedList; -import java.util.List; - -import org.eclipse.emf.ecore.EObject; -import org.eclipse.emf.ecore.EStructuralFeature; -import org.eclipse.emf.ecore.resource.Resource; -import org.eclipse.emf.ecore.util.EcoreUtil; -import org.eclipse.gmf.runtime.notation.Diagram; -import org.eclipse.gmf.runtime.notation.NotationPackage; -import org.eclipse.papyrus.infra.core.resource.IModel; -import org.eclipse.papyrus.infra.core.resource.ModelSet; -import org.eclipse.papyrus.infra.core.services.ServiceException; -import org.eclipse.papyrus.infra.core.services.ServicesRegistry; -import org.eclipse.papyrus.infra.core.utils.ServiceUtils; -import org.eclipse.papyrus.infra.core.utils.ServiceUtilsForActionHandlers; -import org.eclipse.papyrus.infra.emf.utils.EMFHelper; -import org.eclipse.papyrus.infra.gmfdiag.common.Activator; - -import com.google.common.base.Function; -import com.google.common.base.Predicate; -import com.google.common.collect.Iterables; -import com.google.common.collect.Lists; - -/** - * Utilities method to manage notation models. Should be moved in a more - * suitable plugin - */ -public class NotationUtils { - - /** - * Get the notation Resource. - * - * @return - * - * @deprecated Usage of the internal Resource is discouraged. - */ - @Deprecated - public static Resource getNotationResource() { - return getNotationModel().getResource(); - } - - /** - * Gets the NotationModel for the currently selected editor.
- * Warning: This method is designed to be call from ui.handlers. It is not - * designed to be call from Editors. This method can return null if called - * during the MultiEditor initialization. - * - * @see ServiceUtilsForActionHandlers.getInstance().getModelSet() - * - * @return The {@link NotationModel} of the current editor, or null if not - * found. - */ - public static NotationModel getNotationModel() { - - try { - return (NotationModel)ServiceUtilsForActionHandlers.getInstance().getModelSet().getModel(NotationModel.MODEL_ID); - } catch (ServiceException e) { - return null; - } - } - - /** - * Gets the NotationModel for the currently selected editor.
- * Warning: This method is designed to be call from ui.handlers. It is not - * designed to be call from Editors. This method can return null if called - * during the MultiEditor initialization. - * - * @see ServiceUtilsForActionHandlers.getInstance().getModelSet() - * - * @return The {@link NotationModel} of the current editor, or null if not - * found. - * @throws ServiceException - * If an error occurs while getting or starting the service. - */ - public static NotationModel getNotationModelChecked() throws ServiceException { - - return (NotationModel)ServiceUtilsForActionHandlers.getInstance().getModelSet().getModel(NotationModel.MODEL_ID); - } - - /** - * Gets the NotationModel from the {@link ModelSet}.
- * - * @param modelsManager - * The modelManager containing the requested model. - * - * @return The {@link NotationModel} registered in modelManager, or null if - * not found. - */ - public static NotationModel getNotationModel(ModelSet modelsManager) { - - return (NotationModel)modelsManager.getModel(NotationModel.MODEL_ID); - } - - /** - * Gets the direct associated diagram of the specified eObject. - * - * @param eObject - * @param notationResource - * - * @return the associated diagram - */ - public static Diagram getAssociatedDiagram(Resource notationResource, EObject eObject) { - if(notationResource != null) { - for(EObject obj : notationResource.getContents()) { - if(obj instanceof Diagram) { - Diagram diagram = (Diagram)obj; - if(eObject != null && eObject.equals(diagram.getElement())) { - return diagram; - } - } - } - } - return null; - } - - /** - * Gets the direct associated diagram of the specified eObject. - * - * @param eObject - * @param notationResource - * @param resolve - * the resource if true - * - * @return the associated diagram - */ - public static Diagram getAssociatedDiagram(Resource notationResource, EObject eObject, boolean resolve) { - if(notationResource != null && resolve) { - EcoreUtil.resolveAll(notationResource); - } - return getAssociatedDiagram(notationResource, eObject); - } - - /** - * Gets the all the diagrams contained in the specified ancestor eObject - * - * @param notationResource - * @param eObject - * - * @return all the contained diagrams - * - */ - public static List getDiagrams(Resource notationResource, EObject eObject) { - List diagrams = new LinkedList(); - if(notationResource != null) { - for(EObject obj : notationResource.getContents()) { - if(obj instanceof Diagram) { - Diagram diagram = (Diagram)obj; - if(EcoreUtil.isAncestor(eObject, diagram.getElement())) { - diagrams.add(diagram); - } - } - } - } - return diagrams; - } - - /** - * Gets the all the diagrams contained in the specified ancestor eObject - * - * @param notationResource - * @param eObject - * @param resolve - * the resource if true - * - * @return all the contained diagrams - */ - public static List getDiagrams(Resource notationResource, EObject eObject, boolean resolve) { - if(notationResource != null && resolve) { - EcoreUtil.resolveAll(notationResource); - } - return getDiagrams(notationResource, eObject); - } - - /** - * Helper to retrieve the Notation resource associated to a ModelSet. May be null. - * - * @param from - * @return - */ - public static Resource getNotationResource(ModelSet from) { - IModel notationModel = from.getModel(NotationModel.MODEL_ID); - if(notationModel instanceof NotationModel) { - return ((NotationModel)notationModel).getResource(); - } - return null; - } - - /** - * Helper to retrieve the Notation resource associated to a ServicesRegistry. May be null. - * - * @param from - * @return - */ - public static Resource getNotationResource(ServicesRegistry registry) { - try { - ModelSet modelSet = ServiceUtils.getInstance().getModelSet(registry); - return getNotationResource(modelSet); - } catch (ServiceException ex) { - Activator.log.error(ex); - return null; - } - } - - /** - * Gets the loaded associated diagrams of the specified eObject. - * This method uses the cross referencer to compute them. - * - * @param eObject - * @param notationResource - * - * @return the associated diagram - */ - public static List getLoadedAssociatedDiagrams(EObject eObject) { - Predicate p = new Predicate() { - - public boolean apply(EStructuralFeature.Setting setting) { - return setting.getEObject() instanceof Diagram && NotationPackage.Literals.VIEW__ELEMENT.equals(setting.getEStructuralFeature()); - } - }; - Function f = new Function() { - - public Diagram apply(EStructuralFeature.Setting setting) { - return (Diagram)setting.getEObject(); - } - - }; - return Lists.newArrayList(Iterables.transform(Iterables.filter(EMFHelper.getUsages(eObject), p), f)); - } - - /** - * Gets the all the diagrams contained in the specified ancestor eObject and - * currently stored in the specified notation resource. - * - * @param notationResource - * @param eObject - * - * @return all the contained diagrams - * - */ - public static List getAllDescendantDiagramsInResource(EObject eObject, Resource notationResource) { - List diagrams = new ArrayList(); - if(notationResource != null) { - for(EObject obj : notationResource.getContents()) { - if(obj instanceof Diagram) { - Diagram diagram = (Diagram)obj; - if(EcoreUtil.isAncestor(eObject, diagram.getElement())) { - diagrams.add(diagram); - } - } - } - } - return diagrams; - } - - -} +/***************************************************************************** + * Copyright (c) 2009 Atos Origin. + * + * + * 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: + * Emilien Perico (Atos Origin) emilien.perico@atosorigin.com - Initial API and implementation + * + *****************************************************************************/ +package org.eclipse.papyrus.infra.gmfdiag.common.model; + +import java.util.LinkedList; +import java.util.List; + +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.emf.ecore.util.EcoreUtil; +import org.eclipse.gmf.runtime.notation.Diagram; +import org.eclipse.papyrus.infra.core.resource.IModel; +import org.eclipse.papyrus.infra.core.resource.ModelSet; +import org.eclipse.papyrus.infra.core.services.ServiceException; +import org.eclipse.papyrus.infra.core.services.ServicesRegistry; +import org.eclipse.papyrus.infra.core.utils.ServiceUtils; +import org.eclipse.papyrus.infra.core.utils.ServiceUtilsForActionHandlers; +import org.eclipse.papyrus.infra.gmfdiag.common.Activator; +import org.eclipse.papyrus.infra.gmfdiag.common.utils.DiagramUtils; + +/** + * Utilities method to manage notation models. Should be moved in a more + * suitable plugin + */ +public class NotationUtils { + + /** + * Get the notation Resource. + * + * @return + * + * @deprecated Usage of the internal Resource is discouraged. + */ + @Deprecated + public static Resource getNotationResource() { + return getNotationModel().getResource(); + } + + /** + * Gets the NotationModel for the currently selected editor.
+ * Warning: This method is designed to be call from ui.handlers. It is not + * designed to be call from Editors. This method can return null if called + * during the MultiEditor initialization. + * + * @see ServiceUtilsForActionHandlers.getInstance().getModelSet() + * + * @return The {@link NotationModel} of the current editor, or null if not + * found. + */ + public static NotationModel getNotationModel() { + + try { + return (NotationModel)ServiceUtilsForActionHandlers.getInstance().getModelSet().getModel(NotationModel.MODEL_ID); + } catch (ServiceException e) { + return null; + } + } + + /** + * Gets the NotationModel for the currently selected editor.
+ * Warning: This method is designed to be call from ui.handlers. It is not + * designed to be call from Editors. This method can return null if called + * during the MultiEditor initialization. + * + * @see ServiceUtilsForActionHandlers.getInstance().getModelSet() + * + * @return The {@link NotationModel} of the current editor, or null if not + * found. + * @throws ServiceException + * If an error occurs while getting or starting the service. + */ + public static NotationModel getNotationModelChecked() throws ServiceException { + + return (NotationModel)ServiceUtilsForActionHandlers.getInstance().getModelSet().getModel(NotationModel.MODEL_ID); + } + + /** + * Gets the NotationModel from the {@link ModelSet}.
+ * + * @param modelsManager + * The modelManager containing the requested model. + * + * @return The {@link NotationModel} registered in modelManager, or null if + * not found. + */ + public static NotationModel getNotationModel(ModelSet modelsManager) { + + return (NotationModel)modelsManager.getModel(NotationModel.MODEL_ID); + } + + /** + * Gets the direct associated diagram of the specified eObject. + * + * @param eObject + * @param notationResource + * + * @return the associated diagram + */ + public static Diagram getAssociatedDiagram(Resource notationResource, EObject eObject) { + if(notationResource != null) { + for(EObject obj : notationResource.getContents()) { + if(obj instanceof Diagram) { + Diagram diagram = (Diagram)obj; + if (DiagramUtils.getOwner(diagram) == eObject) + return diagram; + } + } + } + return null; + } + + /** + * Gets the direct associated diagram of the specified eObject. + * + * @param eObject + * @param notationResource + * @param resolve + * the resource if true + * + * @return the associated diagram + */ + public static Diagram getAssociatedDiagram(Resource notationResource, EObject eObject, boolean resolve) { + if(notationResource != null && resolve) { + EcoreUtil.resolveAll(notationResource); + } + return getAssociatedDiagram(notationResource, eObject); + } + + /** + * Gets the all the diagrams contained in the specified ancestor eObject + * + * @param notationResource + * @param eObject + * + * @return all the contained diagrams + * + */ + public static List getDiagrams(Resource notationResource, EObject eObject) { + List diagrams = new LinkedList(); + if(notationResource != null) { + for(EObject obj : notationResource.getContents()) { + if(obj instanceof Diagram) { + Diagram diagram = (Diagram)obj; + if (EcoreUtil.isAncestor(eObject, DiagramUtils.getOwner(diagram))) + diagrams.add(diagram); + } + } + } + return diagrams; + } + + /** + * Gets the all the diagrams contained in the specified ancestor eObject + * + * @param notationResource + * @param eObject + * @param resolve + * the resource if true + * + * @return all the contained diagrams + */ + public static List getDiagrams(Resource notationResource, EObject eObject, boolean resolve) { + if(notationResource != null && resolve) { + EcoreUtil.resolveAll(notationResource); + } + return getDiagrams(notationResource, eObject); + } + + /** + * Helper to retrieve the Notation resource associated to a ModelSet. May be null. + * + * @param from + * @return + */ + public static Resource getNotationResource(ModelSet from) { + IModel notationModel = from.getModel(NotationModel.MODEL_ID); + if(notationModel instanceof NotationModel) { + return ((NotationModel)notationModel).getResource(); + } + return null; + } + + /** + * Helper to retrieve the Notation resource associated to a ServicesRegistry. May be null. + * + * @param from + * @return + */ + public static Resource getNotationResource(ServicesRegistry registry) { + try { + ModelSet modelSet = ServiceUtils.getInstance().getModelSet(registry); + return getNotationResource(modelSet); + } catch (ServiceException ex) { + Activator.log.error(ex); + return null; + } + } +} + diff --git a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/providers/NotationLabelProvider.java b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/providers/NotationLabelProvider.java index cd8d0de611f..7a7c2daef82 100644 --- a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/providers/NotationLabelProvider.java +++ b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/providers/NotationLabelProvider.java @@ -11,6 +11,8 @@ *****************************************************************************/ package org.eclipse.papyrus.infra.gmfdiag.common.providers; +import javax.swing.text.Element; + import org.eclipse.emf.ecore.EObject; import org.eclipse.gef.EditPart; import org.eclipse.gmf.runtime.diagram.ui.editparts.ResizableCompartmentEditPart; @@ -19,15 +21,14 @@ import org.eclipse.gmf.runtime.notation.BasicCompartment; import org.eclipse.gmf.runtime.notation.DecorationNode; import org.eclipse.gmf.runtime.notation.Diagram; import org.eclipse.gmf.runtime.notation.View; -import org.eclipse.papyrus.infra.core.editorsfactory.IPageIconsRegistry; -import org.eclipse.papyrus.infra.core.editorsfactory.PageIconsRegistry; -import org.eclipse.papyrus.infra.core.extension.diagrameditor.PluggableEditorFactoryReader; import org.eclipse.papyrus.infra.emf.providers.EMFLabelProvider; +import org.eclipse.papyrus.infra.emf.utils.EMFHelper; import org.eclipse.papyrus.infra.gmfdiag.common.Activator; import org.eclipse.papyrus.infra.gmfdiag.common.types.NotationTypesMap; +import org.eclipse.papyrus.infra.gmfdiag.common.utils.DiagramUtils; +import org.eclipse.papyrus.infra.viewpoints.policy.ViewPrototype; import org.eclipse.swt.graphics.Image; import org.eclipse.uml2.uml.NamedElement; -import org.w3c.dom.Element; /** * A Label Provider for GMF Notation model @@ -37,28 +38,27 @@ public class NotationLabelProvider extends EMFLabelProvider { /** icon for a compartment */ public static final String ICON_COMPARTMENT = "/icons/none_comp_vis.gif"; //$NON-NLS-1$ - private IPageIconsRegistry pageIconsRegistry; - @Override public void dispose() { super.dispose(); - if(pageIconsRegistry != null) { - pageIconsRegistry.dispose(); - pageIconsRegistry = null; + } + + @Override + public Image getImage(Object element) { + EObject eObject = EMFHelper.getEObject(element); + if (eObject != null) { + return getImage(eObject); } + return super.getImage(element); } @Override protected Image getImage(EObject element) { if(element instanceof Diagram) { - //return getPagesIconsRegistry().getEditorIcp - if(pageIconsRegistry == null) { - pageIconsRegistry = new PageIconsRegistry(); - PluggableEditorFactoryReader editorReader = new PluggableEditorFactoryReader(org.eclipse.papyrus.infra.core.Activator.PLUGIN_ID); - editorReader.populate((PageIconsRegistry)pageIconsRegistry); - } - - return pageIconsRegistry.getEditorIcon(element); + ViewPrototype proto = DiagramUtils.getPrototype((Diagram)element); + if (proto == null) + return null; + return proto.getIcon(); } // if the element is a compartment diff --git a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/utils/DiagramUtils.java b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/utils/DiagramUtils.java new file mode 100755 index 00000000000..4e78d0e5047 --- /dev/null +++ b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/utils/DiagramUtils.java @@ -0,0 +1,124 @@ +/***************************************************************************** + * Copyright (c) 2013 CEA LIST. + * + * + * 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: + * Laurent Wouters laurent.wouters@cea.fr - Initial API and implementation + * + *****************************************************************************/ +package org.eclipse.papyrus.infra.gmfdiag.common.utils; + +import org.eclipse.emf.ecore.EObject; +import org.eclipse.gef.EditPart; +import org.eclipse.gmf.runtime.notation.Diagram; +import org.eclipse.gmf.runtime.notation.Style; +import org.eclipse.papyrus.infra.viewpoints.configuration.PapyrusView; +import org.eclipse.papyrus.infra.viewpoints.policy.PolicyChecker; +import org.eclipse.papyrus.infra.viewpoints.policy.ViewPrototype; +import org.eclipse.papyrus.infra.viewpoints.style.PapyrusViewStyle; +import org.eclipse.papyrus.infra.viewpoints.style.StylePackage; + +/** + * Utilities for the management of configuration-related data on views and diagrams + * @author Laurent Wouters + */ +public class DiagramUtils { + + /** + * Gets the diagram associated to the given edit part + * @param part The edit part + * @return The diagram associated to the edit part, or null if none is found + */ + public static Diagram getDiagramFrom(EditPart part) { + EditPart current = part; + while (current != null) { + Object model = current.getModel(); + if (model instanceof Diagram) + return (Diagram)model; + current = current.getParent(); + } + return null; + } + + /** + * Gets the owner of a diagram as it will appear in the model explorer. + * In the case where the diagram does not yet have a defined owner, the diagram's root element will be returned instead. + * @param diagram A diagram + * @return The diagram's owner + */ + public static EObject getOwner(Diagram diagram) { + Style style = diagram.getStyle(StylePackage.Literals.PAPYRUS_VIEW_STYLE); + if (style != null) { + PapyrusViewStyle pvs = (PapyrusViewStyle)style; + EObject value = pvs.getOwner(); + if (value != null) + return value; + } + // This is the legacy fallback for old diagrams + return diagram.getElement(); + } + + /** + * Sets the owner of a diagram as it will appear in the model explorer + * @param diagram A diagram + * @param owner The new diagram's owner + */ + public static void setOwner(Diagram diagram, EObject owner) { + Style style = diagram.getStyle(StylePackage.Literals.PAPYRUS_VIEW_STYLE); + if (style != null) { + PapyrusViewStyle pvs = (PapyrusViewStyle)style; + pvs.setOwner(owner); + } else { + PapyrusViewStyle pvs = (PapyrusViewStyle)diagram.createStyle(StylePackage.Literals.PAPYRUS_VIEW_STYLE); + pvs.setOwner(owner); + } + } + + /** + * Gets the prototype of a diagram + * @param diagram A diagram + * @return The diagram's prototype + */ + public static ViewPrototype getPrototype(Diagram diagram) { + Style style = diagram.getStyle(StylePackage.Literals.PAPYRUS_VIEW_STYLE); + if (style != null) { + PapyrusViewStyle pvs = (PapyrusViewStyle)style; + PapyrusView config = pvs.getConfiguration(); + if (config != null) { + if (PolicyChecker.getCurrent().isInViewpoint(config)) + return ViewPrototype.get(config); + return ViewPrototype.UNAVAILABLE_DIAGRAM; + } + return ViewPrototype.get(diagram.getType(), pvs.getOwner(), diagram.getElement()); + } + // This is the legacy fallback for old diagrams + if ("Package".equals(diagram.getType())) { + for (ViewPrototype prototype : PolicyChecker.getCurrent().getAllPrototypes()) { + if ("UML Package Diagram".equals(prototype.getLabel())) + return prototype; + } + } + return ViewPrototype.get(diagram.getType(), diagram.getElement(), diagram.getElement()); + } + + /** + * Sets the prototype of a diagram + * @param diagram A diagram + * @param configuration The new diagram's prototype + */ + public static void setPrototype(Diagram diagram, ViewPrototype prototype) { + Style style = diagram.getStyle(StylePackage.Literals.PAPYRUS_VIEW_STYLE); + if (style != null) { + PapyrusViewStyle pvs = (PapyrusViewStyle)style; + pvs.setConfiguration(prototype.getConfiguration()); + } else { + PapyrusViewStyle pvs = (PapyrusViewStyle)diagram.createStyle(StylePackage.Literals.PAPYRUS_VIEW_STYLE); + pvs.setConfiguration(prototype.getConfiguration()); + } + } +} diff --git a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.controlmode/src/org/eclipse/papyrus/infra/gmfdiag/controlmode/ControlDiagramsCommand.java b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.controlmode/src/org/eclipse/papyrus/infra/gmfdiag/controlmode/ControlDiagramsCommand.java index 34ed42b72bc..08fb60c2c03 100644 --- a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.controlmode/src/org/eclipse/papyrus/infra/gmfdiag/controlmode/ControlDiagramsCommand.java +++ b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.controlmode/src/org/eclipse/papyrus/infra/gmfdiag/controlmode/ControlDiagramsCommand.java @@ -99,7 +99,7 @@ public class ControlDiagramsCommand extends AbstractControlCommand { if(notationResource == null) { throw new ExecutionException("unable to retrieve old notation resource"); } - return NotationUtils.getAllDescendantDiagramsInResource(getRequest().getTargetObject(), notationResource); + return NotationUtils.getDiagrams(notationResource, getRequest().getTargetObject()); } /** diff --git a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.modelexplorer/META-INF/MANIFEST.MF b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.modelexplorer/META-INF/MANIFEST.MF index a0090e3f2c6..7ad5863dd55 100644 --- a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.modelexplorer/META-INF/MANIFEST.MF +++ b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.modelexplorer/META-INF/MANIFEST.MF @@ -18,7 +18,9 @@ Require-Bundle: org.eclipse.ui, org.eclipse.papyrus.emf.facet.efacet, org.eclipse.papyrus.emf.facet.efacet.metamodel, org.eclipse.papyrus.emf.facet.custom.metamodel, - org.eclipse.papyrus.infra.emf + org.eclipse.papyrus.infra.emf, + org.eclipse.papyrus.infra.gmfdiag.common;bundle-version="1.0.0", + org.eclipse.papyrus.infra.viewpoints.policy;bundle-version="1.0.0" Bundle-Vendor: %providerName Bundle-ActivationPolicy: lazy Bundle-Version: 1.0.0.qualifier diff --git a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.modelexplorer/plugin.xml b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.modelexplorer/plugin.xml index 90d477a5bca..6d23ed58823 100644 --- a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.modelexplorer/plugin.xml +++ b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.modelexplorer/plugin.xml @@ -2,34 +2,9 @@ - - - - - - navElements = NavigationHelper.getInstance().getAllNavigableElements(selectedElement); - // this will sort elements by navigation depth - Collections.sort(navElements); - - for(NavigableElement navElement : navElements) { - // ignore existing elements because we want a hierarchy to - // be created if it is not on the current element - if(navElement instanceof CreatedNavigableElement && creationCondition.create(navElement.getElement())) { - return navElement; - } - } - } - } - } - - return null; - } - - private void createDiagram(NavigableElement navElement, ServicesRegistry registry) { - ModelSet modelSet; - try { - modelSet = ServiceUtils.getInstance().getModelSet(registry); - } catch (ServiceException ex) { - Activator.log.error(ex); - return; - } - - if(navElement != null && modelSet != null) { - try { - CompositeCommand command = NavigationHelper.getLinkCreateAndOpenNavigableDiagramCommand(navElement, creationCommand, null, modelSet); - modelSet.getTransactionalEditingDomain().getCommandStack().execute(new GMFtoEMFCommandWrapper(command)); - } catch (Exception e) { - } - } - } - - /** - * Resolve semantic element - * - * @param object - * the object to resolve - * @return null or the semantic element associated to the - * specified object - */ - protected EObject resolveSemanticObject(Object object) { - if(object instanceof EObject) { - return (EObject)object; - } - Object businessObject = BusinessModelResolver.getInstance().getBusinessModel(object); - if(businessObject instanceof EObject) { - return (EObject)businessObject; - } - return null; - } - - @Override - public void setEnabled(Object evaluationContext) { - if(evaluationContext instanceof IEvaluationContext) { - Object selectionVariable = ((IEvaluationContext)evaluationContext).getDefaultVariable(); - - if(selectionVariable instanceof Collection) { - List selection = (selectionVariable instanceof List) ? (List)selectionVariable : new java.util.ArrayList((Collection)selectionVariable); - if(selection.size() != 1) { - setBaseEnabled(false); - return; - } - - EObject target = EMFHelper.getEObject(selection.get(0)); - setBaseEnabled(creationCondition.create(target)); - } - } - super.setEnabled(evaluationContext); - } - -} diff --git a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.properties/META-INF/MANIFEST.MF b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.properties/META-INF/MANIFEST.MF index d922ee2a532..cbdf8697d24 100644 --- a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.properties/META-INF/MANIFEST.MF +++ b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.properties/META-INF/MANIFEST.MF @@ -19,7 +19,8 @@ Require-Bundle: org.eclipse.ui, org.eclipse.papyrus.infra.core;bundle-version="1.0.0", org.eclipse.papyrus.uml.tools;bundle-version="1.0.0", org.eclipse.papyrus.infra.services.labelprovider;bundle-version="1.0.0", - org.eclipse.papyrus.infra.gmfdiag.preferences;bundle-version="1.0.0" + org.eclipse.papyrus.infra.gmfdiag.preferences;bundle-version="1.0.0", + org.eclipse.papyrus.infra.viewpoints.policy;bundle-version="1.0.0" Export-Package: org.eclipse.papyrus.infra.gmfdiag.properties.extension, org.eclipse.papyrus.infra.gmfdiag.properties.modelelement Bundle-Vendor: %Bundle-Vendor diff --git a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.properties/model/notation/notation.ctx b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.properties/model/notation/notation.ctx index 530b8df9e75..484a6246643 100644 --- a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.properties/model/notation/notation.ctx +++ b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.properties/model/notation/notation.ctx @@ -37,9 +37,6 @@ - - - @@ -184,18 +181,18 @@ - + + + + + + - - - - - @@ -280,112 +277,112 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -398,209 +395,214 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + + + + + + diff --git a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.properties/model/notation/ui/SingleDiagram.xwt b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.properties/model/notation/ui/SingleDiagram.xwt index 0e7eb2296f9..d08acaed9e0 100644 --- a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.properties/model/notation/ui/SingleDiagram.xwt +++ b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.properties/model/notation/ui/SingleDiagram.xwt @@ -11,6 +11,8 @@ - + + + \ No newline at end of file diff --git a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.properties/src/org/eclipse/papyrus/infra/gmfdiag/properties/modelelement/GMFModelElement.java b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.properties/src/org/eclipse/papyrus/infra/gmfdiag/properties/modelelement/GMFModelElement.java index f85b81ae344..a85ef90af4c 100644 --- a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.properties/src/org/eclipse/papyrus/infra/gmfdiag/properties/modelelement/GMFModelElement.java +++ b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.properties/src/org/eclipse/papyrus/infra/gmfdiag/properties/modelelement/GMFModelElement.java @@ -19,17 +19,28 @@ import org.eclipse.emf.databinding.FeaturePath; import org.eclipse.emf.ecore.EObject; import org.eclipse.emf.ecore.EStructuralFeature; import org.eclipse.emf.edit.domain.EditingDomain; +import org.eclipse.gmf.runtime.notation.Diagram; import org.eclipse.gmf.runtime.notation.NotationPackage; +import org.eclipse.gmf.runtime.notation.Style; import org.eclipse.jface.viewers.ILabelProvider; +import org.eclipse.jface.viewers.ILabelProviderListener; import org.eclipse.jface.viewers.LabelProvider; import org.eclipse.papyrus.infra.core.services.ServiceException; import org.eclipse.papyrus.infra.emf.utils.ServiceUtilsForEObject; +import org.eclipse.papyrus.infra.gmfdiag.common.utils.DiagramUtils; import org.eclipse.papyrus.infra.gmfdiag.properties.Activator; import org.eclipse.papyrus.infra.gmfdiag.properties.databinding.GradientObservableValue; +import org.eclipse.papyrus.infra.gmfdiag.properties.provider.ModelContentProvider; +import org.eclipse.papyrus.infra.gmfdiag.properties.util.LegacyOwnerObservable; import org.eclipse.papyrus.infra.services.labelprovider.service.LabelProviderService; +import org.eclipse.papyrus.infra.viewpoints.policy.PolicyChecker; +import org.eclipse.papyrus.infra.viewpoints.policy.ViewPrototype; +import org.eclipse.papyrus.infra.viewpoints.style.StylePackage; +import org.eclipse.papyrus.infra.widgets.providers.IStaticContentProvider; import org.eclipse.papyrus.uml.tools.databinding.PapyrusObservableList; import org.eclipse.papyrus.uml.tools.databinding.PapyrusObservableValue; import org.eclipse.papyrus.views.properties.modelelement.EMFModelElement; +import org.eclipse.swt.graphics.Image; /** * A ModelElement to represent an element of the GMF Notation metamodel @@ -64,8 +75,30 @@ public class GMFModelElement extends EMFModelElement { super(source, domain); } + @Override + protected boolean isFeatureEditable(String propertyPath) { + if (propertyPath.endsWith("owner")) { + return true; + } + return super.isFeatureEditable(propertyPath); + } + @Override protected IObservable doGetObservable(String propertyPath) { + if (propertyPath.endsWith("owner")) { + Diagram diagram = (Diagram)source; + Style style = diagram.getStyle(StylePackage.Literals.PAPYRUS_VIEW_STYLE); + if (style != null) + return new PapyrusObservableValue(style, StylePackage.Literals.PAPYRUS_VIEW_STYLE__OWNER, domain); + return new LegacyOwnerObservable(diagram, StylePackage.Literals.PAPYRUS_VIEW_STYLE__OWNER, domain); + } else if (propertyPath.endsWith("prototype")) { + Diagram diagram = (Diagram)source; + Style style = diagram.getStyle(StylePackage.Literals.PAPYRUS_VIEW_STYLE); + if (style != null) + return new PapyrusObservableValue(style, StylePackage.Literals.PAPYRUS_VIEW_STYLE__CONFIGURATION, domain); + return new LegacyOwnerObservable(diagram, StylePackage.Literals.PAPYRUS_VIEW_STYLE__CONFIGURATION, domain); + } + FeaturePath featurePath = getFeaturePath(propertyPath); EStructuralFeature feature = getFeature(propertyPath); @@ -88,6 +121,22 @@ public class GMFModelElement extends EMFModelElement { @Override public ILabelProvider getLabelProvider(String propertyPath) { + if (propertyPath.endsWith("prototype")) { + return new ILabelProvider() { + public void addListener(ILabelProviderListener listener) { } + public void removeListener(ILabelProviderListener listener) { } + public void dispose() { } + public boolean isLabelProperty(Object element, String property) { return false; } + public Image getImage(Object element) { + ViewPrototype proto = DiagramUtils.getPrototype((Diagram)source); + return proto.getIcon(); + } + public String getText(Object element) { + ViewPrototype proto = DiagramUtils.getPrototype((Diagram)source); + return proto.getQualifiedName(); + } + }; + } try { return ServiceUtilsForEObject.getInstance().getService(LabelProviderService.class, source).getLabelProvider(); } catch (ServiceException ex) { @@ -96,4 +145,43 @@ public class GMFModelElement extends EMFModelElement { } } + /** + * @see org.eclipse.papyrus.views.properties.modelelement.EMFModelElement#getContentProvider(java.lang.String) + */ + @Override + public IStaticContentProvider getContentProvider(String propertyPath) { + if (propertyPath.endsWith("element")) { + Diagram diagram = (Diagram)source; + return new ModelContentProvider(diagram, getRoot(diagram.getElement())) { + @Override + protected boolean isValid(EObject selection, Diagram diagram, ViewPrototype prototype) { + return PolicyChecker.getCurrent().canHaveNewView(selection, DiagramUtils.getOwner(diagram), prototype); + } + }; + } else if (propertyPath.endsWith("owner")) { + Diagram diagram = (Diagram) source; + return new ModelContentProvider(diagram, getRoot(diagram.getElement())) { + @Override + protected boolean isValid(EObject selection, Diagram diagram, ViewPrototype prototype) { + return PolicyChecker.getCurrent().canHaveNewView(diagram.getElement(), selection, prototype); + } + }; + } + return super.getContentProvider(propertyPath); + } + + /** + * Gets the root EObject from the given one + * @param obj An object + * @return The root object which is an ancestor of the given one + */ + private EObject getRoot(EObject obj) { + EObject current = obj; + EObject parent = obj.eContainer(); + while (parent != null) { + current = parent; + parent = parent.eContainer(); + } + return current; + } } diff --git a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.properties/src/org/eclipse/papyrus/infra/gmfdiag/properties/provider/ModelContentProvider.java b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.properties/src/org/eclipse/papyrus/infra/gmfdiag/properties/provider/ModelContentProvider.java new file mode 100755 index 00000000000..78e29021201 --- /dev/null +++ b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.properties/src/org/eclipse/papyrus/infra/gmfdiag/properties/provider/ModelContentProvider.java @@ -0,0 +1,78 @@ +/***************************************************************************** + * Copyright (c) 2013 CEA LIST. + * + * + * 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: + * Laurent Wouters laurent.wouters@cea.fr - Initial API and implementation + * + *****************************************************************************/ +package org.eclipse.papyrus.infra.gmfdiag.properties.provider; + +import org.eclipse.emf.ecore.EObject; +import org.eclipse.gmf.runtime.notation.Diagram; +import org.eclipse.papyrus.infra.gmfdiag.common.utils.DiagramUtils; +import org.eclipse.papyrus.infra.viewpoints.configuration.ui.ECoreModelContentProvider; +import org.eclipse.papyrus.infra.viewpoints.policy.ViewPrototype; +import org.eclipse.papyrus.infra.widgets.providers.IHierarchicContentProvider; +import org.eclipse.papyrus.infra.widgets.providers.IStaticContentProvider; + +/** + * Represents a simple ECore model content provider for the selection of a diagram's root model element + * @author Laurent Wouters + */ +public abstract class ModelContentProvider extends ECoreModelContentProvider implements IStaticContentProvider, IHierarchicContentProvider { + + /** + * The diagram + */ + private Diagram diagram; + + /** + * Initializes the provider with the given root + * @param diagram The diagram for which a root model element is to be provided + * @param root The root object + */ + public ModelContentProvider(Diagram diagram, EObject root) { + super(root); + this.diagram = diagram; + } + + /* (non-Javadoc) + * @see org.eclipse.papyrus.infra.widgets.providers.IStaticContentProvider#getElements() + */ + public Object[] getElements() { + return getElements(null); + } + + /* (non-Javadoc) + * @see org.eclipse.papyrus.infra.widgets.providers.IHierarchicContentProvider#isValidValue(java.lang.Object) + */ + public boolean isValidValue(Object element) { + if (element == null) + return false; + if (!(element instanceof EObject)) + return false; + ViewPrototype prototype = DiagramUtils.getPrototype(diagram); + if (prototype == null) + return false; + return isValid((EObject) element, diagram, prototype); + } + + /** + * Determines whether the selection is valid + * + * @param selection + * The selection + * @param diagram + * The diagram + * @param prototype + * The diagram's prototype + * @return true if the selection is valid + */ + protected abstract boolean isValid(EObject selection, Diagram diagram, ViewPrototype prototype); +} diff --git a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.properties/src/org/eclipse/papyrus/infra/gmfdiag/properties/util/LegacyOwnerObservable.java b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.properties/src/org/eclipse/papyrus/infra/gmfdiag/properties/util/LegacyOwnerObservable.java new file mode 100755 index 00000000000..8b941318fae --- /dev/null +++ b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.properties/src/org/eclipse/papyrus/infra/gmfdiag/properties/util/LegacyOwnerObservable.java @@ -0,0 +1,158 @@ +/***************************************************************************** + * Copyright (c) 2013 CEA LIST. + * + * + * 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: + * Laurent Wouters laurent.wouters@cea.fr - Initial API and implementation + * + *****************************************************************************/ +package org.eclipse.papyrus.infra.gmfdiag.properties.util; + +import org.eclipse.core.databinding.observable.ChangeEvent; +import org.eclipse.core.databinding.observable.IChangeListener; +import org.eclipse.core.databinding.observable.Realm; +import org.eclipse.core.databinding.observable.value.AbstractObservableValue; +import org.eclipse.core.databinding.observable.value.ValueDiff; +import org.eclipse.emf.common.command.AbstractCommand; +import org.eclipse.emf.common.command.Command; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.EReference; +import org.eclipse.emf.ecore.EcorePackage; +import org.eclipse.emf.edit.domain.EditingDomain; +import org.eclipse.gmf.runtime.notation.Diagram; +import org.eclipse.gmf.runtime.notation.NotationPackage; +import org.eclipse.gmf.runtime.notation.Style; +import org.eclipse.papyrus.infra.gmfdiag.common.utils.DiagramUtils; +import org.eclipse.papyrus.infra.viewpoints.style.StylePackage; +import org.eclipse.papyrus.uml.tools.databinding.PapyrusObservableValue; + +/** + * Represents the observable value of a diagram's owner in the case where the diagram does not yet have the viewpoints-compatible style for holding the value itself + * @author Laurent Wouters + */ +public class LegacyOwnerObservable extends AbstractObservableValue { + + private EditingDomain domain; + private Diagram diagram; + private EReference reference; + private IChangeListener styleListener; + private IChangeListener valueListener; + private PapyrusObservableValue styleObservable; + private PapyrusObservableValue valueObservable; + + /** + * Initializes the observables + * @param diagram The diagram to be observed + * @param domain The editing domain + */ + public LegacyOwnerObservable(Diagram diagram, EReference feature, EditingDomain domain) { + super(Realm.getDefault()); + this.domain = domain; + this.diagram = diagram; + this.reference = feature; + buildStyleListener(); + this.styleObservable = new PapyrusObservableValue(diagram, NotationPackage.Literals.VIEW__STYLES, domain); + this.styleObservable.addChangeListener(styleListener); + } + + /* (non-Javadoc) + * @see org.eclipse.core.databinding.observable.value.AbstractObservableValue#isStale() + */ + public boolean isStale() { + return (valueListener != null); + } + + /** + * Builds the listener for the styles property of the diagram + */ + private void buildStyleListener() { + this.styleListener = new IChangeListener() { + public void handleChange(ChangeEvent event) { + if (valueListener != null) + return; + Style style = LegacyOwnerObservable.this.diagram.getStyle(StylePackage.Literals.PAPYRUS_VIEW_STYLE); + if (style != null) + buildValueListener(style); + } + }; + } + + /** + * Builds the listener for the EObjectValue property of the style + * @param style The style to be observed + */ + private void buildValueListener(Style style) { + this.valueListener = new IChangeListener() { + public void handleChange(ChangeEvent event) { + fireValueChange(new ValueDiff() { + public Object getOldValue() { return null; } + public Object getNewValue() { return DiagramUtils.getOwner(diagram); } + }); + } + }; + valueObservable = new PapyrusObservableValue(style, reference, domain); + valueObservable.addChangeListener(valueListener); + } + + /* (non-Javadoc) + * @see org.eclipse.core.databinding.observable.value.IObservableValue#getValueType() + */ + public Object getValueType() { + return EcorePackage.Literals.EOBJECT; + } + + /* (non-Javadoc) + * @see org.eclipse.core.databinding.observable.value.AbstractObservableValue#doGetValue() + */ + protected Object doGetValue() { + return DiagramUtils.getOwner(diagram); + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.core.databinding.observable.value.AbstractObservableValue#doSetValue(java.lang.Object) + */ + protected void doSetValue(Object value) { + Command emfCommand = getCommand((EObject) value); + domain.getCommandStack().execute(emfCommand); + } + + private Command getCommand(final EObject newValue) { + final EObject oldValue = DiagramUtils.getOwner(diagram); + + return new AbstractCommand("Change diagram's owner") { + private boolean createStyle = true; + + public void execute() { + Style style = diagram.getStyle(StylePackage.Literals.PAPYRUS_VIEW_STYLE); + createStyle = (style == null); + DiagramUtils.setOwner(diagram, newValue); + } + + public void redo() { + DiagramUtils.setOwner(diagram, newValue); + } + + @Override + public void undo() { + if (createStyle) { + Style style = diagram.getStyle(StylePackage.Literals.PAPYRUS_VIEW_STYLE); + diagram.getStyles().remove(style); + } else { + DiagramUtils.setOwner(diagram, oldValue); + } + } + + @Override + public boolean prepare() { + return true; + } + }; + } +} -- cgit v1.2.3 From 9f626161fd840f0cdbbd96a0bbba8454f140d8a3 Mon Sep 17 00:00:00 2001 From: Laurent Wouters Date: Fri, 14 Mar 2014 15:50:10 +0100 Subject: 386118: [EMF Facet] Papyrus should progressively support EMF Facet 0.2 Fixing wrong cast in NotationHelper Signed-off-by: Laurent Wouters --- .../eclipse/papyrus/infra/gmfdiag/common/helper/NotationHelper.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'plugins/infra/gmfdiag') diff --git a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/helper/NotationHelper.java b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/helper/NotationHelper.java index 7c43a346c1a..5d9b14456b6 100644 --- a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/helper/NotationHelper.java +++ b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/helper/NotationHelper.java @@ -33,7 +33,8 @@ public class NotationHelper { if(source instanceof View) { return (View)source; } - return (View) EMFHelper.getEObject(source); + EObject obj = EMFHelper.getEObject(source); + return (obj instanceof View) ? (View) obj : null; } /** -- cgit v1.2.3