Coverage Report - org.argouml.uml.diagram.ui.SettingsTabDiagramAppearance
 
Classes in this File Line Coverage Branch Coverage Complexity
SettingsTabDiagramAppearance
94%
112/118
53%
17/32
1.5
SettingsTabDiagramAppearance$1
100%
7/7
100%
2/2
1.5
SettingsTabDiagramAppearance$2
60%
3/5
N/A
1.5
 
 1  
 /* $Id: SettingsTabDiagramAppearance.java 17865 2010-01-12 20:45:26Z 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) 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.ui;
 39  
 
 40  
 import java.awt.BorderLayout;
 41  
 import java.awt.Component;
 42  
 import java.awt.Dimension;
 43  
 import java.awt.FlowLayout;
 44  
 import java.awt.GridBagConstraints;
 45  
 import java.awt.GridBagLayout;
 46  
 import java.awt.Insets;
 47  
 import java.awt.event.ActionEvent;
 48  
 import java.awt.event.ActionListener;
 49  
 
 50  
 import javax.swing.BoxLayout;
 51  
 import javax.swing.JButton;
 52  
 import javax.swing.JCheckBox;
 53  
 import javax.swing.JLabel;
 54  
 import javax.swing.JPanel;
 55  
 
 56  
 import org.argouml.application.api.Argo;
 57  
 import org.argouml.configuration.Configuration;
 58  
 import org.argouml.configuration.ConfigurationKey;
 59  
 import org.argouml.i18n.Translator;
 60  
 import org.argouml.kernel.Project;
 61  
 import org.argouml.kernel.ProjectSettings;
 62  
 import org.argouml.swingext.JLinkButton;
 63  
 import org.argouml.ui.ActionProjectSettings;
 64  
 import org.argouml.ui.ArgoJFontChooser;
 65  
 import org.argouml.ui.GUIProjectSettingsTabInterface;
 66  
 import org.argouml.ui.ShadowComboBox;
 67  
 import org.argouml.uml.diagram.DiagramAppearance;
 68  
 import org.argouml.uml.diagram.DiagramSettings;
 69  
 import org.argouml.uml.diagram.DiagramUndoManager;
 70  
 import org.argouml.util.ArgoFrame;
 71  
 import org.tigris.gef.undo.Memento;
 72  
 
 73  
 /**
 74  
  * Settings tab panel for handling diagram appearance settings.
 75  
  * <p>
 76  
  * It supports different scopes: application and project. The former is stored
 77  
  * in the properties file in the user-directory, the latter in the project file
 78  
  * (.zargo,...).
 79  
  * <p>
 80  
  * This class is written in a way that supports adding more scopes easily.
 81  
  *
 82  
  * @author Aleksandar
 83  
  */
 84  951
 public class SettingsTabDiagramAppearance extends JPanel implements
 85  
         GUIProjectSettingsTabInterface {
 86  
 
 87  
     private Project p;
 88  
     
 89  
     private JButton jbtnDiagramFont;
 90  
     private JCheckBox showBoldNames;
 91  
     private JCheckBox hideBidirectionalArrows;
 92  
     private ShadowComboBox defaultShadowWidth;
 93  
 
 94  
     private String selectedDiagramFontName;
 95  
 
 96  
     private int selectedDiagramFontSize;
 97  
 
 98  
     private int scope;
 99  
 
 100  1800
     private JLabel jlblDiagramFont = null;
 101  
 
 102  
     /**
 103  
      * The constructor. We currently support 2 scopes, but this class is written
 104  
      * in a way to easily extend that.
 105  
      *
 106  
      * @param settingsScope the scope of the settings
 107  
      */
 108  
     public SettingsTabDiagramAppearance(int settingsScope) {
 109  1800
         super();
 110  1800
         scope = settingsScope;
 111  1800
         initialize();
 112  1800
     }
 113  
 
 114  
     /**
 115  
      * This method initializes this
 116  
      */
 117  
     private void initialize() {
 118  
         
 119  1800
         this.setLayout(new BorderLayout());
 120  1800
         JPanel top = new JPanel();
 121  1800
         top.setLayout(new BorderLayout());
 122  
         
 123  1800
         if (scope == Argo.SCOPE_APPLICATION) {
 124  900
             JPanel warning = new JPanel();
 125  900
             warning.setLayout(new BoxLayout(warning, BoxLayout.PAGE_AXIS));
 126  900
             JLabel warningLabel = new JLabel(
 127  
                 Translator.localize("label.warning"));
 128  900
             warningLabel.setAlignmentX(Component.RIGHT_ALIGNMENT);
 129  900
             warning.add(warningLabel);
 130  
 
 131  900
             JLinkButton projectSettings = new JLinkButton();
 132  900
             projectSettings.setAction(new ActionProjectSettings());
 133  900
             projectSettings.setText(
 134  
                 Translator.localize("button.project-settings"));
 135  900
             projectSettings.setIcon(null);
 136  900
             projectSettings.setAlignmentX(Component.RIGHT_ALIGNMENT);
 137  900
             warning.add(projectSettings);
 138  
             
 139  900
             top.add(warning, BorderLayout.NORTH);
 140  
         }
 141  
         
 142  1800
         JPanel settings = new JPanel();
 143  1800
         settings.setLayout(new GridBagLayout());
 144  
 
 145  1800
         GridBagConstraints constraints = new GridBagConstraints();
 146  1800
         constraints.anchor = GridBagConstraints.WEST;
 147  1800
         constraints.fill = GridBagConstraints.HORIZONTAL;
 148  1800
         constraints.gridy = 0;
 149  1800
         constraints.gridx = 0;
 150  1800
         constraints.gridwidth = 1;
 151  1800
         constraints.gridheight = 1;
 152  1800
         constraints.weightx = 1.0;
 153  1800
         constraints.insets = new Insets(0, 30, 0, 4);
 154  
 
 155  1800
         constraints.gridy = GridBagConstraints.RELATIVE;
 156  
 
 157  1800
         showBoldNames = createCheckBox("label.show-bold-names");
 158  1800
         settings.add(showBoldNames, constraints);
 159  
 
 160  1800
         hideBidirectionalArrows = 
 161  
             createCheckBox("label.hide-bidirectional-arrows");
 162  1800
         settings.add(hideBidirectionalArrows, constraints);        
 163  
 
 164  1800
         constraints.insets = new Insets(5, 30, 0, 4);
 165  1800
         JPanel defaultShadowWidthPanel = new JPanel(new FlowLayout(
 166  
             FlowLayout.LEFT, 5, 0));
 167  1800
         JLabel defaultShadowWidthLabel = createLabel(
 168  
             "label.default-shadow-width");
 169  1800
         defaultShadowWidth = new ShadowComboBox();
 170  1800
         defaultShadowWidthLabel.setLabelFor(defaultShadowWidth);
 171  1800
         defaultShadowWidthPanel.add(defaultShadowWidthLabel);
 172  1800
         defaultShadowWidthPanel.add(defaultShadowWidth);
 173  1800
         settings.add(defaultShadowWidthPanel, constraints);
 174  
 
 175  1800
         jlblDiagramFont = new JLabel();
 176  1800
         jlblDiagramFont.setText(Translator
 177  
                 .localize("label.diagramappearance.diagramfont"));       
 178  1800
         JPanel fontButtonPanel = new JPanel(new FlowLayout(
 179  
                 FlowLayout.LEFT, 5, 0));
 180  1800
         JButton fontButton = getJbtnDiagramFont();
 181  1800
         jlblDiagramFont.setLabelFor(fontButton);
 182  1800
         fontButtonPanel.add(jlblDiagramFont);
 183  1800
         fontButtonPanel.add(fontButton);
 184  1800
         settings.add(fontButtonPanel, constraints);
 185  
 
 186  1800
         top.add(settings, BorderLayout.CENTER);
 187  
         
 188  1800
         this.add(top, BorderLayout.NORTH);
 189  1800
         this.setSize(new Dimension(296, 169));
 190  
 
 191  1800
     }
 192  
 
 193  
     private JButton getJbtnDiagramFont() {
 194  1800
         if (jbtnDiagramFont == null) {
 195  1800
             jbtnDiagramFont = new JButton(
 196  
                     Translator.localize("label.diagramappearance.changefont"));
 197  
 
 198  1800
             jbtnDiagramFont.addActionListener(new ActionListener() {
 199  
                 public void actionPerformed(ActionEvent e) {
 200  11
                     ArgoJFontChooser jFontChooser = new ArgoJFontChooser(
 201  
                             ArgoFrame.getFrame(), jbtnDiagramFont,
 202  
                             selectedDiagramFontName, selectedDiagramFontSize);
 203  11
                     jFontChooser.setVisible(true);
 204  
 
 205  10
                     if (jFontChooser.isOk()) {
 206  9
                         selectedDiagramFontName = jFontChooser.getResultName();
 207  9
                         selectedDiagramFontSize = jFontChooser.getResultSize();
 208  
                     }
 209  10
                 }
 210  
             });
 211  
 
 212  
         }
 213  1800
         return jbtnDiagramFont;
 214  
     }
 215  
 
 216  
     /*
 217  
      * @see org.argouml.ui.GUISettingsTabInterface#handleSettingsTabRefresh()
 218  
      */
 219  
     public void handleSettingsTabRefresh() {
 220  143
         if (scope == Argo.SCOPE_APPLICATION) {
 221  19
             showBoldNames.setSelected(getBoolean(
 222  
                     DiagramAppearance.KEY_SHOW_BOLD_NAMES));
 223  19
             selectedDiagramFontName = DiagramAppearance.getInstance()
 224  
                     .getConfiguredFontName();
 225  19
             selectedDiagramFontSize = Configuration
 226  
                     .getInteger(DiagramAppearance.KEY_FONT_SIZE);
 227  
             /*
 228  
              * The next one defaults to TRUE, despite that this is
 229  
              * NOT compatible with older ArgoUML versions
 230  
              * (before 0.28?) that did
 231  
              * not have this setting - see issue 535
 232  
              */
 233  19
             hideBidirectionalArrows.setSelected(Configuration.getBoolean(
 234  
                     DiagramAppearance.KEY_HIDE_BIDIRECTIONAL_ARROWS, true));
 235  19
             defaultShadowWidth.setSelectedIndex(Configuration.getInteger(
 236  
                     DiagramAppearance.KEY_DEFAULT_SHADOW_WIDTH, 1));
 237  
         }
 238  143
         if (scope == Argo.SCOPE_PROJECT) {
 239  124
             assert p != null;
 240  124
             ProjectSettings ps = p.getProjectSettings();
 241  124
             DiagramSettings ds = ps.getDefaultDiagramSettings();
 242  
 
 243  124
             showBoldNames.setSelected(ds.isShowBoldNames());
 244  124
             selectedDiagramFontName = ds.getFontName();
 245  124
             selectedDiagramFontSize = ds.getFontSize();
 246  124
             hideBidirectionalArrows.setSelected(!
 247  
                     ds.isShowBidirectionalArrows());
 248  124
             defaultShadowWidth.setSelectedIndex(
 249  
                     ds.getDefaultShadowWidth());
 250  
         }
 251  143
     }
 252  
 
 253  
     /**
 254  
      * Get a boolean from the configuration.
 255  
      *
 256  
      * @param key a notation key.
 257  
      * @return a boolean
 258  
      */
 259  
     protected static boolean getBoolean(ConfigurationKey key) {
 260  25
         return Configuration.getBoolean(key, false);
 261  
     }
 262  
 
 263  
     /*
 264  
      * @see org.argouml.ui.GUISettingsTabInterface#handleSettingsTabSave()
 265  
      */
 266  
     public void handleSettingsTabSave() {
 267  12
         if (scope == Argo.SCOPE_APPLICATION) {
 268  0
             Configuration.setBoolean(DiagramAppearance.KEY_SHOW_BOLD_NAMES,
 269  
                     showBoldNames.isSelected());
 270  0
             Configuration.setString(DiagramAppearance.KEY_FONT_NAME,
 271  
                     selectedDiagramFontName);
 272  0
             Configuration.setInteger(DiagramAppearance.KEY_FONT_SIZE,
 273  
                     selectedDiagramFontSize);
 274  0
             Configuration.setBoolean(DiagramAppearance.KEY_HIDE_BIDIRECTIONAL_ARROWS,
 275  
                     hideBidirectionalArrows.isSelected());            
 276  0
             Configuration.setInteger(DiagramAppearance.KEY_DEFAULT_SHADOW_WIDTH,
 277  
                     defaultShadowWidth.getSelectedIndex());
 278  
         }
 279  12
         if (scope == Argo.SCOPE_PROJECT) {
 280  12
             assert p != null;
 281  12
             ProjectSettings ps = p.getProjectSettings();
 282  12
             final DiagramSettings ds = ps.getDefaultDiagramSettings();
 283  
 
 284  12
             ds.setShowBoldNames(showBoldNames.isSelected());
 285  12
             ds.setFontName(selectedDiagramFontName);
 286  12
             ds.setFontSize(selectedDiagramFontSize);
 287  12
             ds.setDefaultShadowWidth(defaultShadowWidth.getSelectedIndex());
 288  12
             ds.setShowBidirectionalArrows(!hideBidirectionalArrows.isSelected());
 289  
 
 290  
             /* Update the diagram, and other users of these settings: */
 291  12
             Memento memento = new Memento() {
 292  
                 public void redo() {
 293  12
                     ds.notifyOfChangedSettings();
 294  12
                 }
 295  
 
 296  
                 public void undo() {
 297  0
                     ds.notifyOfChangedSettings();
 298  0
                 }
 299  
             };
 300  
             // TODO: Undo should be managed externally or we should be given 
 301  
             // an Undo manager to use (the project's) rather than using a global one
 302  
 //            if (DiagramUndoManager.getInstance().isGenerateMementos()) {
 303  
 //                DiagramUndoManager.getInstance().addMemento(memento);
 304  
 //            }
 305  12
            memento.redo();
 306  
         }
 307  12
     }
 308  
 
 309  
     /*
 310  
      * @see org.argouml.ui.GUISettingsTabInterface#handleSettingsTabCancel()
 311  
      */
 312  
     public void handleSettingsTabCancel() {
 313  6
         handleSettingsTabRefresh();
 314  6
     }
 315  
 
 316  
     /*
 317  
      * @see org.argouml.ui.GUISettingsTabInterface#handleResetToDefault()
 318  
      */
 319  
     public void handleResetToDefault() {
 320  6
         if (scope == Argo.SCOPE_PROJECT) {
 321  6
             showBoldNames.setSelected(getBoolean(
 322  
                     DiagramAppearance.KEY_SHOW_BOLD_NAMES));
 323  6
             selectedDiagramFontName = DiagramAppearance.getInstance()
 324  
                     .getConfiguredFontName();
 325  6
             selectedDiagramFontSize = Configuration
 326  
                     .getInteger(DiagramAppearance.KEY_FONT_SIZE);
 327  6
             hideBidirectionalArrows.setSelected(Configuration.getBoolean(
 328  
                     DiagramAppearance.KEY_HIDE_BIDIRECTIONAL_ARROWS, true));
 329  6
             defaultShadowWidth.setSelectedIndex(Configuration.getInteger(
 330  
                     DiagramAppearance.KEY_DEFAULT_SHADOW_WIDTH, 1)); 
 331  
         }
 332  6
     }
 333  
 
 334  
     /*
 335  
      * @see org.argouml.ui.GUISettingsTabInterface#getTabKey()
 336  
      */
 337  
     public String getTabKey() {
 338  119
         return "tab.diagramappearance";
 339  
     }
 340  
 
 341  
     /*
 342  
      * @see org.argouml.ui.GUISettingsTabInterface#getTabPanel()
 343  
      */
 344  
     public JPanel getTabPanel() {
 345  119
         return this;
 346  
     }
 347  
 
 348  
     /**
 349  
      * Create a localized JCheckBox.
 350  
      *
 351  
      * @param key the key for the string to be localized
 352  
      * @return a new checkbox with localized text
 353  
      */
 354  
     protected JCheckBox createCheckBox(String key) {
 355  3600
         JCheckBox j = new JCheckBox(Translator.localize(key));
 356  3600
         return j;
 357  
     }
 358  
 
 359  
     /**
 360  
      * Create a localized JLabel.
 361  
      *
 362  
      * @param key the key of the text for the label
 363  
      * @return a new label with a localized text for the given key
 364  
      */
 365  
     protected JLabel createLabel(String key) {
 366  1800
         return new JLabel(Translator.localize(key));
 367  
     }
 368  
 
 369  
     /**
 370  
      * Create a localized JButton.
 371  
      *
 372  
      * @param key the key for the string to be localized
 373  
      * @return a new checkbox with localized text
 374  
      */
 375  
     protected JButton createButton(String key) {
 376  0
         return new JButton(Translator.localize(key));
 377  
     }
 378  
 
 379  
     /*
 380  
      * @see javax.swing.JComponent#setVisible(boolean)
 381  
      */
 382  
     public void setVisible(boolean arg0) {
 383  140
         super.setVisible(arg0);
 384  140
         if (arg0) handleSettingsTabRefresh();
 385  140
     }
 386  
 
 387  
     public void setProject(Project project) {
 388  100
         assert project != null;
 389  100
         p = project;
 390  100
     }
 391  
 } // @jve:decl-index=0:visual-constraint="10,10"