Author: bobtarling
Date: 2008-06-12 18:32:46-0700
New Revision: 14916
Modified:
trunk/src/argouml-app/src/org/argouml/uml/diagram/ui/ModeCreateAssociationEnd.java
trunk/src/argouml-app/src/org/argouml/uml/diagram/ui/ModeCreateGraphEdge.java
Log:
Don't convert FigAssociation to FigNodeAssociation until draw of AssociationEnd complete
Modified: trunk/src/argouml-app/src/org/argouml/uml/diagram/ui/ModeCreateAssociationEnd.java
Url: http://argouml.tigris.org/source/browse/argouml/trunk/src/argouml-app/src/org/argouml/uml/diagram/ui/ModeCreateAssociationEnd.java?view=diff&rev=14916&p1=trunk/src/argouml-app/src/org/argouml/uml/diagram/ui/ModeCreateAssociationEnd.java&p2=trunk/src/argouml-app/src/org/argouml/uml/diagram/ui/ModeCreateAssociationEnd.java&r1=14915&r2=14916
==============================================================================
--- trunk/src/argouml-app/src/org/argouml/uml/diagram/ui/ModeCreateAssociationEnd.java (original)
+++ trunk/src/argouml-app/src/org/argouml/uml/diagram/ui/ModeCreateAssociationEnd.java 2008-06-12 18:32:46-0700
@@ -24,25 +24,17 @@
package org.argouml.uml.diagram.ui;
-import java.awt.Point;
-import java.awt.event.KeyEvent;
-import java.awt.event.MouseEvent;
+import java.awt.Color;
import java.util.Collection;
-import java.util.Iterator;
import org.apache.log4j.Logger;
import org.argouml.model.IllegalModelElementConnectionException;
import org.argouml.model.Model;
-import org.argouml.uml.diagram.static_structure.ui.FigClassifierBox;
import org.tigris.gef.base.Layer;
-import org.tigris.gef.base.ModeCreatePolyEdge;
-import org.tigris.gef.graph.GraphModel;
-import org.tigris.gef.graph.GraphNodeRenderer;
import org.tigris.gef.graph.MutableGraphModel;
import org.tigris.gef.presentation.Fig;
import org.tigris.gef.presentation.FigEdge;
import org.tigris.gef.presentation.FigNode;
-import org.tigris.gef.presentation.FigPoly;
/**
* A Mode to interpret user input while creating an association end.
@@ -54,9 +46,9 @@
* transformed into a n-ary association.
* TODO: Investigate if this can extend ModeCreateGraphEdge
*
- * @author [email protected]
+ * @author Bob Tarling
*/
-public class ModeCreateAssociationEnd extends ModeCreatePolyEdge {
+public class ModeCreateAssociationEnd extends ModeCreateGraphEdge {
/**
* The UID.
@@ -65,263 +57,110 @@
private static final Logger LOG =
Logger.getLogger(ModeCreateAssociationEnd.class);
-
- private FigNode newFigNodeAssociation;
- private Object association;
- private Collection associationEnds;
-
- /*
- * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
- */
- public void mousePressed(MouseEvent me) {
- int x = me.getX(), y = me.getY();
- Fig underMouse = editor.hit(x, y);
- if (underMouse == null) {
- underMouse = editor.hit(x - 16, y - 16, 32, 32);
- }
-
- if (underMouse == null && _npoints == 0) {
- done();
- me.consume();
- return;
- }
-
- if (_npoints > 0) {
- me.consume();
- return;
- }
-
- Object modelElement = underMouse.getOwner();
- if (!Model.getFacade().isAAssociationClass(modelElement)) {
- if (Model.getFacade().isAAssociation(underMouse.getOwner())) {
- association = underMouse.getOwner();
- }
- if (underMouse instanceof FigAssociation) {
- associationEnds =
- Model.getFacade().getConnections(association);
- newFigNodeAssociation = placeTempNode(me);
- underMouse = newFigNodeAssociation;
- setSourceFigNode(newFigNodeAssociation);
- setStartPort(newFigNodeAssociation.getOwner());
- setStartPortFig(newFigNodeAssociation);
- } else if (underMouse instanceof FigNodeAssociation
- || underMouse instanceof FigClassifierBox) {
- if (getSourceFigNode() == null) {
- setSourceFigNode((FigNode) underMouse);
- setStartPort(getSourceFigNode().deepHitPort(x, y));
- }
- if (getStartPort() == null) {
- done();
- me.consume();
- return;
- }
- setStartPortFig(
- getSourceFigNode().getPortFig(getStartPort()));
- } else {
- done();
- me.consume();
- return;
- }
- }
-
- createFig(me);
- me.consume();
+ public Object getMetaType() {
+ return Model.getMetaTypes().getAssociationEnd();
}
- /*
- * @see org.tigris.gef.base.ModeCreatePolyEdge#mouseReleased(java.awt.event.MouseEvent)
- */
- public void mouseReleased(final MouseEvent me) {
- if (me.isConsumed()) {
- return;
- }
- if (getSourceFigNode() == null) {
- done();
- me.consume();
- return;
- }
- final int x = me.getX();
- final int y = me.getY();
- Fig destFig = editor.hit(x, y);
- if (destFig == null) {
- destFig = editor.hit(x - 16, y - 16, 32, 32);
- }
-
- if (destFig != null) {
- Object source = getSourceFigNode().getOwner();
- Object dest = destFig.getOwner();
+ /**
+ * Create an edge of the given type and connect it to the
+ * given nodes.
+ *
+ * @param graphModel the graph model in which to create the connection
+ * element
+ * @param edgeType the UML object type of the connection
+ * @param sourceFigNode the FigNode for the source element
+ * @param destFigNode the FigNode for the destination element
+ */
+ @Override
+ protected FigEdge buildConnection(
+ MutableGraphModel graphModel,
+ Object edgeType,
+ Fig sourceFig,
+ Fig destFig) {
+ try {
+ Object associationEnd =
+ Model.getUmlFactory().buildConnection(
+ edgeType,
+ sourceFig.getOwner(),
+ null,
+ destFig.getOwner(),
+ null,
+ null,
+ null);
+
+ final FigNode sourceFigNode = convertToFigNode(sourceFig);
+ final FigNode destFigNode = convertToFigNode(destFig);
+
+ graphModel.addEdge(associationEnd);
- if (Model.getFacade().isAAssociationClass(source)
- || Model.getFacade().isAAssociationClass(dest)) {
- // TODO: http://argouml.tigris.org/issues/show_bug.cgi?id=2991
- } else if ((Model.getFacade().isAAssociation(source)
- || Model.getFacade().isAClassifier(dest))
- && !Model.getFacade().isAClassifier(source)) {
- mouseReleasedOnClassifier(me, destFig);
- return;
- } else if ((Model.getFacade().isAClassifier(dest)
- || Model.getFacade().isAAssociation(dest))
- && !Model.getFacade().isAClassifier(source)) {
- mouseReleasedOnAssociation(me, destFig);
- return;
+ setNewEdge(associationEnd);
+
+ // Calling connect() will add the edge to the GraphModel and
+ // any LayerPersectives on that GraphModel will get a
+ // edgeAdded event and will add an appropriate FigEdge
+ // (determined by the GraphEdgeRenderer).
+
+ if (getNewEdge() != null) {
+ sourceFigNode.damage();
+ destFigNode.damage();
+ Layer lay = editor.getLayerManager().getActiveLayer();
+ FigEdge fe = (FigEdge) lay.presentationFor(getNewEdge());
+ _newItem.setLineColor(Color.black);
+ fe.setFig(_newItem);
+ fe.setSourcePortFig(sourceFigNode);
+ fe.setSourceFigNode((FigNode) sourceFigNode);
+ fe.setDestPortFig(destFigNode);
+ fe.setDestFigNode((FigNode) destFigNode);
+ return fe;
+ } else {
+ return null;
}
+ } catch (IllegalModelElementConnectionException e) {
+ // We have already confirmed the connection is valid
+ return null;
}
-
- if (!nearLast(x, y)) {
- editor.damageAll();
- Point snapPt = new Point(x, y);
- editor.snap(snapPt);
- ((FigPoly) _newItem).addPoint(snapPt.x, snapPt.y);
- _npoints++;
- editor.damageAll();
- }
- _lastX = x;
- _lastY = y;
- me.consume();
}
-
- /*
- * @see org.tigris.gef.base.ModeCreatePolyEdge#mouseReleased(java.awt.event.MouseEvent)
- */
- private void mouseReleasedOnClassifier(
- final MouseEvent me,
- final Fig destFig) {
- MutableGraphModel graphModel =
- (MutableGraphModel) editor.getGraphModel();
-
- // Order here is very important!
- // 1. Remove the old association FigEdge first
- graphModel.removeEdge(association);
-
- try {
- Model.getUmlFactory().buildConnection(
- Model.getMetaTypes().getAssociationEnd(),
- getSourceFigNode().getOwner(),
- null,
- destFig.getOwner(),
- null,
- null,
- null);
- } catch (IllegalModelElementConnectionException e) {
- LOG.error("Exception", e);
- }
-
- // 3. Make sure the association is a node in the graph model
- graphModel.addNode(association);
-
- associationEnds =
- Model.getFacade().getConnections(association);
-
- endAttached(null);
- me.consume();
- }
-
-
- /*
- * @see org.tigris.gef.base.ModeCreatePolyEdge#mouseReleased(java.awt.event.MouseEvent)
- */
- private void mouseReleasedOnAssociation(
- final MouseEvent me,
- final Fig destFig) {
- int x = me.getX();
- int y = me.getY();
+
+ /**
+ * If the selected Fig is a FigAssociation (an edge) then
+ * convert it to a FigNodeAssociation.
+ * @param fig the select end Fig
+ * @return the fig converted to a FigNode
+ */
+ private FigNode convertToFigNode(Fig fig) {
+ if (fig instanceof FigEdgePort) {
+ fig = fig.getGroup();
+ }
+ if (!(fig instanceof FigAssociation)) {
+ return (FigNode) fig;
+ }
+ final FigAssociation figAssociation = (FigAssociation) fig;
+ final int x = figAssociation.getEdgePort().getX();
+ final int y = figAssociation.getEdgePort().getY();
+ final Object association = fig.getOwner();
+ figAssociation.removeFromDiagram();
- MutableGraphModel graphModel =
+ final MutableGraphModel gm =
(MutableGraphModel) editor.getGraphModel();
-
- Object destAssociation = destFig.getOwner();
-
- // Order here is very important!
- // 1. Remove the old association FigEdge first
- graphModel.removeEdge(destAssociation);
- destFig.removeFromDiagram();
-
- // 2. Add a new association end to the association
- graphModel.connect(
- getStartPort(),
- destAssociation,
- Model.getMetaTypes().getAssociationEnd());
+ gm.addNode(association);
+ final Layer lay = editor.getLayerManager().getActiveLayer();
+ final FigNode figNode = (FigNode) lay.presentationFor(association);
- // 3. Create a new FigNode representing the n-ary assoc
- graphModel.addNode(destAssociation);
-
- // 4. Create a new FigNode representing the n-ary assoc
- Layer lay = editor.getLayerManager().getActiveLayer();
- FigNode figNode = (FigNode) lay.presentationFor(destAssociation);
figNode.setLocation(
x - figNode.getWidth() / 2,
y - figNode.getHeight() / 2);
- editor.add(figNode);
-
- associationEnds =
- Model.getFacade().getConnections(destAssociation);
-
- endAttached(null);
- me.consume();
- }
-
- /**
- * This will be called when the edge is successfully connected.
- * This method is extended to make sure that all edges are in the
- * graph model
- * @param fe the FigEdge drawn
- */
- protected void endAttached(FigEdge fe) {
- MutableGraphModel graphModel =
- (MutableGraphModel) editor.getGraphModel();
- for (Iterator it = associationEnds.iterator(); it.hasNext(); ) {
- graphModel.addEdge(it.next());
- }
- super.endAttached(fe);
- done();
- }
-
- private FigNode placeTempNode(MouseEvent me) {
- FigNode figNode = null;
- GraphModel gm = editor.getGraphModel();
-
- GraphNodeRenderer renderer = editor.getGraphNodeRenderer();
- Layer lay = editor.getLayerManager().getActiveLayer();
- figNode = renderer.getFigNodeFor(gm, lay, association, null);
- figNode.setLocation(
- me.getX() - figNode.getWidth() / 2,
- me.getY() - figNode.getHeight() / 2);
//figNode.setVisible(false);
editor.add(figNode);
editor.getSelectionManager().deselectAll();
+ Collection<Object> associationEnds =
+ Model.getFacade().getConnections(association);
+ for (Object associationEnd : associationEnds) {
+ gm.addEdge(associationEnd);
+ }
+ figNode.updateEdges();
return figNode;
}
- public void keyTyped(KeyEvent ke) {
- if (ke.getKeyChar() == KeyEvent.VK_ESCAPE) {
- LOG.debug("Esc pressed");
- abort();
- done();
- ke.consume();
- }
- }
-
- /**
- * This method must be called if the edge drawing is aborted for
- * any reason.
- * It removes any FigNodeAssociation that may have been created
- * when drawing started from a FigAssociation edge.
- */
- private void abort() {
- LOG.info("Drawing association end aborted");
- if (newFigNodeAssociation != null) {
- editor.remove(newFigNodeAssociation);
- newFigNodeAssociation.removeFromDiagram();
- }
- }
-
- /*
- * @see org.tigris.gef.base.ModeImpl#leave()
- */
- public void leave() {
- abort();
- super.leave();
- }
} /* end class ModeCreateAssociation */
Modified: trunk/src/argouml-app/src/org/argouml/uml/diagram/ui/ModeCreateGraphEdge.java
Url: http://argouml.tigris.org/source/browse/argouml/trunk/src/argouml-app/src/org/argouml/uml/diagram/ui/ModeCreateGraphEdge.java?view=diff&rev=14916&p1=trunk/src/argouml-app/src/org/argouml/uml/diagram/ui/ModeCreateGraphEdge.java&p2=trunk/src/argouml-app/src/org/argouml/uml/diagram/ui/ModeCreateGraphEdge.java&r1=14915&r2=14916
==============================================================================
--- trunk/src/argouml-app/src/org/argouml/uml/diagram/ui/ModeCreateGraphEdge.java (original)
+++ trunk/src/argouml-app/src/org/argouml/uml/diagram/ui/ModeCreateGraphEdge.java 2008-06-12 18:32:46-0700
@@ -30,6 +30,7 @@
import java.awt.event.MouseListener;
import org.apache.log4j.Logger;
+import org.argouml.model.IllegalModelElementConnectionException;
import org.argouml.model.Model;
import org.argouml.uml.diagram.static_structure.ui.FigEdgeNote;
import org.tigris.gef.base.Layer;
@@ -54,6 +55,11 @@
* The Fig from which drawing starts, either a FigNode or a FigEdge
*/
private Fig sourceFig;
+
+ /**
+ * The port where the edge was dropped.
+ */
+ private Fig endPort;
/*
* @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
@@ -141,7 +147,7 @@
} else {
LOG.info("Connection valid");
}
-
+
if (destFig instanceof FigEdgeModelElement
&& !(destFig instanceof FigEdgeNote)) {
FigEdgeModelElement destEdge = (FigEdgeModelElement) destFig;
@@ -170,49 +176,25 @@
p.setComplete(true);
LOG.info("Connecting");
- Object modelElement = graphModel.connect(
- getStartPort(),
- foundPort,
- getMetaType());
- setNewEdge(modelElement);
-
- // Calling connect() will add the edge to the GraphModel and
- // any LayerPersectives on that GraphModel will get a
- // edgeAdded event and will add an appropriate FigEdge
- // (determined by the GraphEdgeRenderer).
-
- if (getNewEdge() != null) {
- getSourceFigNode().damage();
- destFigNode.damage();
- Layer lay = editor.getLayerManager().getActiveLayer();
- FigEdge fe = (FigEdge) lay.presentationFor(getNewEdge());
- _newItem.setLineColor(Color.black);
- fe.setFig(_newItem);
- fe.setSourcePortFig(getStartPortFig());
- fe.setSourceFigNode(getSourceFigNode());
- fe.setDestPortFig(destPortFig);
- fe.setDestFigNode(destFigNode);
-
- if (fe != null) {
- editor.getSelectionManager().select(fe);
- }
- editor.damageAll();
-
- // if the new edge implements the MouseListener
- // interface it has to receive the mouseReleased() event
- if (fe instanceof MouseListener) {
- ((MouseListener) fe).mouseReleased(me);
- }
-
- // set the new edge in place
- if (getSourceFigNode() != null) {
- getSourceFigNode().updateEdges();
- }
- if (destFigNode != null) {
- destFigNode.updateEdges();
- }
- endAttached(fe);
+ FigEdge fe = buildConnection(
+ graphModel,
+ getMetaType(),
+ sourceFig,
+ destFig);
+
+ if (fe != null) {
+ editor.getSelectionManager().select(fe);
}
+ editor.damageAll();
+
+ // if the new edge implements the MouseListener
+ // interface it has to receive the mouseReleased() event
+ if (fe instanceof MouseListener) {
+ ((MouseListener) fe).mouseReleased(me);
+ }
+
+ endAttached(fe);
+
done();
me.consume();
return;
@@ -233,8 +215,8 @@
/**
* Return the meta type of the element that this mode is designed to
- * create. In the case the dependency metatype.
- * @return the dependency meta type.
+ * create.
+ * @return the meta type of the connection required.
*/
protected abstract Object getMetaType();
@@ -254,4 +236,48 @@
dest.getOwner(),
true);
}
+
+ /**
+ * Create an edge of the given type and connect it to the
+ * given nodes.
+ *
+ * @param edgeType the UML object type of the connection
+ * @param fromElement the UML object for the "from" element
+ * @param toElement the UML object for the "to" element
+ */
+ protected FigEdge buildConnection(
+ MutableGraphModel graphModel,
+ Object edgeType,
+ Fig fromElement,
+ Fig destFigNode) {
+ Object modelElement = graphModel.connect(
+ fromElement.getOwner(),
+ destFigNode.getOwner(),
+ edgeType);
+
+ setNewEdge(modelElement);
+
+ // Calling connect() will add the edge to the GraphModel and
+ // any LayerPersectives on that GraphModel will get a
+ // edgeAdded event and will add an appropriate FigEdge
+ // (determined by the GraphEdgeRenderer).
+
+ if (getNewEdge() != null) {
+ getSourceFigNode().damage();
+ destFigNode.damage();
+ Layer lay = editor.getLayerManager().getActiveLayer();
+ FigEdge fe = (FigEdge) lay.presentationFor(getNewEdge());
+ _newItem.setLineColor(Color.black);
+ fe.setFig(_newItem);
+ fe.setSourcePortFig(getStartPortFig());
+ fe.setSourceFigNode(getSourceFigNode());
+ fe.setDestPortFig(destFigNode);
+ fe.setDestFigNode((FigNode) destFigNode);
+ return fe;
+
+ } else {
+ return null;
+ }
+
+ }
}
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.