svn commit: r18856 - trunk/src/argouml-core-diagrams-activity2/src/org/argouml/activity2/diagram

[email protected]
Newsgroups gmane.comp.lang.uml.argouml.cvs
Message-ID <[email protected]>
Author: bobtarling
Date: 2010-11-21 18:32:46-0800
New Revision: 18856

Added:
   trunk/src/argouml-core-diagrams-activity2/src/org/argouml/activity2/diagram/CreateDiagramElementAction.java
   trunk/src/argouml-core-diagrams-activity2/src/org/argouml/activity2/diagram/ModePlaceDiagramElement.java
Modified:
   trunk/src/argouml-core-diagrams-activity2/src/org/argouml/activity2/diagram/UMLActivityDiagram.java
   trunk/src/argouml-core-diagrams-activity2/src/org/argouml/activity2/diagram/diagram.xml

Log:
Begin work to interpret diagram xml

Added: trunk/src/argouml-core-diagrams-activity2/src/org/argouml/activity2/diagram/CreateDiagramElementAction.java
Url: http://argouml.tigris.org/source/browse/argouml/trunk/src/argouml-core-diagrams-activity2/src/org/argouml/activity2/diagram/CreateDiagramElementAction.java?view=markup&pathrev=18856
==============================================================================
--- (empty file)
+++ trunk/src/argouml-core-diagrams-activity2/src/org/argouml/activity2/diagram/CreateDiagramElementAction.java	2010-11-21 18:32:46-0800
@@ -0,0 +1,40 @@
+/* $Id: $
+ *****************************************************************************
+ * Copyright (c) 2010 Contributors - see below
+ * 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:
+ *    Bob Tarling
+ *****************************************************************************
+ */
+
+package org.argouml.activity2.diagram;
+
+import java.awt.event.ActionEvent;
+import org.argouml.application.helpers.ResourceLoaderWrapper;
+import org.argouml.ui.UndoableAction;
+import org.tigris.gef.base.Globals;
+import org.tigris.gef.base.Mode;
+
+class CreateDiagramElementAction extends UndoableAction {
+    
+    final Object metaType;
+    final String style;
+
+    CreateDiagramElementAction(Object metaType, String style, String name) {
+        super(name, ResourceLoaderWrapper.lookupIconResource(
+                ResourceLoaderWrapper.getImageBinding(name)));
+        this.metaType = metaType;
+        this.style = style;
+    }
+    
+    public void actionPerformed(ActionEvent e) {
+        super.actionPerformed(e);
+
+        Mode placeMode = new ModePlaceDiagramElement(metaType, style, "Click to place diagram element");
+        Globals.mode(placeMode, false);
+    }
+}

Added: trunk/src/argouml-core-diagrams-activity2/src/org/argouml/activity2/diagram/ModePlaceDiagramElement.java
Url: http://argouml.tigris.org/source/browse/argouml/trunk/src/argouml-core-diagrams-activity2/src/org/argouml/activity2/diagram/ModePlaceDiagramElement.java?view=markup&pathrev=18856
==============================================================================
--- (empty file)
+++ trunk/src/argouml-core-diagrams-activity2/src/org/argouml/activity2/diagram/ModePlaceDiagramElement.java	2010-11-21 18:32:46-0800
@@ -0,0 +1,142 @@
+package org.argouml.activity2.diagram;
+
+import java.awt.Cursor;
+import java.awt.Graphics;
+import java.awt.Point;
+import java.awt.Rectangle;
+import java.awt.event.KeyEvent;
+import java.awt.event.MouseEvent;
+import java.util.Iterator;
+import java.util.List;
+
+import org.argouml.model.Model;
+import org.argouml.uml.diagram.DiagramSettings;
+import org.tigris.gef.base.FigModifyingModeImpl;
+import org.tigris.gef.base.Globals;
+import org.tigris.gef.base.Layer;
+import org.tigris.gef.di.GraphNode;
+import org.tigris.gef.graph.GraphModel;
+import org.tigris.gef.graph.GraphNodeHooks;
+import org.tigris.gef.graph.GraphNodeRenderer;
+import org.tigris.gef.graph.MutableGraphModel;
+import org.tigris.gef.presentation.Fig;
+import org.tigris.gef.presentation.FigNode;
+import org.tigris.gef.undo.UndoManager;
+
+public class ModePlaceDiagramElement extends FigModifyingModeImpl {
+
+    private final Object metaType;
+    private final String style;
+    private final String instructions;
+    
+    private Object modelElement;
+    private GraphNode graphNode;
+
+    private static final int WIDTH = 90;
+    private static final int HEIGHT = 25;
+    
+    public ModePlaceDiagramElement(
+            Object metaType,
+            String style,
+            String instructions) {
+        this.metaType = metaType;
+        this.style = style;
+        if (instructions == null) {
+            this.instructions = "";
+        } else {
+            this.instructions = instructions;
+        }
+    }
+
+    public String instructions() {
+        return instructions;
+    }
+
+    public Cursor getInitialCursor() {
+        return Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR);
+    }
+
+    // //////////////////////////////////////////////////////////////
+    // event handlers
+
+    /** Move the perpective along with the mouse. */
+    public void mousePressed(MouseEvent me) {
+        if (me.isConsumed()) {
+            return;
+        }
+        UndoManager.getInstance().addMementoLock(this);
+        modelElement = Model.getUmlFactory().buildNode(metaType);
+        start();
+        editor = Globals.curEditor();
+        Layer lay = editor.getLayerManager().getActiveLayer();
+        graphNode = createDiagramElement(lay, modelElement, null);
+        mouseMoved(me);
+        me.consume();
+    }
+    
+    private GraphNode createDiagramElement(Layer lay, Object owner, DiagramSettings settings) {
+        FigBaseNode fig = new FigBaseNode(modelElement, new Rectangle(0, 0, 0, 0), settings);
+        fig.setLayer(lay);
+        DiagramElementBuilder.buildDiagramElement(fig, style, owner, settings);
+        return fig;
+    }
+
+    public void mouseExited(MouseEvent me) {
+        editor.damageAll();
+        me.consume();
+    }
+
+    public void mouseMoved(MouseEvent me) {
+        mouseDragged(me);
+    }
+
+    public void mouseDragged(MouseEvent me) {
+        if (me.isConsumed()) {
+            return;
+        }
+        if (graphNode == null) {
+            me.consume();
+            return;
+        }
+        editor.damageAll();
+        Point snapPt = new Point(me.getX(), me.getY());
+        editor.snap(snapPt);
+        ((Fig) graphNode).setLocation(snapPt.x, snapPt.y);
+        editor.damageAll();
+        me.consume();
+    }
+
+    public void mouseEntered(MouseEvent me) {
+        me.consume();
+    }
+
+    public void mouseReleased(MouseEvent me) {
+        if (me.isConsumed()) {
+            return;
+        }
+
+        MutableGraphModel mgm =
+            (MutableGraphModel) editor.getGraphModel();
+        if (mgm.canAddNode(modelElement)) {
+            UndoManager.getInstance().startChain();
+            editor.add((Fig) graphNode);
+            mgm.addNode(modelElement);
+
+            editor.getSelectionManager().select((Fig) graphNode);
+        }
+        done();
+        me.consume();
+    }
+
+    public void keyTyped(KeyEvent ke) {
+        if (ke.getKeyChar() == KeyEvent.VK_ESCAPE) {
+            leave();
+        }
+    }
+
+    public void paint(Graphics g) {
+        if (graphNode != null) {
+            ((Fig) graphNode).paint(g);
+        }
+    }
+}

Modified: trunk/src/argouml-core-diagrams-activity2/src/org/argouml/activity2/diagram/UMLActivityDiagram.java
Url: http://argouml.tigris.org/source/browse/argouml/trunk/src/argouml-core-diagrams-activity2/src/org/argouml/activity2/diagram/UMLActivityDiagram.java?view=diff&pathrev=18856&r1=18855&r2=18856
==============================================================================
--- trunk/src/argouml-core-diagrams-activity2/src/org/argouml/activity2/diagram/UMLActivityDiagram.java	(original)
+++ trunk/src/argouml-core-diagrams-activity2/src/org/argouml/activity2/diagram/UMLActivityDiagram.java	2010-11-21 18:32:46-0800
@@ -14,6 +14,16 @@
 package org.argouml.activity2.diagram;
 
 import java.awt.Rectangle;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
 
 import org.apache.log4j.Logger;
 import org.argouml.i18n.Translator;
@@ -25,6 +35,13 @@
 import org.argouml.uml.diagram.UmlDiagramRenderer;
 import org.argouml.uml.diagram.static_structure.ui.FigComment;
 import org.argouml.uml.diagram.ui.FigNodeModelElement;
+import org.w3c.dom.DOMException;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
 
 public class UMLActivityDiagram extends BaseDiagram implements ActivityDiagram {
     
@@ -72,6 +89,100 @@
             Model.getMetaTypes().getOutputPin(),
         };
     }
+    
+    private Map<String, Class<?>> metaTypeByName;
+    private Map<Class<?>, String> nameByMetaType;
+    private List<Object> getCreateDiagramElementActions() {
+        try {
+            final Document doc = getDocument();
+            final NodeList nl = doc.getDocumentElement().getChildNodes();
+            for (int i = 0; i < nl.getLength(); ++i) {
+                final Node n = nl.item(i);
+                if (n.getNodeName().equals("classes")) {
+                    final int size = n.getChildNodes().getLength();
+                    nameByMetaType = new HashMap<Class<?>, String>(size);
+                    metaTypeByName = new HashMap<String, Class<?>>(size);
+                    populateClassMaps((Element) n, nameByMetaType, metaTypeByName);
+                } else if (n.getNodeName().equals("toolbar")) {
+                    List<Object> actions = getToolbarActions((Element) n);
+                    return actions;
+                }
+            }
+        } catch (DOMException e) {
+            LOG.error("", e);
+        } catch (IOException e) {
+            LOG.error("", e);
+        } catch (ParserConfigurationException e) {
+            LOG.error("", e);
+        } catch (SAXException e) {
+            LOG.error("", e);
+        }
+        return null;
+    }
+
+    private List<Object> getToolbarActions(Element toolbarNode) {
+        List<Object> toolbarActions = new ArrayList<Object>();
+        final NodeList nl = toolbarNode.getElementsByTagName("*");
+        for (int i = 0; i < nl.getLength(); ++i) {
+            final Element itemNode = (Element) nl.item(i);
+            Object o;
+            String style = itemNode.getNodeName();
+            if (style.equals("dropdown")) {
+                o = getToolbarActions(itemNode);
+            } else {
+                String type = itemNode.getAttribute("type");
+                Class<?> metaType = metaTypeByName.get(type);
+                o = new CreateDiagramElementAction(
+                        metaType, 
+                        style, 
+                        Model.getMetaTypes().getName(metaType));
+            }
+            toolbarActions.add(o);
+        }
+        return toolbarActions;
+    }
+    
+    // TODO: This is currently duplicated from MetaDataCache - must find a
+    // common place in model facade
+    private void populateClassMaps(
+            final Element classesNode,
+            final Map<Class<?>, String> nameByMetaType,
+            final Map<String, Class<?>> metaTypeByName) {
+        final NodeList nl = classesNode.getElementsByTagName("class");
+        for (int i = 0; i < nl.getLength(); ++i) {
+            Node classNode = nl.item(i);
+            String className = classNode.getTextContent();
+            try {
+                final String name = 
+                    classNode.getAttributes().getNamedItem("name").getNodeValue();
+                Class<?> clazz = Class.forName(className);
+                metaTypeByName.put(name, clazz);
+                nameByMetaType.put(clazz, name);
+            } catch (ClassNotFoundException e) {
+                    LOG.error("Class not found " + className, e);
+            }
+        }
+    }
+    
+    
+    private Document getDocument() throws IOException, DOMException, ParserConfigurationException, SAXException {
+        final String filename;
+        filename = "org/argouml/activity2/diagram/diagram.xml";
+        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(filename);
+        InputSource inputSource = new InputSource(inputStream);
+        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+        DocumentBuilder db = dbf.newDocumentBuilder();
+        return db.parse(inputSource);
+    }
+    
+    @Override
+    protected Object[] getUmlActions() {
+        Object[] actions = super.getUmlActions();
+        getCreateDiagramElementActions();
+        return actions;
+    }
+    
+    
 
 
     @Override

Modified: trunk/src/argouml-core-diagrams-activity2/src/org/argouml/activity2/diagram/diagram.xml
Url: http://argouml.tigris.org/source/browse/argouml/trunk/src/argouml-core-diagrams-activity2/src/org/argouml/activity2/diagram/diagram.xml?view=diff&pathrev=18856&r1=18855&r2=18856
==============================================================================
--- trunk/src/argouml-core-diagrams-activity2/src/org/argouml/activity2/diagram/diagram.xml	(original)
+++ trunk/src/argouml-core-diagrams-activity2/src/org/argouml/activity2/diagram/diagram.xml	2010-11-21 18:32:46-0800
@@ -23,25 +23,42 @@
    The other element types indicates the representation used to display
    diagram element when it is created.
    
-   poly-edge - an edge two join two nodes, the edge can be multiple parts
+   poly-edge - an edge to join two nodes, the edge can be multiple parts
                (essentially an unconnected polygon)
    rect - a rectangular node
    rrect - a rectangular node with rounded corners
    pentagon - a pentagon that is formed by a combined rectangle and a triangle much
-               like a direction sign on a signpost. If the concave attribute
-               is set the triangle forms a notch into the rectangle.
-   connector - A connection point required for some nodes.
+               like a direction sign on a signpost.
+   concave-pentagon - similar to a pentagon but with the triangle forms a notch into
+                      the rectangle.
+   connector - A connection point required for some nodes to attach edges.
 -->
 <diagram name="Activity">
+<classes>
+  <class name="AcceptEventAction">org.eclipse.uml2.uml.AcceptEventAction</class>
+  <class name="ActivityParameterNode">org.eclipse.uml2.uml.ActivityParameterNode</class>
+  <class name="CallBehaviorAction">org.eclipse.uml2.uml.CallBehaviorAction</class>
+  <class name="CentralBufferNode">org.eclipse.uml2.uml.CentralBufferNode</class>
+  <class name="ControlFlow">org.eclipse.uml2.uml.ControlFlow</class>
+  <class name="CreateObjectAction">org.eclipse.uml2.uml.CreateObjectAction</class>
+  <class name="DataStoreNode">org.eclipse.uml2.uml.DataStoreNode</class>
+  <class name="DataType">org.eclipse.uml2.uml.DataType</class>
+  <class name="DestroyObjectAction">org.eclipse.uml2.uml.DestroyObjectAction</class>
+  <class name="InputPin">org.eclipse.uml2.uml.InputPin</class>
+  <class name="ObjectFlow">org.eclipse.uml2.uml.ObjectFlow</class>
+  <class name="OutputPin">org.eclipse.uml2.uml.OutputPin</class>
+  <class name="SendSignalAction">org.eclipse.uml2.uml.SendSignalAction</class>
+</classes>
+<toolbar>
   <poly-edge type="ControlFlow" destarrow="true" />
   <poly-edge type="ObjectFlow" destarrow="true" />
   <dropdown>
-    <rrect type="CallObjectAction" />
+    <rrect type="CallBehaviorAction" />
     <rrect type="CreateObjectAction" />
     <rrect type="DestroyObjectAction" />
   </dropdown>
   <pentagon type="SendSignalAction" />
-  <pentagon type="AcceptEventAction" concave="true" />
+  <concave-pentagon type="AcceptEventAction" />
   <dropdown>
     <rect type="ActivityParameterNode" />
     <rect type="CentralBufferNode" />
@@ -49,4 +66,5 @@
   </dropdown>
   <connector type="InputPin" />
   <connector type="OutputPin" />
+</toolbar>
 </diagram>

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

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.