diff options
author | Cedric Dumoulin | 2013-11-06 22:30:12 +0000 |
---|---|---|
committer | Cedric Dumoulin | 2014-01-27 13:54:31 +0000 |
commit | 5a0596a1179a6bae4507cf5ac8c43dd270c9492b (patch) | |
tree | 94b554aca48f89a696af194977d750e0e1f1ead2 /extraplugins/layers/org.eclipse.papyrus.layers.ui | |
parent | d1c34b70a768fe90fb15f7c2d78ba6fa08eb83f8 (diff) | |
download | org.eclipse.papyrus-5a0596a1179a6bae4507cf5ac8c43dd270c9492b.tar.gz org.eclipse.papyrus-5a0596a1179a6bae4507cf5ac8c43dd270c9492b.tar.xz org.eclipse.papyrus-5a0596a1179a6bae4507cf5ac8c43dd270c9492b.zip |
New Commands to add and remove Views from selected items in diagrams.
Diffstat (limited to 'extraplugins/layers/org.eclipse.papyrus.layers.ui')
5 files changed, 570 insertions, 196 deletions
diff --git a/extraplugins/layers/org.eclipse.papyrus.layers.ui/plugin.xml b/extraplugins/layers/org.eclipse.papyrus.layers.ui/plugin.xml index 994f6bbfa8d..5edb3c76861 100644 --- a/extraplugins/layers/org.eclipse.papyrus.layers.ui/plugin.xml +++ b/extraplugins/layers/org.eclipse.papyrus.layers.ui/plugin.xml @@ -76,6 +76,14 @@ id="org.eclipse.papyrus.layers.ui.commands.openlayersexplorerview" name="Open Layers Explorer"> </command> + <command + id="org.eclipse.papyrus.layers.ui.commands.attachitemselectedindiagramtocurrentlayer" + name="Attach Views to Current Layer"> + </command> + <command + id="org.eclipse.papyrus.layers.ui.commands.removeitemselectedindiagramfromcurrentlayer" + name="Remove Views from Current Layer"> + </command> </extension> <extension name="Layers v3" @@ -124,6 +132,14 @@ class="org.eclipse.papyrus.layers3.ui.handlers.OpenLayerExplorerHandler" commandId="org.eclipse.papyrus.layers.ui.commands.openlayersexplorerview"> </handler> + <handler + class="org.eclipse.papyrus.layers3.ui.handlers.AttachDiagramSelectedItemsToCurrentLayer" + commandId="org.eclipse.papyrus.layers.ui.commands.attachitemselectedindiagramtocurrentlayer"> + </handler> + <handler + class="org.eclipse.papyrus.layers3.ui.handlers.RemoveDiagramSelectedItemsFromCurrentLayer" + commandId="org.eclipse.papyrus.layers.ui.commands.removeitemselectedindiagramfromcurrentlayer"> + </handler> </extension> <extension point="org.eclipse.ui.menus"> @@ -266,6 +282,18 @@ style="push"> </command> </menu> + <command + commandId="org.eclipse.papyrus.layers.ui.commands.attachitemselectedindiagramtocurrentlayer" + label="Attach Selected Views to Layer" + style="push" + tooltip="Attach selected views to the layer selected in the LayersExplorer"> + </command> + <command + commandId="org.eclipse.papyrus.layers.ui.commands.removeitemselectedindiagramfromcurrentlayer" + label="Remove Selected Views from Layer" + style="push" + tooltip="Remove selected views from the layer selected in the LayersExplorer"> + </command> </menuContribution> <menuContribution locationURI="popup:org.eclipse.gmf.runtime.diagram.ui.DiagramEditorContextMenu"> @@ -289,6 +317,18 @@ style="push" tooltip="Open the Layers Explorer View"> </command> + <command + commandId="org.eclipse.papyrus.layers.ui.commands.attachitemselectedindiagramtocurrentlayer" + label="Attach Views to Layer" + style="push" + tooltip="Attach selected views to the layer selected in the LayersExplorer"> + </command> + <command + commandId="org.eclipse.papyrus.layers.ui.commands.removeitemselectedindiagramfromcurrentlayer" + label="Remove Views from Layer" + style="push" + tooltip="Remove selected views from the layer selected in the LayersExplorer"> + </command> </menu> </menuContribution> </extension> diff --git a/extraplugins/layers/org.eclipse.papyrus.layers.ui/src/org/eclipse/papyrus/layers3/ui/commands/AttachDiagramViewsToLayer.java b/extraplugins/layers/org.eclipse.papyrus.layers.ui/src/org/eclipse/papyrus/layers3/ui/commands/AttachDiagramViewsToLayer.java index 03ba078e374..4acb91f9efa 100644 --- a/extraplugins/layers/org.eclipse.papyrus.layers.ui/src/org/eclipse/papyrus/layers3/ui/commands/AttachDiagramViewsToLayer.java +++ b/extraplugins/layers/org.eclipse.papyrus.layers.ui/src/org/eclipse/papyrus/layers3/ui/commands/AttachDiagramViewsToLayer.java @@ -1,194 +1,194 @@ -/*******************************************************************************
- * 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:
- * Cedric Dumoulin Cedric.dumoulin@lifl.fr
- ******************************************************************************/
-package org.eclipse.papyrus.layers3.ui.commands;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import org.eclipse.core.commands.ExecutionEvent;
-import org.eclipse.core.expressions.IEvaluationContext;
-import org.eclipse.emf.common.notify.AdapterFactory;
-import org.eclipse.emf.edit.ui.provider.AdapterFactoryContentProvider;
-import org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider;
-import org.eclipse.gmf.runtime.notation.Diagram;
-import org.eclipse.gmf.runtime.notation.View;
-import org.eclipse.jface.viewers.ILabelProvider;
-import org.eclipse.jface.viewers.ITreeContentProvider;
-import org.eclipse.jface.window.Window;
-import org.eclipse.papyrus.infra.core.services.ServiceException;
-import org.eclipse.papyrus.layers.notationmodel.edit.NotationItemProviderAdapterFactory;
-import org.eclipse.papyrus.layers.stackmodel.LayersException;
-import org.eclipse.papyrus.layers.stackmodel.layers.AbstractLayer;
-import org.eclipse.papyrus.layers.stackmodel.layers.LayersStackApplication;
-import org.eclipse.swt.widgets.Display;
-import org.eclipse.ui.dialogs.CheckedTreeSelectionDialog;
-import org.eclipse.uml2.uml.edit.providers.UMLItemProviderAdapterFactory;
-
-
-/**
- * Handler used to select multiples views to attach to the selected Layer.
- * Property are proposed in a selectable tree.
- *
- * @author cedric dumoulin
- *
- */
-public class AttachDiagramViewsToLayer extends AbstractLayersCommand {
-
- /**
- * Constructor.
- *
- */
- public AttachDiagramViewsToLayer() {
- }
-
- /**
- * @see org.eclipse.papyrus.layers3.ui.commands.AbstractLayersCommand#getCommandName()
- *
- * @return
- */
- @Override
- public String getCommandName() {
- return "Attach multiple views";
- }
-
- /**
- * @see org.eclipse.papyrus.layers3.ui.commands.AbstractLayersCommand#doExecute(org.eclipse.core.commands.ExecutionEvent, org.eclipse.core.expressions.IEvaluationContext, java.util.List)
- *
- * @param event
- * @param context
- * @param selections
- */
- @Override
- protected void doExecute(ExecutionEvent event, IEvaluationContext context, List<Object> selections) {
-
- // check enable
- if( ! isEnabled(context, selections)) {
- return;
- }
-
- // Open the dialog to ask the new name
- // TODO dialog should not be in the transaction !! put it outside !
-
- try {
- // Get the layer and application
- LayersStackApplication application = lookupLayersStackApplicationChecked(context);
- AbstractLayer layer = (AbstractLayer)getSelections(context).get(0);
-
- // Get the diagram and the views
- Diagram diagram = layer.getLayersStack().getDiagram();
-
-
- // Label and content providers
-
- AdapterFactory domainAdapterFactory = new UMLItemProviderAdapterFactory();
- AdapterFactory adapterFactory = new NotationItemProviderAdapterFactory(domainAdapterFactory);
- ITreeContentProvider contentProvider = new AdapterFactoryContentProvider(adapterFactory);
- ILabelProvider labelProvider = new AdapterFactoryLabelProvider(adapterFactory);
-
- // Test of SpecificViewContentProvider that do exatly the same filtering
-// final Diagram diag2 = diagram;
-// ILabelProvider labelProvider = ServiceUtilsForIEvaluationContext.getInstance().getService(LabelProviderService.class, context).getLabelProvider();
-// ITreeContentProvider contentProvider = new SpecificViewContentProvider() {
-// @Override
-// public Object[] getElements(Object inputElement) {
-// // TODO Auto-generated method stub
-// return new EObject[]{ diag2};
-// }
-// };
-
-
- CheckedTreeSelectionDialog dialog = new CheckedTreeSelectionDialog(Display.getCurrent().getActiveShell(), labelProvider, contentProvider);
-
- dialog.setTitle("Diagram Views Selection");
- dialog.setMessage("Select Views to attach to the current layer:");
- dialog.setInput(diagram);
- List<View> initialSelection = layer.getViews();
- dialog.setInitialSelections(initialSelection.toArray(new View[0]));
-
- if(dialog.open() != Window.OK) {
- return;
- }
-
- // TODO: improve algorithm:
- // use only the two list (or arrays) of initialSelection and finalSelection
- // Walk the first, for each element,
- // if the element is in the second list
- // remove element in both list (set list[i]=null)
- // else
- // remove element from first list
- // At the end,
- // initialCollection contains unsetted elements (with nulls)
- // finalSelection contains set elements (with nulls)
- // Walk each array/list, and skip nulls.
-
-
-
- // Process selected Properties
- Object[] res = dialog.getResult();
- // Create a list from the array. No better way ...
- // In the same time, create unchanged and set lists
- List<View> finalSelection = new ArrayList<View>(res.length);
- List<View> unchangedProperties = new ArrayList<View>(initialSelection.size());
- List<View> setProperties = new ArrayList<View>(finalSelection.size());
-
- for( Object o : res ) {
- // Create a clone list of the result
- finalSelection.add((View)o);
- // Create the unchanged and set list
- if( initialSelection.contains(o)) {
- unchangedProperties.add((View)o);
- }
- else {
- setProperties.add((View)o);
- }
- }
-
- // We also need the unset list
- // Obtain it by removing unchanged from initialSelection
- List<View> unsetProperties = new ArrayList<View>(initialSelection);
- unsetProperties.removeAll(unchangedProperties);
-
- // Remove unset views
- for( View property : unsetProperties) {
- System.out.println("unset view " + property.getElement());
- layer.getViews().remove(property);
- }
-
- // add set instances
- for( View property : setProperties) {
- System.out.println("set Property " + property.getElement());
- layer.getViews().add(property);
- }
-
- } catch (LayersException e) {
- // silently fails
- e.printStackTrace();
- } catch (org.eclipse.papyrus.infra.core.resource.NotFoundException e) {
- // silently fails
- e.printStackTrace();
- } catch (ServiceException e) {
- // silently fails
- e.printStackTrace();
- }
-
-
- }
-
- /**
- * Return true if it is possible to attach a property.
- */
- @Override
- public boolean isEnabled(IEvaluationContext context, List<Object> selections) {
-
- return selectionFirstElementInstanceOf(selections, AbstractLayer.class);
- }
-
-}
+/******************************************************************************* + * 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: + * Cedric Dumoulin Cedric.dumoulin@lifl.fr + ******************************************************************************/ +package org.eclipse.papyrus.layers3.ui.commands; + +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.core.expressions.IEvaluationContext; +import org.eclipse.emf.common.notify.AdapterFactory; +import org.eclipse.emf.edit.ui.provider.AdapterFactoryContentProvider; +import org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider; +import org.eclipse.gmf.runtime.notation.Diagram; +import org.eclipse.gmf.runtime.notation.View; +import org.eclipse.jface.viewers.ILabelProvider; +import org.eclipse.jface.viewers.ITreeContentProvider; +import org.eclipse.jface.window.Window; +import org.eclipse.papyrus.infra.core.services.ServiceException; +import org.eclipse.papyrus.layers.notationmodel.edit.NotationItemProviderAdapterFactory; +import org.eclipse.papyrus.layers.stackmodel.LayersException; +import org.eclipse.papyrus.layers.stackmodel.layers.AbstractLayer; +import org.eclipse.papyrus.layers.stackmodel.layers.LayersStackApplication; +import org.eclipse.swt.widgets.Display; +import org.eclipse.ui.dialogs.CheckedTreeSelectionDialog; +import org.eclipse.uml2.uml.edit.providers.UMLItemProviderAdapterFactory; + + +/** + * Handler used to select multiples views to attach to the selected Layer. + * Property are proposed in a selectable tree dialog. + * + * @author cedric dumoulin + * + */ +public class AttachDiagramViewsToLayer extends AbstractLayersCommand { + + /** + * Constructor. + * + */ + public AttachDiagramViewsToLayer() { + } + + /** + * @see org.eclipse.papyrus.layers3.ui.commands.AbstractLayersCommand#getCommandName() + * + * @return + */ + @Override + public String getCommandName() { + return "Attach multiple views"; + } + + /** + * @see org.eclipse.papyrus.layers3.ui.commands.AbstractLayersCommand#doExecute(org.eclipse.core.commands.ExecutionEvent, org.eclipse.core.expressions.IEvaluationContext, java.util.List) + * + * @param event + * @param context + * @param selections + */ + @Override + protected void doExecute(ExecutionEvent event, IEvaluationContext context, List<Object> selections) { + + // check enable + if( ! isEnabled(context, selections)) { + return; + } + + // Open the dialog to ask the new name + // TODO dialog should not be in the transaction !! put it outside ! + + try { + // Get the layer and application + LayersStackApplication application = lookupLayersStackApplicationChecked(context); + AbstractLayer layer = (AbstractLayer)getSelections(context).get(0); + + // Get the diagram and the views + Diagram diagram = layer.getLayersStack().getDiagram(); + + + // Label and content providers + + AdapterFactory domainAdapterFactory = new UMLItemProviderAdapterFactory(); + AdapterFactory adapterFactory = new NotationItemProviderAdapterFactory(domainAdapterFactory); + ITreeContentProvider contentProvider = new AdapterFactoryContentProvider(adapterFactory); + ILabelProvider labelProvider = new AdapterFactoryLabelProvider(adapterFactory); + + // Test of SpecificViewContentProvider that do exatly the same filtering +// final Diagram diag2 = diagram; +// ILabelProvider labelProvider = ServiceUtilsForIEvaluationContext.getInstance().getService(LabelProviderService.class, context).getLabelProvider(); +// ITreeContentProvider contentProvider = new SpecificViewContentProvider() { +// @Override +// public Object[] getElements(Object inputElement) { +// // TODO Auto-generated method stub +// return new EObject[]{ diag2}; +// } +// }; + + + CheckedTreeSelectionDialog dialog = new CheckedTreeSelectionDialog(Display.getCurrent().getActiveShell(), labelProvider, contentProvider); + + dialog.setTitle("Diagram Views Selection"); + dialog.setMessage("Select Views to attach to the current layer:"); + dialog.setInput(diagram); + List<View> initialSelection = layer.getViews(); + dialog.setInitialSelections(initialSelection.toArray(new View[0])); + + if(dialog.open() != Window.OK) { + return; + } + + // TODO: improve algorithm: + // use only the two list (or arrays) of initialSelection and finalSelection + // Walk the first, for each element, + // if the element is in the second list + // remove element in both list (set list[i]=null) + // else + // remove element from first list + // At the end, + // initialCollection contains unsetted elements (with nulls) + // finalSelection contains set elements (with nulls) + // Walk each array/list, and skip nulls. + + + + // Process selected Properties + Object[] res = dialog.getResult(); + // Create a list from the array. No better way ... + // In the same time, create unchanged and set lists + List<View> finalSelection = new ArrayList<View>(res.length); + List<View> unchangedProperties = new ArrayList<View>(initialSelection.size()); + List<View> setProperties = new ArrayList<View>(finalSelection.size()); + + for( Object o : res ) { + // Create a clone list of the result + finalSelection.add((View)o); + // Create the unchanged and set list + if( initialSelection.contains(o)) { + unchangedProperties.add((View)o); + } + else { + setProperties.add((View)o); + } + } + + // We also need the unset list + // Obtain it by removing unchanged from initialSelection + List<View> unsetProperties = new ArrayList<View>(initialSelection); + unsetProperties.removeAll(unchangedProperties); + + // Remove unset views + for( View property : unsetProperties) { + System.out.println("unset view " + property.getElement()); + layer.getViews().remove(property); + } + + // add set instances + for( View property : setProperties) { + System.out.println("set Property " + property.getElement()); + layer.getViews().add(property); + } + + } catch (LayersException e) { + // silently fails + e.printStackTrace(); + } catch (org.eclipse.papyrus.infra.core.resource.NotFoundException e) { + // silently fails + e.printStackTrace(); + } catch (ServiceException e) { + // silently fails + e.printStackTrace(); + } + + + } + + /** + * Return true if it is possible to attach a property. + */ + @Override + public boolean isEnabled(IEvaluationContext context, List<Object> selections) { + + return selectionFirstElementInstanceOf(selections, AbstractLayer.class); + } + +} diff --git a/extraplugins/layers/org.eclipse.papyrus.layers.ui/src/org/eclipse/papyrus/layers3/ui/handlers/AttachDiagramSelectedItemsToCurrentLayer.java b/extraplugins/layers/org.eclipse.papyrus.layers.ui/src/org/eclipse/papyrus/layers3/ui/handlers/AttachDiagramSelectedItemsToCurrentLayer.java new file mode 100644 index 00000000000..85d77bfff76 --- /dev/null +++ b/extraplugins/layers/org.eclipse.papyrus.layers.ui/src/org/eclipse/papyrus/layers3/ui/handlers/AttachDiagramSelectedItemsToCurrentLayer.java @@ -0,0 +1,167 @@ +/** + * + */ +package org.eclipse.papyrus.layers3.ui.handlers; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.core.expressions.IEvaluationContext; +import org.eclipse.core.runtime.Platform; +import org.eclipse.gmf.runtime.diagram.ui.resources.editor.parts.DiagramDocumentEditor; +import org.eclipse.gmf.runtime.notation.View; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.jface.viewers.ITreeSelection; +import org.eclipse.papyrus.infra.core.services.ServiceException; +import org.eclipse.papyrus.infra.emf.utils.ServiceUtilsForIEvaluationContext; +import org.eclipse.papyrus.layers.stackmodel.NotFoundException; +import org.eclipse.papyrus.layers.stackmodel.layers.Layer; +import org.eclipse.papyrus.layers3.ui.commands.AbstractLayersCommand; +import org.eclipse.papyrus.layers3.ui.view.LayersExplorerView; +import org.eclipse.papyrus.layers3.ui.view.LayersExplorerViewUtils; +import org.eclipse.ui.IEditorPart; + +/** + * Attach the items selected in the diagram to the current Layer. + * The current layer is the layer currently selected in the {@link LayersExplorerView}. + * + * @author cedric dumoulin + * + */ +public class AttachDiagramSelectedItemsToCurrentLayer extends AbstractLayersCommand { + + /** + * + */ + @Override + public String getCommandName() { + return "Attach Selected Items"; + } + + /** + * + */ + @Override + protected void doExecute(ExecutionEvent event, IEvaluationContext context, List<Object> selections) { + // TODO Auto-generated method stub + + + List<View> diagramSelectedViews; + Layer currentLayer; + try { + diagramSelectedViews = getSelectedViewsFromCurrentDiagramEditor(context); + currentLayer = getSelectedLayerFromLayerExplorer(context); + } catch (NotFoundException e) { + // fail + System.err.println("ERROR - " + this.getClass().getName() + " - " +e.getMessage()); + return; + } + + System.out.println("selected views=" + diagramSelectedViews); + System.out.println("selected layer=" + currentLayer.getName()); + + currentLayer.getViews().addAll(diagramSelectedViews); + } + + /** + * Get the currently selected layer from the LayerExplorer. + * Throws an exception if not found. + * + * @param context + * @return + */ + private Layer getSelectedLayerFromLayerExplorer(IEvaluationContext context) throws NotFoundException { + + + LayersExplorerView layersExplorerView = LayersExplorerViewUtils.findView(context); + ISelection selection; + try { + selection = layersExplorerView.getSite().getSelectionProvider().getSelection(); + } catch (NullPointerException e) { + // Can't get a selection + throw new NotFoundException("Can't get selection object from LayersExplorerView" + e); //$NON-NLS-1$ + } + + if(! (selection instanceof ITreeSelection)) { + throw new NotFoundException("No selection in LayersExplorerView"); //$NON-NLS-1$ + } + + ITreeSelection treeSelection = (ITreeSelection)selection; + + Object firstSelection = treeSelection.getFirstElement(); + if( ! (firstSelection instanceof Layer) ) { + throw new NotFoundException("LayersExplorerView first selection should be of type 'Layer'"); //$NON-NLS-1$ + } + return (Layer)firstSelection; + } + + /** + * Get all selected Views in the currently active diagram. + * If no diagram is active, throw an exception. + * + * @param context + * @return + */ + @SuppressWarnings("unchecked") + private List<View> getSelectedViewsFromCurrentDiagramEditor( IEvaluationContext context ) throws NotFoundException { + // TODO Auto-generated method stub + + DiagramDocumentEditor diagramEditor = getNestedActiveDiagramDocumentEditor(context); + + ISelection selection = diagramEditor.getSite().getSelectionProvider().getSelection(); + + if(! (selection instanceof IStructuredSelection || selection.isEmpty() )) { + return Collections.emptyList(); + } + + IStructuredSelection structuredSelection = (IStructuredSelection)selection; + + // Get the views from the selected part + List<View> result = new ArrayList<>(structuredSelection.size()); +// for( Object obj : structuredSelection.toList() ) { +// if( obj instanceof GraphicalEditPart ) { +// GraphicalEditPart part = (GraphicalEditPart)obj; +// View view = part.getNotationView(); +// result.add(view); +// } +// } + for( Object obj : structuredSelection.toList() ) { + // Adapt the selected object to a View. + View view = (View)Platform.getAdapterManager().getAdapter(obj, View.class); + if( view != null) { + result.add(view); + } + } + + return result; + } + + /** + * Get the active DiagramDocumentEditor. + * Throw an exception if not found. + * + * @param context + * @return + * @throws NotFoundException + */ + private DiagramDocumentEditor getNestedActiveDiagramDocumentEditor( IEvaluationContext context) throws NotFoundException { + + IEditorPart editor; + try { + editor = ServiceUtilsForIEvaluationContext.getInstance().getNestedActiveIEditorPart(context); + } catch (ServiceException e) { + throw new NotFoundException("Selected editor do not provides ServiceRegistry", e); //$NON-NLS-1$ + } + + if(!(editor instanceof DiagramDocumentEditor)) { + throw new NotFoundException("Selected editor do not contains Diagram"); //$NON-NLS-1$ + } + + return (DiagramDocumentEditor)editor; + } + + +} diff --git a/extraplugins/layers/org.eclipse.papyrus.layers.ui/src/org/eclipse/papyrus/layers3/ui/handlers/OpenLayerExplorerHandler.java b/extraplugins/layers/org.eclipse.papyrus.layers.ui/src/org/eclipse/papyrus/layers3/ui/handlers/OpenLayerExplorerHandler.java index 728d53170f4..6f3f178b255 100644 --- a/extraplugins/layers/org.eclipse.papyrus.layers.ui/src/org/eclipse/papyrus/layers3/ui/handlers/OpenLayerExplorerHandler.java +++ b/extraplugins/layers/org.eclipse.papyrus.layers.ui/src/org/eclipse/papyrus/layers3/ui/handlers/OpenLayerExplorerHandler.java @@ -9,8 +9,6 @@ import org.eclipse.core.commands.ExecutionException; import org.eclipse.core.expressions.IEvaluationContext; import org.eclipse.papyrus.layers.stackmodel.NotFoundException; import org.eclipse.papyrus.layers3.ui.view.LayersExplorerViewUtils; -import org.eclipse.ui.IWorkbenchPartSite; -import org.eclipse.ui.handlers.HandlerUtil; /** * Open Layer Explorer ui handler. diff --git a/extraplugins/layers/org.eclipse.papyrus.layers.ui/src/org/eclipse/papyrus/layers3/ui/handlers/RemoveDiagramSelectedItemsFromCurrentLayer.java b/extraplugins/layers/org.eclipse.papyrus.layers.ui/src/org/eclipse/papyrus/layers3/ui/handlers/RemoveDiagramSelectedItemsFromCurrentLayer.java new file mode 100644 index 00000000000..de394f27fb0 --- /dev/null +++ b/extraplugins/layers/org.eclipse.papyrus.layers.ui/src/org/eclipse/papyrus/layers3/ui/handlers/RemoveDiagramSelectedItemsFromCurrentLayer.java @@ -0,0 +1,169 @@ +/** + * + */ +package org.eclipse.papyrus.layers3.ui.handlers; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.core.expressions.IEvaluationContext; +import org.eclipse.core.runtime.Platform; +import org.eclipse.gmf.runtime.diagram.ui.resources.editor.parts.DiagramDocumentEditor; +import org.eclipse.gmf.runtime.notation.View; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.jface.viewers.ITreeSelection; +import org.eclipse.papyrus.infra.core.services.ServiceException; +import org.eclipse.papyrus.infra.emf.utils.ServiceUtilsForIEvaluationContext; +import org.eclipse.papyrus.layers.stackmodel.NotFoundException; +import org.eclipse.papyrus.layers.stackmodel.layers.Layer; +import org.eclipse.papyrus.layers3.ui.commands.AbstractLayersCommand; +import org.eclipse.papyrus.layers3.ui.view.LayersExplorerView; +import org.eclipse.papyrus.layers3.ui.view.LayersExplorerViewUtils; +import org.eclipse.ui.IEditorPart; + +/** + * Remove the items selected in the diagram from the current Layer. + * The current layer is the layer currently selected in the {@link LayersExplorerView}. + * TODO Factorize methods common with {@link AttachDiagramSelectedItemsToCurrentLayer} in a parent class. + * TODO use the {@link #preExecute(ExecutionEvent, IEvaluationContext, List)} method to get the requested selections. + * + * @author cedric dumoulin + * + */ +public class RemoveDiagramSelectedItemsFromCurrentLayer extends AbstractLayersCommand { + + /** + * + */ + @Override + public String getCommandName() { + return "Attach Selected Items"; + } + + /** + * + */ + @Override + protected void doExecute(ExecutionEvent event, IEvaluationContext context, List<Object> selections) { + // TODO Auto-generated method stub + + + List<View> diagramSelectedViews; + Layer currentLayer; + try { + diagramSelectedViews = getSelectedViewsFromCurrentDiagramEditor(context); + currentLayer = getSelectedLayerFromLayerExplorer(context); + } catch (NotFoundException e) { + // fail + System.err.println("ERROR - " + this.getClass().getName() + " - " +e.getMessage()); + return; + } + + System.out.println("selected views=" + diagramSelectedViews); + System.out.println("selected layer=" + currentLayer.getName()); + + currentLayer.getViews().removeAll(diagramSelectedViews); + } + + /** + * Get the currently selected layer from the LayerExplorer. + * Throws an exception if not found. + * + * @param context + * @return + */ + private Layer getSelectedLayerFromLayerExplorer(IEvaluationContext context) throws NotFoundException { + + + LayersExplorerView layersExplorerView = LayersExplorerViewUtils.findView(context); + ISelection selection; + try { + selection = layersExplorerView.getSite().getSelectionProvider().getSelection(); + } catch (NullPointerException e) { + // Can't get a selection + throw new NotFoundException("Can't get selection object from LayersExplorerView" + e); //$NON-NLS-1$ + } + + if(! (selection instanceof ITreeSelection)) { + throw new NotFoundException("No selection in LayersExplorerView"); //$NON-NLS-1$ + } + + ITreeSelection treeSelection = (ITreeSelection)selection; + + Object firstSelection = treeSelection.getFirstElement(); + if( ! (firstSelection instanceof Layer) ) { + throw new NotFoundException("LayersExplorerView first selection should be of type 'Layer'"); //$NON-NLS-1$ + } + return (Layer)firstSelection; + } + + /** + * Get all selected Views in the currently active diagram. + * If no diagram is active, throw an exception. + * + * @param context + * @return + */ + @SuppressWarnings("unchecked") + private List<View> getSelectedViewsFromCurrentDiagramEditor( IEvaluationContext context ) throws NotFoundException { + // TODO Auto-generated method stub + + DiagramDocumentEditor diagramEditor = getNestedActiveDiagramDocumentEditor(context); + + ISelection selection = diagramEditor.getSite().getSelectionProvider().getSelection(); + + if(! (selection instanceof IStructuredSelection || selection.isEmpty() )) { + return Collections.emptyList(); + } + + IStructuredSelection structuredSelection = (IStructuredSelection)selection; + + // Get the views from the selected part + List<View> result = new ArrayList<>(structuredSelection.size()); +// for( Object obj : structuredSelection.toList() ) { +// if( obj instanceof GraphicalEditPart ) { +// GraphicalEditPart part = (GraphicalEditPart)obj; +// View view = part.getNotationView(); +// result.add(view); +// } +// } + for( Object obj : structuredSelection.toList() ) { + // Adapt the selected object to a View. + View view = (View)Platform.getAdapterManager().getAdapter(obj, View.class); + if( view != null) { + result.add(view); + } + } + + return result; + } + + /** + * Get the active DiagramDocumentEditor. + * Throw an exception if not found. + * + * @param context + * @return + * @throws NotFoundException + */ + private DiagramDocumentEditor getNestedActiveDiagramDocumentEditor( IEvaluationContext context) throws NotFoundException { + + IEditorPart editor; + try { + editor = ServiceUtilsForIEvaluationContext.getInstance().getNestedActiveIEditorPart(context); + } catch (ServiceException e) { + throw new NotFoundException("Selected editor do not provides ServiceRegistry", e); //$NON-NLS-1$ + } + + if(!(editor instanceof DiagramDocumentEditor)) { + throw new NotFoundException("Selected editor do not contains Diagram"); //$NON-NLS-1$ + } + + return (DiagramDocumentEditor)editor; + } + + +} |