Coverage Report - org.argouml.uml.ui.UMLComboBox2
 
Classes in this File Line Coverage Branch Coverage Complexity
UMLComboBox2
35%
19/54
0%
0/20
1.909
 
 1  
 /* $Id: UMLComboBox2.java 18735 2010-09-13 11:37:06Z bobtarling $
 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  
  *    tfmorris
 11  
  *****************************************************************************
 12  
  *
 13  
  * Some portions of this file was previously release using the BSD License:
 14  
  */
 15  
 
 16  
 // Copyright (c) 1996-2009 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;
 40  
 
 41  
 import java.awt.event.ActionEvent;
 42  
 import java.awt.event.KeyEvent;
 43  
 
 44  
 import javax.swing.Action;
 45  
 import javax.swing.ComboBoxModel;
 46  
 import javax.swing.JComboBox;
 47  
 import javax.swing.ListCellRenderer;
 48  
 
 49  
 import org.argouml.ui.LookAndFeelMgr;
 50  
 import org.argouml.ui.targetmanager.TargetEvent;
 51  
 import org.argouml.ui.targetmanager.TargetListener;
 52  
 import org.argouml.ui.targetmanager.TargettableModelView;
 53  
 
 54  
 
 55  
 /**
 56  
  * ComboBox for selecting UML Elements.
 57  
  * @deprecated in 0.31.5 by Bob Tarling. Property panel controls are now
 58  
  * internal to the property panel component
 59  
  */
 60  
 @Deprecated
 61  
 public class UMLComboBox2
 62  
     extends JComboBox
 63  
     implements TargettableModelView, TargetListener, 
 64  
         JComboBox.KeySelectionManager {
 65  
 
 66  
     private static final int KEY_TIME_THRESHOLD_MILLIS = 1500;
 67  3189
     private String searchString = "";
 68  
     private long lastKeyEventTime;
 69  
 
 70  
     /**
 71  
      * Constructor for UMLComboBox2.
 72  
      * @deprecated As of ArgoUml version unknown (before 0.13.5),
 73  
      * replaced by {@link #UMLComboBox2(UMLComboBoxModel2, Action, boolean)}
 74  
      * @param model the ComboBoxModel
 75  
      */
 76  
     @Deprecated
 77  
     protected UMLComboBox2(UMLComboBoxModel2 model) {
 78  900
         super(model);
 79  900
         setFont(LookAndFeelMgr.getInstance().getStandardFont());
 80  900
         addActionListener(this);
 81  900
         addPopupMenuListener(model);
 82  900
     }
 83  
 
 84  
     /**
 85  
      * Construct a UMLComboBox2.
 86  
      * 
 87  
      * @param model a UMLComboBoxModel2 which provides UML elements for the user
 88  
      *            to choose from
 89  
      * @param action action to invoke when an item is selected
 90  
      * @param showIcon true if an icon should be shown in front of the items
 91  
      */
 92  
     public UMLComboBox2(UMLComboBoxModel2 model, Action action,
 93  
                         boolean showIcon) {
 94  2289
         super(model);
 95  2289
         setFont(LookAndFeelMgr.getInstance().getStandardFont());
 96  2289
         lastKeyEventTime = 0;
 97  2289
         addActionListener(action);
 98  
         // setDoubleBuffered(true);
 99  2289
         setKeySelectionManager(this);
 100  2289
         setRenderer(new UMLListCellRenderer2(showIcon));
 101  2289
         addPopupMenuListener(model);
 102  2289
     }
 103  
 
 104  
     /**
 105  
      * Construct a UML Element ComboxBox which shows icons in front of each item
 106  
      *
 107  
      * @param arg0 the ComboBoxModel
 108  
      * @param action the action
 109  
      */
 110  
     public UMLComboBox2(UMLComboBoxModel2 arg0, Action action) {
 111  0
         this(arg0, action, true);
 112  0
     }
 113  
 
 114  
     /*
 115  
      * @see java.awt.event.ActionListener#actionPerformed(ActionEvent)
 116  
      */
 117  
     @Override
 118  
     public void actionPerformed(ActionEvent arg0) {
 119  0
         int i = getSelectedIndex();
 120  0
         if (i >= 0) {
 121  0
             doIt(arg0);
 122  
         }
 123  0
     }
 124  
     
 125  
     /**
 126  
      * Implementation of the JComboBox.KeySelectionManager interface. Helps with
 127  
      * navigating through the ComboBox when using the keyboard.
 128  
      * 
 129  
      * @param key The key which has been pressed
 130  
      * @param model Current ComboBoxModel
 131  
      * @return int The index of the item that is to be selected
 132  
      * @see javax.swing.JComboBox.KeySelectionManager#selectionForKey(char, javax.swing.ComboBoxModel)
 133  
      */
 134  
     public int selectionForKey(char key, ComboBoxModel model)
 135  
     {        
 136  0
         long now = System.currentTimeMillis();
 137  0
         int index = -1;
 138  0
         int startAtIndex = 0;
 139  
 
 140  
         // Implements backspace functionality
 141  0
         if (searchString != null && key == KeyEvent.VK_BACK_SPACE
 142  
                 && searchString.length() > 0) {
 143  0
             searchString = searchString.substring(0, searchString.length() - 1);
 144  
         }
 145  
         else {
 146  0
             if (lastKeyEventTime + KEY_TIME_THRESHOLD_MILLIS < now) {
 147  0
                 searchString = Character.toString(key);
 148  
             }
 149  
             else {
 150  0
                 searchString = searchString + key;
 151  0
                 startAtIndex = getSelectedIndex();
 152  0
                 if (startAtIndex < 0) {
 153  0
                     startAtIndex = 0;
 154  
                 }
 155  
             }
 156  
         }
 157  
         
 158  0
         String umlElemName = "";
 159  0
         String searchStringLowerCase = searchString.toLowerCase();
 160  0
         ListCellRenderer cellRenderer = getRenderer();
 161  
         
 162  0
         for (int i = startAtIndex, length = model.getSize(); i < length; i++) {
 163  0
             Object value = model.getElementAt(i);
 164  0
             if (cellRenderer instanceof UMLListCellRenderer2) {
 165  0
                 umlElemName = 
 166  
                     ((UMLListCellRenderer2) cellRenderer).makeText(value);
 167  
             } else {
 168  0
                 umlElemName = value.toString();
 169  
             }
 170  0
             if (umlElemName.toLowerCase().startsWith(searchStringLowerCase)) {
 171  0
                 index = i;
 172  0
                 break;
 173  
             }
 174  
         }
 175  0
         lastKeyEventTime = now;
 176  
 
 177  0
         return index;
 178  
     }
 179  
 
 180  
     /**
 181  
      * The 'body' of the actionPerformed method. Is only called if there is
 182  
      * actually a selection made.
 183  
      *
 184  
      * @param event the event
 185  
      */
 186  0
     protected void doIt(@SuppressWarnings("unused") ActionEvent event) { }
 187  
 
 188  
     /**
 189  
      * Utility method to get the current target.
 190  
      *
 191  
      * @return Object
 192  
      */
 193  
     public Object getTarget() {
 194  0
         return ((UMLComboBoxModel2) getModel()).getTarget();
 195  
     }
 196  
 
 197  
 
 198  
     /*
 199  
      * @see org.argouml.ui.targetmanager.TargettableModelView#getTargettableModel()
 200  
      */
 201  
     public TargetListener getTargettableModel() {
 202  2289
         return (TargetListener) getModel();
 203  
     }
 204  
 
 205  
     /*
 206  
      * @see org.argouml.ui.targetmanager.TargetListener#targetAdded(org.argouml.ui.targetmanager.TargetEvent)
 207  
      */
 208  
     public void targetAdded(TargetEvent e) {
 209  0
         if (e.getNewTarget() != getTarget()) {
 210  0
             removeActionListener(this);
 211  
         }
 212  0
     }
 213  
 
 214  
     /*
 215  
      * @see org.argouml.ui.targetmanager.TargetListener#targetRemoved(org.argouml.ui.targetmanager.TargetEvent)
 216  
      */
 217  
     public void targetRemoved(TargetEvent e) {
 218  176
         removeActionListener(this);
 219  176
     }
 220  
 
 221  
     /*
 222  
      * @see org.argouml.ui.targetmanager.TargetListener#targetSet(org.argouml.ui.targetmanager.TargetEvent)
 223  
      */
 224  
     public void targetSet(TargetEvent e) {
 225  4547
         addActionListener(this);
 226  4547
     }
 227  
 }