svn commit: r17513 - trunk/src/argouml-app/src/org/argouml/uml: . diagram/ui

Thomas Neustupny <[email protected]>
Newsgroups gmane.comp.lang.uml.argouml.cvs
Message-ID <[email protected]>
Author: thn
Date: 2009-11-21 10:06:02-0800
New Revision: 17513

Modified:
   trunk/src/argouml-app/src/org/argouml/uml/ActionAddStereotype.java
   trunk/src/argouml-app/src/org/argouml/uml/StereotypeUtility.java
   trunk/src/argouml-app/src/org/argouml/uml/diagram/ui/FigEdgeModelElement.java
   trunk/src/argouml-app/src/org/argouml/uml/diagram/ui/FigNodeModelElement.java

Log:
(un)apply stereotypes to a multiselection of figs
(feature for Bob ;-) )

Modified: trunk/src/argouml-app/src/org/argouml/uml/ActionAddStereotype.java
Url: http://argouml.tigris.org/source/browse/argouml/trunk/src/argouml-app/src/org/argouml/uml/ActionAddStereotype.java?view=diff&pathrev=17513&r1=17512&r2=17513
==============================================================================
--- trunk/src/argouml-app/src/org/argouml/uml/ActionAddStereotype.java	(original)
+++ trunk/src/argouml-app/src/org/argouml/uml/ActionAddStereotype.java	2009-11-21 10:06:02-0800
@@ -25,6 +25,8 @@
 package org.argouml.uml;
 
 import java.awt.event.ActionEvent;
+import java.util.ArrayList;
+import java.util.Collection;
 
 import javax.swing.Action;
 
@@ -37,51 +39,82 @@
 import org.argouml.notation.providers.uml.NotationUtilityUml;
 import org.tigris.gef.undo.UndoableAction;
 
-
 /**
- * Action to add a sterotype to a model element.
+ * Action to add a sterotype to a collection of model elements.
+ * 
  * @author Bob Tarling
  */
 @UmlModelMutator
 public class ActionAddStereotype extends UndoableAction {
-    
-    private Object modelElement;
+
+    private Collection<Object> modelElements;
+
     private Object stereotype;
 
     /**
      * Constructor.
-     *
+     * 
      * @param me The model element.
      * @param st The stereotype.
      */
     public ActionAddStereotype(Object me, Object st) {
-        super(Translator.localize(buildString(st)),
-                null);
+        super(Translator.localize(buildString(st)), null);
+        // Set the tooltip string:
+        putValue(Action.SHORT_DESCRIPTION, Translator.localize(buildString(st)));
+        modelElements = new ArrayList<Object>();
+        if (me != null) {
+            modelElements.add(me);
+        }
+        stereotype = st;
+    }
+
+    /**
+     * Constructor.
+     * 
+     * @param coll The collection of model elements.
+     * @param st The stereotype.
+     */
+    public ActionAddStereotype(Collection<Object> coll, Object st) {
+        super(Translator.localize(buildString(st)), null);
         // Set the tooltip string:
-        putValue(Action.SHORT_DESCRIPTION, 
-                Translator.localize(buildString(st)));
-        modelElement = me;
+        putValue(Action.SHORT_DESCRIPTION, Translator.localize(buildString(st)));
+        modelElements = new ArrayList<Object>();
+        if (coll != null) {
+            modelElements.addAll(coll);
+        }
         stereotype = st;
     }
-    
+
     private static String buildString(Object st) {
         Project p = ProjectManager.getManager().getCurrentProject();
         ProjectSettings ps = p.getProjectSettings();
-        return NotationUtilityUml.generateStereotype(st, 
-                ps.getNotationSettings().isUseGuillemets());
+        return NotationUtilityUml.generateStereotype(st, ps
+                .getNotationSettings().isUseGuillemets());
     }
 
     /*
-     * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
+     * @see
+     * java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
      */
     @Override
     public void actionPerformed(ActionEvent ae) {
-    	super.actionPerformed(ae);
-        if (Model.getFacade().getStereotypes(modelElement)
-                .contains(stereotype)) {
-            Model.getCoreHelper().removeStereotype(modelElement, stereotype);
-        } else {
-            Model.getCoreHelper().addStereotype(modelElement, stereotype);
+        super.actionPerformed(ae);
+        boolean isContained = false;
+        if (!modelElements.isEmpty()) {
+            isContained = Model.getFacade().getStereotypes(
+                    modelElements.iterator().next()).contains(stereotype);
+        }
+        for (Object modelElement : modelElements) {
+            if (isContained
+                    && Model.getFacade().getStereotypes(modelElement).contains(
+                            stereotype)) {
+                Model.getCoreHelper()
+                        .removeStereotype(modelElement, stereotype);
+            } else if (!isContained
+                    && !Model.getFacade().getStereotypes(modelElement)
+                            .contains(stereotype)) {
+                Model.getCoreHelper().addStereotype(modelElement, stereotype);
+            }
         }
     }
 
@@ -91,6 +124,10 @@
     @Override
     public Object getValue(String key) {
         if ("SELECTED".equals(key)) {
+            if (modelElements.isEmpty()) {
+                return Boolean.FALSE;
+            }
+            Object modelElement = modelElements.iterator().next();
             if (Model.getFacade().getStereotypes(modelElement).contains(
                     stereotype)) {
                 return Boolean.TRUE;

Modified: trunk/src/argouml-app/src/org/argouml/uml/StereotypeUtility.java
Url: http://argouml.tigris.org/source/browse/argouml/trunk/src/argouml-app/src/org/argouml/uml/StereotypeUtility.java?view=diff&pathrev=17513&r1=17512&r2=17513
==============================================================================
--- trunk/src/argouml-app/src/org/argouml/uml/StereotypeUtility.java	(original)
+++ trunk/src/argouml-app/src/org/argouml/uml/StereotypeUtility.java	2009-11-21 10:06:02-0800
@@ -55,6 +55,13 @@
         super();
     }
 
+    /**
+     * Returns an array of all applicable actions for adding stereotypes
+     * for a given model element.
+     * 
+     * @param modelElement the given model element
+     * @return the array with actions for adding stereotype UML objects
+     */
     public static Action[] getApplyStereotypeActions(Object modelElement) {
         Set availableStereotypes = getAvailableStereotypes(modelElement);
         
@@ -72,6 +79,29 @@
     }
 
     /**
+     * Returns an array of all applicable actions for adding stereotypes
+     * for a given collection of model elements.
+     * 
+     * @param elements the given collection of model elements
+     * @return the array with actions for adding stereotype UML objects
+     */
+    public static Action[] getApplyStereotypeActions(Collection elements) {
+        Set availableStereotypes = getAvailableStereotypes(elements);
+        
+        if (!availableStereotypes.isEmpty()) {
+            Action[] menuActions = new Action[availableStereotypes.size()];
+
+            Iterator it = availableStereotypes.iterator();
+            for (int i = 0; it.hasNext(); ++i) {
+                menuActions[i] = new ActionAddStereotype(elements, 
+                        it.next());
+            }
+            return menuActions;
+        }
+        return new Action[0];
+    }
+
+    /**
      * Returns a set of all unique applicable stereotypes 
      * for a given modelelement.
      * 
@@ -133,6 +163,25 @@
         return availableStereotypes;
     }
 
+    /**
+     * Returns a set (union) of all unique applicable stereotypes 
+     * for a given collection of model elements.
+     * TODO: This is not optimized for performance.
+     * 
+     * @param elements the given collection of model elements
+     * @return the set with stereotype UML objects
+     */
+    public static Set<Object> getAvailableStereotypes(Collection elements) {
+        Set<Object> availableStereotypes = 
+            new TreeSet<Object>(new PathComparator());
+        if (elements != null) {
+            for (Object element : elements) {
+                availableStereotypes.addAll(getAvailableStereotypes(element));
+            }
+        }
+        return availableStereotypes;
+    }
+
     private static Collection<Object> getTopLevelStereotypes(
             Collection<Object> topLevelModels) {
         Collection<Object> ret = new ArrayList<Object>();

Modified: trunk/src/argouml-app/src/org/argouml/uml/diagram/ui/FigEdgeModelElement.java
Url: http://argouml.tigris.org/source/browse/argouml/trunk/src/argouml-app/src/org/argouml/uml/diagram/ui/FigEdgeModelElement.java?view=diff&pathrev=17513&r1=17512&r2=17513
==============================================================================
--- trunk/src/argouml-app/src/org/argouml/uml/diagram/ui/FigEdgeModelElement.java	(original)
+++ trunk/src/argouml-app/src/org/argouml/uml/diagram/ui/FigEdgeModelElement.java	2009-11-21 10:06:02-0800
@@ -36,6 +36,8 @@
 import java.beans.PropertyChangeEvent;
 import java.beans.PropertyChangeListener;
 import java.beans.VetoableChangeListener;
+import java.util.ArrayList;
+import java.util.Collection;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
@@ -351,19 +353,20 @@
                 popUpActions.add(0, new JSeparator());
                 popUpActions.add(0, critiques);
             }
+        }
 
-            // Add stereotypes submenu
-            Action[] stereoActions = getApplyStereotypeActions();
-            if (stereoActions != null && stereoActions.length > 0) {
-                popUpActions.add(0, new JSeparator());
-                ArgoJMenu stereotypes = new ArgoJMenu(
-                        "menu.popup.apply-stereotypes");
-                for (int i = 0; i < stereoActions.length; ++i) {
-                    stereotypes.addCheckItem(stereoActions[i]);
-                }
-                popUpActions.add(0, stereotypes);
+        // Add stereotypes submenu
+        Action[] stereoActions = getApplyStereotypeActions();
+        if (stereoActions != null && stereoActions.length > 0) {
+            popUpActions.add(0, new JSeparator());
+            ArgoJMenu stereotypes = new ArgoJMenu(
+                    "menu.popup.apply-stereotypes");
+            for (int i = 0; i < stereoActions.length; ++i) {
+                stereotypes.addCheckItem(stereoActions[i]);
             }
+            popUpActions.add(0, stereotypes);
         }
+
         return popUpActions;
     }
     
@@ -374,7 +377,23 @@
      * @return array of Actions 
      */
     protected Action[] getApplyStereotypeActions() {
-        return StereotypeUtility.getApplyStereotypeActions(getOwner());
+        Collection<Object> elements = new ArrayList<Object>();
+        Object owner = getOwner();
+        if (owner != null) {
+            elements.add(owner);
+        }
+        for (Object o : TargetManager.getInstance().getTargets()) {
+            Object element = null;
+            if (Model.getFacade().isAUMLElement(o)) {
+                element = o;
+            } else if (o instanceof Fig) {
+                element = ((Fig) o).getOwner();
+            }
+            if (element != null && element != owner) {
+                elements.add(element);
+            }
+        }
+        return StereotypeUtility.getApplyStereotypeActions(elements);
     }
 
     /**

Modified: trunk/src/argouml-app/src/org/argouml/uml/diagram/ui/FigNodeModelElement.java
Url: http://argouml.tigris.org/source/browse/argouml/trunk/src/argouml-app/src/org/argouml/uml/diagram/ui/FigNodeModelElement.java?view=diff&pathrev=17513&r1=17512&r2=17513
==============================================================================
--- trunk/src/argouml-app/src/org/argouml/uml/diagram/ui/FigNodeModelElement.java	(original)
+++ trunk/src/argouml-app/src/org/argouml/uml/diagram/ui/FigNodeModelElement.java	2009-11-21 10:06:02-0800
@@ -640,20 +640,38 @@
                 popUpActions.add(0, new JSeparator());
                 popUpActions.add(0, critiques);
             }
+        }
 
-            // Add stereotypes submenu
-            final Action[] stereoActions =
-                StereotypeUtility.getApplyStereotypeActions(getOwner());
-            if (stereoActions != null) {
-                popUpActions.add(0, new JSeparator());
-                final ArgoJMenu stereotypes =
-                    new ArgoJMenu("menu.popup.apply-stereotypes");
-                for (Action action : stereoActions) {
-                    stereotypes.addCheckItem(action);
-                }
-                popUpActions.add(0, stereotypes);
+        // Add stereotypes submenu
+        Collection<Object> elements = new ArrayList<Object>();
+        Object owner = getOwner();
+        if (owner != null) {
+            elements.add(owner);
+        }
+        for (Object o : TargetManager.getInstance().getTargets()) {
+            Object element = null;
+            if (Model.getFacade().isAUMLElement(o)) {
+                element = o;
+            } else if (o instanceof Fig) {
+                element = ((Fig) o).getOwner();
+            }
+            if (element != null && element != owner) {
+                elements.add(element);
+            }
+        }
+        final Action[] stereoActions =
+            StereotypeUtility.getApplyStereotypeActions(elements);
+        if (stereoActions != null) {
+            popUpActions.add(0, new JSeparator());
+            final ArgoJMenu stereotypes =
+                new ArgoJMenu("menu.popup.apply-stereotypes");
+            for (Action action : stereoActions) {
+                stereotypes.addCheckItem(action);
             }
+            popUpActions.add(0, stereotypes);
+        }
             
+        if (TargetManager.getInstance().getTargets().size() == 1) {
             // add stereotype view submenu
             ArgoJMenu stereotypesView =
                 new ArgoJMenu("menu.popup.stereotype-view");

------------------------------------------------------
http://argouml.tigris.org/ds/viewMessage.do?dsForumId=5905&dsMessageId=2422844

To unsubscribe from this discussion, e-mail: [[email protected]].
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.