Clover coverage report - DrJava Test Coverage (drjava-20120304-r5456)
Coverage timestamp: Sun Mar 4 2012 03:13:23 CST
file stats: LOC: 237   Methods: 16
NCLOC: 137   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ToolbarOptionComponent.java 7.9% 53.8% 37.5% 40.1%
coverage coverage
 1    /*BEGIN_COPYRIGHT_BLOCK
 2    *
 3    * Copyright (c) 2001-2010, JavaPLT group at Rice University (drjava@rice.edu)
 4    * All rights reserved.
 5    *
 6    * Redistribution and use in source and binary forms, with or without
 7    * modification, are permitted provided that the following conditions are met:
 8    * * Redistributions of source code must retain the above copyright
 9    * notice, this list of conditions and the following disclaimer.
 10    * * Redistributions in binary form must reproduce the above copyright
 11    * notice, this list of conditions and the following disclaimer in the
 12    * documentation and/or other materials provided with the distribution.
 13    * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
 14    * names of its contributors may be used to endorse or promote products
 15    * derived from this software without specific prior written permission.
 16    *
 17    * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 18    * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 19    * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 20    * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 21    * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 22    * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 23    * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 24    * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 25    * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 26    * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 27    * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 28    *
 29    * This software is Open Source Initiative approved Open Source Software.
 30    * Open Source Initative Approved is a trademark of the Open Source Initiative.
 31    *
 32    * This file is part of DrJava. Download the current version of this project
 33    * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
 34    *
 35    * END_COPYRIGHT_BLOCK*/
 36   
 37    package edu.rice.cs.drjava.ui.config;
 38   
 39    import java.awt.*;
 40    import java.awt.event.*;
 41    import javax.swing.*;
 42   
 43    import edu.rice.cs.drjava.*;
 44    import edu.rice.cs.drjava.config.*;
 45    import edu.rice.cs.util.swing.SwingFrame;
 46   
 47    /**
 48    * The special option component for the toolbar text and toolbar icon options.
 49    * Not a true OptionComponent, in that it actually represents and governs the
 50    * configuration of two BooleanOptions (i.e. those corresponding to TOOLBAR_TEXT_ENABLED
 51    * and TOOLBAR_ICONS_ENABLED) bypassing the the normal graphical representation
 52    * with JRadioButtons, in order to comply with the special circumstances regarding
 53    * their setting.
 54    * @version $Id: ToolbarOptionComponent.java 5232 2010-04-24 00:14:05Z mgricken $
 55    */
 56    public class ToolbarOptionComponent extends OptionComponent<Boolean,JComponent> {
 57   
 58    private JRadioButton _noneButton;
 59    private JRadioButton _textButton;
 60    private JRadioButton _iconsButton;
 61    private JRadioButton _textAndIconsButton;
 62    private ButtonGroup _group;
 63    private JPanel _buttonPanel;
 64   
 65    //String constants used to identify which one of the four choices is selected.
 66    public static final String NONE = "none";
 67    public static final String TEXT_ONLY = "text only";
 68    public static final String ICONS_ONLY= "icons only";
 69    public static final String TEXT_AND_ICONS = "text and icons";
 70   
 71    /** The constructor does not take an option since we have specific knowledge of the
 72    * two options we'll need for this component. We simpy access them as needed, and use
 73    * OptionComponent's degenerate constructor.
 74    * @param title the title for this panel
 75    * @param parent the parent frame
 76    */
 77  38 public ToolbarOptionComponent(String title, SwingFrame parent) {
 78  38 super(title, parent);
 79   
 80  38 _noneButton = new JRadioButton(NONE);
 81  38 _noneButton.setActionCommand(NONE);
 82  38 _noneButton.addActionListener(new ActionListener() {
 83  0 public void actionPerformed(ActionEvent e) { notifyChangeListeners(); }
 84    });
 85   
 86  38 _textButton = new JRadioButton(TEXT_ONLY);
 87  38 _textButton.setActionCommand(TEXT_ONLY);
 88  38 _textButton.addActionListener(new ActionListener() {
 89  0 public void actionPerformed(ActionEvent e) { notifyChangeListeners(); }
 90    });
 91   
 92  38 _iconsButton = new JRadioButton(ICONS_ONLY);
 93  38 _iconsButton.setActionCommand(ICONS_ONLY);
 94  38 _iconsButton.addActionListener(new ActionListener() {
 95  0 public void actionPerformed(ActionEvent e) { notifyChangeListeners(); }
 96    });
 97   
 98  38 _textAndIconsButton = new JRadioButton(TEXT_AND_ICONS);
 99  38 _textAndIconsButton.setActionCommand(TEXT_AND_ICONS);
 100  38 _textAndIconsButton.addActionListener(new ActionListener() {
 101  0 public void actionPerformed(ActionEvent e) { notifyChangeListeners(); }
 102    });
 103   
 104  38 resetToCurrent();
 105   
 106  38 _group = new ButtonGroup();
 107  38 _group.add(_noneButton);
 108  38 _group.add(_textButton);
 109  38 _group.add(_iconsButton);
 110  38 _group.add(_textAndIconsButton);
 111   
 112  38 _buttonPanel = new JPanel();
 113  38 _buttonPanel.setLayout(new GridLayout(0,1));
 114  38 _buttonPanel.setBorder(BorderFactory.createEtchedBorder());
 115  38 _buttonPanel.add(_noneButton);
 116  38 _buttonPanel.add(_textButton);
 117  38 _buttonPanel.add(_iconsButton);
 118  38 _buttonPanel.add(_textAndIconsButton);
 119   
 120  38 DrJava.getConfig().addOptionListener(OptionConstants.TOOLBAR_TEXT_ENABLED,
 121    new OptionListener<Boolean>() {
 122  0 public void optionChanged(OptionEvent<Boolean> oe) { resetToCurrent(); }
 123    });
 124  38 DrJava.getConfig().addOptionListener(OptionConstants.TOOLBAR_ICONS_ENABLED,
 125    new OptionListener<Boolean>() {
 126  0 public void optionChanged(OptionEvent<Boolean> oe) { resetToCurrent(); }
 127    });
 128  38 DrJava.getConfig().addOptionListener(OptionConstants.TOOLBAR_ENABLED,
 129    new OptionListener<Boolean>() {
 130  0 public void optionChanged(OptionEvent<Boolean> oe) { resetToCurrent(); }
 131    });
 132   
 133  38 setComponent(_buttonPanel);
 134    }
 135   
 136    /** Constructor that allows for a tooltip description. */
 137  38 public ToolbarOptionComponent(String title, SwingFrame parent, String description) {
 138  38 this(title, parent);
 139  38 setDescription(description);
 140    }
 141   
 142    /** Sets the tooltip description text for this option.
 143    * @param description the tooltip text
 144    */
 145  38 public void setDescription(String description) {
 146  38 _buttonPanel.setToolTipText(description);
 147  38 _noneButton.setToolTipText(description);
 148  38 _textButton.setToolTipText(description);
 149  38 _iconsButton.setToolTipText(description);
 150  38 _textAndIconsButton.setToolTipText(description);
 151  38 _label.setToolTipText(description);
 152    }
 153   
 154    /** Selects the radio button corresponding to the current config options. */
 155  38 public void resetToCurrent() {
 156  38 _setSelected(DrJava.getConfig().getSetting(OptionConstants.TOOLBAR_TEXT_ENABLED).booleanValue(),
 157    DrJava.getConfig().getSetting(OptionConstants.TOOLBAR_ICONS_ENABLED).booleanValue(),
 158    DrJava.getConfig().getSetting(OptionConstants.TOOLBAR_ENABLED).booleanValue());
 159    }
 160   
 161    /** Selects the radio button corresponding to the default values. */
 162  0 public void resetToDefault() {
 163  0 _setSelected(OptionConstants.TOOLBAR_TEXT_ENABLED.getDefault().booleanValue(),
 164    OptionConstants.TOOLBAR_ICONS_ENABLED.getDefault().booleanValue(),
 165    OptionConstants.TOOLBAR_ENABLED.getDefault().booleanValue());
 166    }
 167   
 168    /** Selects the radio button corresponding to the specified configuration.
 169    * @param textEnabled Whether toolbar text is enabled
 170    * @param iconsEnabled Whether toolbar icons are enabled
 171    */
 172  38 private void _setSelected(boolean textEnabled, boolean iconsEnabled, boolean isEnabled) {
 173  0 if (! isEnabled) { _noneButton.setSelected(true); }
 174  38 else if (textEnabled && iconsEnabled) { _textAndIconsButton.setSelected(true); }
 175    else {
 176  0 if (textEnabled) _textButton.setSelected(true);
 177  0 else if (iconsEnabled) _iconsButton.setSelected(true);
 178    }
 179    }
 180   
 181    /** Updates the config object with the new setting. Should run in event thread.
 182    * @return true if the new value is set successfully
 183    */
 184  0 public boolean updateConfig() {
 185  0 String btnIdent = _group.getSelection().getActionCommand();
 186  0 boolean textWasEnabled = DrJava.getConfig().getSetting(OptionConstants.TOOLBAR_TEXT_ENABLED).booleanValue();
 187  0 boolean iconsWereEnabled = DrJava.getConfig().getSetting(OptionConstants.TOOLBAR_ICONS_ENABLED).booleanValue();
 188  0 boolean wasEnabled = DrJava.getConfig().getSetting(OptionConstants.TOOLBAR_ENABLED).booleanValue();
 189   
 190  0 if (btnIdent.equals(NONE)) {
 191  0 if (wasEnabled) { DrJava.getConfig().setSetting(OptionConstants.TOOLBAR_ENABLED, Boolean.FALSE); }
 192    }
 193  0 if (btnIdent.equals(TEXT_ONLY)) {
 194  0 if (! textWasEnabled) { DrJava.getConfig().setSetting(OptionConstants.TOOLBAR_TEXT_ENABLED, Boolean.TRUE); }
 195  0 if (iconsWereEnabled) { DrJava.getConfig().setSetting(OptionConstants.TOOLBAR_ICONS_ENABLED, Boolean.FALSE); }
 196  0 if (! wasEnabled) { DrJava.getConfig().setSetting(OptionConstants.TOOLBAR_ENABLED, Boolean.TRUE); }
 197    }
 198   
 199  0 if (btnIdent.equals(ICONS_ONLY)) {
 200  0 if (! iconsWereEnabled) { DrJava.getConfig().setSetting(OptionConstants.TOOLBAR_ICONS_ENABLED, Boolean.TRUE); }
 201  0 if (textWasEnabled) { DrJava.getConfig().setSetting(OptionConstants.TOOLBAR_TEXT_ENABLED, Boolean.FALSE); }
 202  0 if (! wasEnabled) { DrJava.getConfig().setSetting(OptionConstants.TOOLBAR_ENABLED, Boolean.TRUE); }
 203    }
 204   
 205  0 if (btnIdent.equals(TEXT_AND_ICONS)) {
 206  0 if (! textWasEnabled) { DrJava.getConfig().setSetting(OptionConstants.TOOLBAR_TEXT_ENABLED, Boolean.TRUE); }
 207  0 if (! iconsWereEnabled) { DrJava.getConfig().setSetting(OptionConstants.TOOLBAR_ICONS_ENABLED, Boolean.TRUE); }
 208  0 if (! wasEnabled) { DrJava.getConfig().setSetting(OptionConstants.TOOLBAR_ENABLED, Boolean.TRUE); }
 209    }
 210   
 211  0 return true;
 212    }
 213   
 214   
 215    /** Displays the given value.
 216    */
 217  0 public void setValue(Boolean value) {
 218  0 resetToCurrent();
 219    }
 220   
 221    /** Set the JComponent to display for this OptionComponent.
 222    * @param component GUI component */
 223  38 public void setComponent(JComponent component) {
 224  38 _guiComponent = component;
 225  38 if (_guiComponent!=null) {
 226  38 boolean wasEditable = DrJava.getConfig().isEditable(OptionConstants.TOOLBAR_TEXT_ENABLED);
 227  38 wasEditable = wasEditable && DrJava.getConfig().isEditable(OptionConstants.TOOLBAR_ICONS_ENABLED);
 228  38 wasEditable = wasEditable && DrJava.getConfig().isEditable(OptionConstants.TOOLBAR_ENABLED);
 229   
 230  38 _guiComponent.setEnabled(wasEditable);
 231    // also enable/disable all subcomponents (see Java bug 4177727)
 232  38 for (Component subComponent: _guiComponent.getComponents()) {
 233  152 subComponent.setEnabled(wasEditable);
 234    }
 235    }
 236    }
 237    }