Coverage Report - org.argouml.uml.diagram.DiagramAppearance
 
Classes in this File Line Coverage Branch Coverage Complexity
DiagramAppearance
96%
27/28
75%
3/4
1.4
 
 1  
 /* $Id: DiagramAppearance.java 17850 2010-01-12 19:53:35Z 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) 2007-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  
 package org.argouml.uml.diagram;
 39  
 
 40  
 import java.awt.Font;
 41  
 import java.beans.PropertyChangeEvent;
 42  
 import java.beans.PropertyChangeListener;
 43  
 
 44  
 import javax.swing.UIManager;
 45  
 
 46  
 import org.apache.log4j.Logger;
 47  
 import org.argouml.application.events.ArgoDiagramAppearanceEvent;
 48  
 import org.argouml.application.events.ArgoEventPump;
 49  
 import org.argouml.application.events.ArgoEventTypes;
 50  
 import org.argouml.configuration.Configuration;
 51  
 import org.argouml.configuration.ConfigurationKey;
 52  
 
 53  
 /**
 54  
  * Provides centralized methods dealing with diagram appearance.
 55  
  * <p>
 56  
  * These settings do not apply to the appearance of the ArgoUML application! <p>
 57  
  * 
 58  
  * In the MVC pattern, this is part of the Model.
 59  
  *
 60  
  * @stereotype singleton
 61  
  * @author Aleksandar
 62  
  */
 63  
 public final class DiagramAppearance implements PropertyChangeListener {
 64  
 
 65  
     /**
 66  
      * Define a static log4j category variable for ArgoUML diagram appearance.
 67  
      */
 68  900
     private static final Logger LOG = Logger.getLogger(DiagramAppearance.class);
 69  
 
 70  
     /**
 71  
      * The configuration key for the font name.
 72  
      */
 73  900
     public static final ConfigurationKey KEY_FONT_NAME = Configuration.makeKey(
 74  
             "diagramappearance", "fontname");
 75  
 
 76  
     /**
 77  
      * The configuration key for the font size.
 78  
      */
 79  900
     public static final ConfigurationKey KEY_FONT_SIZE = Configuration.makeKey(
 80  
             "diagramappearance", "fontsize");
 81  
 
 82  
     /**
 83  
      * The configuration key that indicates whether to show bold names.
 84  
      */
 85  900
     public static final ConfigurationKey KEY_SHOW_BOLD_NAMES =
 86  
         Configuration.makeKey("notation", "show", "bold", "names");
 87  
 
 88  
     /**
 89  
      * Default value for the shadow size of classes, interfaces etc.
 90  
      */
 91  900
     public static final ConfigurationKey KEY_DEFAULT_SHADOW_WIDTH =
 92  
         Configuration.makeKey("notation", "default", "shadow-width");
 93  
 
 94  
     /**
 95  
      * Indicates if the user wants to see the arrows when both
 96  
      * association ends in an association are navigable.
 97  
      */    
 98  900
     public static final ConfigurationKey KEY_HIDE_BIDIRECTIONAL_ARROWS =
 99  
         Configuration.makeKey("notation", "hide", "bidirectional-arrows");
 100  
     
 101  
     /**
 102  
      * The instance.
 103  
      */
 104  900
     private static final DiagramAppearance SINGLETON = new DiagramAppearance();
 105  
 
 106  
     /**
 107  
      * Used for FigNodeModelElement#setStereotypeView(). 
 108  
      * Represents the default view for 
 109  
      * stereotypes applied to this node.
 110  
      * 
 111  
      * @see org.argouml.uml.diagram.ui.ActionStereotypeViewTextual
 112  
      */
 113  
     public static final int STEREOTYPE_VIEW_TEXTUAL = 0;
 114  
 
 115  
     /**
 116  
      * Used for FigNodeModelElement#setStereotypeView(). 
 117  
      * Represents the view for stereotypes where the 
 118  
      * default representation is replaced by a provided
 119  
      * icon. 
 120  
      * 
 121  
      * @see org.argouml.uml.diagram.ui.ActionStereotypeViewBigIcon
 122  
      */
 123  
     public static final int STEREOTYPE_VIEW_BIG_ICON = 1;
 124  
 
 125  
     /**
 126  
      * Used for FigNodeModelElement#setStereotypeView(). 
 127  
      * Represents the view for stereotypes where the 
 128  
      * default view is adorned with a small version of the
 129  
      * provided icon.
 130  
      * 
 131  
      * @see org.argouml.uml.diagram.ui.ActionStereotypeViewSmallIcon
 132  
      */
 133  
     public static final int STEREOTYPE_VIEW_SMALL_ICON = 2;
 134  
 
 135  
     /**
 136  
      * The constructor.
 137  
      * TODO: Why does this method not handle all settings
 138  
      * (KEY_DEFAULT_SHADOW_WIDTH is missing)?
 139  
      */
 140  900
     private DiagramAppearance() {
 141  900
         Configuration.addListener(DiagramAppearance.KEY_FONT_NAME, this);
 142  900
         Configuration.addListener(DiagramAppearance.KEY_FONT_SIZE, this);
 143  900
         Configuration.addListener(KEY_SHOW_BOLD_NAMES, this);
 144  900
         Configuration.addListener(KEY_HIDE_BIDIRECTIONAL_ARROWS, this);
 145  900
     }
 146  
 
 147  
     /**
 148  
      * @return the singleton
 149  
      */
 150  
     public static DiagramAppearance getInstance() {
 151  993
         return SINGLETON;
 152  
     }
 153  
 
 154  
     /*
 155  
      * Called after the diagram font gets changed. <p>
 156  
      * 
 157  
      * TODO: Do we need to do anything here?
 158  
      *
 159  
      * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
 160  
      */
 161  
     public void propertyChange(PropertyChangeEvent pce) {
 162  1800
         LOG.info("Diagram appearance change:" + pce.getOldValue() + " to "
 163  
                 + pce.getNewValue());
 164  1800
         ArgoEventPump.fireEvent(
 165  
                 new ArgoDiagramAppearanceEvent(ArgoEventTypes.DIAGRAM_FONT_CHANGED, pce));
 166  1800
     }
 167  
 
 168  
     /**
 169  
      * Gets font name. If it doesn't exist in configuration it creates new
 170  
      * entries in configuration for appearance.
 171  
      * 
 172  
      * TODO: Why create in a getter?
 173  
      *
 174  
      * @return the name of the configured font
 175  
      */
 176  
     public String getConfiguredFontName() {
 177  993
         String fontName = Configuration
 178  
                 .getString(DiagramAppearance.KEY_FONT_NAME);
 179  993
         if (fontName.equals("")) {
 180  900
             Font f = getStandardFont();
 181  900
             fontName = f.getName();
 182  
 
 183  900
             Configuration.setString(DiagramAppearance.KEY_FONT_NAME, f
 184  
                     .getName());
 185  900
             Configuration.setInteger(DiagramAppearance.KEY_FONT_SIZE, f
 186  
                     .getSize());
 187  
         }
 188  
 
 189  993
         return fontName;
 190  
     }
 191  
     
 192  
     /**
 193  
      * This is the same function as 
 194  
      * LookAndFeelMgr.getInstance().getStandardFont();
 195  
      * but used for a totally different puropose: here it determines 
 196  
      * a default font when none is set. In the LookAndFeelMgr it
 197  
      * determines the looks of the UI. 
 198  
      * 
 199  
      * @return the standard textfield font
 200  
      */
 201  
     private Font getStandardFont() {
 202  900
         Font font = UIManager.getDefaults().getFont("TextField.font");
 203  900
         if (font == null) {
 204  0
             font = (new javax.swing.JTextField()).getFont();
 205  
         }
 206  900
         return font;
 207  
     }
 208  
 }