Coverage Report - org.argouml.uml.diagram.ui.ActionAddExtensionPoint
 
Classes in this File Line Coverage Branch Coverage Complexity
ActionAddExtensionPoint
68%
15/22
50%
5/10
1.667
ActionAddExtensionPoint$1
55%
5/9
N/A
1.667
 
 1  
 /* $Id: ActionAddExtensionPoint.java 18709 2010-08-30 22:38:37Z bobtarling $
 2  
  *****************************************************************************
 3  
  * Copyright (c) 2009-2010 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  
  *    Bob Tarling
 11  
  *    Laurent Braud
 12  
  *****************************************************************************
 13  
  *
 14  
  * Some portions of this file was previously release using the BSD License:
 15  
  */
 16  
 
 17  
 // Copyright (c) 1996-2006 The Regents of the University of California. All
 18  
 // Rights Reserved. Permission to use, copy, modify, and distribute this
 19  
 // software and its documentation without fee, and without a written
 20  
 // agreement is hereby granted, provided that the above copyright notice
 21  
 // and this paragraph appear in all copies.  This software program and
 22  
 // documentation are copyrighted by The Regents of the University of
 23  
 // California. The software program and documentation are supplied "AS
 24  
 // IS", without any accompanying services from The Regents. The Regents
 25  
 // does not warrant that the operation of the program will be
 26  
 // uninterrupted or error-free. The end-user understands that the program
 27  
 // was developed for research purposes and is advised not to rely
 28  
 // exclusively on the program for any reason.  IN NO EVENT SHALL THE
 29  
 // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
 30  
 // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
 31  
 // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
 32  
 // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
 33  
 // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY
 34  
 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 35  
 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
 36  
 // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
 37  
 // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT,
 38  
 // UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 39  
 
 40  
 package org.argouml.uml.diagram.ui;
 41  
 
 42  
 import java.awt.event.ActionEvent;
 43  
 
 44  
 import javax.swing.Action;
 45  
 
 46  
 import org.argouml.application.helpers.ResourceLoaderWrapper;
 47  
 import org.argouml.i18n.Translator;
 48  
 import org.argouml.model.Model;
 49  
 import org.argouml.ui.targetmanager.TargetEvent;
 50  
 import org.argouml.ui.targetmanager.TargetListener;
 51  
 import org.argouml.ui.targetmanager.TargetManager;
 52  
 import org.argouml.ui.UndoableAction;
 53  
 
 54  
 /**
 55  
  * A class to implement the addition of extension points to use cases.<p>
 56  
  *
 57  
  * This is a singleton. Implemented with a private constructor and a static
 58  
  * access method. Marked as final, since it can't sensibly be subclassed (the
 59  
  * access method wouldn't work properly).<p>
 60  
  *
 61  
  * @author  Jeremy Bennett (mail@jeremybennett.com).
 62  
  * @stereotype singleton
 63  
  */
 64  20
 public final class ActionAddExtensionPoint extends UndoableAction {
 65  
 
 66  
     /**
 67  
      * Our private copy of the instance. Only accessible through the proper
 68  
      * access method.
 69  
      */
 70  
     private static ActionAddExtensionPoint singleton;
 71  
 
 72  
 
 73  
     ///////////////////////////////////////////////////////////////////////////
 74  
     //
 75  
     // Constructors
 76  
     //
 77  
     ///////////////////////////////////////////////////////////////////////////
 78  
 
 79  
     /**
 80  
      * Constructor is private, since it cannot be called directly for a
 81  
      * singleton. Make use of the access funtion.<p>
 82  
      */
 83  
     public ActionAddExtensionPoint() {
 84  53
         super(Translator.localize("button.new-extension-point"),
 85  
                 ResourceLoaderWrapper.lookupIcon("button.new-extension-point"));
 86  
         // Set the tooltip string:
 87  53
         putValue(Action.SHORT_DESCRIPTION, 
 88  
                 Translator.localize("button.new-extension-point"));
 89  53
     }
 90  
 
 91  
 
 92  
     ///////////////////////////////////////////////////////////////////////////
 93  
     //
 94  
     // Main methods
 95  
     //
 96  
     ///////////////////////////////////////////////////////////////////////////
 97  
 
 98  
 
 99  
     /**
 100  
      * Get the single instance of the action.<p>
 101  
      *
 102  
      * Since we are a singleton, this is the only way of accessing the
 103  
      * instance, which is created if it does not exist.<p>
 104  
      *
 105  
      * @return The singleton instance.
 106  
      */
 107  
     public static ActionAddExtensionPoint singleton() {
 108  
 
 109  
         // Create the singleton if it does not exist, and then return it
 110  
 
 111  54
         if (singleton == null) {
 112  53
             singleton = new ActionAddExtensionPoint();
 113  
             
 114  
             // When a new target is selected, we have to check if it 's a use case.
 115  
             //Then, the icone "add extension point" have to become enabled.
 116  53
             TargetManager.getInstance().addTargetListener(new TargetListener() {
 117  
                
 118  
                 public void targetAdded(TargetEvent e) {
 119  0
                     setTarget();
 120  0
                 }
 121  
                 public void targetRemoved(TargetEvent e) {
 122  0
                     setTarget();
 123  0
                 }
 124  
 
 125  
                 public void targetSet(TargetEvent e) {
 126  10
                     setTarget();
 127  10
                 }
 128  
                 private void setTarget() {
 129  10
                     singleton.setEnabled(singleton.shouldBeEnabled());
 130  10
                 }
 131  
             });
 132  53
             singleton.setEnabled(singleton.shouldBeEnabled());
 133  
         }
 134  
 
 135  54
         return singleton;
 136  
     }
 137  
 
 138  
     /**
 139  
      * Called if this action is invoked.<p>
 140  
      *
 141  
      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
 142  
      * @param ae  The action that caused us to be invoked.
 143  
      */
 144  
     public void actionPerformed(ActionEvent ae) {
 145  0
         super.actionPerformed(ae);
 146  
 
 147  
         // Find the target in the project browser. We can only do anything if
 148  
         // its a use case.
 149  
 
 150  0
         Object target = TargetManager.getInstance().getModelTarget();
 151  
 
 152  0
         if (!(Model.getFacade().isAUseCase(target))) {
 153  0
             return;
 154  
         }
 155  
 
 156  
         // Create a new extension point and make it the browser target. Then
 157  
         // invoke the superclass action method.
 158  
 
 159  0
         Object ep =
 160  
             Model.getUseCasesFactory()
 161  
                 .buildExtensionPoint(target);
 162  
 
 163  0
         TargetManager.getInstance().setTarget(ep);
 164  0
     }
 165  
 
 166  
 
 167  
     /**
 168  
      * A predicate to determine if this action is enabled.<p>
 169  
      *
 170  
      * @see org.tigris.gef.undo.UndoableAction#isEnabled()
 171  
      * @return  <code>true</code> if the superclass affirms this action is
 172  
      *          enabled and the target is a use case. <code>false</code>
 173  
      *          otherwise.
 174  
      */
 175  
     public boolean isEnabled() {
 176  162
         Object target = TargetManager.getInstance().getModelTarget();
 177  
 
 178  162
         return super.isEnabled()
 179  
                 && (Model.getFacade().isAUseCase(target));
 180  
         
 181  
     }
 182  
     
 183  
     /**
 184  
      * Called when a new target is selected, in order to display Action
 185  
      * @return true if the target is a use case, otherwise false
 186  
      * 
 187  
      * @since 2010-08-30
 188  
      */
 189  
     public boolean shouldBeEnabled() {
 190  63
         Object target = TargetManager.getInstance().getSingleModelTarget();
 191  63
         if (target == null) {
 192  1
             return false;
 193  
         }
 194  62
         return Model.getFacade().isAUseCase(target);
 195  
     }
 196  
 
 197  
 } /* end class ActionAddExtensionPoint */