Coverage Report - org.argouml.uml.diagram.ui.ModeAddToDiagram
 
Classes in this File Line Coverage Branch Coverage Complexity
AddToDiagramMemento
0%
0/19
0%
0/6
3.2
ModeAddToDiagram
0%
0/71
0%
0/34
3.2
 
 1  
 /* $Id: ModeAddToDiagram.java 17865 2010-01-12 20:45:26Z linus $
 2  
  *****************************************************************************
 3  
  * Copyright (c) 2009 Contributors - see below
 4  
  * All rights reserved. This program and the accompanying materials
 5  
  * are made available under the terms of the Eclipse Public License v1.0
 6  
  * which accompanies this distribution, and is available at
 7  
  * http://www.eclipse.org/legal/epl-v10.html
 8  
  *
 9  
  * Contributors:
 10  
  *    tfmorris
 11  
  *****************************************************************************
 12  
  *
 13  
  * Some portions of this file was previously release using the BSD License:
 14  
  */
 15  
 
 16  
 // Copyright (c) 2008 The Regents of the University of California. All
 17  
 // Rights Reserved. Permission to use, copy, modify, and distribute this
 18  
 // software and its documentation without fee, and without a written
 19  
 // agreement is hereby granted, provided that the above copyright notice
 20  
 // and this paragraph appear in all copies.  This software program and
 21  
 // documentation are copyrighted by The Regents of the University of
 22  
 // California. The software program and documentation are supplied "AS
 23  
 // IS", without any accompanying services from The Regents. The Regents
 24  
 // does not warrant that the operation of the program will be
 25  
 // uninterrupted or error-free. The end-user understands that the program
 26  
 // was developed for research purposes and is advised not to rely
 27  
 // exclusively on the program for any reason.  IN NO EVENT SHALL THE
 28  
 // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
 29  
 // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
 30  
 // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
 31  
 // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
 32  
 // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY
 33  
 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 34  
 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
 35  
 // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
 36  
 // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT,
 37  
 // UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 38  
 
 39  
 package org.argouml.uml.diagram.ui;
 40  
 
 41  
 import java.awt.Cursor;
 42  
 import java.awt.Point;
 43  
 import java.awt.Rectangle;
 44  
 import java.awt.event.KeyEvent;
 45  
 import java.awt.event.MouseEvent;
 46  
 import java.util.ArrayList;
 47  
 import java.util.Collection;
 48  
 import java.util.List;
 49  
 
 50  
 import org.apache.log4j.Logger;
 51  
 import org.argouml.uml.diagram.ArgoDiagram;
 52  
 import org.argouml.uml.diagram.DiagramUtils;
 53  
 import org.tigris.gef.base.Editor;
 54  
 import org.tigris.gef.base.FigModifyingModeImpl;
 55  
 import org.tigris.gef.base.Layer;
 56  
 import org.tigris.gef.graph.GraphNodeRenderer;
 57  
 import org.tigris.gef.graph.MutableGraphModel;
 58  
 import org.tigris.gef.presentation.Fig;
 59  
 import org.tigris.gef.presentation.FigNode;
 60  
 import org.tigris.gef.undo.Memento;
 61  
 import org.tigris.gef.undo.UndoManager;
 62  
 
 63  
 /**
 64  
  * This is the mode the editor is in having selected a number of model elements
 65  
  * and executing Add To Diagram.
 66  
  */
 67  
 
 68  
 public class ModeAddToDiagram extends FigModifyingModeImpl {
 69  
 
 70  
     private static final long serialVersionUID = 8861862975789222877L;
 71  
 
 72  
     /**
 73  
      * The nodes to be added.
 74  
      */
 75  
     private final Collection<Object> modelElements;
 76  
 
 77  0
     private final boolean addRelatedEdges = true;
 78  
 
 79  
     private final String instructions; 
 80  
 
 81  0
     private static final Logger LOG = Logger.getLogger(ModeAddToDiagram.class);
 82  
 
 83  
     /**
 84  
      * Create a mode to add the given elements to the diagram
 85  
      * @param modelElements the model elements to add
 86  
      * @param instructions the instruction to place in status bar
 87  
      */
 88  
     public ModeAddToDiagram(
 89  
             final Collection<Object> modelElements, 
 90  0
             final String instructions) {
 91  0
         this.modelElements = modelElements;
 92  0
         if (instructions == null) {
 93  0
             this.instructions = "";
 94  
         } else {
 95  0
             this.instructions = instructions;
 96  
         }
 97  0
     } 
 98  
 
 99  
     ////////////////////////////////////////////////////////////////
 100  
     // user feedback
 101  
 
 102  
     /**
 103  
      * @return A string to be shown in the status bar of the Editor when this
 104  
      * mode is on top of the ModeManager.
 105  
      */
 106  
     @Override
 107  
     public String instructions() {
 108  0
         return instructions;
 109  
     }
 110  
 
 111  
     /**
 112  
      * @return a cross hair cursor
 113  
      */
 114  
     public Cursor getInitialCursor() {
 115  0
         return Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR);
 116  
     }
 117  
 
 118  
     /**
 119  
      * On mouse release add the elements to the diagram
 120  
      * @param me the mouse event
 121  
      */
 122  
     @Override
 123  
     public void mouseReleased(final MouseEvent me) {
 124  0
             if (me.isConsumed()) {
 125  0
             if (LOG.isDebugEnabled()) {
 126  0
                 LOG.debug("MouseReleased but rejected as already consumed");
 127  
             }
 128  0
             return;
 129  
         }
 130  
         // TODO: Use per-project undo manager, not global
 131  0
         UndoManager.getInstance().addMementoLock(this);
 132  0
         start();
 133  0
             MutableGraphModel gm = (MutableGraphModel) editor.getGraphModel();
 134  
         
 135  0
         final int x = me.getX();
 136  0
         final int y = me.getY();
 137  0
         editor.damageAll();
 138  0
         final Point snapPt = new Point(x, y);
 139  0
         editor.snap(snapPt);
 140  0
         editor.damageAll();
 141  0
         int count = 0;
 142  
         
 143  0
         Layer lay = editor.getLayerManager().getActiveLayer();
 144  0
         GraphNodeRenderer renderer = editor.getGraphNodeRenderer();
 145  
         
 146  0
         final List<FigNode> placedFigs =
 147  
             new ArrayList<FigNode>(modelElements.size());
 148  
         
 149  0
         ArgoDiagram diag = DiagramUtils.getActiveDiagram();
 150  0
         if (diag instanceof UMLDiagram) {
 151  0
             for (final Object node : modelElements) {
 152  0
                 if (((UMLDiagram) diag).doesAccept(node)) {
 153  0
                     final FigNode pers =
 154  
                         renderer.getFigNodeFor(gm, lay, node, null);
 155  0
                     pers.setLocation(snapPt.x + (count++ * 100), snapPt.y);
 156  0
                     if (LOG.isDebugEnabled()) {
 157  0
                         LOG.debug("mouseMoved: Location set ("
 158  
                                 + pers.getX() + "," + pers.getY() + ")");
 159  
                     }
 160  
                     // TODO: Use per-project undo manager, not global
 161  0
                     UndoManager.getInstance().startChain();
 162  0
                     editor.add(pers);
 163  0
                     gm.addNode(node);
 164  
                     if (addRelatedEdges) {
 165  0
                         gm.addNodeRelatedEdges(node);
 166  
                     }
 167  
     
 168  0
                     Fig encloser = null;
 169  0
                     final Rectangle bbox = pers.getBounds();
 170  0
                     final List<Fig> otherFigs = lay.getContents();
 171  0
                     for (final Fig otherFig : otherFigs) {
 172  0
                         if (!(otherFig.getUseTrapRect())) {
 173  0
                             continue;
 174  
                         }
 175  0
                         if (!(otherFig instanceof FigNode)) {
 176  0
                             continue;
 177  
                         }
 178  0
                         if (!otherFig.isVisible()) {
 179  0
                             continue;
 180  
                         }
 181  0
                         if (otherFig.equals(pers)) {
 182  0
                             continue;
 183  
                         }
 184  0
                         final Rectangle trap = otherFig.getTrapRect();
 185  0
                         if (trap != null
 186  
                                 && trap.contains(bbox.x, bbox.y)
 187  
                                 && trap.contains(
 188  
                                         bbox.x + bbox.width, 
 189  
                                         bbox.y + bbox.height)) {
 190  0
                             encloser = otherFig;
 191  
                         }
 192  0
                     }
 193  0
                     pers.setEnclosingFig(encloser);
 194  
                     
 195  0
                     placedFigs.add(pers);
 196  0
                 }
 197  
             }
 198  
         }
 199  
         
 200  
         // TODO: Use per-project undo manager, not global
 201  0
         UndoManager.getInstance().removeMementoLock(this);
 202  0
         if (UndoManager.getInstance().isGenerateMementos()) {
 203  0
             AddToDiagramMemento memento =
 204  
                 new AddToDiagramMemento(editor, placedFigs);
 205  0
             UndoManager.getInstance().addMemento(memento);
 206  
         }
 207  0
         UndoManager.getInstance().addMementoLock(this);
 208  0
         editor.getSelectionManager().select(placedFigs);
 209  
         
 210  0
         done();
 211  0
         me.consume();
 212  0
     }
 213  
 
 214  
     public void keyTyped(KeyEvent ke) {
 215  0
         if (ke.getKeyChar() == KeyEvent.VK_ESCAPE) {
 216  0
             LOG.debug("ESC pressed");
 217  0
             leave();
 218  
         }
 219  0
     }
 220  
 }
 221  
 
 222  
 class AddToDiagramMemento extends Memento {
 223  
     
 224  
     private final List<FigNode> nodesPlaced;
 225  
     private final Editor editor;
 226  
     private final MutableGraphModel mgm;
 227  
     
 228  0
     AddToDiagramMemento(final Editor ed, final List<FigNode> nodesPlaced) {
 229  0
         this.nodesPlaced = nodesPlaced;
 230  0
         this.editor = ed;
 231  0
         this.mgm = (MutableGraphModel) editor.getGraphModel();
 232  0
     }
 233  
     
 234  
     public void undo() {
 235  
         // TODO: Use per-project undo manager, not global
 236  0
             UndoManager.getInstance().addMementoLock(this);
 237  0
         for (FigNode figNode : nodesPlaced) {
 238  0
             mgm.removeNode(figNode.getOwner());
 239  0
             editor.remove(figNode);
 240  
         }
 241  0
         UndoManager.getInstance().removeMementoLock(this);
 242  0
     }
 243  
     public void redo() {
 244  
         // TODO: Use per-project undo manager, not global
 245  0
         UndoManager.getInstance().addMementoLock(this);
 246  0
         for (FigNode figNode : nodesPlaced) {
 247  0
             editor.add(figNode);
 248  0
             mgm.addNode(figNode.getOwner());
 249  
         }
 250  0
         UndoManager.getInstance().removeMementoLock(this);
 251  0
     }
 252  
     public void dispose() {
 253  0
     }
 254  
     
 255  
     public String toString() {
 256  0
         return (isStartChain() ? "*" : " ")
 257  
             + "AddToDiagramMemento";
 258  
     }
 259  
 }