Coverage Report - org.argouml.uml.ui.foundation.core.ActionAddAttribute
 
Classes in this File Line Coverage Branch Coverage Complexity
ActionAddAttribute
50%
13/26
43%
7/16
2.125
ActionAddAttribute$1
77%
7/9
N/A
2.125
 
 1  
 /* $Id: ActionAddAttribute.java 17897 2010-01-12 21:36:40Z 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.ui.foundation.core;
 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.kernel.Project;
 48  
 import org.argouml.kernel.ProjectManager;
 49  
 import org.argouml.kernel.UmlModelMutator;
 50  
 import org.argouml.model.Model;
 51  
 import org.argouml.ui.targetmanager.TargetEvent;
 52  
 import org.argouml.ui.targetmanager.TargetListener;
 53  
 import org.argouml.ui.targetmanager.TargetManager;
 54  
 import org.argouml.ui.UndoableAction;
 55  
 
 56  
 /**
 57  
  * Action to add an attribute to a Classifier or AssociationEnd.<p>
 58  
  * 
 59  
  * This class shall be the only one that knows 
 60  
  * when this tool should be downlighted or not.
 61  
  */
 62  
 @UmlModelMutator
 63  854
 public class ActionAddAttribute extends UndoableAction {
 64  
 
 65  
     private static ActionAddAttribute targetFollower;
 66  
 
 67  
     /**
 68  
      * The constructor for this class.
 69  
      */
 70  
     public ActionAddAttribute() {
 71  900
         super(Translator.localize("button.new-attribute"),
 72  
                 ResourceLoaderWrapper.lookupIcon("button.new-attribute"));
 73  
         // Set the tooltip string:
 74  900
         putValue(Action.SHORT_DESCRIPTION, 
 75  
                 Translator.localize("button.new-attribute"));
 76  900
     }
 77  
 
 78  
     public static ActionAddAttribute getTargetFollower() {
 79  1028
         if (targetFollower == null) {
 80  900
             targetFollower  = new ActionAddAttribute();
 81  900
             TargetManager.getInstance().addTargetListener(new TargetListener() {
 82  
                 public void targetAdded(TargetEvent e) {
 83  0
                     setTarget();
 84  0
                 }
 85  
                 public void targetRemoved(TargetEvent e) {
 86  88
                     setTarget();
 87  88
                 }
 88  
 
 89  
                 public void targetSet(TargetEvent e) {
 90  339
                     setTarget();
 91  339
                 }
 92  
                 private void setTarget() {
 93  427
                     targetFollower.setEnabled(targetFollower.shouldBeEnabled());
 94  427
                 }
 95  
             });
 96  900
             targetFollower.setEnabled(targetFollower.shouldBeEnabled());
 97  
         }
 98  1028
         return targetFollower;
 99  
     }
 100  
 
 101  
     /*
 102  
      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
 103  
      */
 104  
     public void actionPerformed(ActionEvent ae) {
 105  
 
 106  0
         super.actionPerformed(ae);
 107  
 
 108  0
         Object target = TargetManager.getInstance().getSingleModelTarget();
 109  0
         Object classifier = null;
 110  
 
 111  0
         if (Model.getFacade().isAClassifier(target)
 112  
                 || Model.getFacade().isAAssociationEnd(target)) {
 113  0
             classifier = target;
 114  0
         } else if (Model.getFacade().isAFeature(target)) {
 115  0
             classifier = Model.getFacade().getOwner(target);
 116  
         } else {
 117  0
             return;
 118  
         }
 119  
 
 120  0
         Project project = ProjectManager.getManager().getCurrentProject();
 121  0
         Object attrType = project.getDefaultAttributeType();
 122  0
         Object attr =
 123  
             Model.getCoreFactory().buildAttribute2(
 124  
                 classifier,
 125  
                 attrType);
 126  0
         TargetManager.getInstance().setTarget(attr);
 127  0
     }
 128  
 
 129  
     /**
 130  
      * @return true if this tool should be enabled
 131  
      */
 132  
     public boolean shouldBeEnabled() {
 133  1327
         Object target = TargetManager.getInstance().getSingleModelTarget();
 134  1327
         if (target == null) {
 135  126
             return false;
 136  
         }
 137  1201
         return Model.getFacade().isAClassifier(target)
 138  
             || Model.getFacade().isAFeature(target)
 139  
             || Model.getFacade().isAAssociationEnd(target);
 140  
     }
 141  
 
 142  
     /**
 143  
      * The UID.
 144  
      */
 145  
     private static final long serialVersionUID = -111785878370086329L;
 146  
 } /* end class ActionAddAttribute */