Coverage Report - org.argouml.uml.diagram.ui.ActionAddExistingNodes
 
Classes in this File Line Coverage Branch Coverage Complexity
ActionAddExistingNodes
0%
0/41
0%
0/24
4.75
 
 1  
 /* $Id: ActionAddExistingNodes.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  
  *    bobtarling
 11  
  *****************************************************************************
 12  
  *
 13  
  * Some portions of this file was previously release using the BSD License:
 14  
  */
 15  
 
 16  
 // Copyright (c) 2006-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  
 
 40  
 package org.argouml.uml.diagram.ui;
 41  
 
 42  
 import java.awt.Point;
 43  
 import java.awt.event.ActionEvent;
 44  
 import java.util.Collection;
 45  
 
 46  
 import org.argouml.i18n.Translator;
 47  
 import org.argouml.model.Model;
 48  
 import org.argouml.ui.targetmanager.TargetManager;
 49  
 import org.argouml.uml.diagram.ArgoDiagram;
 50  
 import org.argouml.uml.diagram.DiagramUtils;
 51  
 import org.tigris.gef.base.Editor;
 52  
 import org.tigris.gef.base.Globals;
 53  
 import org.tigris.gef.graph.GraphModel;
 54  
 import org.tigris.gef.graph.MutableGraphModel;
 55  
 import org.argouml.ui.UndoableAction;
 56  
 
 57  
 /**
 58  
 * ActionAddExistingNodes enables pasting of existing nodes into a Diagram.
 59  
 *
 60  
 * @author Thomas Neustupny
 61  
 */
 62  
 public class ActionAddExistingNodes extends UndoableAction {
 63  
 
 64  
     /**
 65  
      * The UML objects to be added to the diagram.
 66  
      */
 67  
     private Collection objects;
 68  
 
 69  
     /**
 70  
      * The Constructor.
 71  
      *
 72  
      * @param name the localized name of the action
 73  
      * @param coll the UML objects to be added
 74  
      */
 75  
     public ActionAddExistingNodes(String name, Collection coll) {
 76  0
         super(name);
 77  0
         objects = coll;
 78  0
     }
 79  
 
 80  
     /*
 81  
      * @see javax.swing.Action#isEnabled()
 82  
      */
 83  
     @Override
 84  
     public boolean isEnabled() {
 85  0
         ArgoDiagram dia = DiagramUtils.getActiveDiagram();
 86  0
         if (dia == null) {
 87  0
             return false;
 88  
         }
 89  0
         MutableGraphModel gm = (MutableGraphModel) dia.getGraphModel();
 90  0
         for (Object o : objects) {
 91  0
             if (gm.canAddNode(o)) {
 92  0
                 return true;
 93  
             }
 94  
         }
 95  0
         return false;
 96  
     }
 97  
 
 98  
     @Override
 99  
     public void actionPerformed(ActionEvent ae) {
 100  0
         super.actionPerformed(ae);
 101  0
         Editor ce = Globals.curEditor();
 102  0
         GraphModel gm = ce.getGraphModel();
 103  0
         if (!(gm instanceof MutableGraphModel)) {
 104  0
             return;
 105  
         }
 106  
 
 107  0
         String instructions =
 108  
             Translator.localize(
 109  
                 "misc.message.click-on-diagram-to-add");
 110  0
         Globals.showStatus(instructions);
 111  
         
 112  0
         final ModeAddToDiagram placeMode = new ModeAddToDiagram(
 113  
                 objects,
 114  
                 instructions);
 115  
 
 116  0
         Globals.mode(placeMode, false);
 117  0
     }
 118  
     
 119  
     /**
 120  
      * @param modelElements the modelelements to add Nodes for
 121  
      * @param location the point where to drop the node.
 122  
      *               Also <code>null</code> is acceptable.
 123  
      * @param diagram the diagram to add the nodes to
 124  
      */
 125  
     public static void addNodes(Collection modelElements, 
 126  
             Point location, ArgoDiagram diagram) {
 127  0
         MutableGraphModel gm = (MutableGraphModel) diagram.getGraphModel();
 128  0
         Collection oldTargets = TargetManager.getInstance().getTargets();
 129  0
         int count = 0;
 130  0
         for (Object me : modelElements) {
 131  0
             if (diagram instanceof UMLDiagram 
 132  
                     && ((UMLDiagram) diagram).doesAccept(me)) {
 133  0
                 ((UMLDiagram) diagram).drop(me, location);
 134  0
             } else if (Model.getFacade().isANaryAssociation(me)) {
 135  0
                 AddExistingNodeCommand cmd =
 136  
                     new AddExistingNodeCommand(me, location,
 137  
                             count++);
 138  0
                 cmd.execute();
 139  0
             } else if (Model.getFacade().isAUMLElement(me)) {
 140  0
                 if (gm.canAddEdge(me)) {
 141  0
                     gm.addEdge(me);
 142  
                     // TODO: An AssociationClass should be possible to add
 143  
                     // as a side effect of adding a node and its related
 144  
                     // edges, but that doesn't work as things are currently
 145  
                     // structured. - tfm 20061208
 146  0
                     if (Model.getFacade().isAAssociationClass(me)) {
 147  0
                         ModeCreateAssociationClass.buildInActiveLayer(
 148  
                                 Globals.curEditor(), 
 149  
                                 me);
 150  
                     }
 151  0
                 } else if (gm.canAddNode(me)) {
 152  0
                     AddExistingNodeCommand cmd =
 153  
                         new AddExistingNodeCommand(me, location,
 154  
                                 count++);
 155  0
                     cmd.execute();
 156  0
                 }
 157  
             }
 158  
         }
 159  0
         TargetManager.getInstance().setTargets(oldTargets);
 160  0
     }
 161  
 }