Coverage Report - org.argouml.util.ToolBarUtility
 
Classes in this File Line Coverage Branch Coverage Complexity
ToolBarUtility
35%
18/51
33%
10/30
4
ToolBarUtility$PopupActionsListener
63%
7/11
50%
3/6
4
 
 1  
 /* $Id: ToolBarUtility.java 17887 2010-01-12 21:17:18Z 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  
  *    mvw
 11  
  *****************************************************************************
 12  
  *
 13  
  * Some portions of this file was previously release using the BSD License:
 14  
  */
 15  
 
 16  
 // Copyright (c) 2006 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.util;
 40  
 
 41  
 import java.awt.Component;
 42  
 import java.beans.PropertyChangeEvent;
 43  
 import java.beans.PropertyChangeListener;
 44  
 import java.util.Collection;
 45  
 
 46  
 import javax.swing.Action;
 47  
 import javax.swing.JButton;
 48  
 import javax.swing.JToolBar;
 49  
 
 50  
 import org.apache.log4j.Logger;
 51  
 import org.argouml.configuration.Configuration;
 52  
 import org.argouml.configuration.ConfigurationKey;
 53  
 import org.tigris.toolbar.ToolBarManager;
 54  
 import org.tigris.toolbar.toolbutton.PopupToolBoxButton;
 55  
 
 56  
 /**
 57  
  * This utility class contains additional functionality related to the 
 58  
  * org.tigris.toolbar project for the ArgoUML application.
 59  
  *
 60  
  * @author Michiel
 61  
  */
 62  0
 public class ToolBarUtility {
 63  
     
 64  900
     private static final Logger LOG = Logger.getLogger(ToolBarUtility.class);
 65  
     
 66  
     /**
 67  
      * Manages the selection of the default tool 
 68  
      * in a popup tool in the toolbar. <p>
 69  
      * 
 70  
      * I.e. in a toolbar, you can have tools that can be opened,
 71  
      * into a grid of tools. The last used tool is remembered, 
 72  
      * and put at the top when the popup is closed, i.e.
 73  
      * is the only tool that remains visible. This remembering is
 74  
      * persistent, hence stored in the configuration file,
 75  
      * under a certain key (i.e. name).
 76  
      * 
 77  
      * @param actions the array of actions that make up the popup
 78  
      * @param key appendix for the key for the configuration file
 79  
      */
 80  
     public static void manageDefault(Object[] actions, String key) {
 81  6545
         Action defaultAction = null;
 82  6545
         ConfigurationKey k =
 83  
             Configuration.makeKey("default", "popupactions", key);
 84  6545
         String defaultName = Configuration.getString(k);
 85  6545
         PopupActionsListener listener = new PopupActionsListener(k);
 86  26697
         for (int i = 0; i < actions.length; ++i) {
 87  20152
             if (actions[i] instanceof Action) {
 88  15088
                 Action a = (Action) actions[i];
 89  15088
                 if (a.getValue(Action.NAME).equals(defaultName)) {
 90  0
                     defaultAction = a;
 91  
                 }
 92  15088
                 a.addPropertyChangeListener(listener);
 93  15088
             } else if (actions[i] instanceof Object[]) {
 94  5064
                 Object[] actionRow = (Object[]) actions[i];
 95  15192
                 for (int j = 0; j < actionRow.length; ++j) {
 96  10128
                     Action a = (Action) actionRow[j];
 97  10128
                     if (a.getValue(Action.NAME).equals(defaultName)) {
 98  0
                         defaultAction = a;
 99  
                     }
 100  10128
                     a.addPropertyChangeListener(listener);
 101  
                 }
 102  
             }
 103  
         }
 104  
 
 105  6545
         if (defaultAction != null) {
 106  0
             defaultAction.putValue("isDefault", Boolean.valueOf(true));
 107  
         }
 108  6545
     }
 109  
 
 110  0
     static class PopupActionsListener implements PropertyChangeListener {
 111  
         private boolean blockEvents;
 112  
         private ConfigurationKey key;
 113  
 
 114  
         /**
 115  
          * Constructor.
 116  
          *
 117  
          * @param k
 118  
          */
 119  6545
         public PopupActionsListener(ConfigurationKey k) {
 120  6545
             key = k;
 121  6545
         }
 122  
 
 123  
         /*
 124  
          * @see java.beans.PropertyChangeListener#propertyChange(
 125  
          *         java.beans.PropertyChangeEvent)
 126  
          */
 127  
         public void propertyChange(PropertyChangeEvent evt) {
 128  32
             if (evt.getSource() instanceof Action) {
 129  32
                 Action a = (Action) evt.getSource();
 130  32
                 if (!blockEvents && evt.getPropertyName().equals("popped")) {
 131  0
                     blockEvents = true;
 132  
                     /* Switch the value back off, so that we will
 133  
                      * get notified again next time.
 134  
                      */
 135  0
                     a.putValue("popped", Boolean.valueOf(false));
 136  0
                     blockEvents = false;
 137  0
                     Configuration.setString(key,
 138  
                             (String) a.getValue(Action.NAME));
 139  
                 }
 140  
             }
 141  32
         }
 142  
     }
 143  
 
 144  
     /** 
 145  
      * TODO: Use the following function to have a dropdown set of tools: 
 146  
      * ToolBarFactory.addItemsToToolBar(buttonPanel, actions, true); 
 147  
      * Instead, this temporary solution: 
 148  
      *
 149  
      * @param buttonPanel the toolbar
 150  
      * @param actions an array of actions representing the tool layout
 151  
      */
 152  
     public static void addItemsToToolBar(JToolBar buttonPanel, 
 153  
             Object[] actions) {
 154  0
         JButton button = buildPopupToolBoxButton(actions, false);
 155  0
         if (!ToolBarManager.alwaysUseStandardRollover()) {
 156  0
             button.setBorderPainted(false);
 157  
         }
 158  0
         buttonPanel.add(button);
 159  0
     }
 160  
     
 161  
     /** 
 162  
      * TODO: Use the following function to have a dropdown set of tools: 
 163  
      * ToolBarFactory.addItemsToToolBar(buttonPanel, actions, true); 
 164  
      * Instead, this temporary solution: 
 165  
      *
 166  
      * @param buttonPanel the toolbar
 167  
      * @param actions an array of actions representing the tool layout
 168  
      */
 169  
     public static void addItemsToToolBar(JToolBar buttonPanel, 
 170  
             Collection actions) {
 171  0
         addItemsToToolBar(buttonPanel, actions.toArray());
 172  0
     }
 173  
     
 174  
     /**
 175  
      * TODO: Move this into the toolbar project.
 176  
      */
 177  
     private static PopupToolBoxButton buildPopupToolBoxButton(Object[] actions, 
 178  
             boolean rollover) {
 179  0
         PopupToolBoxButton toolBox = null;
 180  0
         for (int i = 0; i < actions.length; ++i) {
 181  0
             if (actions[i] instanceof Action) {
 182  0
                 LOG.info("Adding a " + actions[i] + " to the toolbar");
 183  0
                 Action a = (Action) actions[i];
 184  0
                 if (toolBox == null) {
 185  0
                     toolBox = new PopupToolBoxButton(a, 0, 1, rollover);
 186  
                 }
 187  0
                 toolBox.add(a);
 188  0
             } else if (actions[i] instanceof Component) {
 189  0
                 toolBox.add((Component) actions[i]);
 190  0
             } else if (actions[i] instanceof Object[]) {
 191  0
                 Object[] actionRow = (Object[]) actions[i];
 192  0
                 for (int j = 0; j < actionRow.length; ++j) {
 193  0
                     Action a = (Action) actionRow[j];
 194  0
                     if (toolBox == null) {
 195  0
                         int cols = actionRow.length;
 196  0
                         toolBox = new PopupToolBoxButton(a, 0, cols, rollover);
 197  
                     }
 198  0
                     toolBox.add(a);
 199  
                 }
 200  0
             } else {
 201  0
                 LOG.error("Can't add a " + actions[i] + " to the toolbar");
 202  
             }
 203  
         }
 204  0
         return toolBox;
 205  
     }
 206  
 
 207  
 }