svn commit: r16201 - trunk/src/argouml-app: src/org/argouml/kernel src/org/argouml/uml/diagram src/org/argouml/uml/diagram/ui tests/org/argouml/uml/ui

[email protected]
Newsgroups gmane.comp.lang.uml.argouml.cvs
Message-ID <[email protected]>
Author: tfmorris
Date: 2008-11-28 13:41:00-0800
New Revision: 16201

Modified:
   trunk/src/argouml-app/src/org/argouml/kernel/ProjectImpl.java
   trunk/src/argouml-app/src/org/argouml/uml/diagram/ArgoDiagramImpl.java
   trunk/src/argouml-app/src/org/argouml/uml/diagram/ui/DiagramNameDocument.java
   trunk/src/argouml-app/src/org/argouml/uml/diagram/ui/UMLDiagram.java
   trunk/src/argouml-app/tests/org/argouml/uml/ui/AbstractTestActionAddDiagram.java
   trunk/src/argouml-app/tests/org/argouml/uml/ui/TestActionActivityGraphDiagram.java
   trunk/src/argouml-app/tests/org/argouml/uml/ui/TestActionCollaborationDiagram.java
   trunk/src/argouml-app/tests/org/argouml/uml/ui/TestActionStateDiagram.java

Log:
RESOLVED - Issue 5534: Move duplicate diagram name management from diagram to project
http://argouml.tigris.org/issues/show_bug.cgi?id=5534

Modified: trunk/src/argouml-app/src/org/argouml/kernel/ProjectImpl.java
Url: http://argouml.tigris.org/source/browse/argouml/trunk/src/argouml-app/src/org/argouml/kernel/ProjectImpl.java?view=diff&rev=16201&p1=trunk/src/argouml-app/src/org/argouml/kernel/ProjectImpl.java&p2=trunk/src/argouml-app/src/org/argouml/kernel/ProjectImpl.java&r1=16200&r2=16201
==============================================================================
--- trunk/src/argouml-app/src/org/argouml/kernel/ProjectImpl.java	(original)
+++ trunk/src/argouml-app/src/org/argouml/kernel/ProjectImpl.java	2008-11-28 13:41:00-0800
@@ -26,10 +26,10 @@
 
 import java.beans.PropertyChangeEvent;
 import java.beans.PropertyChangeListener;
+import java.beans.PropertyVetoException;
 import java.beans.VetoableChangeSupport;
 import java.io.File;
 import java.net.URI;
-import java.net.URISyntaxException;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
@@ -48,7 +48,6 @@
 import org.argouml.i18n.Translator;
 import org.argouml.model.InvalidElementException;
 import org.argouml.model.Model;
-import org.argouml.persistence.PersistenceManager;
 import org.argouml.profile.Profile;
 import org.argouml.profile.ProfileException;
 import org.argouml.profile.ProfileFacade;
@@ -253,6 +252,15 @@
      * @param d the diagram
      */
     private void addDiagramMember(ArgoDiagram d) {
+        // Check for duplicate name and rename if necessary
+        int serial = getDiagramCount();
+        while (!isValidDiagramName(d.getName())) {
+            try {
+                d.setName(d.getName() + " " + serial);
+            } catch (PropertyVetoException e) {
+                serial++;            
+            }
+        }
         ProjectMember pm = new ProjectMemberDiagram(d, this);
         addDiagram(d);
         // if diagram added successfully, add the member too

Modified: trunk/src/argouml-app/src/org/argouml/uml/diagram/ArgoDiagramImpl.java
Url: http://argouml.tigris.org/source/browse/argouml/trunk/src/argouml-app/src/org/argouml/uml/diagram/ArgoDiagramImpl.java?view=diff&rev=16201&p1=trunk/src/argouml-app/src/org/argouml/uml/diagram/ArgoDiagramImpl.java&p2=trunk/src/argouml-app/src/org/argouml/uml/diagram/ArgoDiagramImpl.java&r1=16200&r2=16201
==============================================================================
--- trunk/src/argouml-app/src/org/argouml/uml/diagram/ArgoDiagramImpl.java	(original)
+++ trunk/src/argouml-app/src/org/argouml/uml/diagram/ArgoDiagramImpl.java	2008-11-28 13:41:00-0800
@@ -27,6 +27,7 @@
 import java.beans.PropertyChangeEvent;
 import java.beans.PropertyChangeListener;
 import java.beans.PropertyVetoException;
+import java.beans.VetoableChangeListener;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
@@ -86,7 +87,8 @@
  * the namespace of its statemachine.
  */
 public abstract class ArgoDiagramImpl extends Diagram 
-    implements PropertyChangeListener, ArgoDiagram, IItemUID {
+    implements PropertyChangeListener, VetoableChangeListener, ArgoDiagram, 
+    IItemUID {
 
     private ItemUID id;
 
@@ -184,6 +186,9 @@
         ArgoEventPump.addListener(ArgoEventTypes.ANY_NOTATION_EVENT, this);
         ArgoEventPump.addListener(
                 ArgoEventTypes.ANY_DIAGRAM_APPEARANCE_EVENT, this);
+
+        // Listen for name changes so we can veto them if we don't like them
+        addVetoableChangeListener(this);
     }
     
 
@@ -664,5 +669,28 @@
     public void notationRemoved(ArgoNotationEvent e) {
         // Do nothing
     }
+
+
+    /**
+     * Receive vetoable change event. GEF will call this method with the 'name'
+     * property when it attempts to set the name. If this will be a duplicate
+     * for the project, we can veto the requested change.
+     * 
+     * @param evt the change event
+     * @throws PropertyVetoException if the name is illegal. Usuallly this means
+     *             a duplicate in the project.
+     * @see java.beans.VetoableChangeListener#vetoableChange(java.beans.PropertyChangeEvent)
+     */
+    public void vetoableChange(PropertyChangeEvent evt)
+        throws PropertyVetoException {
+        
+        if ("name".equals(evt.getPropertyName())) {
+            if (project != null) {
+                if (!project.isValidDiagramName((String) evt.getNewValue())) {
+                    throw new PropertyVetoException("Invalid name", evt);
+                }
+            }
+        }    
+    }
     
 }

Modified: trunk/src/argouml-app/src/org/argouml/uml/diagram/ui/DiagramNameDocument.java
Url: http://argouml.tigris.org/source/browse/argouml/trunk/src/argouml-app/src/org/argouml/uml/diagram/ui/DiagramNameDocument.java?view=diff&rev=16201&p1=trunk/src/argouml-app/src/org/argouml/uml/diagram/ui/DiagramNameDocument.java&p2=trunk/src/argouml-app/src/org/argouml/uml/diagram/ui/DiagramNameDocument.java&r1=16200&r2=16201
==============================================================================
--- trunk/src/argouml-app/src/org/argouml/uml/diagram/ui/DiagramNameDocument.java	(original)
+++ trunk/src/argouml-app/src/org/argouml/uml/diagram/ui/DiagramNameDocument.java	2008-11-28 13:41:00-0800
@@ -30,6 +30,7 @@
 import javax.swing.event.DocumentEvent;
 import javax.swing.event.DocumentListener;
 import javax.swing.text.BadLocationException;
+import javax.swing.text.DefaultHighlighter;
 
 import org.apache.log4j.Logger;
 import org.argouml.ui.targetmanager.TargetEvent;
@@ -56,8 +57,11 @@
     private JTextField field;
     private boolean stopEvents = false;
 
+    private Object highlightTag = null;
+
     /**
      * The constructor.
+     * @param theField the input text field
      */
     public DiagramNameDocument(JTextField theField) {
         field = theField;
@@ -143,9 +147,22 @@
                     /* Prevent triggering too many events by setName(). */
                     if (!oldName.equals(newName)) {
                         d.setName(newName);
+                        if (highlightTag != null) {
+                            field.getHighlighter()
+                                    .removeHighlight(highlightTag);
+                            highlightTag = null;
+                        }
                     }
                 } catch (PropertyVetoException pe) {
-                    LOG.debug(pe);
+                    // Provide feedback to the user that their name was
+                    // not accepted
+                    try {
+                        highlightTag  = field.getHighlighter().addHighlight(0, 
+                                field.getText().length(), 
+                                DefaultHighlighter.DefaultPainter);
+                    } catch (BadLocationException e1) {
+                        LOG.debug("Nested exception", e1);
+                    }
                 } catch (BadLocationException ble) {
                     LOG.debug(ble);
                 }

Modified: trunk/src/argouml-app/src/org/argouml/uml/diagram/ui/UMLDiagram.java
Url: http://argouml.tigris.org/source/browse/argouml/trunk/src/argouml-app/src/org/argouml/uml/diagram/ui/UMLDiagram.java?view=diff&rev=16201&p1=trunk/src/argouml-app/src/org/argouml/uml/diagram/ui/UMLDiagram.java&p2=trunk/src/argouml-app/src/org/argouml/uml/diagram/ui/UMLDiagram.java&r1=16200&r2=16201
==============================================================================
--- trunk/src/argouml-app/src/org/argouml/uml/diagram/ui/UMLDiagram.java	(original)
+++ trunk/src/argouml-app/src/org/argouml/uml/diagram/ui/UMLDiagram.java	2008-11-28 13:41:00-0800
@@ -42,7 +42,6 @@
 import org.argouml.gefext.ArgoModeCreateFigSpline;
 import org.argouml.i18n.Translator;
 import org.argouml.kernel.Project;
-import org.argouml.kernel.ProjectManager;
 import org.argouml.model.Model;
 import org.argouml.ui.CmdCreateNode;
 import org.argouml.uml.UUIDHelper;
@@ -98,15 +97,6 @@
     private static final Logger LOG = Logger.getLogger(UMLDiagram.class);
 
     /**
-     * The serial number for new diagrams.
-     * Used to create an unique number for the name of the diagram.
-     * <p>
-     * TODO: How is this going to work if it's not static and this isn't a 
-     * singleton class?
-     */
-    private int diagramSerial = 1;
-
-    /**
      * Tool to add a comment node.
      */
     private static Action actionComment =
@@ -561,18 +551,22 @@
     }
 
     /**
-     * Reset the diagram serial counter to the initial value.
-     * This should e.g. be done when the menuitem File->New is activated.
+     * Reset the diagram serial counter to the initial value. This should e.g.
+     * be done when the menuitem File->New is activated.
+     * 
+     * @deprecated for 0.27.3 by tfmorris. This is a noop. Diagram name
+     *             duplication is checked for and managed at the project level.
      */
     public void resetDiagramSerial() {
-        diagramSerial = 1;
     }
 
     /**
      * @return Returns the diagramSerial.
+     * @deprecated for 0.27.3 by tfmorris. This is always returns 1. Diagram
+     *             naming is managed at the project level.
      */
     protected int getNextDiagramSerial() {
-        return diagramSerial++;
+        return 1;
     }
 
     /**
@@ -602,19 +596,13 @@
      * @return String
      */
     protected String getNewDiagramName() {
-        String name = getLabelName() + " " + getNextDiagramSerial();
-        Project project = getProject();
-        // If this gets called from the constructor the project
-        // won't be set yet, so we'll allow anything
-        if (project != null && !project.isValidDiagramName(name)) {
-            name = getNewDiagramName();
-        }
-        return name;
+        // TODO: Add "unnamed" or "new" or something? (Localized, of course)
+        return /*"unnamed " + */ getLabelName();
     }
 
     /**
      * Method to test it the diagram can accept a certain object.
-     * This should be overriden by any diagram that wants to accept a certain
+     * This should be overridden by any diagram that wants to accept a certain
      * type of object. All other diagrams should not bother since the default
      * answer is false, ie. don't accept the object.
      * @param objectToAccept The object which acceptability will be checked.

Modified: trunk/src/argouml-app/tests/org/argouml/uml/ui/AbstractTestActionAddDiagram.java
Url: http://argouml.tigris.org/source/browse/argouml/trunk/src/argouml-app/tests/org/argouml/uml/ui/AbstractTestActionAddDiagram.java?view=diff&rev=16201&p1=trunk/src/argouml-app/tests/org/argouml/uml/ui/AbstractTestActionAddDiagram.java&p2=trunk/src/argouml-app/tests/org/argouml/uml/ui/AbstractTestActionAddDiagram.java&r1=16200&r2=16201
==============================================================================
--- trunk/src/argouml-app/tests/org/argouml/uml/ui/AbstractTestActionAddDiagram.java	(original)
+++ trunk/src/argouml-app/tests/org/argouml/uml/ui/AbstractTestActionAddDiagram.java	2008-11-28 13:41:00-0800
@@ -28,9 +28,8 @@
 import java.util.List;
 
 import junit.framework.TestCase;
-import org.argouml.model.InitializeModel;
 
-import org.argouml.kernel.ProjectManager;
+import org.argouml.model.InitializeModel;
 import org.argouml.model.Model;
 import org.argouml.notation.InitNotation;
 import org.argouml.notation.providers.java.InitNotationJava;
@@ -156,22 +155,6 @@
         	   action.isValidNamespace(diagram.getNamespace()));
     }
 
-    /**
-     * Tests if two diagrams created have different names.
-     */
-    public void testDifferentNames() {
-	ArgoDiagram diagram1 = action.createDiagram(ns);
-	// This next line is needed to register the diagram in the project,
-        // since creating a next diagram will need the new name to be compared
-        // with existing diagrams in the project, to validate
-        // there are no duplicates.
-	ProjectManager.getManager().getCurrentProject().addMember(diagram1);
-        ArgoDiagram diagram2 = action.createDiagram(ns);
-        Model.getPump().flushModelEvents();
-	assertTrue(
-		   "The created diagrams have the same name",
-		   !(diagram1.getName().equals(diagram2.getName())));
-    }
 
     /**
      * Tests if the namespace created by getNamespace() is a valid namespace for

Modified: trunk/src/argouml-app/tests/org/argouml/uml/ui/TestActionActivityGraphDiagram.java
Url: http://argouml.tigris.org/source/browse/argouml/trunk/src/argouml-app/tests/org/argouml/uml/ui/TestActionActivityGraphDiagram.java?view=diff&rev=16201&p1=trunk/src/argouml-app/tests/org/argouml/uml/ui/TestActionActivityGraphDiagram.java&p2=trunk/src/argouml-app/tests/org/argouml/uml/ui/TestActionActivityGraphDiagram.java&r1=16200&r2=16201
==============================================================================
--- trunk/src/argouml-app/tests/org/argouml/uml/ui/TestActionActivityGraphDiagram.java	(original)
+++ trunk/src/argouml-app/tests/org/argouml/uml/ui/TestActionActivityGraphDiagram.java	2008-11-28 13:41:00-0800
@@ -28,7 +28,6 @@
 
 import junit.framework.TestCase;
 
-import org.argouml.kernel.ProjectManager;
 import org.argouml.model.InitializeModel;
 import org.argouml.model.Model;
 import org.argouml.profile.init.InitProfileSubsystem;
@@ -110,32 +109,7 @@
         assertTrue("All elements of diagram not deleted", leftovers.isEmpty());
     }
 
-    /**
-     * Tests if two diagrams created have different names.
-     */
-    public void testDifferentNames() {
-        action.actionPerformed(null);
-        Object d = TargetManager.getInstance().getTarget();
-        assertTrue("No diagram generated", d instanceof ArgoDiagram);
-        Model.getPump().flushModelEvents();
-        ArgoDiagram diagram1 = (ArgoDiagram) d;
-        // This next line is needed to register the diagram in the project,
-        // since creating a next diagram will need the new name to be compared
-        // with existing diagrams in the project, to validate
-        // there are no duplicates.
-        ProjectManager.getManager().getCurrentProject().addMember(diagram1);
 
-        TargetManager.getInstance().setTarget(ns);
-        action.actionPerformed(null);
-        d = TargetManager.getInstance().getTarget();
-        assertTrue("No diagram generated", d instanceof ArgoDiagram);
-        Model.getPump().flushModelEvents();
-        ArgoDiagram diagram2 = (ArgoDiagram) d;
-
-        Model.getPump().flushModelEvents();
-        assertFalse("The created diagrams have the same name",
-                   diagram1.getName().equals(diagram2.getName()));
-    }
 
 
 }

Modified: trunk/src/argouml-app/tests/org/argouml/uml/ui/TestActionCollaborationDiagram.java
Url: http://argouml.tigris.org/source/browse/argouml/trunk/src/argouml-app/tests/org/argouml/uml/ui/TestActionCollaborationDiagram.java?view=diff&rev=16201&p1=trunk/src/argouml-app/tests/org/argouml/uml/ui/TestActionCollaborationDiagram.java&p2=trunk/src/argouml-app/tests/org/argouml/uml/ui/TestActionCollaborationDiagram.java&r1=16200&r2=16201
==============================================================================
--- trunk/src/argouml-app/tests/org/argouml/uml/ui/TestActionCollaborationDiagram.java	(original)
+++ trunk/src/argouml-app/tests/org/argouml/uml/ui/TestActionCollaborationDiagram.java	2008-11-28 13:41:00-0800
@@ -29,9 +29,8 @@
 import java.util.List;
 
 import junit.framework.TestCase;
-import org.argouml.model.InitializeModel;
 
-import org.argouml.kernel.ProjectManager;
+import org.argouml.model.InitializeModel;
 import org.argouml.model.Model;
 import org.argouml.profile.init.InitProfileSubsystem;
 import org.argouml.ui.targetmanager.TargetManager;
@@ -145,33 +144,6 @@
         assertNotNull("The diagram has no name", diagram.getName());
     }
 
-    /**
-     * Tests if two diagrams created have different names.
-     */
-    public void testDifferentNames() {
-        action.actionPerformed(null);
-        Object d = TargetManager.getInstance().getTarget();
-        assertTrue("No diagram generated", d instanceof ArgoDiagram);
-        Model.getPump().flushModelEvents();
-        ArgoDiagram diagram1 = (ArgoDiagram) d;
-        // This next line is needed to register the diagram in the project,
-        // since creating a next diagram will need the new name to be compared
-        // with existing diagrams in the project, to validate
-        // there are no duplicates.
-        ProjectManager.getManager().getCurrentProject().addMember(diagram1);
-
-        TargetManager.getInstance().setTarget(ns);
-        action.actionPerformed(null);
-        d = TargetManager.getInstance().getTarget();
-        assertTrue("No diagram generated", d instanceof ArgoDiagram);
-        Model.getPump().flushModelEvents();
-        ArgoDiagram diagram2 = (ArgoDiagram) d;
-
-        Model.getPump().flushModelEvents();
-        assertTrue(
-                   "The created diagrams have the same name",
-                   !(diagram1.getName().equals(diagram2.getName())));
-    }
 
     /**
      * Tests if the list with namespaces defined in getValidNamespaceClasses

Modified: trunk/src/argouml-app/tests/org/argouml/uml/ui/TestActionStateDiagram.java
Url: http://argouml.tigris.org/source/browse/argouml/trunk/src/argouml-app/tests/org/argouml/uml/ui/TestActionStateDiagram.java?view=diff&rev=16201&p1=trunk/src/argouml-app/tests/org/argouml/uml/ui/TestActionStateDiagram.java&p2=trunk/src/argouml-app/tests/org/argouml/uml/ui/TestActionStateDiagram.java&r1=16200&r2=16201
==============================================================================
--- trunk/src/argouml-app/tests/org/argouml/uml/ui/TestActionStateDiagram.java	(original)
+++ trunk/src/argouml-app/tests/org/argouml/uml/ui/TestActionStateDiagram.java	2008-11-28 13:41:00-0800
@@ -29,9 +29,8 @@
 import java.util.List;
 
 import junit.framework.TestCase;
-import org.argouml.model.InitializeModel;
 
-import org.argouml.kernel.ProjectManager;
+import org.argouml.model.InitializeModel;
 import org.argouml.model.Model;
 import org.argouml.profile.init.InitProfileSubsystem;
 import org.argouml.ui.targetmanager.TargetManager;
@@ -143,33 +142,6 @@
         assertNotNull("The diagram has no name", diagram.getName());
     }
 
-    /**
-     * Tests if two diagrams created have different names.
-     */
-    public void testDifferentNames() {
-        action.actionPerformed(null);
-        Object d = TargetManager.getInstance().getTarget();
-        assertTrue("No diagram generated", d instanceof ArgoDiagram);
-        Model.getPump().flushModelEvents();
-        ArgoDiagram diagram1 = (ArgoDiagram) d;
-        // This next line is needed to register the diagram in the project,
-        // since creating a next diagram will need the new name to be compared
-        // with existing diagrams in the project, to validate
-        // there are no duplicates.
-        ProjectManager.getManager().getCurrentProject().addMember(diagram1);
-
-        TargetManager.getInstance().setTarget(ns);
-        action.actionPerformed(null);
-        d = TargetManager.getInstance().getTarget();
-        assertTrue("No diagram generated", d instanceof ArgoDiagram);
-        Model.getPump().flushModelEvents();
-        ArgoDiagram diagram2 = (ArgoDiagram) d;
-
-        Model.getPump().flushModelEvents();
-        assertTrue(
-                   "The created diagrams have the same name",
-                   !(diagram1.getName().equals(diagram2.getName())));
-    }
 
     /**
      * Tests if the list with namespaces defined in getValidNamespaceClasses
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.