diff options
Diffstat (limited to 'plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/src/org/eclipse/papyrus/uml')
8 files changed, 2575 insertions, 1 deletions
diff --git a/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/src/org/eclipse/papyrus/uml/diagram/common/Messages.java b/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/src/org/eclipse/papyrus/uml/diagram/common/Messages.java index 3df09963ffb..b31c97e5235 100644 --- a/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/src/org/eclipse/papyrus/uml/diagram/common/Messages.java +++ b/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/src/org/eclipse/papyrus/uml/diagram/common/Messages.java @@ -8,7 +8,7 @@ * * Contributors: Francisco Javier Cano Muñoz (Prodevelop) - initial API implementation * Emilien Perico (Atos Origin) - add tags for links with keywords - * + * Gabriel Pascual (ALL4TEC) gabriel.pascual@all4tec.net - Initial API and implementation ******************************************************************************/ package org.eclipse.papyrus.uml.diagram.common; @@ -18,6 +18,8 @@ public class Messages extends NLS { private static final String BUNDLE_NAME = "messages"; //$NON-NLS-1$ + public static String CreateUmlElementDialog_New_Element_Name; + public static String ElementImportParser_undefined_value; public static String IconStylePreferencePage_cheerful_style; @@ -26,8 +28,32 @@ public class Messages extends NLS { public static String IconStylePreferencePage_icon_style_group; + public static String InterfaceManagerDialog_CreateANewInterface; + + public static String InterfaceManagerDialog_CreateNewInterface; + + public static String InterfaceManagerDialog_SelectedTheProvidedInterfaces; + + public static String InterfaceManagerDialog_SelectionHasToBeAPackageOrAClassifier; + + public static String InterfaceManagerDialog_SelectTheParentOfThisNewInterface; + + public static String InterfaceManagerDialog_SelectTheRequiredInterfaces; + + public static String InterfaceManagerDialog_ThePortIsTypedWithAnInterfaceWarningMessage; + + public static String InterfaceManagerDialog_Title; + public static String ModelElementSelectionPageMessage; + public static String ManageProvidedInterfacesHandler_OK; + + public static String ManageProvidedInterfacesHandler_TheTypeOfThePortIsNotDefined; + + public static String SelectionValidated; + + public static String NoSelectionFound; + public static String UMLNewDiagramFileWizard_RootSelectionPageSelectionTitle; public static String UMLNewDiagramFileWizard_RootSelectionPageNoSelectionMessage; diff --git a/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/src/org/eclipse/papyrus/uml/diagram/common/actions/handlers/ManagePortInterfacesHandler.java b/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/src/org/eclipse/papyrus/uml/diagram/common/actions/handlers/ManagePortInterfacesHandler.java new file mode 100644 index 00000000000..9bb6838f7bd --- /dev/null +++ b/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/src/org/eclipse/papyrus/uml/diagram/common/actions/handlers/ManagePortInterfacesHandler.java @@ -0,0 +1,172 @@ +/***************************************************************************** + * Copyright (c) 2009-2011 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: + * Yann Tanguy (CEA LIST) yann.tanguy@cea.fr - Initial API and implementation + * Gabriel Pascual (ALL4TEC) gabriel.pascual@all4tec.net - Initial API and implementation + *****************************************************************************/ +package org.eclipse.papyrus.uml.diagram.common.actions.handlers; + +import org.eclipse.core.commands.AbstractHandler; +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.core.commands.ExecutionException; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.gef.EditPart; +import org.eclipse.gef.commands.Command; +import org.eclipse.gef.commands.UnexecutableCommand; +import org.eclipse.gmf.runtime.common.ui.util.DisplayUtils; +import org.eclipse.jface.dialogs.Dialog; +import org.eclipse.jface.dialogs.MessageDialog; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.papyrus.commands.wrappers.GEFtoEMFCommandWrapper; +import org.eclipse.papyrus.infra.core.services.ServiceException; +import org.eclipse.papyrus.infra.emf.utils.EMFHelper; +import org.eclipse.papyrus.infra.emf.utils.ServiceUtilsForEObject; +import org.eclipse.papyrus.uml.diagram.common.Messages; +import org.eclipse.papyrus.uml.diagram.common.dialogs.IPortInterfaceManagerDialog; +import org.eclipse.papyrus.uml.diagram.common.dialogs.InterfaceManagerDialog; +import org.eclipse.ui.handlers.HandlerUtil; +import org.eclipse.uml2.uml.Classifier; +import org.eclipse.uml2.uml.Interface; +import org.eclipse.uml2.uml.Port; +import org.eclipse.uml2.uml.Type; + +/** + * This handler provides the action to manage the provided and required {@link Interface} for a {@link Port}. + */ +public class ManagePortInterfacesHandler extends AbstractHandler { + + /** The port edit part. */ + private EditPart portEditPart = null; + + + /** + * Instantiates a new manage port interfaces handler. + */ + public ManagePortInterfacesHandler() { + super(); + } + + /** + * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent) + * + * @param event + * @return + * @throws ExecutionException + */ + public Object execute(ExecutionEvent event) throws ExecutionException { + ISelection selection = HandlerUtil.getCurrentSelection(event); + + // Do something only if there is a selection + if(selection.isEmpty()) { + return null; + } + + // Handle structured selection + if(selection instanceof IStructuredSelection) { + IStructuredSelection structuredSelection = (IStructuredSelection)selection; + + // Verify if selection is an EditPart + Object firstElement = structuredSelection.getFirstElement(); + if(firstElement instanceof EditPart) { + portEditPart = (EditPart)firstElement; + + // Verify if semantic element is a Port + EObject selectedElement = EMFHelper.getEObject(portEditPart); + if(selectedElement instanceof Port) { + Port port = (Port)selectedElement; + + // Run action + ManageProvidedInterfaceAction action = new ManageProvidedInterfaceAction(port); + try { + ServiceUtilsForEObject.getInstance().getTransactionalEditingDomain(port).getCommandStack().execute(new GEFtoEMFCommandWrapper(action.getCommand())); + } catch (ServiceException ex) { + throw new ExecutionException("An unexpected exception occurred", ex); + } + } + } + } + + return null; + } + + /** + * Gets the interface manager dialog. + * <p> + * Default implementation returns {@link InterfaceManagerDialog}. + * </p> + * + * @param port + * the port + * @return the interface manager dialog + */ + public IPortInterfaceManagerDialog getInterfaceManagerDialog(Port port) { + return new InterfaceManagerDialog(DisplayUtils.getDefaultShell(), port); + } + + /** + * Gets the port edit part. + * + * @return the port edit part + */ + protected EditPart getPortEditPart() { + return portEditPart; + } + + /** + * This class provides the action to manage the provided and required interfaces. + */ + public class ManageProvidedInterfaceAction { + + /** the {@link Port}. */ + private Port port = null; + + /** the {@link Type} of the port. */ + private Type type = null; + + /** + * + * Constructor. + * + * @param editpart + * the editpart of the port + */ + public ManageProvidedInterfaceAction(Port port) { + this.port = port; + this.type = port.getType(); + } + + /** + * Returns the command to add/remove provided/required interfaces. + * + * @return the command + * the command to add/remove provided/required interfaces + */ + public Command getCommand() { + if(type != null && !(type instanceof Classifier)) { + return UnexecutableCommand.INSTANCE; + } else if(type == null) { + // Display message if Port type is undefined + MessageDialog dialog = new MessageDialog(DisplayUtils.getDefaultShell(), Messages.InterfaceManagerDialog_Title, null, Messages.ManageProvidedInterfacesHandler_TheTypeOfThePortIsNotDefined, MessageDialog.WARNING, new String[]{ Messages.ManageProvidedInterfacesHandler_OK }, 0); + dialog.open(); + + } else { + + // Launch manager + IPortInterfaceManagerDialog dialog = getInterfaceManagerDialog(port); + if(dialog.open() == Dialog.OK) { + return dialog.getCommand(); + } + } + return UnexecutableCommand.INSTANCE; + } + } + +} diff --git a/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/src/org/eclipse/papyrus/uml/diagram/common/dialogs/CreateUmlElementDialog.java b/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/src/org/eclipse/papyrus/uml/diagram/common/dialogs/CreateUmlElementDialog.java new file mode 100644 index 00000000000..b2b92eba063 --- /dev/null +++ b/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/src/org/eclipse/papyrus/uml/diagram/common/dialogs/CreateUmlElementDialog.java @@ -0,0 +1,417 @@ +/***************************************************************************** + * Copyright (c) 2009-2011 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: + * Yann Tanguy (CEA LIST) yann.tanguy@cea.fr - Initial API and implementation + * Gabriel Pascual (ALL4TEC) gabriel.pascual@all4tec.net - Initial API and implementation + *****************************************************************************/ +package org.eclipse.papyrus.uml.diagram.common.dialogs; + +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.core.commands.ExecutionException; +import org.eclipse.core.runtime.Assert; +import org.eclipse.core.runtime.IAdaptable; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.emf.ecore.EClass; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.transaction.TransactionalEditingDomain; +import org.eclipse.gef.commands.Command; +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.common.core.command.UnexecutableCommand; +import org.eclipse.gmf.runtime.diagram.ui.commands.ICommandProxy; +import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand; +import org.eclipse.gmf.runtime.emf.type.core.IElementType; +import org.eclipse.gmf.runtime.emf.type.core.commands.CreateRelationshipCommand; +import org.eclipse.gmf.runtime.emf.type.core.requests.CreateElementRequest; +import org.eclipse.gmf.runtime.emf.type.core.requests.IEditCommandRequest; +import org.eclipse.jface.viewers.ILabelProvider; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.jface.viewers.ITreeContentProvider; +import org.eclipse.papyrus.infra.services.edit.service.ElementEditServiceUtils; +import org.eclipse.papyrus.infra.services.edit.service.IElementEditService; +import org.eclipse.papyrus.uml.diagram.common.Messages; +import org.eclipse.papyrus.uml.tools.utils.ElementUtil; +import org.eclipse.papyrus.uml.tools.utils.NamedElementUtil; +import org.eclipse.swt.SWT; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.swt.widgets.Text; +import org.eclipse.ui.dialogs.ElementTreeSelectionDialog; +import org.eclipse.uml2.uml.Interface; +import org.eclipse.uml2.uml.NamedElement; +import org.eclipse.uml2.uml.Package; +import org.eclipse.uml2.uml.UMLPackage; + +/** + * This dialog allows to create easily a new element in a UML Model. + */ +public class CreateUmlElementDialog extends ElementTreeSelectionDialog { + + /** the hinted type of the element to create. */ + private IElementType hintedType; + + /** the parent container of the element to create. */ + private Object parentContainer; + + /** the type of the element to create. */ + private String type; + + /** the root of the model. */ + private Package pack; + + /** the text field used to display the choose the name of the future element. */ + private Text text; + + /** the editing domain. */ + private TransactionalEditingDomain domain; + + /** The element to create is represented by this field. */ + private NewElementRepresentation elementToCreate; + + /** this Set contains names to exclude from the proposed name. */ + private List<String> forbiddenNames; + + /** + * Constructor. + * + * @param parent + * the parent + * @param domain + * the domain + * @param labelProvider + * the label provider + * @param contentProvider + * the content provider + * @param pack + * the pack + * @param hintedType + * the hinted type + */ + public CreateUmlElementDialog(Shell parent, TransactionalEditingDomain domain, ILabelProvider labelProvider, ITreeContentProvider contentProvider, Package pack, IElementType hintedType) { + super(parent, labelProvider, contentProvider); + Assert.isNotNull(pack); + Assert.isNotNull(hintedType); + this.pack = pack; + this.hintedType = hintedType; + this.domain = domain; + init(); + } + + /** + * Initializes the field {@link #type};. + */ + protected void init() { + EClass eClass = hintedType.getEClass(); + this.type = eClass.getName(); + this.parentContainer = null; + this.forbiddenNames = null; + } + + /** + * Sets the forbidden names. + * + * @param forbiddenNames + * the new forbidden names + */ + public void setForbiddenNames(List<String> forbiddenNames) { + this.forbiddenNames = forbiddenNames; + } + + /** + * Creates the dialog area. + * + * @param parent + * the parent + * @return the control + * @see org.eclipse.ui.dialogs.ElementTreeSelectionDialog#createDialogArea(org.eclipse.swt.widgets.Composite) + */ + @Override + protected Control createDialogArea(Composite parent) { + GridData data = (GridData)parent.getLayoutData(); + data.grabExcessHorizontalSpace = true; + data.horizontalSpan = 1; + data.horizontalAlignment = SWT.FILL; + data.verticalAlignment = SWT.FILL; + Composite composite = (Composite)super.createDialogArea(parent); + composite.setLayout(new GridLayout()); + GridData dat = new GridData(GridData.FILL_BOTH); + composite.setLayoutData(dat); + createNameSection(parent); + return composite; + } + + /** + * Creates the name section and put it at the top of the composite. + * + * @param parent + * the composite parent + */ + protected void createNameSection(Composite parent) { + Composite nameSection = new Composite(parent, SWT.NONE); + // the label + Label label = new Label(nameSection, SWT.NONE); + label.setText(Messages.CreateUmlElementDialog_New_Element_Name); + // the text field + text = new Text(nameSection, SWT.BORDER); + text.setText(getDefaultName()); + GridData textData = new GridData(GridData.FILL_HORIZONTAL); + text.setLayoutData(textData); + // layout for the nameSection + GridLayout layout = new GridLayout(2, false); + GridData data = new GridData(GridData.FILL_BOTH); + nameSection.setLayout(layout); + nameSection.setLayoutData(data); + // we set the name section in first! + nameSection.moveAbove(null); + } + + /** + * Returns the default name for the new Element. + * + * @return the default name + * the default name for the new Element + */ + protected String getDefaultName() { + boolean existsInterface = false; + String defaultName; + int max = 0; + do { + List<Interface> allInterface = ElementUtil.getInstancesFilteredByType(pack, Interface.class, null); + for(Object object : allInterface) { + String name = NamedElementUtil.getName((NamedElement)object); + if(name != null && name.startsWith(type)) { + name = name.substring(type.length()); + Integer val = null; + try { + val = Integer.parseInt(name); + } catch (NumberFormatException e) { + // Nothing to do + } + if(val != null) { + max = Math.max(Integer.valueOf(val), max); + existsInterface = true; + } + } + } + if(existsInterface) { + defaultName = type + new Integer(max + 1).toString(); + } else { + defaultName = type + new Integer(max).toString(); + } + max++; + } while(!isCorrectName(defaultName)); + return defaultName; + } + + /** + * Tests if the proposed name is not forbidden + * + * @param name + * a name + * @return + * <code>true</code> if the proposed name is not forbidden + */ + protected boolean isCorrectName(String name) { + if(forbiddenNames == null) { + return true; + } + return !forbiddenNames.contains(name); + } + + /** + * Returns the command to create the new element which the chosen name. + * + * @return the command + * the command to create the new element which the chosen name + */ + public Command getCommand() { + CompositeCommand cmd = new CompositeCommand("Create new element command"); //$NON-NLS-1$ + // request to create the new Interface + if(elementToCreate.getCreateElementRequest() != null) { + CreateElementRequest request = elementToCreate.getCreateElementRequest(); + ICommand command = getCommand(request); + if(command.canExecute()) { + cmd.add(command); + SetNameCommand nameCommand = new SetNameCommand(domain, "Set Name Command", null, request, elementToCreate); //$NON-NLS-1$ + if(nameCommand.canExecute()) { + cmd.add(nameCommand); + } + } + return new ICommandProxy(cmd); + } else { + return new ICommandProxy(new CustomCreateElementCommand(domain, "CustomCreateElementCommand", null, elementToCreate)); //$NON-NLS-1$ + } + } + + /** + * Compute result. + * + * @see org.eclipse.ui.dialogs.ElementTreeSelectionDialog#computeResult() + */ + @Override + protected void computeResult() { + java.util.List<Object> result = new ArrayList<Object>(); + result.add(elementToCreate); + setResult(result); + } + + /** + * Set the value to {@link #name} and {@link #parentContainer}. + * + * @see org.eclipse.ui.dialogs.SelectionStatusDialog#okPressed() + */ + @Override + protected void okPressed() { + ISelection selection = getTreeViewer().getSelection(); + parentContainer = ((IStructuredSelection)selection).getFirstElement(); + CreateElementRequest request = null; + if(parentContainer instanceof EObject) { + request = new CreateElementRequest(domain, (EObject)parentContainer, this.hintedType); + } + elementToCreate = new NewElementRepresentation(this.text.getText(), parentContainer, hintedType, request); + super.okPressed(); + } + + /** + * Returns a command corresponding to this request. + * + * @param request + * a request + * @return the command + * The command corresponding to this request or an {@link UnexecutableCommand} when the command can not be build + */ + private ICommand getCommand(IEditCommandRequest request) { + IElementEditService provider = ElementEditServiceUtils.getCommandProvider(UMLPackage.eINSTANCE.getNamedElement()); + { + if(provider != null) { + ICommand cmd = provider.getEditCommand(request); + if(cmd != null && cmd.canExecute()) { + return cmd; + } + } + } + return org.eclipse.gmf.runtime.common.core.command.UnexecutableCommand.INSTANCE; + } + + /** + * This command is used to set the chosen name to the new element. + */ + public class SetNameCommand extends AbstractTransactionalCommand { + + /** the representation that will own the new element when it will be created. */ + private NewElementRepresentation representation; + + /** + * Constructor. + * + * @param domain + * the domain + * @param label + * the label + * @param affectedFiles + * the affected files + * @param previousRequest + * the previous request + * @param representation + * the representation + */ + public SetNameCommand(TransactionalEditingDomain domain, String label, List<?> affectedFiles, CreateElementRequest previousRequest, NewElementRepresentation representation) { + super(domain, label, affectedFiles); + this.representation = representation; + } + + /** + * Do execute with result. + * + * @param monitor + * the monitor + * @param info + * the info + * @return the command result + * @throws ExecutionException + * the execution exception + * @see org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand#doExecuteWithResult(org.eclipse.core.runtime.IProgressMonitor, + * org.eclipse.core.runtime.IAdaptable) + */ + @Override + protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException { + if(representation.getEObject() instanceof NamedElement) { + NamedElement el = (NamedElement)representation.getEObject(); + el.setName(representation.getName()); + } + return CommandResult.newOKCommandResult(representation); + } + } + + /** + * This command allows to create a new element when its parent is not yet created when the command is build + * + * We need a such command, because {@link CreateRelationshipCommand#getCommandResult()} return <code>null</code> when the command has not been + * executed. + */ + public class CustomCreateElementCommand extends AbstractTransactionalCommand { + + /** the representation of the element to create. */ + private NewElementRepresentation elementToCreate; + + /** + * Constructor. + * + * @param domain + * the domain + * @param label + * the label + * @param affectedFiles + * the affected files + * @param elementToCreate + * the element to create + */ + public CustomCreateElementCommand(TransactionalEditingDomain domain, String label, List<?> affectedFiles, NewElementRepresentation elementToCreate) { + super(domain, label, affectedFiles); + this.elementToCreate = elementToCreate; + } + + /** + * Do execute with result. + * + * @param monitor + * the monitor + * @param info + * the info + * @return the command result + * @throws ExecutionException + * the execution exception + * @see org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand#doExecuteWithResult(org.eclipse.core.runtime.IProgressMonitor, + * org.eclipse.core.runtime.IAdaptable) + */ + @Override + protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException { + EObject container = elementToCreate.getEObjectParent(); + CreateElementRequest request = new CreateElementRequest(domain, container, hintedType); + ICommand command = getCommand(request); + command.execute(new NullProgressMonitor(), null); + EObject newEl = request.getNewElement(); + if(newEl instanceof NamedElement) { + ((NamedElement)newEl).setName(elementToCreate.getName()); + } + elementToCreate.setCreateElement(request.getNewElement()); + return CommandResult.newOKCommandResult(elementToCreate); + } + } +} diff --git a/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/src/org/eclipse/papyrus/uml/diagram/common/dialogs/ExtendedSelectionEditor.java b/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/src/org/eclipse/papyrus/uml/diagram/common/dialogs/ExtendedSelectionEditor.java new file mode 100644 index 00000000000..378c68c59b9 --- /dev/null +++ b/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/src/org/eclipse/papyrus/uml/diagram/common/dialogs/ExtendedSelectionEditor.java @@ -0,0 +1,546 @@ +/***************************************************************************** + * 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: + * Patrick Tessier (CEA LIST) - Initial API and implementation + /*****************************************************************************/ +package org.eclipse.papyrus.uml.diagram.common.dialogs; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.LinkedList; + +import org.eclipse.jface.viewers.ILabelProvider; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.jface.viewers.StructuredSelection; +import org.eclipse.jface.viewers.TableViewer; +import org.eclipse.papyrus.infra.widgets.Activator; +import org.eclipse.papyrus.infra.widgets.editors.IElementSelector; +import org.eclipse.papyrus.infra.widgets.messages.Messages; +import org.eclipse.papyrus.infra.widgets.providers.CollectionContentProvider; +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.events.SelectionListener; +import org.eclipse.swt.layout.FillLayout; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Table; + +/** + * Extension of selection editor with a table to display icon of element + * normally this class must inherits of SelectionEditor but list and table are type that are not compatible, so entire creation of this class was + * needed + */ +public class ExtendedSelectionEditor extends Composite implements SelectionListener { + + + /** the initial selection */ + protected final java.util.List<Object> initialSelection; + + /** The object selector */ + private IElementSelector selector; + + /** The SWT Composite in which the selector is drawn */ + protected Composite selectorSection; + + /** The message section */ + protected Composite messageSection; + + /** The additional button section */ + protected Composite createAdditionalButtonSection; + + /** The up/down buttons section */ + protected Composite rightButtonsSection; + + /** The listViewer for chosen elements */ + protected TableViewer selectedElementsViewer; + + /** The list for chosen elements */ + protected Table selectedElements; + + /** The add action button */ + private Button add; + + /** The remove action button */ + private Button remove; + + /** The add all action button */ + private Button addAll; + + /** The remove all action button */ + private Button removeAll; + + /** the up action button */ + private Button up; + + /** the down action button */ + private Button down; + + /** The label provider for the listViewer of chosen elements */ + private ILabelProvider labelProvider; + + /** The currently chosen elements */ + protected final Collection<Object> currentSelection; + + /** the listener for the additional button */ + private SelectionListener additionalButtonSelectionListener; + + /** the message for the editor */ + private String message; + + + /* + * This dialog can provide a Button to do some optional action + */ + + /** Indicates if the dialog provides a button */ + private boolean withAdditionalButton = false; + + /** the label of the button */ + private String additionalButtonLabel = null; + + /** the button */ + private Button additionalButton; + + /** The add/remove/addAll buttons section */ + protected Composite buttonSection; + + /** + * + * Constructor. + * + * @param parent + * the parent of this composite + * @param selector + * the selector + * @param labelProvider + * the label provider + * @param initialSelection + * the initial selection + */ + public ExtendedSelectionEditor(Composite parent, IElementSelector selector, ILabelProvider labelProvider, java.util.List<Object> initialSelection) { + super(parent, SWT.NONE); + this.selector = selector; + this.labelProvider = labelProvider; + this.initialSelection = new ArrayList<Object>(); + for(Object current : initialSelection) { + this.initialSelection.add(current); + } + currentSelection = initialSelection; + } + + /** + * This methods create this editor + */ + public void create() { + Composite parent = this; + GridLayout layout = new GridLayout(1, true); + GridData data = new GridData(); + data.grabExcessHorizontalSpace = true; + data.grabExcessVerticalSpace = true; + parent.setLayout(layout); + createMessageSection(parent); + createBody(parent); + createCreateButtonSection(parent); + } + + /** + * This method create the body of the editor : + * <ul> + * <li>the selector</li> + * <li>the list</li> + * <li>the buttons Add, AddAll, Remove, RemoveAll, Up, Down</li> + * </ul> + * + * @param parent + * the parent composite + */ + protected void createBody(Composite parent) { + + Composite par = new Composite(parent, SWT.NONE); + createSelectorSection(par); + createControlsSection(par); + createListSection(par); + createRightButtonsSection(par); + GridLayout layout = new GridLayout(); + layout.makeColumnsEqualWidth = false; + layout.numColumns = 4; + par.setLayout(layout); + GridData data = new GridData(SWT.FILL, SWT.FILL, true, true); + par.setLayoutData(data); + + } + + /** + * Creates the Message section + * + * @param parent + * the parent of the {@link Composite} + */ + protected void createMessageSection(Composite parent) { + messageSection = new Composite(parent, SWT.NONE); + Label label = new Label(messageSection, SWT.NONE); + label.setText(message); + messageSection.setLayout(new FillLayout(SWT.HORIZONTAL)); + } + + /** + * Creates the button section + * + * @param parent + * the parent of the {@link Composite} + */ + protected void createCreateButtonSection(Composite parent) { + if(this.withAdditionalButton) { + buttonSection = new Composite(parent, SWT.NONE); + additionalButton = new Button(buttonSection, SWT.PUSH); + if(additionalButtonLabel != null) { + additionalButton.setText(additionalButtonLabel); + } + buttonSection.setLayout(new FillLayout()); + this.additionalButton.addSelectionListener(additionalButtonSelectionListener); + } + } + + /** + * Creates the selector section + * + * @param parent + * The composite in which the section is created + */ + protected void createSelectorSection(Composite parent) { + selectorSection = new Composite(parent, SWT.NONE); + selectorSection.setLayout(new FillLayout()); + GridData data = new GridData(SWT.FILL, SWT.FILL, true, true); + data.widthHint = 400; + + // data.exclude = true; + selectorSection.setLayoutData(data); + selector.createControls(selectorSection); + + } + + /** + * Creates the main controls section (Add, remove, Add all, remove all) + * + * @param parent + * The composite in which the section is created + */ + protected void createControlsSection(Composite parent) { + buttonSection = new Composite(parent, SWT.NONE); + buttonSection.setLayout(new GridLayout(1, true)); + + add = new Button(buttonSection, SWT.PUSH); + add.setImage(Activator.getDefault().getImage("/icons/arrow_right.gif")); //$NON-NLS-1$ + add.addSelectionListener(this); + add.setToolTipText(Messages.MultipleValueSelectorDialog_AddSelectedElements); + + remove = new Button(buttonSection, SWT.PUSH); + remove.setImage(Activator.getDefault().getImage("/icons/arrow_left.gif")); //$NON-NLS-1$ + remove.addSelectionListener(this); + remove.setToolTipText(Messages.MultipleValueEditor_RemoveSelectedElements); + + addAll = new Button(buttonSection, SWT.PUSH); + addAll.setImage(Activator.getDefault().getImage("/icons/arrow_double.gif")); //$NON-NLS-1$ + addAll.addSelectionListener(this); + addAll.setToolTipText(Messages.MultipleValueSelectorDialog_AddAllElements); + + removeAll = new Button(buttonSection, SWT.PUSH); + removeAll.setImage(Activator.getDefault().getImage("/icons/arrow_left_double.gif")); //$NON-NLS-1$ + removeAll.addSelectionListener(this); + removeAll.setToolTipText(Messages.MultipleValueSelectorDialog_RemoveAllElements); + } + + + /** + * Creates the list displaying the currently selected elements + * + * @param parent + * The composite in which the section is created + */ + protected void createListSection(Composite parent) { + Composite listSection = new Composite(parent, SWT.NONE); + selectedElements = new Table(listSection, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL); + GridData data = new GridData(SWT.FILL, SWT.FILL, true, true); + data.widthHint = 400; + listSection.setLayout(new FillLayout()); + listSection.setLayoutData(data); + + selectedElementsViewer = new TableViewer(selectedElements); + + selectedElementsViewer.setContentProvider(CollectionContentProvider.instance); + + if(labelProvider != null) + selectedElementsViewer.setLabelProvider(labelProvider); + + selectedElementsViewer.setInput(currentSelection); + selector.setSelectedElements(currentSelection.toArray()); + + } + + /** + * Creates the up/down controls section + * + * @param parent + * The composite in which the section is created + */ + protected void createRightButtonsSection(Composite parent) { + rightButtonsSection = new Composite(parent, SWT.NONE); + rightButtonsSection.setLayout(new GridLayout(1, true)); + + up = new Button(rightButtonsSection, SWT.PUSH); + up.setImage(Activator.getDefault().getImage("/icons/Up_12x12.gif")); //$NON-NLS-1$ + up.addSelectionListener(this); + up.setToolTipText(Messages.MultipleValueEditor_MoveSelectedElementsUp); + + down = new Button(rightButtonsSection, SWT.PUSH); + down.setImage(Activator.getDefault().getImage("/icons/Down_12x12.gif")); //$NON-NLS-1$ + down.addSelectionListener(this); + down.setToolTipText(Messages.MultipleValueEditor_MoveSelectedElementsDown); + } + + + + /** + * {@inheritDoc} Handles the events on one of the control buttons + * + * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent) + * + * @param e + * The event that occurred + */ + public void widgetSelected(SelectionEvent e) { + if(e.widget == add) { + addAction(); + } else if(e.widget == remove) { + removeAction(); + } else if(e.widget == addAll) { + addAllAction(); + } else if(e.widget == removeAll) { + removeAllAction(); + } else if(e.widget == up) { + upAction(); + } else if(e.widget == down) { + downAction(); + } + } + + /** + * Handles the "Add" action + */ + protected void addAction() { + Object[] elements = selector.getSelectedElements(); + addElements(elements); + } + + /** + * Handles the "Up" action + */ + protected void upAction() { + IStructuredSelection selection = (IStructuredSelection)selectedElementsViewer.getSelection(); + + //We need a list to move objects. LinkedHashSet can't do that + java.util.List<Object> list = new LinkedList<Object>(currentSelection); + for(Object o : selection.toArray()) { + int oldIndex = list.indexOf(o); + if(oldIndex > 0) + move(list, oldIndex, oldIndex - 1); + } + + currentSelection.clear(); + currentSelection.addAll(list); + + IStructuredSelection selectionCopy = new StructuredSelection(selection.toArray()); + selectedElementsViewer.setSelection(selectionCopy); + selectedElementsViewer.refresh(); + } + + /** + * Handles the "Down" action + */ + protected void downAction() { + IStructuredSelection selection = (IStructuredSelection)selectedElementsViewer.getSelection(); + + //We need a list to move objects. LinkedHashSet can't do that + java.util.List<Object> list = new LinkedList<Object>(currentSelection); + + int maxIndex = list.size() - 1; + + Object[] selectionArray = selection.toArray(); + for(int i = selectionArray.length - 1; i >= 0; i--) { + Object o = selectionArray[i]; + int oldIndex = list.indexOf(o); + if(oldIndex < maxIndex) + move(list, oldIndex, oldIndex + 1); + } + + currentSelection.clear(); + currentSelection.addAll(list); + + IStructuredSelection selectionCopy = new StructuredSelection(selection.toArray()); + selectedElementsViewer.setSelection(selectionCopy); + selectedElementsViewer.refresh(); + } + + /** + * Moves an element from oldIndex to newIndex + * + * @param list + * The list in which to move the object + * @param oldIndex + * @param newIndex + */ + protected void move(java.util.List<Object> list, int oldIndex, int newIndex) { + int size = list.size(); + + if(oldIndex < 0 || oldIndex >= size) + throw new IndexOutOfBoundsException("oldIndex: " + oldIndex + ", size:" + size); //$NON-NLS-1$ //$NON-NLS-2$ + if(newIndex < 0 || newIndex >= size) + throw new IndexOutOfBoundsException("newIndex: " + newIndex + ", size:" + size); //$NON-NLS-1$ //$NON-NLS-2$ + Object element = list.remove(oldIndex); + list.add(newIndex, element); + } + + /** + * Handles the "Remove" action + */ + protected void removeAction() { + IStructuredSelection selection = (IStructuredSelection)selectedElementsViewer.getSelection(); + if(selection.isEmpty()) + return; + + for(Object element : selection.toArray()) { + currentSelection.remove(element); + } + + selector.setSelectedElements(currentSelection.toArray()); + selectedElementsViewer.setSelection(null); + selectedElementsViewer.refresh(); + } + + /** + * Handles the "Remove all" action + */ + protected void removeAllAction() { + currentSelection.clear(); + selector.setSelectedElements(new Object[0]); + selectedElementsViewer.setSelection(null); + selectedElementsViewer.refresh(); + } + + /** + * Handles the "Add All" action + */ + protected void addAllAction() { + Object[] elements = selector.getAllElements(); + addElements(elements); + } + + /** + * Adds the specified elements to the currently selected elements (For + * "Add" and "Add all" actions) + * + * @param elements + * The elements to be added + */ + protected void addElements(Object[] elements) { + if(elements != null) { + currentSelection.addAll(Arrays.asList(elements)); + selectedElementsViewer.refresh(); + } + } + + + /** + * + * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent) + * + * @param e + */ + public void widgetDefaultSelected(SelectionEvent e) { + //Nothing to do + } + + /** + * + * @param withAdditionalButton + * @param messageButton + * @param listener + */ + public void setAdditionalButton(boolean withAdditionalButton, String messageButton, SelectionListener listener) { + this.withAdditionalButton = withAdditionalButton; + this.additionalButtonLabel = messageButton; + this.additionalButtonSelectionListener = listener; + } + + /** + * Set the message displayed by the Editor + * + * @param string + * the message displayed by the editor + */ + public void setMessage(String string) { + this.message = string; + } + + /** + * Returns all selected elements + * + * @return + * all selected elements + */ + public Collection<?> getSelectedElements() { + return currentSelection; + } + + + /** + * Returns the list of the elements to remove + * + * @return + * the list of the elements to remove + */ + public java.util.List<Object> getElementToRemove() { + java.util.List<Object> removedObject = new ArrayList<Object>(); + for(Object current : this.initialSelection) { + if(!getSelectedElements().contains(current)) { + removedObject.add(current); + } + } + return removedObject; + } + + /** + * Returns the list of the elements to add + * + * @return + * the list of the elements to add + */ + public java.util.List<Object> getElementToAdd() { + java.util.List<Object> addedObject = new ArrayList<Object>(); + for(Object current : getSelectedElements()) { + if(!this.initialSelection.contains(current)) { + addedObject.add(current); + } + } + return addedObject; + } + + /** + * + * @return the table viewer in order to get the element selection + */ + public TableViewer getSelectedElementsViewer() { + return selectedElementsViewer; + } +} diff --git a/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/src/org/eclipse/papyrus/uml/diagram/common/dialogs/IPortInterfaceManagerDialog.java b/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/src/org/eclipse/papyrus/uml/diagram/common/dialogs/IPortInterfaceManagerDialog.java new file mode 100644 index 00000000000..11842933f7e --- /dev/null +++ b/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/src/org/eclipse/papyrus/uml/diagram/common/dialogs/IPortInterfaceManagerDialog.java @@ -0,0 +1,36 @@ +/***************************************************************************** + * Copyright (c) 2014 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: + * Gabriel Pascual (ALL4TEC) gabriel.pascual@all4tec.net - Initial API and implementation + *****************************************************************************/ +package org.eclipse.papyrus.uml.diagram.common.dialogs; + +import org.eclipse.gef.commands.Command; + + +/** + * Interface for Port interface manager dialog. + */ +public interface IPortInterfaceManagerDialog { + + /** + * Returns the commands to apply the changes. + * + * @return the commands to apply the changes + */ + Command getCommand(); + + /** + * @see org.eclipse.jface.dialogs.Dialog#open() + * + * @return + */ + int open(); + +} diff --git a/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/src/org/eclipse/papyrus/uml/diagram/common/dialogs/InterfaceManagerDialog.java b/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/src/org/eclipse/papyrus/uml/diagram/common/dialogs/InterfaceManagerDialog.java new file mode 100644 index 00000000000..d5dcde44442 --- /dev/null +++ b/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/src/org/eclipse/papyrus/uml/diagram/common/dialogs/InterfaceManagerDialog.java @@ -0,0 +1,1024 @@ +/***************************************************************************** + * Copyright (c) 2009-2011 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: + * Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation + * Gabriel Pascual (ALL4TEC) gabriel.pascual@all4tec.fr - Initial API and implementation + *****************************************************************************/ +package org.eclipse.papyrus.uml.diagram.common.dialogs; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.eclipse.core.commands.ExecutionException; +import org.eclipse.core.runtime.Assert; +import org.eclipse.core.runtime.IAdaptable; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.emf.common.util.EList; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.transaction.TransactionalEditingDomain; +import org.eclipse.gef.commands.Command; +import org.eclipse.gef.commands.CompoundCommand; +import org.eclipse.gef.commands.UnexecutableCommand; +import org.eclipse.gmf.runtime.common.core.command.CommandResult; +import org.eclipse.gmf.runtime.common.core.command.ICommand; +import org.eclipse.gmf.runtime.diagram.ui.commands.ICommandProxy; +import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand; +import org.eclipse.gmf.runtime.emf.type.core.ElementTypeRegistry; +import org.eclipse.gmf.runtime.emf.type.core.requests.AbstractEditCommandRequest; +import org.eclipse.gmf.runtime.emf.type.core.requests.CreateRelationshipRequest; +import org.eclipse.gmf.runtime.emf.type.core.requests.DestroyReferenceRequest; +import org.eclipse.jface.viewers.ILabelProvider; +import org.eclipse.jface.viewers.ITreeContentProvider; +import org.eclipse.jface.viewers.Viewer; +import org.eclipse.papyrus.infra.core.editor.IMultiDiagramEditor; +import org.eclipse.papyrus.infra.core.services.ServiceException; +import org.eclipse.papyrus.infra.core.utils.ServiceUtils; +import org.eclipse.papyrus.infra.services.edit.service.ElementEditServiceUtils; +import org.eclipse.papyrus.infra.services.edit.service.IElementEditService; +import org.eclipse.papyrus.infra.widgets.editors.IElementSelector; +import org.eclipse.papyrus.infra.widgets.editors.SelectionEditor; +import org.eclipse.papyrus.infra.widgets.providers.AbstractStaticContentProvider; +import org.eclipse.papyrus.infra.widgets.selectors.ReferenceSelector; +import org.eclipse.papyrus.uml.diagram.common.Activator; +import org.eclipse.papyrus.uml.diagram.common.Messages; +import org.eclipse.papyrus.uml.diagram.common.util.Util; +import org.eclipse.papyrus.uml.diagram.common.util.Visitor; +import org.eclipse.papyrus.uml.tools.commands.DestroyDependencyWithoutSupplierCommand; +import org.eclipse.papyrus.uml.tools.providers.UMLLabelProvider; +import org.eclipse.papyrus.uml.tools.utils.ElementUtil; +import org.eclipse.swt.SWT; +import org.eclipse.swt.custom.CLabel; +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.events.SelectionListener; +import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.ui.IEditorPart; +import org.eclipse.ui.IWorkbenchPage; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.dialogs.ISelectionStatusValidator; +import org.eclipse.ui.dialogs.SelectionDialog; +import org.eclipse.uml2.uml.Classifier; +import org.eclipse.uml2.uml.Dependency; +import org.eclipse.uml2.uml.Element; +import org.eclipse.uml2.uml.Interface; +import org.eclipse.uml2.uml.NamedElement; +import org.eclipse.uml2.uml.Namespace; +import org.eclipse.uml2.uml.Package; +import org.eclipse.uml2.uml.Port; +import org.eclipse.uml2.uml.Realization; +import org.eclipse.uml2.uml.UMLPackage; +import org.eclipse.uml2.uml.Usage; +import org.eclipse.uml2.uml.edit.providers.ImageItemProvider; +import org.eclipse.uml2.uml.edit.providers.UMLItemProviderAdapterFactory; + + +/** + * This class provides a Dialog to manage easily the provided and the required Interfaces for a {@link Port}. + */ +public class InterfaceManagerDialog extends SelectionDialog implements IPortInterfaceManagerDialog { + + /** the selected port. */ + private Port port; + + /** the list of the provided interfaces. */ + private List<Object> providedInterfaces; + + /** the list of the required interfaces. */ + private List<Object> requiredInterfaces; + + /** the {@link ReferenceSelector} for the provided interfaces. */ + private ReferenceSelector providedSelector; + + /** the {@link ReferenceSelector} for the required interfaces. */ + private ReferenceSelector requiredSelector; + + /** the Editor for the provided interfaces. */ + private SelectionEditorWrapper providedEditor; + + /** the Editor for the required interfaces. */ + private SelectionEditorWrapper requiredEditor; + + /** In this map, we link the {@link Interface} with their {@link Usage} This map is used when we want remove an Interface from a Usage. */ + private Map<Interface, List<Usage>> requiredInterfacesMappedWithUsage = new HashMap<Interface, List<Usage>>(); + + /** + * In this map, we link the {@link Interface} with their {@link Realization} This map is used when we want remove an Interface from a Realization. + */ + private Map<Interface, List<Realization>> providedInterfacesMappedWithRealization = new HashMap<Interface, List<Realization>>(); + + /** the label provider for selector. */ + private ILabelProvider selectorLabelProvider = new SelectorLabelProvider(); + + /** the label provider for the create new element dialog. */ + private final ILabelProvider newElementLabelProvider = new CreateElementLabelProvider(); + + /** the domain used for the commands. */ + private TransactionalEditingDomain domain; + + /** the model. */ + private Package model = null; + + /** this command contains the list of the command to create the new Interfaces. */ + private CompoundCommand interfaceCreationCommand = new CompoundCommand("Create Interfaces Commands"); //$NON-NLS-1$ + + /** This list contains representation of the Interface before their creation. */ + private List<NewElementRepresentation> interfaceRepresentations; + + /** boolean used to know if the {@link Port} is typed with an {@link Interface}. */ + private boolean typedWithInterface; + + /** path for the warning icons. */ + private static final String ICON_WARNING = "/icons/warning.gif"; //$NON-NLS-1$ + + /** + * This list contains the names used for the new elements. + * We need to remember these names are used in order don't suggest them when we create other new elements + */ + private List<String> forbiddenNames; + + + /** + * + * Constructor. + * + * @param parentShell + * the parent {@link Shell} + * @param port + * the {@link Port} for which we want manage the Interfaces + */ + public InterfaceManagerDialog(Shell parentShell, Port port) { + super(parentShell); + this.setTitle(Messages.InterfaceManagerDialog_Title); + this.port = port; + Assert.isTrue(this.port.getType() instanceof Classifier); + this.typedWithInterface = (this.port.getType() instanceof Interface); + } + + /** + * Creates the dialog. + * + * @see org.eclipse.jface.dialogs.Dialog#create() + */ + @Override + public void create() { + super.create(); + init(); + } + + + + /** + * This method initialize some fields of the class. + */ + protected void init() { + + this.forbiddenNames = new ArrayList<String>(); + this.interfaceRepresentations = new ArrayList<NewElementRepresentation>(); + EList<Namespace> namespaces = port.allNamespaces(); + Element el = namespaces.get(namespaces.size() - 1); + if(el instanceof Package) { + model = (Package)el; + } else { + model = el.getNearestPackage(); + } + + // fill the hashmap to link Interfaces with Usage and InterfaceRealization + buildProvidedInterfaces(); + buildRequiredInterfaces(); + + // we build the list of the provided interfaces + this.providedInterfaces = new ArrayList<Object>(); + for(Interface current : this.port.getProvideds()) { + this.providedInterfaces.add(current); + } + // when the port is typed by an Interface, we don't propose this interface in the possible Provided Interfaces list. + if(typedWithInterface) { + this.providedInterfaces.remove(this.port.getType()); + } + + // we build the list of the required interfaces + this.requiredInterfaces = new ArrayList<Object>(); + for(Interface current : this.port.getRequireds()) { + this.requiredInterfaces.add(current); + } + + // initialize the transactional editing domain + IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); + IEditorPart editor = activePage.getActiveEditor(); + if(editor instanceof IMultiDiagramEditor) { + try { + this.domain = ServiceUtils.getInstance().getTransactionalEditingDomain(((IMultiDiagramEditor)editor).getServicesRegistry()); + } catch (ServiceException e) { + e.printStackTrace(); + } + } + + // selectors creation + createSelectors(); + // editors creation + createEditors(); + + } + + /** + * This method fills the map {@link #requiredInterfacesMappedWithUsage}. + */ + protected void buildRequiredInterfaces() { + List<Usage> allUsage = ElementUtil.getInstancesFilteredByType(this.model, Usage.class, null); + List<Classifier> classifiers = new ArrayList<Classifier>(); + classifiers.add((Classifier)this.port.getType()); + + classifiers.addAll(Util.getAllSuperClasses(null, (Classifier)this.port.getType())); + + for(Object current : allUsage) { + Usage usage = (Usage)current; + for(Classifier classifier : classifiers) {// we test if the current Usage is linked with the type of the Port + if(usage.getClients().contains(classifier)) {// this Usage concerns the current port + for(EObject target : usage.getSuppliers()) { + if(this.port.getRequireds().contains(target)) { + List<Usage> usages = requiredInterfacesMappedWithUsage.get(target); + if(usages != null) { + usages.add(usage); + } else { + usages = new ArrayList<Usage>(); + usages.add(usage); + this.requiredInterfacesMappedWithUsage.put((Interface)target, usages); + } + } + } + break; + } + } + } + } + + /** + * This method fills the map {@link #providedInterfacesMappedWithRealization}. + */ + protected void buildProvidedInterfaces() { + List<Realization> allRealization = ElementUtil.getInstancesFilteredByType(this.model, Realization.class, null); + List<Classifier> classifiers = new ArrayList<Classifier>(); + classifiers.add((Classifier)this.port.getType()); + + classifiers.addAll(Util.getAllSuperClasses(null, (Classifier)this.port.getType())); + + for(Object current : allRealization) { + Realization realization = (Realization)current; + for(Classifier classifier : classifiers) {// we test if the current Realization is linked with the type of the Port + if(realization.getClients().contains(classifier)) {// this Realization concerns the current port + for(EObject target : realization.getSuppliers()) { + if(this.port.getProvideds().contains(target)) { + List<Realization> realizations = providedInterfacesMappedWithRealization.get(target); + if(realizations != null) { + realizations.add(realization); + } else { + realizations = new ArrayList<Realization>(); + realizations.add(realization); + this.providedInterfacesMappedWithRealization.put((Interface)target, realizations); + } + } + } + break; + } + } + } + } + + /** + * This method creates the selectors {@link #providedSelector} and {@link #requiredSelector}. + */ + protected void createSelectors() { + // the provided selector + this.providedSelector = new ReferenceSelector(true); + this.providedSelector.setLabelProvider(selectorLabelProvider); + this.providedSelector.setContentProvider(new AbstractStaticContentProvider() { + + /** + * + * @see org.eclipse.papyrus.editors.providers.IStaticContentProvider#getElements() + * + * @return + */ + @Override + public Object[] getElements() { + List<Object> tmp = new ArrayList<Object>(); + tmp.addAll(getAllAvailableInterfaces(model)); + if(typedWithInterface) { + tmp.remove(port.getType()); + } + // we add the new interfaces which are not yet created + tmp.addAll(interfaceRepresentations); + return tmp.toArray(); + } + }); + // the required selector + this.requiredSelector = new ReferenceSelector(true); + this.requiredSelector.setLabelProvider(selectorLabelProvider); + this.requiredSelector.setContentProvider(new AbstractStaticContentProvider() { + + /** + * + * @see org.eclipse.papyrus.editors.providers.IStaticContentProvider#getElements() + * + * @return + */ + @Override + public Object[] getElements() { + List<Object> tmp = new ArrayList<Object>(); + tmp.addAll(getAllAvailableInterfaces(model)); + + // we add the new interfaces which are not yet created + tmp.addAll(interfaceRepresentations); + return tmp.toArray(); + } + }); + } + + /** + * This method create the 2 Editors {@link #providedEditor} and {@link #requiredEditor} and provides the listener for the button of these editors. + */ + protected void createEditors() { + Composite parent = (Composite)getDialogArea(); + CreateButtonListener listener = new CreateButtonListener(); + // we set a specific message if the port is typed with an Interface + if(typedWithInterface) { + Composite messageSection = new Composite(parent, SWT.BORDER); + CLabel label = new CLabel(messageSection, SWT.NONE); + + label.setText(Messages.InterfaceManagerDialog_ThePortIsTypedWithAnInterfaceWarningMessage); + Image errorImage = Activator.getPluginIconImage(Activator.ID, ICON_WARNING); + label.setImage(errorImage); + GridLayout layout = new GridLayout(1, true); + GridData data = new GridData(SWT.FILL, SWT.FILL, true, false); + messageSection.setLayout(layout); + messageSection.setLayoutData(data); + messageSection.setEnabled(false); + } + this.providedEditor = createEditor(parent, providedSelector, Messages.InterfaceManagerDialog_SelectedTheProvidedInterfaces, false, listener, this.providedInterfaces); + this.requiredEditor = createEditor(parent, requiredSelector, Messages.InterfaceManagerDialog_SelectTheRequiredInterfaces, true, listener, this.requiredInterfaces); + + this.providedEditor.create(); + this.requiredEditor.create(); + + GridLayout layout = (GridLayout)parent.getLayout(); + layout.numColumns = 1; + layout.makeColumnsEqualWidth = true; + GridData data = new GridData(SWT.FILL, SWT.FILL, true, true); + this.providedEditor.setLayoutData(data); + this.requiredEditor.setLayoutData(data); + getShell().setSize(950, 700); + getShell().layout(); + } + + /** + * Creates a {@link SelectionEditor} and returns it. + * + * @param parent + * the composite parent + * @param selector + * the selector used by the created editor + * @param message + * the message to display in the created editor + * @param withCreateButton + * indicates if the editor provides an additional button for a specific action + * @param listener + * the listener for the button + * @param initialSelection + * the initial selection for this editor + * @return the created {@link SelectionEditor} + */ + protected SelectionEditorWrapper createEditor(Composite parent, IElementSelector selector, String message, boolean withCreateButton, SelectionListener listener, List<Object> initialSelection) { + SelectionEditor editor = new SelectionEditor(parent, selector, selectorLabelProvider, initialSelection); + editor.setMessage(message); + editor.setAdditionalButton(withCreateButton, Messages.InterfaceManagerDialog_CreateNewInterface, listener); + return new SelectionEditorWrapper(editor, editor.getClass()); + } + + /** + * Returns all the available Interfaces. + * + * @param pack + * the pack + * @return all the available Interfaces + * //TODO add the interfaces of the package import! + */ + protected List<Interface> getAllAvailableInterfaces(Package pack) { + + // Set ensure there is not duplicated interface + Set<Interface> otherInterfaces = new HashSet<Interface>(); + + List<Element> interfaces = Visitor.getOwnedAndImportedElement(pack, Interface.class); + for(Namespace namespace : Visitor.getOwnedAndImportedNamespaces(pack)) { + interfaces.addAll(Visitor.getOwnedAndImportedElement(namespace, Interface.class)); + + } + + for(Element element : interfaces) { + otherInterfaces.add((Interface)element); + } + + return Arrays.asList(otherInterfaces.toArray(new Interface[otherInterfaces.size()])); + } + + + /** + * Gets the command. + * + * @return the command + * @see org.eclipse.papyrus.uml.diagram.common.dialogs.IPortInterfaceManagerDialog#getCommand() + */ + public Command getCommand() { + CompoundCommand command = new CompoundCommand("Command to manage interfaces on Port"); //$NON-NLS-1$ + if(!interfaceCreationCommand.isEmpty()) { + command.add(interfaceCreationCommand); + } + Command cmd = getCommandsToCreateElements(); + if(cmd.canExecute()) { + command.add(cmd); + } + cmd = getCommandToRemoveReferencedInterfaces(); + if(cmd.canExecute()) { + command.add(cmd); + } + return command; + } + + /** + * Returns the command to remove referenced Interfaces in Usage and InterfaceRealization. + * + * @return The command to remove referenced Interfaces the Usage and InterfaceRealization. + * Moreover, we add a command to destroy Usage and InterfaceRealization when they have any Suppliers after this action + */ + protected Command getCommandToRemoveReferencedInterfaces() { + CompoundCommand command = new CompoundCommand("Command to destroy Usage and InterfaceRealization"); //$NON-NLS-1$ + + Set<Dependency> relationshipToDestroy = new HashSet<Dependency>(); + // commands to destroy Usage + for(Object current : this.requiredEditor.getElementToRemove()) { + // we get all usage associated with this interface + List<Usage> usages = requiredInterfacesMappedWithUsage.get(current); + for(Usage currentUsage : usages) { + // we remove the reference to the Interface in the Usage + DestroyReferenceRequest request = new DestroyReferenceRequest(domain, currentUsage, UMLPackage.eINSTANCE.getDependency_Supplier(), (EObject)current, false); + Command cmd = getCommand(request); + if(cmd.canExecute()) { + command.add(cmd); + relationshipToDestroy.add(currentUsage); + } + } + } + + // commands to destroy InterfaceRealization + for(Object current : this.providedEditor.getElementToRemove()) { + // we get all usage associated with this interface + List<Realization> realizations = providedInterfacesMappedWithRealization.get(current); + for(Realization currentRealization : realizations) { + // we remove the reference to the Interface in the Usage + DestroyReferenceRequest request = new DestroyReferenceRequest(domain, currentRealization, UMLPackage.eINSTANCE.getDependency_Supplier(), (EObject)current, false); + Command cmd = getCommand(request); + if(cmd.canExecute()) { + command.add(cmd); + relationshipToDestroy.add(currentRealization); + } + } + } + if(!relationshipToDestroy.isEmpty()) { + command.add(new ICommandProxy(new DestroyDependencyWithoutSupplierCommand(this.domain, relationshipToDestroy, getCommandProvider()))); + return command; + } + + // nothing to do + return UnexecutableCommand.INSTANCE; + } + + /** + * Returns the command to create new Usage and new InterfaceRealization. + * + * @return the command to create new Usage and new InterfaceRealization + */ + protected Command getCommandsToCreateElements() { + CompoundCommand command = new CompoundCommand("Command to create Usage and InterfaceRealization"); //$NON-NLS-1$ + // commands for Usage Creation + for(Object current : this.requiredEditor.getElementToAdd()) { + if(current instanceof EObject) { + CreateRelationshipRequest request = new CreateRelationshipRequest(this.port.getType().eContainer(), this.port.getType(), (EObject)current, ElementTypeRegistry.getInstance().getType("org.eclipse.papyrus.uml.Usage")); + Command cmd = getCommand(request); + if(cmd.canExecute()) { + command.add(cmd); + } + } else {// its an NewElementRepresentation + + CreateUsageCommand cmd = new CreateUsageCommand(this.domain, this.port.getType().eContainer(), this.port.getType(), (NewElementRepresentation)current, null); + if(cmd.canExecute()) { + command.add(new ICommandProxy(cmd)); + } + } + } + + // commands for InterfaceRealization Creation + for(Object current : this.providedEditor.getElementToAdd()) { + if(current instanceof EObject) { + CreateRelationshipRequest request = new CreateRelationshipRequest(this.port.getType(), this.port.getType(), (EObject)current, ElementTypeRegistry.getInstance().getType("org.eclipse.papyrus.uml.InterfaceRealization")); + Command cmd = getCommand(request); + if(cmd.canExecute()) { + command.add(cmd); + } + } else {// its an NewElementRepresentation + CreateInterfaceRealizationCommand cmd = new CreateInterfaceRealizationCommand(this.domain, this.port.getType(), this.port.getType(), (NewElementRepresentation)current, null); + if(cmd.canExecute()) { + command.add(new ICommandProxy(cmd)); + } + } + } + if(!command.isEmpty()) { + return command; + } + return UnexecutableCommand.INSTANCE; + } + + /** + * Return the command corresponding to the request. + * + * @param req + * a request + * @return the command corresponding to the request + */ + protected Command getCommand(AbstractEditCommandRequest req) { + IElementEditService provider = getCommandProvider(); + if(provider != null) { + ICommand setCommand = provider.getEditCommand(req); + if(setCommand != null) { + return new ICommandProxy(setCommand.reduce()); + } + } + return UnexecutableCommand.INSTANCE; + } + + /** + * Gets the command provider. + * + * @return the command provider + */ + private IElementEditService getCommandProvider() { + IElementEditService provider = ElementEditServiceUtils.getCommandProvider(model); + return provider; + } + + + + /** + * Gets the selector label provider. + * + * @return the selector label provider + */ + public ILabelProvider getSelectorLabelProvider() { + return selectorLabelProvider; + } + + + /** + * Sets the selector label provider. + * + * @param selectorLabelProvider + * the new selector label provider + */ + public void setSelectorLabelProvider(ILabelProvider selectorLabelProvider) { + this.selectorLabelProvider = selectorLabelProvider; + } + + + + /** + * Gets the provided editor. + * + * @return the provided editor + */ + public SelectionEditorWrapper getProvidedEditor() { + return providedEditor; + } + + + /** + * Gets the required editor. + * + * @return the required editor + */ + public SelectionEditorWrapper getRequiredEditor() { + return requiredEditor; + } + + + /** + * This listener is used to know when the user clicks on the Button to create a new {@link Interface}. + * + * @see CreateButtonEvent + */ + public class CreateButtonListener implements SelectionListener { + + /** + * Widget selected. + * + * @param e + * the e + * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent) + */ + @Override + public void widgetSelected(SelectionEvent e) { + + CreateUmlElementDialog dialog = new CreateUmlElementDialog(getShell(), domain, newElementLabelProvider, new CustomContentProvider(), model, ElementTypeRegistry.getInstance().getType("org.eclipse.papyrus.uml.Interface")); + dialog.setTitle(Messages.InterfaceManagerDialog_CreateANewInterface); + dialog.setMessage(Messages.InterfaceManagerDialog_SelectTheParentOfThisNewInterface); + dialog.setHelpAvailable(false); + dialog.setForbiddenNames(forbiddenNames); + List<Object> input = new ArrayList<Object>(); + input.add(model); + dialog.setInput(input); + dialog.setValidator(new ParentInterfaceValidator()); + if(dialog.open() == org.eclipse.jface.dialogs.Dialog.OK) { + interfaceCreationCommand.add(dialog.getCommand()); + Object[] res = dialog.getResult(); + for(Object current : res) { + if(current instanceof NewElementRepresentation) { + interfaceRepresentations.add((NewElementRepresentation)current); + forbiddenNames.add(((NewElementRepresentation)current).getName()); + } + } + providedSelector.refresh(); + requiredSelector.refresh(); + } + } + + /** + * Widget default selected. + * + * @param e + * the e + * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent) + */ + @Override + public void widgetDefaultSelected(SelectionEvent e) { + + } + + } + + /** + * This class provides a validator for the selection of the parent of the new Interface. + */ + public class ParentInterfaceValidator implements ISelectionStatusValidator { + + /** + * Selection has to be a Classifier or an Interface to allow creation. + * + * @param selection + * the selection + * @return the i status + * @see org.eclipse.ui.dialogs.ISelectionStatusValidator#validate(java.lang.Object[]) + */ + @Override + public IStatus validate(Object[] selection) { + + IStatus status = new Status(IStatus.ERROR, Activator.ID, Messages.NoSelectionFound); + if(selection.length >= 1) { + for(int i = 0; i < selection.length; i++) { + if(selection[i] instanceof Package || selection[i] instanceof Classifier || selection[i] instanceof NewElementRepresentation) { + status = new Status(IStatus.OK, Activator.ID, Messages.SelectionValidated); + } else { + status = new Status(IStatus.ERROR, Activator.ID, Messages.InterfaceManagerDialog_SelectionHasToBeAPackageOrAClassifier); + break; + } + } + } + return status; + } + } + + /** + * + * This class provides a CustomLabelProvider for this dialog. + * This class manages the {@link NewElementRepresentation} and consider them like Interfaces + * + * + */ + public class SelectorLabelProvider extends CreateElementLabelProvider { + + /** + * Gets the text. + * + * @param element + * the element + * @return the text + * @see org.eclipse.papyrus.uml.diagram.common.providers.EditorLabelProvider#getText(java.lang.Object) + */ + @Override + public String getText(Object element) { + if(element instanceof NewElementRepresentation) { + return ((NewElementRepresentation)element).getQualifiedName(); + } else if(element instanceof NamedElement) { + return ((NamedElement)element).getQualifiedName(); + } + return super.getText(element); + } + + } + + /** + * The Class CreateElementLabelProvider. + */ + public class CreateElementLabelProvider extends UMLLabelProvider { + + /** + * Gets the image. + * + * @param element + * the element + * @return the image + * @see org.eclipse.papyrus.uml.diagram.common.providers.EditorLabelProvider#getImage(java.lang.Object) + */ + @Override + public Image getImage(Object element) { + if(element instanceof NewElementRepresentation) { + + ImageItemProvider imageProvider = new ImageItemProvider(new UMLItemProviderAdapterFactory()); + Object obj = imageProvider.getImage(((NewElementRepresentation)element).getEObject()); + return org.eclipse.papyrus.uml.tools.Activator.getDefault().getImageForUMLMetaclass(UMLPackage.eINSTANCE.getInterface()); + } + return super.getImage(element); + } + + /** + * Gets the text. + * + * @param element + * the element + * @return the text + * @see org.eclipse.papyrus.uml.diagram.common.providers.EditorLabelProvider#getText(java.lang.Object) + */ + @Override + public String getText(Object element) { + if(element instanceof NewElementRepresentation) { + return ((NewElementRepresentation)element).getName(); + } + return super.getText(element); + } + + } + + /** + * + * This content provider is used in this dialog. It returns the available element in the model + the future element which are + * {@link NewElementRepresentation} + * + * + */ + public class CustomContentProvider implements ITreeContentProvider { + + /** + * Dispose. + * + * @see org.eclipse.jface.viewers.IContentProvider#dispose() + */ + @Override + public void dispose() { + } + + /** + * Input changed. + * + * @param viewer + * the viewer + * @param oldInput + * the old input + * @param newInput + * the new input + * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object) + */ + @Override + public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { + + } + + /** + * Gets the elements. + * + * @param inputElement + * the input element + * @return the elements + * @see org.eclipse.jface.viewers.ITreeContentProvider#getElements(java.lang.Object) + */ + @Override + public Object[] getElements(Object inputElement) { + if(inputElement instanceof List<?>) { + return ((List<?>)inputElement).toArray(); + } + return new Object[0]; + } + + /** + * Gets the children. + * + * @param parentElement + * the parent element + * @return the children + * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object) + */ + @Override + public Object[] getChildren(Object parentElement) { + List<Object> children = new ArrayList<Object>(); + List<?> tmp = null; + if(parentElement instanceof Package) { + tmp = ((Package)parentElement).getOwnedMembers(); + } else if(parentElement instanceof Classifier) { + tmp = ((Classifier)parentElement).getOwnedMembers(); + } + if(tmp != null) { + for(Object current : tmp) { + if(current instanceof Classifier || current instanceof Package) { + children.add(current); + } + } + } + // we add the future Interfaces to this selection + for(NewElementRepresentation current : interfaceRepresentations) { + if(current.getParent() == parentElement) { + children.add(current); + } + } + return children.toArray(); + } + + /** + * Gets the parent. + * + * @param element + * the element + * @return the parent + * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object) + */ + @Override + public Object getParent(Object element) { + if(element == model) { + return model; + } else if(element instanceof NamedElement) { + return ((NamedElement)element).eContainer(); + } + return null; + } + + /** + * Checks for children. + * + * @param element + * the element + * @return true, if successful + * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object) + */ + @Override + public boolean hasChildren(Object element) { + return getChildren(element).length != 0; + } + + } + + /** + * This command is used to create a New Usage, when its target (an Interface) doesn't exist when the command is created + * (but this Interface will exist when the command will be executed). + */ + public class CreateUsageCommand extends AbstractTransactionalCommand { + + /** The container for the new Usage. */ + private EObject container; + + /** the source of this Usage. */ + private EObject source; + + /** the target : the representation of the new interface. */ + private NewElementRepresentation target; + + /** + * + * Constructor. + * + * @param domain + * the domain + * @param container + * the container for the Usage + * @param source + * the source for the Usage + * @param target + * the target for the Usage + * @param affectedFiles + * the affected files + */ + public CreateUsageCommand(TransactionalEditingDomain domain, EObject container, EObject source, NewElementRepresentation target, List<?> affectedFiles) { + super(domain, "Create Usage Command", affectedFiles); //$NON-NLS-1$ + this.container = container; + this.source = source; + this.target = target; + } + + /** + * Do execute with result. + * + * @param monitor + * the monitor + * @param info + * the info + * @return the command result + * @throws ExecutionException + * the execution exception + * @see org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand#doExecuteWithResult(org.eclipse.core.runtime.IProgressMonitor, + * org.eclipse.core.runtime.IAdaptable) + */ + @Override + protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException { + CreateRelationshipRequest request = new CreateRelationshipRequest(domain, container, source, target.getEObject(), ElementTypeRegistry.getInstance().getType("org.eclipse.papyrus.uml.Usage")); + Command command = getCommand(request); + if(command.canExecute()) { + command.execute(); + } + return CommandResult.newOKCommandResult(request.getNewElement()); + } + + } + + /** + * This command is used to create a New InterfaceRealition, when its target (an Interface) doesn't exist when the command is created + * (but this Interface will exist when the command will be executed). + */ + public class CreateInterfaceRealizationCommand extends AbstractTransactionalCommand { + + /** The container for the new InterfaceRealization. */ + private EObject container; + + /** The source for the new InterfaceRealization. */ + private EObject source; + + /** the target : the representation of the new interface. */ + private NewElementRepresentation target; + + /** + * + * Constructor. + * + * @param domain + * the domain + * @param container + * the container for the InterfaceRealization + * @param source + * the source for the InterfaceRealization + * @param target + * the target for the InterfaceRealization + * @param affectedFiles + * the affected files + */ + public CreateInterfaceRealizationCommand(TransactionalEditingDomain domain, EObject container, EObject source, NewElementRepresentation target, List<?> affectedFiles) { + super(domain, "Create InterfaceRealization Command", affectedFiles); //$NON-NLS-1$ + this.container = container; + this.source = source; + this.target = target; + } + + /** + * Do execute with result. + * + * @param monitor + * the monitor + * @param info + * the info + * @return the command result + * @throws ExecutionException + * the execution exception + * @see org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand#doExecuteWithResult(org.eclipse.core.runtime.IProgressMonitor, + * org.eclipse.core.runtime.IAdaptable) + */ + @Override + protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException { + + CreateRelationshipRequest request = new CreateRelationshipRequest(domain, container, source, target.getEObject(), ElementTypeRegistry.getInstance().getType("org.eclipse.papyrus.uml.InterfaceRealization")); + Command command = getCommand(request); + if(command.canExecute()) { + command.execute(); + } + return CommandResult.newOKCommandResult(request.getNewElement()); + } + + } +} diff --git a/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/src/org/eclipse/papyrus/uml/diagram/common/dialogs/NewElementRepresentation.java b/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/src/org/eclipse/papyrus/uml/diagram/common/dialogs/NewElementRepresentation.java new file mode 100644 index 00000000000..43717966d4d --- /dev/null +++ b/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/src/org/eclipse/papyrus/uml/diagram/common/dialogs/NewElementRepresentation.java @@ -0,0 +1,173 @@ +/***************************************************************************** + * Copyright (c) 2009-2011 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: + * Yann Tanguy (CEA LIST) yann.tanguy@cea.fr - Initial API and implementation + * + *****************************************************************************/ +package org.eclipse.papyrus.uml.diagram.common.dialogs; + +import org.eclipse.emf.ecore.EObject; +import org.eclipse.gmf.runtime.emf.type.core.IElementType; +import org.eclipse.gmf.runtime.emf.type.core.IHintedType; +import org.eclipse.gmf.runtime.emf.type.core.requests.CreateElementRequest; +import org.eclipse.uml2.uml.NamedElement; + +/** + * This class provides facilities to represent a New Element before its real creation. + * We can store in this class : + * <ul> + * <li>the name of the new element</li> + * <li>the parent of the new element</li> + * <li>the {@link IHintedType} of the new element</li> + * <li>the request to build the new element (optional)</li> + * <li>the element itself when it has been created</li> + * </ul> + * + * + * + */ +public class NewElementRepresentation { + + /** the name of the new element */ + private String name; + + /** the parent of the new element */ + private Object parent; + + /** the created element itself */ + private EObject createdElement; + + /** the {@link IHintedType} of the new element */ + private IElementType hintedType; + + /** this field is used only when the element have an existing parent */ + private CreateElementRequest request; + + /** + * + * Constructor. + * + * @param name + * the name of the new element + * @param parent + * the parent of the new element + * @param hintedType + * the hinted type of the new element + * @param request + * the request to build the new element (can be <code>null</code>) + */ + public NewElementRepresentation(String name, Object parent, IElementType hintedType, CreateElementRequest request) { + this.name = name; + this.parent = parent; + this.createdElement = null; + this.hintedType = hintedType; + this.request = request; + } + + /** + * Getter for {@link #hintedType} + * + * @return + * {@link #hintedType} + */ + public IElementType getHintedType() { + return this.hintedType; + } + + /** + * Getter for {@link #name} + * + * @return + * {@link #name} + */ + public String getName() { + return this.name; + } + + /** + * Getter for {@link #parent} + * + * @return + * {@link #parent} + */ + public Object getParent() { + return this.parent; + } + + /** + * Returns the parent of this element + * + * @return + * the parent of this element or <code>null</code> if the parent is a {@link NewElementRepresentation} itself and if it has not been + * created + */ + public EObject getEObjectParent() { + if(parent instanceof EObject) { + return (EObject)this.parent; + } else if(parent instanceof NewElementRepresentation) { + return ((NewElementRepresentation)parent).getEObject(); + } + return null; + } + + /** + * Returns the created object + * + * @return + * the created Object or <code>null</code> if the object has not been created + */ + public EObject getEObject() { + if(this.request != null) { + return this.request.getNewElement(); + } + return this.createdElement; + } + + /** + * Setter for {@link #createdElement} + * + * @param newElement + * + */ + public void setCreateElement(EObject newElement) { + this.createdElement = newElement; + } + + /** + * Setter for {@link #request} + * + * @return + * {@link #request} + */ + public CreateElementRequest getCreateElementRequest() { + return this.request; + } + + /** + * + * @see java.lang.Object#toString() + * + * @return + */ + @Override + public String toString() { + return "Name : " + this.name + ", parent : " + this.parent; + } + + public String getQualifiedName() { + String qualifiedName = ""; + if(parent instanceof NewElementRepresentation) { + qualifiedName = ((NewElementRepresentation)parent).getQualifiedName(); + } else if(parent instanceof NamedElement) { + qualifiedName = ((NamedElement)parent).getQualifiedName() + "::" + this.name; + } + return qualifiedName; + } +} diff --git a/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/src/org/eclipse/papyrus/uml/diagram/common/dialogs/SelectionEditorWrapper.java b/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/src/org/eclipse/papyrus/uml/diagram/common/dialogs/SelectionEditorWrapper.java new file mode 100644 index 00000000000..8b04a1aadd2 --- /dev/null +++ b/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/src/org/eclipse/papyrus/uml/diagram/common/dialogs/SelectionEditorWrapper.java @@ -0,0 +1,180 @@ +/***************************************************************************** + * Copyright (c) 2014 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: + * Gabriel Pascual (ALL4TEC) gabriel.pascual@all4tec.net - Initial API and implementation + *****************************************************************************/ +package org.eclipse.papyrus.uml.diagram.common.dialogs; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.papyrus.infra.widgets.editors.SelectionEditor; +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.events.SelectionListener; + + +/** + * Wrapper for both selection editor. + * <ul> + * <li>SelectionEditor</li> + * <li>ExtendedSelectionEditor</li> + * </ul> + * + * @author Gabriel Pascual (ALL4TEC) + * + */ +public class SelectionEditorWrapper implements SelectionListener { + + /** The wrapped selection editor. */ + private SelectionListener wrappedSelectionEditor = null; + + /** The wrapped editor class. */ + private Class<?> wrappedEditorClass = null; + + /** + * Instantiates a new selection editor wrapper. + * + * @param selectionEditor + * the selection editor + * @param editorClass + * the editor class + */ + public SelectionEditorWrapper(SelectionListener selectionEditor, Class<?> editorClass) { + wrappedSelectionEditor = selectionEditor; + wrappedEditorClass = editorClass; + } + + /** + * Creates the. + */ + public void create() { + if(SelectionEditor.class.equals(wrappedEditorClass)) { + ((SelectionEditor)wrappedSelectionEditor).create(); + } else if(ExtendedSelectionEditor.class.equals(wrappedEditorClass)) { + ((ExtendedSelectionEditor)wrappedSelectionEditor).create(); + } + + } + + /** + * Sets the layout data. + * + * @param layoutData + * the new layout data + */ + public void setLayoutData(Object layoutData) { + if(SelectionEditor.class.equals(wrappedEditorClass)) { + ((SelectionEditor)wrappedSelectionEditor).setLayoutData(layoutData); + } else if(ExtendedSelectionEditor.class.equals(wrappedEditorClass)) { + ((ExtendedSelectionEditor)wrappedSelectionEditor).setLayoutData(layoutData); + } + + } + + /** + * Gets the selected elements. + * + * @return the selected elements + */ + public Collection<?> getSelectedElements() { + + Collection<?> selectedElements = null; + if(SelectionEditor.class.equals(wrappedEditorClass)) { + selectedElements = ((SelectionEditor)wrappedSelectionEditor).getSelectedElements(); + } else if(ExtendedSelectionEditor.class.equals(wrappedEditorClass)) { + selectedElements = ((ExtendedSelectionEditor)wrappedSelectionEditor).getSelectedElements(); + } + + return selectedElements; + } + + /** + * Gets the element to remove. + * + * @return the element to remove + */ + public List<Object> getElementToRemove() { + List<Object> elementsToRemove = null; + if(SelectionEditor.class.equals(wrappedEditorClass)) { + elementsToRemove = ((SelectionEditor)wrappedSelectionEditor).getElementToRemove(); + } else if(ExtendedSelectionEditor.class.equals(wrappedEditorClass)) { + elementsToRemove = ((ExtendedSelectionEditor)wrappedSelectionEditor).getElementToRemove(); + } else { + elementsToRemove = new ArrayList<Object>(); + } + + return elementsToRemove; + } + + /** + * Gets the element to add. + * + * @return the element to add + */ + public List<Object> getElementToAdd() { + List<Object> elementsToAdd = null; + if(SelectionEditor.class.equals(wrappedEditorClass)) { + elementsToAdd = ((SelectionEditor)wrappedSelectionEditor).getElementToAdd(); + } else if(ExtendedSelectionEditor.class.equals(wrappedEditorClass)) { + elementsToAdd = ((ExtendedSelectionEditor)wrappedSelectionEditor).getElementToAdd(); + } else { + elementsToAdd = new ArrayList<Object>(); + } + + return elementsToAdd; + } + + /** + * Gets the selection. + * + * @return the selection + */ + public ISelection getSelection() { + if(ExtendedSelectionEditor.class.equals(wrappedEditorClass)) { + return ((ExtendedSelectionEditor)wrappedSelectionEditor).getSelectedElementsViewer().getSelection(); + } + return null; + } + + /** + * Refresh. + */ + public void refresh() { + if(ExtendedSelectionEditor.class.equals(wrappedEditorClass)) { + ((ExtendedSelectionEditor)wrappedSelectionEditor).getSelectedElementsViewer().refresh(); + } + + } + + /** + * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent) + * + * @param e + */ + @Override + public void widgetSelected(SelectionEvent e) { + wrappedSelectionEditor.widgetSelected(e); + + } + + /** + * Widget default selected. + * + * @param e + * the e + * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent) + */ + @Override + public void widgetDefaultSelected(SelectionEvent e) { + wrappedSelectionEditor.widgetDefaultSelected(e); + + } +} |