Coverage Report - org.argouml.notation.ui.SettingsTabNotation
 
Classes in this File Line Coverage Branch Coverage Complexity
SettingsTabNotation
93%
133/143
53%
16/30
1.692
 
 1  
 /* $Id: SettingsTabNotation.java 17829 2010-01-12 18:55:38Z 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-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.notation.ui;
 40  
 
 41  
 import java.awt.BorderLayout;
 42  
 import java.awt.Component;
 43  
 import java.awt.FlowLayout;
 44  
 import java.awt.GridBagConstraints;
 45  
 import java.awt.GridBagLayout;
 46  
 import java.awt.Insets;
 47  
 
 48  
 import javax.swing.BoxLayout;
 49  
 import javax.swing.JCheckBox;
 50  
 import javax.swing.JComboBox;
 51  
 import javax.swing.JLabel;
 52  
 import javax.swing.JPanel;
 53  
 
 54  
 import org.argouml.application.api.Argo;
 55  
 import org.argouml.configuration.Configuration;
 56  
 import org.argouml.configuration.ConfigurationKey;
 57  
 import org.argouml.i18n.Translator;
 58  
 import org.argouml.kernel.Project;
 59  
 import org.argouml.kernel.ProjectSettings;
 60  
 import org.argouml.notation.Notation;
 61  
 import org.argouml.notation.NotationName;
 62  
 import org.argouml.notation.NotationSettings;
 63  
 import org.argouml.swingext.JLinkButton;
 64  
 import org.argouml.ui.ActionProjectSettings;
 65  
 import org.argouml.ui.GUIProjectSettingsTabInterface;
 66  
 
 67  
 /**
 68  
  * Settings tab panel for handling Notation settings. <p>
 69  
  *
 70  
  * It supports different scopes: application and project.
 71  
  * The former is stored in the properties file in the user-directory,
 72  
  * the latter in the project file (.zargo,...). <p>
 73  
  *
 74  
  * This class is written in a way that supports
 75  
  * adding more scopes easily.
 76  
  *
 77  
  * @author Thierry Lach
 78  
  * @since  0.9.4
 79  
  */
 80  900
 public class SettingsTabNotation
 81  
     extends JPanel
 82  
     implements GUIProjectSettingsTabInterface {
 83  
 
 84  
     private JPanel topPanel;
 85  
     private JComboBox notationLanguage;
 86  
     private JCheckBox useGuillemots;
 87  
     private JCheckBox showAssociationNames;
 88  
     private JCheckBox showVisibility;
 89  
     private JCheckBox showMultiplicity;
 90  
     private JCheckBox showInitialValue;
 91  
     private JCheckBox showProperties;
 92  
     private JCheckBox showTypes;
 93  
     private JCheckBox showStereotypes;
 94  
     private JCheckBox showSingularMultiplicities;
 95  
 
 96  
     private int scope;
 97  
     private Project p;
 98  
 
 99  
     /**
 100  
      * The constructor.
 101  
      * We currently support 2 scopes, but this class is written
 102  
      * in a way to easily extend that.
 103  
      *
 104  
      * @param settingsScope the scope of the settings
 105  
      */
 106  
     public SettingsTabNotation(int settingsScope) {
 107  1800
         super();
 108  1800
         scope = settingsScope;
 109  1800
     }
 110  
 
 111  
     private void buildPanel() {
 112  119
         setLayout(new BorderLayout());
 113  
 
 114  119
         topPanel = new JPanel();
 115  119
         topPanel.setLayout(new BorderLayout());
 116  
 
 117  119
         if (scope == Argo.SCOPE_APPLICATION) {
 118  19
             JPanel warning = new JPanel();
 119  19
             warning.setLayout(new BoxLayout(warning, BoxLayout.PAGE_AXIS));
 120  19
             JLabel warningLabel = new JLabel(Translator
 121  
                     .localize("label.warning"));
 122  19
             warningLabel.setAlignmentX(Component.RIGHT_ALIGNMENT);
 123  19
             warning.add(warningLabel);
 124  
 
 125  19
             JLinkButton projectSettings = new JLinkButton();
 126  19
             projectSettings.setAction(new ActionProjectSettings());
 127  19
             projectSettings.setText(Translator
 128  
                     .localize("button.project-settings"));
 129  19
             projectSettings.setIcon(null);
 130  19
             projectSettings.setAlignmentX(Component.RIGHT_ALIGNMENT);
 131  19
             warning.add(projectSettings);
 132  
 
 133  19
             topPanel.add(warning, BorderLayout.NORTH);
 134  
         }
 135  
 
 136  119
         JPanel settings = new JPanel();
 137  119
         settings.setLayout(new GridBagLayout());
 138  
 
 139  119
         GridBagConstraints constraints = new GridBagConstraints();
 140  119
         constraints.anchor = GridBagConstraints.WEST;
 141  119
         constraints.fill = GridBagConstraints.HORIZONTAL;
 142  119
         constraints.gridy = 0;
 143  119
         constraints.gridx = 0;
 144  119
         constraints.gridwidth = 1;
 145  119
         constraints.gridheight = 1;
 146  119
         constraints.weightx = 1.0;
 147  119
         constraints.insets = new Insets(0, 30, 0, 4);
 148  
 
 149  119
         constraints.gridy = GridBagConstraints.RELATIVE;
 150  
 
 151  119
         JPanel notationLanguagePanel =
 152  
             new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 0));
 153  119
         JLabel notationLanguageLabel =
 154  
             createLabel("label.notation-language");
 155  119
         notationLanguage = new NotationComboBox();
 156  119
         notationLanguageLabel.setLabelFor(notationLanguage);
 157  119
         notationLanguagePanel.add(notationLanguageLabel);
 158  119
         notationLanguagePanel.add(notationLanguage);
 159  119
         settings.add(notationLanguagePanel, constraints);
 160  
 
 161  119
         useGuillemots = createCheckBox("label.use-guillemots");
 162  119
         settings.add(useGuillemots, constraints);
 163  
 
 164  
         // 2002-07-31
 165  
         // Jaap Branderhorst
 166  
         // from here made visibility etc. configurable
 167  
 
 168  119
         showAssociationNames = createCheckBox("label.show-associationnames");
 169  119
         settings.add(showAssociationNames, constraints);
 170  
 
 171  119
         showVisibility = createCheckBox("label.show-visibility");
 172  119
         settings.add(showVisibility, constraints);
 173  
 
 174  119
         showMultiplicity = createCheckBox("label.show-multiplicity");
 175  119
         settings.add(showMultiplicity, constraints);
 176  
 
 177  119
         showInitialValue = createCheckBox("label.show-initialvalue");
 178  119
         settings.add(showInitialValue, constraints);
 179  
 
 180  119
         showProperties = createCheckBox("label.show-properties");
 181  119
         settings.add(showProperties, constraints);
 182  
 
 183  119
         showTypes = createCheckBox("label.show-types");
 184  119
         settings.add(showTypes, constraints);
 185  
 
 186  119
         showStereotypes = createCheckBox("label.show-stereotypes");
 187  119
         settings.add(showStereotypes, constraints);
 188  
 
 189  119
         showSingularMultiplicities = 
 190  
             createCheckBox("label.show-singular-multiplicities");
 191  119
         settings.add(showSingularMultiplicities, constraints);
 192  
 
 193  119
         topPanel.add(settings, BorderLayout.CENTER);
 194  
 
 195  119
         add(topPanel, BorderLayout.NORTH);
 196  119
     }
 197  
 
 198  
     /*
 199  
      * @see org.argouml.ui.GUISettingsTabInterface#handleSettingsTabRefresh()
 200  
      */
 201  
     public void handleSettingsTabRefresh() {
 202  139
         if (scope == Argo.SCOPE_APPLICATION) {
 203  19
             useGuillemots.setSelected(getBoolean(
 204  
                     Notation.KEY_USE_GUILLEMOTS));
 205  19
             notationLanguage.setSelectedItem(Notation.getConfiguredNotation());
 206  19
             showAssociationNames.setSelected(Configuration.getBoolean(
 207  
                     Notation.KEY_SHOW_ASSOCIATION_NAMES, true));
 208  19
             showVisibility.setSelected(getBoolean(
 209  
                     Notation.KEY_SHOW_VISIBILITY));
 210  19
             showInitialValue.setSelected(getBoolean(
 211  
                     Notation.KEY_SHOW_INITIAL_VALUE));
 212  19
             showProperties.setSelected(getBoolean(
 213  
                     Notation.KEY_SHOW_PROPERTIES));
 214  
             /*
 215  
              * The next one defaults to TRUE, to stay compatible with older
 216  
              * ArgoUML versions that did not have this setting:
 217  
              */
 218  19
             showTypes.setSelected(Configuration.getBoolean(
 219  
                     Notation.KEY_SHOW_TYPES, true));
 220  19
             showMultiplicity.setSelected(getBoolean(
 221  
                     Notation.KEY_SHOW_MULTIPLICITY));
 222  19
             showStereotypes.setSelected(getBoolean(
 223  
                     Notation.KEY_SHOW_STEREOTYPES));
 224  
             /*
 225  
              * The next one defaults to TRUE, despite that this is
 226  
              * NOT compatible with older ArgoUML versions
 227  
              * (before 0.24) that did
 228  
              * not have this setting - see issue 1395 for the rationale:
 229  
              */
 230  19
             showSingularMultiplicities.setSelected(Configuration.getBoolean(
 231  
                     Notation.KEY_SHOW_SINGULAR_MULTIPLICITIES, true));
 232  
         }
 233  139
         if (scope == Argo.SCOPE_PROJECT) {
 234  120
             assert p != null;
 235  120
             ProjectSettings ps = p.getProjectSettings();
 236  120
             NotationSettings ns = ps.getNotationSettings();
 237  
 
 238  120
             notationLanguage.setSelectedItem(Notation.findNotation(
 239  
                     ps.getNotationLanguage()));
 240  120
             useGuillemots.setSelected(ps.getUseGuillemotsValue());
 241  120
             showAssociationNames.setSelected(ns.isShowAssociationNames());
 242  120
             showVisibility.setSelected(ns.isShowVisibilities());
 243  120
             showMultiplicity.setSelected(ns.isShowMultiplicities());
 244  120
             showInitialValue.setSelected(ns.isShowInitialValues());
 245  120
             showProperties.setSelected(ns.isShowProperties());
 246  120
             showTypes.setSelected(ns.isShowTypes());
 247  120
             showStereotypes.setSelected(ps.getShowStereotypesValue());
 248  120
             showSingularMultiplicities.setSelected(
 249  
                     ns.isShowSingularMultiplicities());
 250  
         }
 251  139
     }
 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  138
         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
             Notation.setDefaultNotation(
 269  
                     (NotationName) notationLanguage.getSelectedItem());
 270  0
             Configuration.setBoolean(Notation.KEY_USE_GUILLEMOTS,
 271  
                     useGuillemots.isSelected());
 272  0
             Configuration.setBoolean(Notation.KEY_SHOW_ASSOCIATION_NAMES,
 273  
                     showAssociationNames.isSelected());
 274  0
             Configuration.setBoolean(Notation.KEY_SHOW_VISIBILITY,
 275  
                     showVisibility.isSelected());
 276  0
             Configuration.setBoolean(Notation.KEY_SHOW_MULTIPLICITY,
 277  
                     showMultiplicity.isSelected());
 278  0
             Configuration.setBoolean(Notation.KEY_SHOW_INITIAL_VALUE,
 279  
                     showInitialValue.isSelected());
 280  0
             Configuration.setBoolean(Notation.KEY_SHOW_PROPERTIES,
 281  
                     showProperties.isSelected());
 282  0
             Configuration.setBoolean(Notation.KEY_SHOW_TYPES,
 283  
                     showTypes.isSelected());
 284  0
             Configuration.setBoolean(Notation.KEY_SHOW_STEREOTYPES,
 285  
                     showStereotypes.isSelected());
 286  0
             Configuration.setBoolean(Notation.KEY_SHOW_SINGULAR_MULTIPLICITIES,
 287  
                     showSingularMultiplicities.isSelected());
 288  
         }
 289  12
         if (scope == Argo.SCOPE_PROJECT) {
 290  12
             assert p != null;
 291  12
             ProjectSettings ps = p.getProjectSettings();
 292  12
             NotationSettings ns = ps.getNotationSettings();
 293  12
             NotationName nn = (NotationName) notationLanguage.getSelectedItem();
 294  12
             if (nn != null) {
 295  12
                 ps.setNotationLanguage(nn.getConfigurationValue());
 296  
             }
 297  12
             ps.setUseGuillemots(useGuillemots.isSelected());
 298  12
             ns.setShowAssociationNames(showAssociationNames.isSelected());
 299  12
             ns.setShowVisibilities(showVisibility.isSelected());
 300  12
             ns.setShowMultiplicities(showMultiplicity.isSelected());
 301  12
             ns.setShowInitialValues(showInitialValue.isSelected());
 302  12
             ns.setShowProperties(showProperties.isSelected());
 303  12
             ns.setShowTypes(showTypes.isSelected());
 304  12
             ps.setShowStereotypes(showStereotypes.isSelected());
 305  12
             ns.setShowSingularMultiplicities(
 306  
                     showSingularMultiplicities.isSelected());
 307  
         }
 308  12
     }
 309  
 
 310  
     /*
 311  
      * @see org.argouml.ui.GUISettingsTabInterface#handleSettingsTabCancel()
 312  
      */
 313  
     public void handleSettingsTabCancel() {
 314  6
         handleSettingsTabRefresh();
 315  6
     }
 316  
 
 317  
     /*
 318  
      * @see org.argouml.ui.GUISettingsTabInterface#handleResetToDefault()
 319  
      */
 320  
     public void handleResetToDefault() {
 321  6
         if (scope == Argo.SCOPE_PROJECT) {
 322  6
             notationLanguage.setSelectedItem(Notation.getConfiguredNotation());
 323  6
             useGuillemots.setSelected(getBoolean(
 324  
                     Notation.KEY_USE_GUILLEMOTS));
 325  6
             showAssociationNames.setSelected(Configuration.getBoolean(
 326  
                     Notation.KEY_SHOW_ASSOCIATION_NAMES, true));
 327  6
             showVisibility.setSelected(getBoolean(
 328  
                     Notation.KEY_SHOW_VISIBILITY));
 329  6
             showMultiplicity.setSelected(getBoolean(
 330  
                     Notation.KEY_SHOW_MULTIPLICITY));
 331  6
             showInitialValue.setSelected(getBoolean(
 332  
                     Notation.KEY_SHOW_INITIAL_VALUE));
 333  6
             showProperties.setSelected(Configuration.getBoolean(
 334  
                     Notation.KEY_SHOW_PROPERTIES));
 335  6
             showTypes.setSelected(Configuration.getBoolean(
 336  
                     Notation.KEY_SHOW_TYPES, true));
 337  6
             showStereotypes.setSelected(Configuration.getBoolean(
 338  
                     Notation.KEY_SHOW_STEREOTYPES));
 339  6
             showSingularMultiplicities.setSelected(Configuration.getBoolean(
 340  
                     Notation.KEY_SHOW_SINGULAR_MULTIPLICITIES));
 341  
         }
 342  6
     }
 343  
 
 344  
     /*
 345  
      * @see org.argouml.ui.GUISettingsTabInterface#getTabKey()
 346  
      */
 347  
     public String getTabKey() {
 348  119
         return "tab.notation";
 349  
     }
 350  
 
 351  
     /*
 352  
      * @see org.argouml.ui.GUISettingsTabInterface#getTabPanel()
 353  
      */
 354  
     public JPanel getTabPanel() {
 355  119
         if (topPanel == null) {
 356  119
             buildPanel();
 357  
         }
 358  119
         return this;
 359  
     }
 360  
 
 361  
     /**
 362  
      * Create a localized JCheckBox.
 363  
      *
 364  
      * @param key the key for the string to be localized
 365  
      * @return a new checkbox with localized text
 366  
      */
 367  
     protected JCheckBox createCheckBox(String key) {
 368  1071
         JCheckBox j = new JCheckBox(Translator.localize(key));
 369  1071
         return j;
 370  
     }
 371  
 
 372  
     /**
 373  
      * Create a localized JLabel.
 374  
      *
 375  
      * @param key the key of the text for the label
 376  
      * @return a new label with a localized text for the given key
 377  
      */
 378  
     protected JLabel createLabel(String key) {
 379  119
         return new JLabel(Translator.localize(key));
 380  
     }
 381  
 
 382  
     /*
 383  
      * @see javax.swing.JComponent#setVisible(boolean)
 384  
      */
 385  
     public void setVisible(boolean visible) {
 386  136
         super.setVisible(visible);
 387  136
         if (visible) {
 388  14
             handleSettingsTabRefresh();
 389  
         }
 390  136
     }
 391  
 
 392  
     public void setProject(Project project) {
 393  100
         assert project != null;
 394  100
         p = project;
 395  100
     }
 396  
 }