Coverage Report - org.argouml.uml.ui.UMLListCellRenderer2
 
Classes in this File Line Coverage Branch Coverage Complexity
UMLListCellRenderer2
55%
41/74
34%
16/46
6
 
 1  
 /* $Id: UMLListCellRenderer2.java 17881 2010-01-12 21:09:28Z 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  
  *    tfmorris
 11  
  *****************************************************************************
 12  
  *
 13  
  * Some portions of this file was previously release using the BSD License:
 14  
  */
 15  
 
 16  
 // Copyright (c) 1996-2008 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.Component;
 42  
 import java.util.Iterator;
 43  
 import java.util.List;
 44  
 
 45  
 import javax.swing.DefaultListCellRenderer;
 46  
 import javax.swing.JLabel;
 47  
 import javax.swing.JList;
 48  
 import javax.swing.UIManager;
 49  
 
 50  
 import org.argouml.application.helpers.ResourceLoaderWrapper;
 51  
 import org.argouml.i18n.Translator;
 52  
 import org.argouml.model.InvalidElementException;
 53  
 import org.argouml.model.Model;
 54  
 
 55  
 /**
 56  
  * The default cell renderer for uml model elements. Used by UMLList2 and its
 57  
  * children.
 58  
  *
 59  
  * This class must be efficient as it is called many 1000's of times.
 60  
  * 
 61  
  * TODO: MVW: Since this class is the only one that knows 
 62  
  * which UML objects influence the rendering, should it 
 63  
  * not be listening to model change events?
 64  
  *
 65  
  * @author jaap.branderhorst@xs4all.nl
 66  
  * @since Jan 2, 2003
 67  
  */
 68  
 public class UMLListCellRenderer2 extends DefaultListCellRenderer {
 69  
 
 70  
 //    private static final Logger LOG =
 71  
 //        Logger.getLogger(UMLListCellRenderer2.class);
 72  
 
 73  
     /**
 74  
      * True if the icon for the modelelement should be shown. The icon is, for
 75  
      * instance, a small class symbol for a class.
 76  
      */
 77  
     private boolean showIcon;
 78  
 
 79  
     /**
 80  
      * True if the containment path should be shown 
 81  
      * (to help the user disambiguate elements with the same name);
 82  
      */
 83  
     private boolean showPath;
 84  
 
 85  
     /**
 86  
      * Constructor for UMLListCellRenderer2.
 87  
      *
 88  
      * @param showTheIcon true if the list should show icons
 89  
      */
 90  
     public UMLListCellRenderer2(boolean showTheIcon) {
 91  3189
         this(showTheIcon, true);
 92  3189
     }
 93  
     
 94  
     /**
 95  
      * Constructor for UMLListCellRenderer2.
 96  
      *
 97  
      * @param showTheIcon true if the list should show icons
 98  
      * @param showThePath true if the list should show containment path
 99  
      */
 100  4989
     public UMLListCellRenderer2(boolean showTheIcon, boolean showThePath) {
 101  
 
 102  
         // only need to this from super()
 103  4989
         updateUI();
 104  4989
         setAlignmentX(LEFT_ALIGNMENT);
 105  
 
 106  4989
         showIcon = showTheIcon;
 107  4989
         showPath = showThePath;
 108  4989
     }
 109  
 
 110  
     /*
 111  
      * @see javax.swing.ListCellRenderer#getListCellRendererComponent(javax.swing.JList,
 112  
      *      java.lang.Object, int, boolean, boolean)
 113  
      */
 114  
     @Override
 115  
     public Component getListCellRendererComponent(JList list, Object value,
 116  
             int index, boolean isSelected, boolean cellHasFocus) {
 117  
         // Leave logging commented out by default for efficiency
 118  
 //        LOG.debug("determine rendering for: " + value);
 119  
 //        LOG.debug("show icon: " + showIcon);
 120  4338
         if (Model.getFacade().isAUMLElement(value)) {
 121  
 
 122  
 //            LOG.debug("is a Base or Multiplicity");
 123  4338
             String text = makeText(value);
 124  4338
             setText(text);
 125  
 
 126  4338
             if (showIcon) {
 127  
 
 128  
                 // ----- setup similar to the super() implementation -----
 129  4338
                 setComponentOrientation(list.getComponentOrientation());
 130  4338
                 if (isSelected) {
 131  0
                     setForeground(list.getSelectionForeground());
 132  0
                     setBackground(list.getSelectionBackground());
 133  
                 } else {
 134  4338
                     setForeground(list.getForeground());
 135  4338
                     setBackground(list.getBackground());
 136  
                 }
 137  
 
 138  4338
                 setEnabled(list.isEnabled());
 139  4338
                 setFont(list.getFont());
 140  4338
                 setBorder((cellHasFocus) ? UIManager
 141  
                         .getBorder("List.focusCellHighlightBorder")
 142  
                         : noFocusBorder);
 143  
                 // --------------------------------------------------------
 144  4338
                 setIcon(ResourceLoaderWrapper.getInstance()
 145  
                         .lookupIcon(value));
 146  
             } else {
 147  
                 // hack to make sure that the right height is
 148  
                 // applied when no icon is used.
 149  0
                 return super.getListCellRendererComponent(list, text, index,
 150  
                         isSelected, cellHasFocus);
 151  
             }
 152  
 
 153  4338
         } else if (value instanceof String) {
 154  0
             return super.getListCellRendererComponent(list, value, index,
 155  
                     isSelected, cellHasFocus);
 156  0
         } else if (value == null || value.equals("")) {
 157  0
             JLabel label = new JLabel(" ");
 158  0
             label.setIcon(null);
 159  0
             return label;
 160  
         }
 161  
 
 162  4338
         return this;
 163  
     }
 164  
 
 165  
     /**
 166  
      * Makes the text that must be placed on the label that is returned.
 167  
      * If there is no name for the given modelelement, then
 168  
      * (anon xxx) is shown, with xxx the type name.
 169  
      *
 170  
      * @param value the given modelelement
 171  
      * @return String the text to be shown
 172  
      */
 173  
     public String makeText(Object value) {
 174  7287
         if (value instanceof String) {
 175  0
             return (String) value;
 176  
         }
 177  7287
         String name = null;
 178  7287
         if (Model.getFacade().isAParameter(value)) {
 179  0
             Object type = Model.getFacade().getType(value);
 180  0
             name = getName(value);
 181  0
             String typeName = null;
 182  0
             if (type != null) {
 183  0
                 typeName = Model.getFacade().getName(type);
 184  
             }
 185  0
             if (typeName != null || "".equals(typeName)) {
 186  0
                 name = Translator.localize(
 187  
                         "misc.name.withType",
 188  
                         new Object[] {name, typeName});
 189  
             }
 190  0
             return name;
 191  
         }
 192  7287
         if (Model.getFacade().isAUMLElement(value)) {
 193  
             try {
 194  7287
                 name = getName(value);
 195  7287
                 if (Model.getFacade().isAStereotype(value)) {
 196  0
                     String baseString = "";
 197  0
                     Iterator bases =
 198  
                             Model.getFacade().getBaseClasses(value).iterator();
 199  0
                     if (bases.hasNext()) {
 200  0
                         baseString = makeText(bases.next());
 201  0
                         while (bases.hasNext()) {
 202  0
                             baseString = Translator.localize(
 203  
                                     "misc.name.baseClassSeparator",
 204  
                                     new Object[] {baseString, 
 205  
                                                   makeText(bases.next())
 206  
                                     }
 207  
                             );
 208  
                         }
 209  
                     }
 210  0
                     name = Translator.localize(
 211  
                             "misc.name.withBaseClasses",
 212  
                             new Object[] {name, baseString});
 213  0
                 } else if (showPath) {
 214  7287
                     List pathList =
 215  
                             Model.getModelManagementHelper().getPathList(value);
 216  
                     String path;
 217  7287
                     if (pathList.size() > 1) {
 218  1004
                         path = (String) pathList.get(0);
 219  1004
                         for (int i = 1; i < pathList.size() - 1; i++) {
 220  0
                             String n = (String) pathList.get(i);
 221  0
                             path = Translator.localize(
 222  
                                             "misc.name.pathSeparator",
 223  
                                             new Object[] {path, n});
 224  
                         }
 225  1004
                         name = Translator.localize(
 226  
                                         "misc.name.withPath",
 227  
                                         new Object[] {name, path});
 228  
                     }
 229  
                 }
 230  0
             } catch (InvalidElementException e) {
 231  0
                 name = Translator.localize("misc.name.deleted");
 232  7287
             }
 233  0
         } else if (Model.getFacade().isAMultiplicity(value)) {
 234  0
             name = Model.getFacade().getName(value);
 235  
         } else {
 236  0
             name = makeTypeName(value);
 237  
         }
 238  7287
         return name;
 239  
 
 240  
     }
 241  
 
 242  
     private String getName(Object value) {
 243  7287
         String name = Model.getFacade().getName(value);
 244  7287
         if (name == null || name.equals("")) {
 245  602
             name = Translator.localize(
 246  
                             "misc.name.unnamed",
 247  
                             new Object[] {makeTypeName(value)});
 248  
         }
 249  7287
         return name;
 250  
     }
 251  
 
 252  
     private String makeTypeName(Object elem) {
 253  602
         if (Model.getFacade().isAUMLElement(elem)) {
 254  602
             return Model.getFacade().getUMLClassName(elem);
 255  
         }
 256  0
         return null;
 257  
     }
 258  
 
 259  
 }