Coverage Report - org.argouml.uml.diagram.ui.ActionAddMessage
 
Classes in this File Line Coverage Branch Coverage Complexity
ActionAddMessage
35%
10/28
16%
1/6
1.444
ActionAddMessage$1
11%
1/9
N/A
1.444
 
 1  
 /* $Id: ActionAddMessage.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) 1996-2007 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.event.ActionEvent;
 42  
 
 43  
 import javax.swing.Action;
 44  
 
 45  
 import org.argouml.application.helpers.ResourceLoaderWrapper;
 46  
 import org.argouml.i18n.Translator;
 47  
 import org.argouml.model.Model;
 48  
 import org.argouml.ui.targetmanager.TargetEvent;
 49  
 import org.argouml.ui.targetmanager.TargetListener;
 50  
 import org.argouml.ui.targetmanager.TargetManager;
 51  
 import org.tigris.gef.base.Editor;
 52  
 import org.tigris.gef.base.Globals;
 53  
 import org.tigris.gef.base.Layer;
 54  
 import org.tigris.gef.graph.GraphModel;
 55  
 import org.tigris.gef.graph.GraphNodeRenderer;
 56  
 import org.tigris.gef.presentation.FigNode;
 57  
 import org.argouml.ui.UndoableAction;
 58  
 
 59  
 /**
 60  
  * Action to add a message.
 61  
  * @stereotype singleton
 62  
  */
 63  0
 public class ActionAddMessage extends UndoableAction {
 64  
 
 65  
     private static ActionAddMessage targetFollower;
 66  
     
 67  
     /**
 68  
      * The constructor.
 69  
      */
 70  
     public ActionAddMessage() {
 71  19
         super(Translator.localize("action.add-message"),
 72  
                 ResourceLoaderWrapper.lookupIcon("action.add-message"));
 73  
         // Set the tooltip string:
 74  19
         putValue(Action.SHORT_DESCRIPTION, 
 75  
                 Translator.localize("action.add-message"));
 76  19
     }
 77  
 
 78  
     public static ActionAddMessage getTargetFollower() {
 79  19
         if (targetFollower == null) {
 80  19
             targetFollower  = new ActionAddMessage();
 81  19
             TargetManager.getInstance().addTargetListener(new TargetListener() {
 82  
                 public void targetAdded(TargetEvent e) {
 83  0
                     setTarget();
 84  0
                 }
 85  
                 public void targetRemoved(TargetEvent e) {
 86  0
                     setTarget();
 87  0
                 }
 88  
 
 89  
                 public void targetSet(TargetEvent e) {
 90  0
                     setTarget();
 91  0
                 }
 92  
                 private void setTarget() {
 93  0
                     targetFollower.setEnabled(targetFollower.shouldBeEnabled());
 94  0
                 }
 95  
             });
 96  19
             targetFollower.setEnabled(targetFollower.shouldBeEnabled());
 97  
         }
 98  19
         return targetFollower;
 99  
     }
 100  
 
 101  
     /*
 102  
      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
 103  
      */
 104  
     public void actionPerformed(ActionEvent ae) {
 105  0
         super.actionPerformed(ae);
 106  0
             Object target =  TargetManager.getInstance().getModelTarget();
 107  
 
 108  0
             if (!(Model.getFacade().isAAssociationRole(target))
 109  
             && Model.getFacade().isACollaboration(Model.getFacade()
 110  
                 .getNamespace(target))) {
 111  0
                 return;
 112  
             }
 113  
         // So, the target is a MAssociationRole
 114  0
             this.addMessage(target);
 115  0
     }
 116  
 
 117  
     /**
 118  
      * Add a message to an associationRole: it builds it using the
 119  
      * Factory method and then it creates the Fig and adds it to the
 120  
      * diagram.
 121  
      *
 122  
      * @param associationrole the associationRole to which the new message
 123  
      *                        must be added
 124  
      */
 125  
     private void addMessage(Object associationrole) {
 126  0
         Object collaboration = Model.getFacade().getNamespace(associationrole);
 127  0
         Object message =
 128  
             Model.getCollaborationsFactory()
 129  
                     .buildMessage(collaboration, associationrole);
 130  0
         Editor e = Globals.curEditor();
 131  0
         GraphModel gm = e.getGraphModel();
 132  0
         Layer lay = e.getLayerManager().getActiveLayer();
 133  0
         GraphNodeRenderer gr = e.getGraphNodeRenderer();
 134  0
         FigNode figMsg = gr.getFigNodeFor(gm, lay, message, null);
 135  0
         ((FigMessage) figMsg).addPathItemToFigAssociationRole(lay);
 136  
 
 137  0
         gm.getNodes().add(message); /*MVW This is not the correct way,
 138  
         * but it allows connecting a CommentEdge to it!
 139  
         * See e.g. ActionAddNote for the correct way.
 140  
         * Testcase:
 141  
         * 1. Select the message.
 142  
         * 2. Click the Comment tool.
 143  
         * */
 144  
 
 145  0
         TargetManager.getInstance().setTarget(message);
 146  0
     }
 147  
     
 148  
     /*
 149  
      * @see org.tigris.gef.undo.UndoableAction#isEnabled()
 150  
      */
 151  
     public boolean shouldBeEnabled() {
 152  19
         Object target =  TargetManager.getInstance().getModelTarget();
 153  19
         return Model.getFacade().isAAssociationRole(target);
 154  
     }
 155  
     
 156  
 }  /* end class ActionAddMessage */