Coverage Report - org.argouml.ui.ProjectSettingsDialog
 
Classes in this File Line Coverage Branch Coverage Complexity
ProjectSettingsDialog
88%
74/84
69%
25/36
2.111
ProjectSettingsDialog$1
100%
3/3
N/A
2.111
ProjectSettingsDialog$2
100%
3/3
N/A
2.111
 
 1  
 /* $Id: ProjectSettingsDialog.java 17841 2010-01-12 19:17:52Z 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) 2006-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.ui;
 40  
 
 41  
 import java.awt.Dimension;
 42  
 import java.awt.event.ActionEvent;
 43  
 import java.awt.event.ActionListener;
 44  
 import java.awt.event.WindowEvent;
 45  
 import java.awt.event.WindowListener;
 46  
 import java.util.Iterator;
 47  
 
 48  
 import javax.swing.JButton;
 49  
 import javax.swing.JPanel;
 50  
 import javax.swing.JTabbedPane;
 51  
 import javax.swing.SwingConstants;
 52  
 
 53  
 import org.argouml.application.api.GUISettingsTabInterface;
 54  
 import org.argouml.i18n.Translator;
 55  
 import org.argouml.kernel.Project;
 56  
 import org.argouml.kernel.ProjectManager;
 57  
 import org.argouml.util.ArgoDialog;
 58  
 
 59  
 /**
 60  
  * A dialog panel that allows the user to set 
 61  
  * preferences with project scope, i.e. 
 62  
  * settings that are stored in the project file (e.g. ".zargo").
 63  
  * 
 64  
  * @author michiel
 65  
  */
 66  12
 public class ProjectSettingsDialog 
 67  
                 extends ArgoDialog implements WindowListener {
 68  
 
 69  
     private JButton applyButton;
 70  
     private JButton resetToDefaultButton;
 71  
 
 72  
     private JTabbedPane tabs;
 73  
 
 74  
     private boolean doingShow;
 75  
 
 76  
     private boolean windowOpen;
 77  
 
 78  
     /**
 79  
      * The constructor for this dialog of settings with project scope.
 80  
      */
 81  
     public ProjectSettingsDialog() {
 82  100
         super(Translator.localize("dialog.file.properties"),
 83  
                 ArgoDialog.OK_CANCEL_OPTION,
 84  
                 true);
 85  
         
 86  100
         tabs = new JTabbedPane();
 87  
         
 88  100
         applyButton = new JButton(Translator.localize("button.apply"));
 89  100
         String mnemonic = Translator.localize("button.apply.mnemonic");
 90  100
         if (mnemonic != null && mnemonic.length() > 0) {
 91  100
             applyButton.setMnemonic(mnemonic.charAt(0));
 92  
         }
 93  100
         applyButton.addActionListener(new ActionListener() {
 94  
             public void actionPerformed(ActionEvent e) {
 95  6
                 handleSave();
 96  6
             }
 97  
         });
 98  100
         addButton(applyButton);
 99  
         
 100  100
         resetToDefaultButton = new JButton(
 101  
                 Translator.localize("button.reset-to-default"));
 102  100
         mnemonic = Translator.localize("button.reset-to-default.mnemonic");
 103  100
         if (mnemonic != null && mnemonic.length() > 0) {
 104  100
             resetToDefaultButton.setMnemonic(mnemonic.charAt(0));
 105  
         }
 106  100
         resetToDefaultButton.addActionListener(new ActionListener() {
 107  
             public void actionPerformed(ActionEvent e) {
 108  6
                 handleResetToDefault();
 109  6
             }
 110  
         });
 111  100
         addButton(resetToDefaultButton);
 112  
        
 113  
         // Add settings from the settings registry.
 114  100
         Iterator iter = GUI.getInstance().getProjectSettingsTabs().iterator();
 115  500
         while (iter.hasNext()) {
 116  400
             GUISettingsTabInterface stp =
 117  
                 (GUISettingsTabInterface) iter.next();
 118  
 
 119  400
             tabs.addTab(
 120  
                     Translator.localize(stp.getTabKey()),
 121  
                     stp.getTabPanel());
 122  400
         }
 123  
 
 124  
         // Increase width to accommodate all tabs on one row.
 125  100
         final int minimumWidth = 480;
 126  100
         tabs.setPreferredSize(new Dimension(Math.max(tabs
 127  
                 .getPreferredSize().width, minimumWidth), tabs
 128  
                 .getPreferredSize().height));
 129  
 
 130  100
         tabs.setTabPlacement(SwingConstants.LEFT);
 131  100
         setContent(tabs);
 132  100
         addWindowListener(this);
 133  100
     }
 134  
 
 135  
     /**
 136  
      * Replacement of {@link java.awt.Component#setVisible(boolean)}
 137  
      */
 138  
     public void showDialog() {
 139  
         // If a recursive call from setVisible(), just return
 140  100
         if (doingShow) {
 141  0
             return;
 142  
         }
 143  100
         doingShow = true;
 144  100
         handleRefresh();
 145  100
         setVisible(true);
 146  12
         toFront();
 147  12
         doingShow = false;
 148  
         // windowOpen state will be changed when window is activated
 149  12
     }
 150  
 
 151  
     /*
 152  
      * @see java.awt.event.ActionListener#actionPerformed(
 153  
      *      java.awt.event.ActionEvent)
 154  
      */
 155  
     public void actionPerformed(ActionEvent ev) {
 156  12
         super.actionPerformed(ev);
 157  12
         if (ev.getSource() == getOkButton()) {
 158  6
             handleSave();
 159  6
         } else if (ev.getSource() == getCancelButton()) {
 160  6
             handleCancel();
 161  
         }
 162  12
     }
 163  
 
 164  
     /*
 165  
      * Called when the user has pressed Save. Performs "Save" in all Tabs.
 166  
      */
 167  
     private void handleSave() {
 168  60
         for (int i = 0; i < tabs.getComponentCount(); i++) {
 169  48
             Object o = tabs.getComponent(i);
 170  48
             if (o instanceof GUISettingsTabInterface) {
 171  48
                 ((GUISettingsTabInterface) o).handleSettingsTabSave();
 172  
             }
 173  
         }
 174  12
         windowOpen = false;
 175  12
     }
 176  
 
 177  
     /*
 178  
      * Called when the user has pressed Cancel. Performs "Cancel" in all Tabs.
 179  
      */
 180  
     private void handleCancel() {
 181  30
         for (int i = 0; i < tabs.getComponentCount(); i++) {
 182  24
             Object o = tabs.getComponent(i);
 183  24
             if (o instanceof GUISettingsTabInterface) {
 184  24
                 ((GUISettingsTabInterface) o).handleSettingsTabCancel();
 185  
             }
 186  
         }
 187  6
         windowOpen = false;
 188  6
     }
 189  
 
 190  
     /**
 191  
      * Perform "Refresh" in all Tabs.
 192  
      */
 193  
     private void handleRefresh() {
 194  500
         for (int i = 0; i < tabs.getComponentCount(); i++) {
 195  400
             Object o = tabs.getComponent(i);
 196  400
             if (o instanceof GUISettingsTabInterface) {
 197  400
                 if (o instanceof GUIProjectSettingsTabInterface) {
 198  
                     /* We have to tell the settings tab in which project
 199  
                      * the settings reside: */
 200  400
                     Project p = ProjectManager.getManager().getCurrentProject();
 201  400
                     ((GUIProjectSettingsTabInterface) o).setProject(p);
 202  
                 }
 203  400
                 ((GUISettingsTabInterface) o).handleSettingsTabRefresh();
 204  
             }
 205  
         }
 206  100
     }
 207  
 
 208  
     private void handleOpen() {
 209  
         // We only request focus the first time we become visible
 210  200
         if (!windowOpen) {
 211  100
             getOkButton().requestFocusInWindow();
 212  100
             windowOpen = true;
 213  
         }
 214  200
     }
 215  
     
 216  
     private void handleResetToDefault() {
 217  30
         for (int i = 0; i < tabs.getComponentCount(); i++) {
 218  24
             Object o = tabs.getComponent(i);
 219  24
             if (o instanceof GUISettingsTabInterface) {
 220  24
                 ((GUISettingsTabInterface) o).handleResetToDefault();
 221  
             }
 222  
         }
 223  6
     }
 224  
     
 225  
     /*
 226  
      * @see java.awt.event.WindowListener#windowActivated(java.awt.event.WindowEvent)
 227  
      */
 228  
     public void windowActivated(WindowEvent e) {
 229  100
         handleOpen();
 230  100
     }
 231  
 
 232  
     /*
 233  
      * @see java.awt.event.WindowListener#windowClosed(java.awt.event.WindowEvent)
 234  
      */
 235  
     public void windowClosed(WindowEvent e) {
 236  
         // ignored - we only care about open/closing
 237  12
     }
 238  
 
 239  
     /*
 240  
      * @see java.awt.event.WindowListener#windowDeactivated(java.awt.event.WindowEvent)
 241  
      */
 242  
     public void windowDeactivated(WindowEvent e) {
 243  
         // ignored - we only care about open/closing
 244  51
     }
 245  
 
 246  
     /*
 247  
      * @see java.awt.event.WindowListener#windowDeiconified(java.awt.event.WindowEvent)
 248  
      */
 249  
     public void windowDeiconified(WindowEvent e) {
 250  
         // ignored - we only care about open/closing
 251  0
     }
 252  
 
 253  
     /*
 254  
      * @see java.awt.event.WindowListener#windowIconified(java.awt.event.WindowEvent)
 255  
      */
 256  
     public void windowIconified(WindowEvent e) {
 257  
         // ignored - we only care about open/closing
 258  0
     }
 259  
 
 260  
     /*
 261  
      * @see java.awt.event.WindowListener#windowOpened(java.awt.event.WindowEvent)
 262  
      */
 263  
     public void windowOpened(WindowEvent e) {
 264  100
         handleOpen();
 265  100
     }
 266  
 
 267  
     /*
 268  
      * @see java.awt.event.WindowListener#windowClosing(java.awt.event.WindowEvent)
 269  
      */
 270  
     public void windowClosing(WindowEvent e) {
 271  
         // Handle the same as an explicit cancel
 272  0
         handleCancel();
 273  0
     }
 274  
 
 275  
     /**
 276  
      * Show the dialog with the tab as selected
 277  
      * 
 278  
      * @param tab tab to be selected
 279  
      */
 280  
     public void showDialog(JPanel tab) {
 281  
         try {
 282  0
             tabs.setSelectedComponent(tab);           
 283  0
         } catch (Throwable t) {
 284  
             
 285  0
         }
 286  0
         showDialog();
 287  0
     }
 288  
 
 289  
 }