diff options
2 files changed, 1165 insertions, 1070 deletions
diff --git a/plugins/uml/properties/org.eclipse.papyrus.uml.properties/src/org/eclipse/papyrus/uml/properties/profile/ui/compositesformodel/AppliedStereotypeCompositeOnModel.java b/plugins/uml/properties/org.eclipse.papyrus.uml.properties/src/org/eclipse/papyrus/uml/properties/profile/ui/compositesformodel/AppliedStereotypeCompositeOnModel.java index 812b00961d4..22cdbdd617b 100644 --- a/plugins/uml/properties/org.eclipse.papyrus.uml.properties/src/org/eclipse/papyrus/uml/properties/profile/ui/compositesformodel/AppliedStereotypeCompositeOnModel.java +++ b/plugins/uml/properties/org.eclipse.papyrus.uml.properties/src/org/eclipse/papyrus/uml/properties/profile/ui/compositesformodel/AppliedStereotypeCompositeOnModel.java @@ -1,540 +1,625 @@ -/*****************************************************************************
- * Copyright (c) 2008 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:
- * Chokri Mraidha (CEA LIST) Chokri.Mraidha@cea.fr - Initial API and implementation
- * Patrick Tessier (CEA LIST) Patrick.Tessier@cea.fr - modification
- *
- *****************************************************************************/
-package org.eclipse.papyrus.uml.properties.profile.ui.compositesformodel;
-
-import java.util.ArrayList;
-import java.util.Iterator;
-
-import org.eclipse.emf.common.notify.Notification;
-import org.eclipse.emf.common.notify.impl.NotificationImpl;
-import org.eclipse.emf.common.util.BasicEList;
-import org.eclipse.emf.common.util.EList;
-import org.eclipse.emf.transaction.RecordingCommand;
-import org.eclipse.emf.transaction.TransactionalEditingDomain;
-import org.eclipse.jface.viewers.ISelectionChangedListener;
-import org.eclipse.jface.viewers.IStructuredSelection;
-import org.eclipse.jface.viewers.SelectionChangedEvent;
-import org.eclipse.papyrus.infra.core.services.ServiceException;
-import org.eclipse.papyrus.infra.emf.utils.ServiceUtilsForEObject;
-import org.eclipse.papyrus.uml.profile.Activator;
-import org.eclipse.papyrus.uml.profile.preference.ProfilePreferenceConstants;
-import org.eclipse.papyrus.uml.profile.tree.ProfileElementContentProvider;
-import org.eclipse.papyrus.uml.profile.tree.ProfileElementLabelProvider;
-import org.eclipse.papyrus.uml.profile.tree.ProfileElementTreeViewerFilter;
-import org.eclipse.papyrus.uml.profile.tree.objects.AppliedStereotypePropertyTreeObject;
-import org.eclipse.papyrus.uml.profile.tree.objects.AppliedStereotypeTreeObject;
-import org.eclipse.papyrus.uml.profile.tree.objects.StereotypedElementTreeObject;
-import org.eclipse.papyrus.uml.profile.utils.Util;
-import org.eclipse.papyrus.uml.properties.profile.ui.dialogs.ChooseSetStereotypeDialog;
-import org.eclipse.papyrus.uml.properties.profile.ui.panels.AppliedStereotypePanel;
-import org.eclipse.swt.SWT;
-import org.eclipse.swt.custom.CLabel;
-import org.eclipse.swt.widgets.Composite;
-import org.eclipse.swt.widgets.Display;
-import org.eclipse.swt.widgets.Tree;
-import org.eclipse.swt.widgets.TreeItem;
-import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetWidgetFactory;
-import org.eclipse.uml2.uml.Element;
-import org.eclipse.uml2.uml.Stereotype;
-
-// TODO: Auto-generated Javadoc
-/**
- * This composite is used to display applied stereotype in the model. It allows applying or desapply a stereotype
- */
-public class AppliedStereotypeCompositeOnModel extends DecoratedTreeComposite implements ISelectionChangedListener {
-
- /**
- * Gets the domain.
- *
- * @return the domain
- */
- public TransactionalEditingDomain getEditingDomain(Element context) {
- try {
- return ServiceUtilsForEObject.getInstance().getTransactionalEditingDomain(context);
- } catch (ServiceException ex) {
- Activator.log.error(ex);
- return null;
- }
- }
-
- /** The panel that display applied stereotypes. */
- private AppliedStereotypePanel appliedStereotypePanel;
-
- /** The label. */
- protected CLabel label;
-
- /**
- * The default constructor.
- *
- * @param parent
- * the parent Composite for this panel
- */
- public AppliedStereotypeCompositeOnModel(AppliedStereotypePanel parent) {
- super(parent, SWT.NONE, "Applied stereotypes", true);
-
- appliedStereotypePanel = parent;
- }
-
- /**
- * create a composite applied stereotype on model.
- *
- * @param parent
- * the parent composite
- */
- public AppliedStereotypeCompositeOnModel(Composite parent) {
- super(parent, SWT.NONE, "Applied stereotypes", true);
- }
-
- /**
- * apply a stereotype on current selected element.
- */
- protected void addAppliedStereotype() {
-
- // Open stereotype selection (may add or remove)
- ChooseSetStereotypeDialog dialog = new ChooseSetStereotypeDialog(this.getShell(), getElement());
- int result = dialog.open();
-
- if(result == ChooseSetStereotypeDialog.OK) {
- // Retrieve selected element
- Element element = getElement();
-
- // compare the 2 lists (present list and future list
- EList<Stereotype> oldStereotypeList = element.getAppliedStereotypes();
- ArrayList<Stereotype> newStereotypeList = dialog.getSelectedElements();
-
- // Keep newStereotype order (will be used at the end of the method)
- EList<Stereotype> newOrderList = new BasicEList<Stereotype>();
- newOrderList.addAll(newStereotypeList);
-
- // If the 2 lists differ, apply the new list of stereotypes
- if(!(newStereotypeList.equals(oldStereotypeList))) {
-
- // Parse old list :
- // if stereotype is in the new list : it is already applied
- // --> don't unapply it
- // --> remove it from new list
- Iterator<Stereotype> it = oldStereotypeList.iterator();
- while(it.hasNext()) {
- Stereotype currentStOld = it.next();
- if(newStereotypeList.contains(currentStOld)) {
- newStereotypeList.remove(currentStOld);
- } else {
- unapplyStereotype(element, currentStOld);
- }
- }
-
- // Already applied stereotype should have been removed
- // apply others
- Iterator<Stereotype> newApplyStereotypes = newStereotypeList.iterator();
- while(newApplyStereotypes.hasNext()) {
- Stereotype currentStereotype = newApplyStereotypes.next();
- applyStereotype(element, currentStereotype);
- }
-
- // Update Stereotype order
- // this.reorderStereotypeApplications(element, newOrderList);
-
- // checkSelection(null);
- selectionChanged(null);
-
- if(appliedStereotypePanel != null) {
- appliedStereotypePanel.refresh();
- }
- }
- }
-
- }
-
- /**
- * Button action : open a selection dialog box that allow the user to choose stereotypes to apply (or unapply).
- */
- @Override
- public void addButtonPressed() {
- addAppliedStereotype();
- }
-
- /**
- *
- * {@inheritDoc}
- */
- @Override
- public Composite createContent(Composite parent, TabbedPropertySheetWidgetFactory factory) {
- super.createContent(parent, factory);
- createStereotypesTree();
-
- removeButton.setToolTipText("Remove stereotype");
- addButton.setToolTipText("Apply stereotype");
-
- return this;
- }
-
- /**
- * Creates the stereotypes tree.
- *
- * @return the tree of applied stereotypes and properties
- */
- private void createStereotypesTree() {
- // Tree viewer shows applied stereotypes
- treeViewer.setContentProvider(new ProfileElementContentProvider());
- treeViewer.setLabelProvider(new ProfileElementLabelProvider());
- treeViewer.addFilter(new ProfileElementTreeViewerFilter());
- treeViewer.addSelectionChangedListener(this);
- }
-
- /**
- * Button action : modify display order of stereotypes (selected elements are pushed down in the list).
- */
- @Override
- public void downButtonPressed() {
- int nbrOfSelection = getTree().getSelectionCount();
- if(nbrOfSelection < 1) {
- return;
- }
-
- TreeItem[] items = getTree().getSelection();
- int indexLast = getTree().indexOf(items[items.length - 1]);
- if(indexLast + 1 >= getElement().getAppliedStereotypes().size()) {
- // do nothing
- return;
- }
-
- for(int i = 0; i < nbrOfSelection; i++) {
- TreeItem item = items[nbrOfSelection - 1 - i];
- if(item.getData() instanceof AppliedStereotypeTreeObject) {
- AppliedStereotypeTreeObject sTO = (AppliedStereotypeTreeObject)item.getData();
- EList stereotypes = new BasicEList();
- stereotypes.addAll(element.getAppliedStereotypes());
-
- int index = stereotypes.indexOf(sTO.getStereotype());
- if((index == -1) || (index >= stereotypes.size() - 1)) {
- // Not found of already on top...
- return;
- }
-
- stereotypes.move(index + 1, sTO.getStereotype());
- this.reorderStereotypeApplications(element, stereotypes);
- }
- }
- }
-
- /**
- * Edits the item.
- *
- * @param item
- * the item
- */
- @Override
- public void editItem(TreeItem item) {
- // do nothing
- }
-
- /**
- * Gets the selected.
- *
- * @return Returns the selected element.
- */
- public Element getSelected() {
- return appliedStereotypePanel.getSelected();
- }
-
- /**
- * Gets the tree.
- *
- * @return the tree
- */
- public Tree getTree() {
- return treeViewer.getTree();
- }
-
- /**
- * Checks if is in stereotype display.
- *
- * @param st
- * the stereotype
- *
- * @return true, if checks if is in stereotype display
- */
- protected Boolean isInStereotypeDisplay(Stereotype st) {
- return false;
- }
-
- /**
- * Redraw the treeViewer while preserving selections and non-collapsed tree elements
- * It is not sufficient to redraw only selected elements as an optimization, since
- * derived stereotype attributes (that are not selected) might change in response to
- * changing other attributes.
- *
- * @param propertyView
- */
- public void refreshTreeViewer() {
- treeViewer.refresh();
- }
-
- /**
- * Refresh the content of applied the applied stereotype tree.
- */
- @Override
- public void refresh() {
- if(treeViewer.getTree() != null && !(treeViewer.getTree().isDisposed())) {
- treeViewer.setInput(null);
- treeViewer.refresh();
- if(element != null) {
- treeViewer.setInput(new StereotypedElementTreeObject(element));
- }
- StereotypedElementTreeObject rTO = (StereotypedElementTreeObject)treeViewer.getInput();
- if(rTO == null) {
- return;
- }
-
- // If the property is Multivalued show Up - Down
- if((rTO.getChildren() != null) && (rTO.getChildren().length > 1)) {
- upButton.setEnabled(true);
- downButton.setEnabled(true);
- } else {
- upButton.setEnabled(false);
- downButton.setEnabled(false);
- }
-
- if((rTO.getChildren() != null) && (rTO.getChildren().length == 0)) {
- removeButton.setEnabled(false);
- } else {
- removeButton.setEnabled(true);
- }
- }
- }
-
- /**
- * Button action : unapply the stereotypes selected by the user in the stereotype tree.
- */
- @Override
- public void removeButtonPressed() {
- unapplyStereotype();
- }
-
- /**
- * Selection changed.
- *
- * @param event
- * the event
- */
- public void selectionChanged(SelectionChangedEvent event) {
- if(appliedStereotypePanel != null) {
- if(event == null) {
- appliedStereotypePanel.setSelectedProperty(null);
- return;
- }
-
- IStructuredSelection structSelection = (IStructuredSelection)event.getSelection();
- Object selection = structSelection.getFirstElement();
- if(selection instanceof AppliedStereotypePropertyTreeObject) {
- appliedStereotypePanel.setSelectedProperty((AppliedStereotypePropertyTreeObject)selection);
- } else {
- appliedStereotypePanel.setSelectedProperty(null);
- }
-
- }
- }
-
- /**
- * Sets the input.
- *
- * @param element
- * the element
- */
- public void setInput(StereotypedElementTreeObject element) {
- treeViewer.setInput(element);
- if(Activator.getDefault().getPreferenceStore().getBoolean(ProfilePreferenceConstants.EXPAND_STEREOTYPES_TREE)) {
- treeViewer.expandAll();
- }
- }
-
- /**
- * unapply stereotype on current selected element.
- */
- protected void unapplyStereotype() {
- int nbrOfSelection = getTree().getSelectionCount();
- if(nbrOfSelection == 0) {
- return;
- }
-
- for(int i = 0; i < nbrOfSelection; i++) {
- TreeItem item = getTree().getSelection()[i];
- if(item.getData() instanceof AppliedStereotypeTreeObject) {
- AppliedStereotypeTreeObject sTO = (AppliedStereotypeTreeObject)item.getData();
- unapplyStereotype(element, sTO.getStereotype());
- sTO.removeMe();
- }
- }
- if(appliedStereotypePanel != null) {
- appliedStereotypePanel.refresh();
- } else {
- refresh();
- }
- }
-
- /**
- * Button action : modify display order of stereotypes (selected elements are pushed up in the list).
- */
- @Override
- public void upButtonPressed() {
- int nbrOfSelection = getTree().getSelectionCount();
- if(nbrOfSelection < 1) {
- return;
- }
-
- TreeItem[] items = getTree().getSelection();
- int indexFirst = getTree().indexOf(items[0]);
- if(indexFirst == 0) {
- // do nothing
- return;
- }
-
- for(int i = 0; i < nbrOfSelection; i++) {
- TreeItem item = items[i];
- if(item.getData() instanceof AppliedStereotypeTreeObject) {
- AppliedStereotypeTreeObject sTO = (AppliedStereotypeTreeObject)item.getData();
- EList stereotypes = new BasicEList();
- stereotypes.addAll(element.getAppliedStereotypes());
-
- int index = stereotypes.indexOf(sTO.getStereotype());
- if(index < 1) {
- return;
- }
-
- stereotypes.move(index - 1, sTO.getStereotype());
- this.reorderStereotypeApplications(element, stereotypes);
- }
- }
- if(appliedStereotypePanel != null) {
- appliedStereotypePanel.refresh();
- } else {
- refresh();
- }
- }
-
- /**
- * Apply stereotype.
- *
- * @param elt
- * the elt
- * @param st
- * the st
- */
- public void applyStereotype(final Element elt, final Stereotype st) {
- try {
- final TransactionalEditingDomain domain = getEditingDomain(elt);
- domain.runExclusive(new Runnable() {
-
- public void run() {
-
- Display.getCurrent().asyncExec(new Runnable() {
-
- public void run() {
- domain.getCommandStack().execute(new RecordingCommand(domain) {
-
- @Override
- protected void doExecute() {
- elt.applyStereotype(st);
- refresh();
- }
- });
- }
- });
- }
- });
-
- } catch (Exception e) {
- Activator.log.error(e);
- }
-
- }
-
- /**
- * Unapply stereotype.
- *
- * @param elt
- * the uml element
- * @param st
- * the stereotype to unapply
- */
- protected void unapplyStereotype(final Element elt, final Stereotype st) {
- // bugfix: a selected element is not necessary a diagram element (ex: selection in the outline)
- try {
- final TransactionalEditingDomain domain = getEditingDomain(elt);
- domain.runExclusive(new Runnable() {
-
- public void run() {
-
- Display.getCurrent().asyncExec(new Runnable() {
-
- public void run() {
- domain.getCommandStack().execute(new RecordingCommand(domain) {
-
- @Override
- protected void doExecute() {
- elt.unapplyStereotype(st);
- elt.eNotify(new NotificationImpl(Notification.SET, true, true, true));
- refresh();
- }
- });
- }
- });
- }
- });
-
- } catch (Exception e) {
- Activator.log.error(e);
- }
-
- }
-
- /**
- * change the order of applied stereotype
- *
- * @param element
- * the UML element where stereotypes are applied
- * @param stereotypes
- * the lis of applied stereotypes with the wanted order
- */
- public void reorderStereotypeApplications(final Element element, final EList stereotypes) {
- try {
- final TransactionalEditingDomain domain = getEditingDomain(element);
- domain.runExclusive(new Runnable() {
-
- public void run() {
-
- Display.getCurrent().asyncExec(new Runnable() {
-
- public void run() {
- domain.getCommandStack().execute(new RecordingCommand(domain) {
-
- @Override
- protected void doExecute() {
- Util.reorderStereotypeApplications(element, stereotypes);
- refresh();
- }
- });
- }
- });
- }
- });
-
- } catch (Exception e) {
- Activator.log.error(e);
- }
-
- }
-
-}
+/***************************************************************************** + * Copyright (c) 2008 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: + * Chokri Mraidha (CEA LIST) Chokri.Mraidha@cea.fr - Initial API and implementation + * Patrick Tessier (CEA LIST) Patrick.Tessier@cea.fr - modification + * + *****************************************************************************/ +package org.eclipse.papyrus.uml.properties.profile.ui.compositesformodel; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; + +import org.eclipse.emf.common.notify.Notification; +import org.eclipse.emf.common.notify.impl.NotificationImpl; +import org.eclipse.emf.common.util.BasicEList; +import org.eclipse.emf.common.util.EList; +import org.eclipse.emf.transaction.RecordingCommand; +import org.eclipse.emf.transaction.TransactionalEditingDomain; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.ISelectionChangedListener; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.jface.viewers.SelectionChangedEvent; +import org.eclipse.jface.viewers.StructuredSelection; +import org.eclipse.papyrus.infra.core.services.ServiceException; +import org.eclipse.papyrus.infra.emf.utils.ServiceUtilsForEObject; +import org.eclipse.papyrus.uml.profile.Activator; +import org.eclipse.papyrus.uml.profile.preference.ProfilePreferenceConstants; +import org.eclipse.papyrus.uml.profile.tree.ProfileElementContentProvider; +import org.eclipse.papyrus.uml.profile.tree.ProfileElementLabelProvider; +import org.eclipse.papyrus.uml.profile.tree.ProfileElementTreeViewerFilter; +import org.eclipse.papyrus.uml.profile.tree.objects.AppliedStereotypePropertyTreeObject; +import org.eclipse.papyrus.uml.profile.tree.objects.AppliedStereotypeTreeObject; +import org.eclipse.papyrus.uml.profile.tree.objects.StereotypedElementTreeObject; +import org.eclipse.papyrus.uml.profile.tree.objects.TreeObject; +import org.eclipse.papyrus.uml.profile.utils.Util; +import org.eclipse.papyrus.uml.properties.profile.ui.dialogs.ChooseSetStereotypeDialog; +import org.eclipse.papyrus.uml.properties.profile.ui.panels.AppliedStereotypePanel; +import org.eclipse.swt.SWT; +import org.eclipse.swt.custom.CLabel; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Tree; +import org.eclipse.swt.widgets.TreeItem; +import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetWidgetFactory; +import org.eclipse.uml2.uml.Element; +import org.eclipse.uml2.uml.Stereotype; + +// TODO: Auto-generated Javadoc +/** + * This composite is used to display applied stereotype in the model. It allows applying or desapply a stereotype + */ +public class AppliedStereotypeCompositeOnModel extends DecoratedTreeComposite implements ISelectionChangedListener { + + /** + * Gets the domain. + * + * @return the domain + */ + public TransactionalEditingDomain getEditingDomain(Element context) { + try { + return ServiceUtilsForEObject.getInstance().getTransactionalEditingDomain(context); + } catch (ServiceException ex) { + Activator.log.error(ex); + return null; + } + } + + /** The panel that display applied stereotypes. */ + private AppliedStereotypePanel appliedStereotypePanel; + + /** The label. */ + protected CLabel label; + + /** + * The default constructor. + * + * @param parent + * the parent Composite for this panel + */ + public AppliedStereotypeCompositeOnModel(AppliedStereotypePanel parent) { + super(parent, SWT.NONE, "Applied stereotypes", true); + + appliedStereotypePanel = parent; + } + + /** + * create a composite applied stereotype on model. + * + * @param parent + * the parent composite + */ + public AppliedStereotypeCompositeOnModel(Composite parent) { + super(parent, SWT.NONE, "Applied stereotypes", true); + } + + /** + * apply a stereotype on current selected element. + */ + protected void addAppliedStereotype() { + + // Open stereotype selection (may add or remove) + ChooseSetStereotypeDialog dialog = new ChooseSetStereotypeDialog(this.getShell(), getElement()); + int result = dialog.open(); + + if(result == ChooseSetStereotypeDialog.OK) { + // Retrieve selected element + Element element = getElement(); + + // compare the 2 lists (present list and future list + EList<Stereotype> oldStereotypeList = element.getAppliedStereotypes(); + ArrayList<Stereotype> newStereotypeList = dialog.getSelectedElements(); + + // Keep newStereotype order (will be used at the end of the method) + EList<Stereotype> newOrderList = new BasicEList<Stereotype>(); + newOrderList.addAll(newStereotypeList); + + // If the 2 lists differ, apply the new list of stereotypes + if(!(newStereotypeList.equals(oldStereotypeList))) { + + // Parse old list : + // if stereotype is in the new list : it is already applied + // --> don't unapply it + // --> remove it from new list + Iterator<Stereotype> it = oldStereotypeList.iterator(); + while(it.hasNext()) { + Stereotype currentStOld = it.next(); + if(newStereotypeList.contains(currentStOld)) { + newStereotypeList.remove(currentStOld); + } else { + unapplyStereotype(element, currentStOld); + } + } + + // Already applied stereotype should have been removed + // apply others + Iterator<Stereotype> newApplyStereotypes = newStereotypeList.iterator(); + while(newApplyStereotypes.hasNext()) { + Stereotype currentStereotype = newApplyStereotypes.next(); + applyStereotype(element, currentStereotype); + } + + // Update Stereotype order + // this.reorderStereotypeApplications(element, newOrderList); + + // checkSelection(null); + selectionChanged(null); + + if(appliedStereotypePanel != null) { + appliedStereotypePanel.refresh(); + } + } + } + + } + + /** + * Button action : open a selection dialog box that allow the user to choose stereotypes to apply (or unapply). + */ + @Override + public void addButtonPressed() { + addAppliedStereotype(); + } + + /** + * + * {@inheritDoc} + */ + @Override + public Composite createContent(Composite parent, TabbedPropertySheetWidgetFactory factory) { + super.createContent(parent, factory); + createStereotypesTree(); + + removeButton.setToolTipText("Remove stereotype"); + addButton.setToolTipText("Apply stereotype"); + + return this; + } + + /** + * Creates the stereotypes tree. + * + * @return the tree of applied stereotypes and properties + */ + private void createStereotypesTree() { + // Tree viewer shows applied stereotypes + treeViewer.setContentProvider(new ProfileElementContentProvider()); + treeViewer.setLabelProvider(new ProfileElementLabelProvider()); + treeViewer.addFilter(new ProfileElementTreeViewerFilter()); + treeViewer.addSelectionChangedListener(this); + } + + /** + * Button action : modify display order of stereotypes (selected elements are pushed down in the list). + */ + @Override + public void downButtonPressed() { + int nbrOfSelection = getTree().getSelectionCount(); + if(nbrOfSelection < 1) { + return; + } + + TreeItem[] items = getTree().getSelection(); + int indexLast = getTree().indexOf(items[items.length - 1]); + if(indexLast + 1 >= getElement().getAppliedStereotypes().size()) { + // do nothing + return; + } + + for(int i = 0; i < nbrOfSelection; i++) { + TreeItem item = items[nbrOfSelection - 1 - i]; + if(item.getData() instanceof AppliedStereotypeTreeObject) { + AppliedStereotypeTreeObject sTO = (AppliedStereotypeTreeObject)item.getData(); + EList stereotypes = new BasicEList(); + stereotypes.addAll(element.getAppliedStereotypes()); + + int index = stereotypes.indexOf(sTO.getStereotype()); + if((index == -1) || (index >= stereotypes.size() - 1)) { + // Not found of already on top... + return; + } + + stereotypes.move(index + 1, sTO.getStereotype()); + this.reorderStereotypeApplications(element, stereotypes); + } + } + } + + /** + * Edits the item. + * + * @param item + * the item + */ + @Override + public void editItem(TreeItem item) { + // do nothing + } + + /** + * Gets the selected. + * + * @return Returns the selected element. + */ + public Element getSelected() { + return appliedStereotypePanel.getSelected(); + } + + /** + * Gets the tree. + * + * @return the tree + */ + public Tree getTree() { + return treeViewer.getTree(); + } + + /** + * Checks if is in stereotype display. + * + * @param st + * the stereotype + * + * @return true, if checks if is in stereotype display + */ + protected Boolean isInStereotypeDisplay(Stereotype st) { + return false; + } + + /** + * Redraw the treeViewer while preserving selections and non-collapsed tree elements + * It is not sufficient to redraw only selected elements as an optimization, since + * derived stereotype attributes (that are not selected) might change in response to + * changing other attributes. + * + * @param propertyView + */ + public void refreshTreeViewer() { + treeViewer.refresh(); + } + + /** + * Refresh the content of applied the applied stereotype tree. + */ + @Override + public void refresh() { + if(treeViewer.getTree() != null && !(treeViewer.getTree().isDisposed())) { + treeViewer.setInput(null); + treeViewer.refresh(); + if(element != null) { + treeViewer.setInput(new StereotypedElementTreeObject(element)); + } + StereotypedElementTreeObject rTO = (StereotypedElementTreeObject)treeViewer.getInput(); + if(rTO == null) { + return; + } + + // If the property is Multivalued show Up - Down + if((rTO.getChildren() != null) && (rTO.getChildren().length > 1)) { + upButton.setEnabled(true); + downButton.setEnabled(true); + } else { + upButton.setEnabled(false); + downButton.setEnabled(false); + } + + if((rTO.getChildren() != null) && (rTO.getChildren().length == 0)) { + removeButton.setEnabled(false); + } else { + removeButton.setEnabled(true); + } + } + } + + /** + * Button action : unapply the stereotypes selected by the user in the stereotype tree. + */ + @Override + public void removeButtonPressed() { + unapplyStereotype(); + } + + /** + * Selection changed. + * + * @param event + * the event + */ + public void selectionChanged(SelectionChangedEvent event) { + if(appliedStereotypePanel != null) { + if(event == null) { + appliedStereotypePanel.setSelectedProperty(null); + return; + } + + IStructuredSelection structSelection = (IStructuredSelection)event.getSelection(); + Object selection = structSelection.getFirstElement(); + if(selection instanceof AppliedStereotypePropertyTreeObject) { + appliedStereotypePanel.setSelectedProperty((AppliedStereotypePropertyTreeObject)selection); + } else { + appliedStereotypePanel.setSelectedProperty(null); + } + + } + } + + /** + * Sets the input. + * + * @param element + * the element + */ + public void setInput(StereotypedElementTreeObject element) { + treeViewer.setInput(element); + if(Activator.getDefault().getPreferenceStore().getBoolean(ProfilePreferenceConstants.EXPAND_STEREOTYPES_TREE)) { + treeViewer.expandAll(); + } + } + + /** + * unapply stereotype on current selected element. + */ + protected void unapplyStereotype() { + int nbrOfSelection = getTree().getSelectionCount(); + if(nbrOfSelection == 0) { + return; + } + + for(int i = 0; i < nbrOfSelection; i++) { + TreeItem item = getTree().getSelection()[i]; + if(item.getData() instanceof AppliedStereotypeTreeObject) { + AppliedStereotypeTreeObject sTO = (AppliedStereotypeTreeObject)item.getData(); + unapplyStereotype(element, sTO.getStereotype()); + sTO.removeMe(); + } + } + if(appliedStereotypePanel != null) { + appliedStereotypePanel.refresh(); + } else { + refresh(); + } + } + + /** + * Button action : modify display order of stereotypes (selected elements are pushed up in the list). + */ + @Override + public void upButtonPressed() { + int nbrOfSelection = getTree().getSelectionCount(); + if(nbrOfSelection < 1) { + return; + } + + TreeItem[] items = getTree().getSelection(); + int indexFirst = getTree().indexOf(items[0]); + if(indexFirst == 0) { + // do nothing + return; + } + + for(int i = 0; i < nbrOfSelection; i++) { + TreeItem item = items[i]; + if(item.getData() instanceof AppliedStereotypeTreeObject) { + AppliedStereotypeTreeObject sTO = (AppliedStereotypeTreeObject)item.getData(); + EList stereotypes = new BasicEList(); + stereotypes.addAll(element.getAppliedStereotypes()); + + int index = stereotypes.indexOf(sTO.getStereotype()); + if(index < 1) { + return; + } + + stereotypes.move(index - 1, sTO.getStereotype()); + this.reorderStereotypeApplications(element, stereotypes); + } + } + if(appliedStereotypePanel != null) { + appliedStereotypePanel.refresh(); + } else { + refresh(); + } + } + + /** + * + * @see org.eclipse.papyrus.uml.properties.profile.ui.compositesformodel.DecoratedTreeComposite#keepSelection(org.eclipse.jface.viewers.ISelection) + * + * @param pSelection + */ + @Override + public void keepSelection(ISelection pSelection) { + getDisplay().asyncExec(new SelectionKeeper(pSelection)); + } + + /** + * Apply stereotype. + * + * @param elt + * the elt + * @param st + * the st + */ + public void applyStereotype(final Element elt, final Stereotype st) { + try { + final TransactionalEditingDomain domain = getEditingDomain(elt); + domain.runExclusive(new Runnable() { + + public void run() { + + Display.getCurrent().asyncExec(new Runnable() { + + public void run() { + domain.getCommandStack().execute(new RecordingCommand(domain) { + + @Override + protected void doExecute() { + elt.applyStereotype(st); + refresh(); + } + }); + } + }); + } + }); + + } catch (Exception e) { + Activator.log.error(e); + } + + } + + /** + * Unapply stereotype. + * + * @param elt + * the uml element + * @param st + * the stereotype to unapply + */ + protected void unapplyStereotype(final Element elt, final Stereotype st) { + // bugfix: a selected element is not necessary a diagram element (ex: selection in the outline) + try { + final TransactionalEditingDomain domain = getEditingDomain(elt); + domain.runExclusive(new Runnable() { + + public void run() { + + Display.getCurrent().asyncExec(new Runnable() { + + public void run() { + domain.getCommandStack().execute(new RecordingCommand(domain) { + + @Override + protected void doExecute() { + elt.unapplyStereotype(st); + elt.eNotify(new NotificationImpl(Notification.SET, true, true, true)); + refresh(); + } + }); + } + }); + } + }); + + } catch (Exception e) { + Activator.log.error(e); + } + + } + + /** + * change the order of applied stereotype + * + * @param element + * the UML element where stereotypes are applied + * @param stereotypes + * the lis of applied stereotypes with the wanted order + */ + public void reorderStereotypeApplications(final Element element, final EList stereotypes) { + try { + final TransactionalEditingDomain domain = getEditingDomain(element); + domain.runExclusive(new Runnable() { + + public void run() { + + Display.getCurrent().asyncExec(new Runnable() { + + public void run() { + domain.getCommandStack().execute(new RecordingCommand(domain) { + + @Override + protected void doExecute() { + Util.reorderStereotypeApplications(element, stereotypes); + refresh(); + } + }); + } + }); + } + }); + + } catch (Exception e) { + Activator.log.error(e); + } + + } + + /** + * + * @author gpascual + * + */ + private class SelectionKeeper implements Runnable { + + /** Selection to keep. */ + ISelection selection = null; + + /** + * + * Constructor. + * + */ + public SelectionKeeper(ISelection selection) { + this.selection = selection; + } + + public void run() { + Object[] vSelectedElements = extractSelectedElements(selection); + Object[] vCorrespondingElements = getCorrespondingElements(vSelectedElements); + ISelection vSelection = new StructuredSelection(vCorrespondingElements); + treeViewer.setSelection(vSelection); + + } + + + + + + private Object[] getCorrespondingElements(Object[] vSelectedElements) { + StereotypedElementTreeObject vStereotypesTree = (StereotypedElementTreeObject)treeViewer.getInput(); + List<Object> vReturn = new ArrayList<Object>(); + for(Object vStereotype : vSelectedElements) { + if(vStereotype instanceof AppliedStereotypeTreeObject) { + AppliedStereotypeTreeObject vTreeObject = findAppliedStereotypeInTree(((AppliedStereotypeTreeObject)vStereotype).getStereotype(), vStereotypesTree); + vReturn.add(vTreeObject); + } + } + + return vReturn.toArray(); + } + + private AppliedStereotypeTreeObject findAppliedStereotypeInTree(Stereotype stereotype, StereotypedElementTreeObject vStereotypesTree) { + AppliedStereotypeTreeObject vAppliedStereotypeObject = null; + + for(TreeObject vChild : vStereotypesTree.getChildren()) { + if(vChild instanceof AppliedStereotypeTreeObject) { + if(stereotype.equals(((AppliedStereotypeTreeObject)vChild).getStereotype())) { + vAppliedStereotypeObject = (AppliedStereotypeTreeObject)vChild; + } + } + } + + return vAppliedStereotypeObject; + + } + + private Object[] extractSelectedElements(ISelection pSelection) { + List<Object> vObjectsList = new ArrayList<Object>(); + if(pSelection instanceof IStructuredSelection) { + vObjectsList.addAll(Arrays.asList(((IStructuredSelection)pSelection).toArray())); + } + return vObjectsList.toArray(); + } + } + + +} diff --git a/plugins/uml/properties/org.eclipse.papyrus.uml.properties/src/org/eclipse/papyrus/uml/properties/profile/ui/compositesformodel/DecoratedTreeComposite.java b/plugins/uml/properties/org.eclipse.papyrus.uml.properties/src/org/eclipse/papyrus/uml/properties/profile/ui/compositesformodel/DecoratedTreeComposite.java index 608c1f9f3fd..8bbdb56a7d2 100644 --- a/plugins/uml/properties/org.eclipse.papyrus.uml.properties/src/org/eclipse/papyrus/uml/properties/profile/ui/compositesformodel/DecoratedTreeComposite.java +++ b/plugins/uml/properties/org.eclipse.papyrus.uml.properties/src/org/eclipse/papyrus/uml/properties/profile/ui/compositesformodel/DecoratedTreeComposite.java @@ -1,530 +1,540 @@ -/*****************************************************************************
- * Copyright (c) 2008 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:
- * Chokri Mraidha (CEA LIST) Chokri.Mraidha@cea.fr - Initial API and implementation
- * Patrick Tessier (CEA LIST) Patrick.Tessier@cea.fr - modification
- *
- *****************************************************************************/
-package org.eclipse.papyrus.uml.properties.profile.ui.compositesformodel;
-
-import org.eclipse.gef.commands.CommandStack;
-import org.eclipse.jface.viewers.TreeViewer;
-import org.eclipse.papyrus.uml.profile.ImageManager;
-import org.eclipse.papyrus.uml.profile.tree.ProfileElementTreeViewer;
-import org.eclipse.papyrus.uml.profile.tree.PropertyValueTreeViewer;
-import org.eclipse.swt.SWT;
-import org.eclipse.swt.custom.CLabel;
-import org.eclipse.swt.events.MouseEvent;
-import org.eclipse.swt.events.MouseListener;
-import org.eclipse.swt.layout.FormAttachment;
-import org.eclipse.swt.layout.FormData;
-import org.eclipse.swt.layout.FormLayout;
-import org.eclipse.swt.widgets.Button;
-import org.eclipse.swt.widgets.Composite;
-import org.eclipse.swt.widgets.Event;
-import org.eclipse.swt.widgets.Listener;
-import org.eclipse.swt.widgets.Tree;
-import org.eclipse.swt.widgets.TreeItem;
-import org.eclipse.ui.IEditorPart;
-import org.eclipse.ui.PlatformUI;
-import org.eclipse.ui.views.properties.tabbed.ITabbedPropertyConstants;
-import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetWidgetFactory;
-import org.eclipse.uml2.uml.Element;
-
-
-/**
- * Base class for a complex section composite. This composite has a label, a
- * tree that describes a tree structure, and four buttons on the side of the
- * table to add an element into the tree, remove selected element(s), move up
- * or down the selected element.
- *
- * @author Remi SCHNEKENBURGER
- */
-public abstract class DecoratedTreeComposite extends Composite implements ISectionComposite {
-
- // child composites for the table composite
-
- // /** main composite of this complex composite */
- // protected Composite composite;
- /**
- * Label above the table.
- */
- protected CLabel label;
-
- /**
- * Table that displays a property for the current element.
- */
- protected Tree tree;
-
- /**
- * The tree viewer.
- */
- protected TreeViewer treeViewer;
-
- /**
- * Button that adds an element.
- */
- protected Button addButton;
-
- /**
- * Button that removes an element.
- */
- protected Button removeButton;
-
- /**
- * button that moves the element up.
- */
- protected Button upButton;
-
- /**
- * button that moves the element down.
- */
- protected Button downButton;
-
- // listeners for buttons
- /**
- * Listener for the add button.
- */
- protected MouseListener addButtonlistener;
-
- /**
- * Listener for the delete button.
- */
- protected MouseListener removeButtonlistener;
-
- /**
- * Listener for the up button.
- */
- protected MouseListener upButtonlistener;
-
- /**
- * Listener for the down button.
- */
- protected MouseListener downButtonlistener;
-
- /**
- *
- */
- protected Listener treeListener;
-
- /**
- * Element selected in the Papyrus modeler.
- */
- protected Element element;
-
-
- // Construction parameters for the composite
- /**
- * text of the label.
- */
- protected String name;
-
- /**
- * returns the element that is selected in Papyrus tool, for which
- * properties are displayed in the property section.
- *
- * @return the element
- */
- public Element getElement() {
- return element;
- }
-
- /**
- * Sets the element that holds property displyed in property section.
- *
- * @param element
- * the element to set
- */
- public void setElement(Element element) {
- this.element = element;
- }
-
- /**
- * Constructor.
- *
- * @param style
- * @param isStereotypeTree
- * @param name
- * text of the Label on the top left of this composite
- * @param parent
- */
- public DecoratedTreeComposite(Composite parent, int style, String name, boolean isStereotypeTree) {
- super(parent, style);
- this.name = name;
- this.setLayout(new FormLayout());
-
- this.addButtonlistener = new AddButtonlistener();
- this.removeButtonlistener = new RemoveButtonlistener();
- this.upButtonlistener = new UpButtonlistener();
- this.downButtonlistener = new DownButtonlistener();
-
- if(isStereotypeTree) {
- treeViewer = new ProfileElementTreeViewer(this);
- } else {
- // Property tree
- treeViewer = new PropertyValueTreeViewer(this);
- }
- }
-
- /*
- * (non-Javadoc)
- *
- * @see com.cea.papyrus.ui.composites.ISectionComposite#createContent(org.eclipse.swt.widgets.Composite,
- * org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetWidgetFactory)
- */
- /**
- *
- *
- * @param factory
- * @param parent
- *
- * @return
- */
- public Composite createContent(Composite parent,
- TabbedPropertySheetWidgetFactory factory) {
-
- FormData data;
-
- /////////////////////////////////////////////////////////////////////////////
- // Create and place button vertically on the left side
- // Button : Add Element
- // Button Delete Element
- removeButton = factory.createButton(this, "", SWT.PUSH);
- removeButton.setVisible(true);
- removeButton.setImage(ImageManager.IMG_DELETE);
- removeButton.setToolTipText("Delete selected element(s)");
- data = new FormData();
- data.top = new FormAttachment(0, 0);
- data.right = new FormAttachment(100, -ITabbedPropertyConstants.HSPACE);
- removeButton.setLayoutData(data);
- removeButton.addMouseListener(removeButtonlistener);
-
- addButton = factory.createButton(this, "", SWT.PUSH);
- addButton.setVisible(true);
- addButton.setImage(ImageManager.IMG_ADD);
- addButton.setToolTipText("Add a new element");
-
- data = new FormData();
- data.top = new FormAttachment(0, 0);
- data.right = new FormAttachment(removeButton, -ITabbedPropertyConstants.HSPACE);
- addButton.setLayoutData(data);
- addButton.addMouseListener(addButtonlistener);
-
- // Button Up
- upButton = factory.createButton(this, "", SWT.PUSH);
- upButton.setVisible(true);
- upButton.setImage(ImageManager.IMG_UP);
- upButton.setToolTipText("Up");
-
- data = new FormData();
- data.top = new FormAttachment(0, 0);
- data.right = new FormAttachment(addButton, -ITabbedPropertyConstants.HSPACE);
- upButton.setLayoutData(data);
- upButton.addMouseListener(upButtonlistener);
-
-
- // Button Down
- downButton = factory.createButton(this, "", SWT.PUSH);
- downButton.setVisible(true);
- downButton.setImage(ImageManager.IMG_DOWN);
- downButton.setToolTipText("Down");
-
- data = new FormData();
- data.top = new FormAttachment(0, 0);
- data.right = new FormAttachment(upButton, -ITabbedPropertyConstants.HSPACE);
- downButton.setLayoutData(data);
- downButton.addMouseListener(downButtonlistener);
-
- // Create label
- label = factory.createCLabel(this, name + ":", SWT.NONE);
- label.setLayout(new FormLayout());
- data = new FormData();
- data.left = new FormAttachment(0, ITabbedPropertyConstants.HSPACE);
- data.right = new FormAttachment(downButton, -ITabbedPropertyConstants.HSPACE - 30);
- data.top = new FormAttachment(0, 0);
- label.setLayoutData(data);
-
- /////////////////////////////////////////////////////////////////////////////
- // Create and place Table
- tree = treeViewer.getTree();
- tree.setLayout(new FormLayout());
- tree.setVisible(true);
- tree.addListener(SWT.MouseDoubleClick, treeListener = new EditItemListener());
-
- data = new FormData();
- data.top = new FormAttachment(addButton, ITabbedPropertyConstants.VSPACE);
- data.left = new FormAttachment(0, ITabbedPropertyConstants.HSPACE);
- data.right = new FormAttachment(100, -ITabbedPropertyConstants.HSPACE);
- data.bottom = new FormAttachment(100, -ITabbedPropertyConstants.VSPACE);
-
- tree.setLayoutData(data);
- return this;
- }
-
- /*
- * (non-Javadoc)
- *
- * @see com.cea.papyrus.ui.composites.ISectionComposite#refresh()
- */
- /**
- *
- */
- public void refresh() {
- }
-
- // /**
- // * Sets the layout data to the main composite of this complex element
- // * @param data the new LayoutData
- // */
- // public void setLayoutData(Object data) {
- // composite.setLayoutData(data);
- // }
- //
- // public Composite getMainComposite() {
- // return composite;
- // }
-
- /**
- * Returns the CommmandStack of the current editor.
- *
- * @return the CommmandStack of the current editor
- */
- public CommandStack getCommandStack() {
- if(getActiveEditor() != null) {
- return (CommandStack)getActiveEditor().getAdapter(CommandStack.class);
- }
- return null;
- }
-
- /**
- * Returns the current Editor.
- *
- * @return the current editor
- */
- public IEditorPart getActiveEditor() {
- return PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
- }
-
- /**
- * action executed when the add button is pressed.
- */
- public void addButtonPressed() {
- }
-
- /**
- * action executed when the remove button is pressed.
- */
- public abstract void removeButtonPressed();
-
- /**
- * action executed when the up button is pressed.
- */
- public abstract void upButtonPressed();
-
- /**
- * action executed when the down button is pressed.
- */
- public abstract void downButtonPressed();
-
- /**
- * action executed when a table item is edited.
- *
- * @param item
- */
- public abstract void editItem(TreeItem item);
-
- /**
- * Listener for the Add Button
- * Specific behavior is implemented in {@link DecoratedTreeComposite#addButtonPressed()}.
- *
- * @author Remi SCHNEKENBURGER
- */
- private class AddButtonlistener implements MouseListener {
-
- /**
- *
- *
- * @param e
- */
- public void mouseDoubleClick(MouseEvent e) {
- // do nothing
- }
-
- /**
- *
- *
- * @param e
- */
- public void mouseDown(MouseEvent e) {
- // do nothing
- }
-
- /**
- *
- *
- * @param e
- */
- public void mouseUp(MouseEvent e) {
- addButtonPressed();
- refresh();
- }
- }
-
- /**
- * Listener for the Remove Button
- * Specific behavior is implemented in {@link DecoratedTreeComposite#removeButtonPressed()}.
- *
- * @author Remi SCHNEKENBURGER
- */
- private class RemoveButtonlistener implements MouseListener {
-
- /**
- *
- *
- * @param e
- */
- public void mouseDoubleClick(MouseEvent e) {
- // do nothing
- }
-
- /**
- *
- *
- * @param e
- */
- public void mouseDown(MouseEvent e) {
- // do nothing
- }
-
- /**
- *
- *
- * @param e
- */
- public void mouseUp(MouseEvent e) {
- removeButtonPressed();
- refresh();
- }
- }
-
- /**
- * Listener for the Up Button
- * Specific behavior is implemented in {@link DecoratedTreeComposite#upButtonPressed()}.
- *
- * @author Remi SCHNEKENBURGER
- */
- private class UpButtonlistener implements MouseListener {
-
- /**
- *
- *
- * @param e
- */
- public void mouseDoubleClick(MouseEvent e) {
- // do nothing
- }
-
- /**
- *
- *
- * @param e
- */
- public void mouseDown(MouseEvent e) {
- // do nothing
- }
-
- /**
- *
- *
- * @param e
- */
- public void mouseUp(MouseEvent e) {
- TreeItem[] treeItems = tree.getSelection();
- upButtonPressed();
- refresh();
- // Keep selection
- tree.setSelection(treeItems);
- }
- }
-
- /**
- * Listener for the Down Button
- * Specific behavior is implemented in {@link DecoratedTreeComposite#downButtonPressed()}.
- *
- * @author Remi SCHNEKENBURGER
- */
- private class DownButtonlistener implements MouseListener {
-
- /**
- *
- *
- * @param e
- */
- public void mouseDoubleClick(MouseEvent e) {
- // do nothing
- }
-
- /**
- *
- *
- * @param e
- */
- public void mouseDown(MouseEvent e) {
- // do nothing
- }
-
- /**
- *
- *
- * @param e
- */
- public void mouseUp(MouseEvent e) {
- TreeItem[] treeItems = tree.getSelection();
- downButtonPressed();
- refresh();
- // Keep selection
- tree.setSelection(treeItems);
- }
- }
-
- /**
- *
- */
- private class EditItemListener implements Listener {
-
- /**
- *
- *
- * @param event
- */
- public void handleEvent(Event event) {
- if(tree.getSelection().length > 0) {
- TreeItem item = tree.getSelection()[0];
- editItem(item);
- }
- }
- }
-
- /**
- * Dipose listeners.
- */
- public void disposeListeners() {
- if(addButton != null && !addButton.isDisposed())
- addButton.removeMouseListener(addButtonlistener);
- if(removeButton != null && !removeButton.isDisposed())
- removeButton.removeMouseListener(removeButtonlistener);
- if(upButton != null && !upButton.isDisposed())
- upButton.removeMouseListener(upButtonlistener);
- if(downButton != null && !downButton.isDisposed())
- downButton.removeMouseListener(downButtonlistener);
- if(tree != null && !tree.isDisposed())
- tree.removeListener(SWT.MouseDoubleClick, treeListener);
- }
-}
+/***************************************************************************** + * Copyright (c) 2008 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: + * Chokri Mraidha (CEA LIST) Chokri.Mraidha@cea.fr - Initial API and implementation + * Patrick Tessier (CEA LIST) Patrick.Tessier@cea.fr - modification + * + *****************************************************************************/ +package org.eclipse.papyrus.uml.properties.profile.ui.compositesformodel; + +import org.eclipse.gef.commands.CommandStack; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.TreeViewer; +import org.eclipse.papyrus.uml.profile.ImageManager; +import org.eclipse.papyrus.uml.profile.tree.ProfileElementTreeViewer; +import org.eclipse.papyrus.uml.profile.tree.PropertyValueTreeViewer; +import org.eclipse.swt.SWT; +import org.eclipse.swt.custom.CLabel; +import org.eclipse.swt.events.MouseEvent; +import org.eclipse.swt.events.MouseListener; +import org.eclipse.swt.layout.FormAttachment; +import org.eclipse.swt.layout.FormData; +import org.eclipse.swt.layout.FormLayout; +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Event; +import org.eclipse.swt.widgets.Listener; +import org.eclipse.swt.widgets.Tree; +import org.eclipse.swt.widgets.TreeItem; +import org.eclipse.ui.IEditorPart; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.views.properties.tabbed.ITabbedPropertyConstants; +import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetWidgetFactory; +import org.eclipse.uml2.uml.Element; + + +/** + * Base class for a complex section composite. This composite has a label, a + * tree that describes a tree structure, and four buttons on the side of the + * table to add an element into the tree, remove selected element(s), move up + * or down the selected element. + * + * @author Remi SCHNEKENBURGER + */ +public abstract class DecoratedTreeComposite extends Composite implements ISectionComposite { + + // child composites for the table composite + + // /** main composite of this complex composite */ + // protected Composite composite; + /** + * Label above the table. + */ + protected CLabel label; + + /** + * Table that displays a property for the current element. + */ + protected Tree tree; + + /** + * The tree viewer. + */ + protected TreeViewer treeViewer; + + /** + * Button that adds an element. + */ + protected Button addButton; + + /** + * Button that removes an element. + */ + protected Button removeButton; + + /** + * button that moves the element up. + */ + protected Button upButton; + + /** + * button that moves the element down. + */ + protected Button downButton; + + // listeners for buttons + /** + * Listener for the add button. + */ + protected MouseListener addButtonlistener; + + /** + * Listener for the delete button. + */ + protected MouseListener removeButtonlistener; + + /** + * Listener for the up button. + */ + protected MouseListener upButtonlistener; + + /** + * Listener for the down button. + */ + protected MouseListener downButtonlistener; + + /** + * + */ + protected Listener treeListener; + + /** + * Element selected in the Papyrus modeler. + */ + protected Element element; + + + // Construction parameters for the composite + /** + * text of the label. + */ + protected String name; + + /** + * returns the element that is selected in Papyrus tool, for which + * properties are displayed in the property section. + * + * @return the element + */ + public Element getElement() { + return element; + } + + /** + * Sets the element that holds property displyed in property section. + * + * @param element + * the element to set + */ + public void setElement(Element element) { + this.element = element; + } + + /** + * Constructor. + * + * @param style + * @param isStereotypeTree + * @param name + * text of the Label on the top left of this composite + * @param parent + */ + public DecoratedTreeComposite(Composite parent, int style, String name, boolean isStereotypeTree) { + super(parent, style); + this.name = name; + this.setLayout(new FormLayout()); + + this.addButtonlistener = new AddButtonlistener(); + this.removeButtonlistener = new RemoveButtonlistener(); + this.upButtonlistener = new UpButtonlistener(); + this.downButtonlistener = new DownButtonlistener(); + + if(isStereotypeTree) { + treeViewer = new ProfileElementTreeViewer(this); + } else { + // Property tree + treeViewer = new PropertyValueTreeViewer(this); + } + } + + /* + * (non-Javadoc) + * + * @see com.cea.papyrus.ui.composites.ISectionComposite#createContent(org.eclipse.swt.widgets.Composite, + * org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetWidgetFactory) + */ + /** + * + * + * @param factory + * @param parent + * + * @return + */ + public Composite createContent(Composite parent, TabbedPropertySheetWidgetFactory factory) { + + FormData data; + + ///////////////////////////////////////////////////////////////////////////// + // Create and place button vertically on the left side + // Button : Add Element + // Button Delete Element + removeButton = factory.createButton(this, "", SWT.PUSH); + removeButton.setVisible(true); + removeButton.setImage(ImageManager.IMG_DELETE); + removeButton.setToolTipText("Delete selected element(s)"); + data = new FormData(); + data.top = new FormAttachment(0, 0); + data.right = new FormAttachment(100, -ITabbedPropertyConstants.HSPACE); + removeButton.setLayoutData(data); + removeButton.addMouseListener(removeButtonlistener); + + addButton = factory.createButton(this, "", SWT.PUSH); + addButton.setVisible(true); + addButton.setImage(ImageManager.IMG_ADD); + addButton.setToolTipText("Add a new element"); + + data = new FormData(); + data.top = new FormAttachment(0, 0); + data.right = new FormAttachment(removeButton, -ITabbedPropertyConstants.HSPACE); + addButton.setLayoutData(data); + addButton.addMouseListener(addButtonlistener); + + // Button Up + upButton = factory.createButton(this, "", SWT.PUSH); + upButton.setVisible(true); + upButton.setImage(ImageManager.IMG_UP); + upButton.setToolTipText("Up"); + + data = new FormData(); + data.top = new FormAttachment(0, 0); + data.right = new FormAttachment(addButton, -ITabbedPropertyConstants.HSPACE); + upButton.setLayoutData(data); + upButton.addMouseListener(upButtonlistener); + + + // Button Down + downButton = factory.createButton(this, "", SWT.PUSH); + downButton.setVisible(true); + downButton.setImage(ImageManager.IMG_DOWN); + downButton.setToolTipText("Down"); + + data = new FormData(); + data.top = new FormAttachment(0, 0); + data.right = new FormAttachment(upButton, -ITabbedPropertyConstants.HSPACE); + downButton.setLayoutData(data); + downButton.addMouseListener(downButtonlistener); + + // Create label + label = factory.createCLabel(this, name + ":", SWT.NONE); + label.setLayout(new FormLayout()); + data = new FormData(); + data.left = new FormAttachment(0, ITabbedPropertyConstants.HSPACE); + data.right = new FormAttachment(downButton, -ITabbedPropertyConstants.HSPACE - 30); + data.top = new FormAttachment(0, 0); + label.setLayoutData(data); + + ///////////////////////////////////////////////////////////////////////////// + // Create and place Table + tree = treeViewer.getTree(); + tree.setLayout(new FormLayout()); + tree.setVisible(true); + tree.addListener(SWT.MouseDoubleClick, treeListener = new EditItemListener()); + + data = new FormData(); + data.top = new FormAttachment(addButton, ITabbedPropertyConstants.VSPACE); + data.left = new FormAttachment(0, ITabbedPropertyConstants.HSPACE); + data.right = new FormAttachment(100, -ITabbedPropertyConstants.HSPACE); + data.bottom = new FormAttachment(100, -ITabbedPropertyConstants.VSPACE); + + tree.setLayoutData(data); + return this; + } + + /* + * (non-Javadoc) + * + * @see com.cea.papyrus.ui.composites.ISectionComposite#refresh() + */ + /** + * + */ + public void refresh() { + } + + // /** + // * Sets the layout data to the main composite of this complex element + // * @param data the new LayoutData + // */ + // public void setLayoutData(Object data) { + // composite.setLayoutData(data); + // } + // + // public Composite getMainComposite() { + // return composite; + // } + + /** + * Returns the CommmandStack of the current editor. + * + * @return the CommmandStack of the current editor + */ + public CommandStack getCommandStack() { + if(getActiveEditor() != null) { + return (CommandStack)getActiveEditor().getAdapter(CommandStack.class); + } + return null; + } + + /** + * Returns the current Editor. + * + * @return the current editor + */ + public IEditorPart getActiveEditor() { + return PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor(); + } + + /** + * action executed when the add button is pressed. + */ + public void addButtonPressed() { + } + + /** + * @param pSelection + */ + public void keepSelection(ISelection pSelection) { + + } + + /** + * action executed when the remove button is pressed. + */ + public abstract void removeButtonPressed(); + + /** + * action executed when the up button is pressed. + */ + public abstract void upButtonPressed(); + + /** + * action executed when the down button is pressed. + */ + public abstract void downButtonPressed(); + + /** + * action executed when a table item is edited. + * + * @param item + */ + public abstract void editItem(TreeItem item); + + /** + * Listener for the Add Button + * Specific behavior is implemented in {@link DecoratedTreeComposite#addButtonPressed()}. + * + * @author Remi SCHNEKENBURGER + */ + private class AddButtonlistener implements MouseListener { + + /** + * + * + * @param e + */ + public void mouseDoubleClick(MouseEvent e) { + // do nothing + } + + /** + * + * + * @param e + */ + public void mouseDown(MouseEvent e) { + // do nothing + } + + /** + * + * + * @param e + */ + public void mouseUp(MouseEvent e) { + addButtonPressed(); + refresh(); + } + } + + /** + * Listener for the Remove Button + * Specific behavior is implemented in {@link DecoratedTreeComposite#removeButtonPressed()}. + * + * @author Remi SCHNEKENBURGER + */ + private class RemoveButtonlistener implements MouseListener { + + /** + * + * + * @param e + */ + public void mouseDoubleClick(MouseEvent e) { + // do nothing + } + + /** + * + * + * @param e + */ + public void mouseDown(MouseEvent e) { + // do nothing + } + + /** + * + * + * @param e + */ + public void mouseUp(MouseEvent e) { + removeButtonPressed(); + refresh(); + } + } + + /** + * Listener for the Up Button + * Specific behavior is implemented in {@link DecoratedTreeComposite#upButtonPressed()}. + * + * @author Remi SCHNEKENBURGER + */ + private class UpButtonlistener implements MouseListener { + + /** + * + * + * @param e + */ + public void mouseDoubleClick(MouseEvent e) { + // do nothing + } + + /** + * + * + * @param e + */ + public void mouseDown(MouseEvent e) { + // do nothing + } + + /** + * + * + * @param e + */ + public void mouseUp(MouseEvent e) { + ISelection selection = treeViewer.getSelection(); + downButtonPressed(); + refresh(); + + // Keep selection + keepSelection(selection); + } + } + + /** + * Listener for the Down Button + * Specific behavior is implemented in {@link DecoratedTreeComposite#downButtonPressed()}. + * + * @author Remi SCHNEKENBURGER + */ + private class DownButtonlistener implements MouseListener { + + /** + * + * + * @param e + */ + public void mouseDoubleClick(MouseEvent e) { + // do nothing + } + + /** + * + * + * @param e + */ + public void mouseDown(MouseEvent e) { + // do nothing + } + + /** + * + * + * @param e + */ + public void mouseUp(MouseEvent e) { + ISelection vSelection = treeViewer.getSelection(); + + upButtonPressed(); + refresh(); + // Keep selection + keepSelection(vSelection); + + } + } + + /** + * + */ + private class EditItemListener implements Listener { + + /** + * + * + * @param event + */ + public void handleEvent(Event event) { + if(tree.getSelection().length > 0) { + TreeItem item = tree.getSelection()[0]; + editItem(item); + } + } + } + + /** + * Dipose listeners. + */ + public void disposeListeners() { + if(addButton != null && !addButton.isDisposed()) + addButton.removeMouseListener(addButtonlistener); + if(removeButton != null && !removeButton.isDisposed()) + removeButton.removeMouseListener(removeButtonlistener); + if(upButton != null && !upButton.isDisposed()) + upButton.removeMouseListener(upButtonlistener); + if(downButton != null && !downButton.isDisposed()) + downButton.removeMouseListener(downButtonlistener); + if(tree != null && !tree.isDisposed()) + tree.removeListener(SWT.MouseDoubleClick, treeListener); + } +} |