Clover coverage report - DrJava Test Coverage (drjava-20110828-r5448)
Coverage timestamp: Sun Aug 28 2011 03:13:33 CDT
file stats: LOC: 178   Methods: 14
NCLOC: 101   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ColorOptionComponent.java 80% 78.8% 57.1% 75%
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    import edu.rice.cs.drjava.config.*;
 43    import edu.rice.cs.drjava.*;
 44    import edu.rice.cs.util.swing.SwingFrame;
 45   
 46    /** Graphical form of a ColorOption.
 47    * @version $Id: ColorOptionComponent.java 5232 2010-04-24 00:14:05Z mgricken $
 48    */
 49    public class ColorOptionComponent extends OptionComponent<Color,JPanel> {
 50    private JButton _button;
 51    private JTextField _colorField;
 52    private JPanel _panel;
 53    private Color _color;
 54    private boolean _isBackgroundColor;
 55    private boolean _isBoldText;
 56   
 57    /** Main constructor for ColorOptionComponent.
 58    * @param opt The ColorOption to display
 59    * @param text The text to display in the label of the component
 60    * @param parent The Frame displaying this component
 61    */
 62  3 public ColorOptionComponent (ColorOption opt, String text, SwingFrame parent) {
 63  3 this(opt, text, parent, false);
 64    }
 65   
 66    /** An alternate constructor, allowing the caller to specify whether this color is a background color. If so,
 67    * the button will display the color as its background.
 68    */
 69  3 public ColorOptionComponent(ColorOption opt, String text, SwingFrame parent, boolean isBackgroundColor) {
 70  3 this(opt, text, parent, isBackgroundColor, false);
 71    }
 72   
 73  1181 public ColorOptionComponent(ColorOption opt, String text, SwingFrame parent, boolean isBackgroundColor, boolean isBoldText)
 74    {
 75  1181 super(opt, text, parent);
 76  1181 _isBackgroundColor = isBackgroundColor;
 77  1181 _isBoldText = isBoldText;
 78  1181 _button = new JButton();
 79  1181 _button.addActionListener(new ActionListener() {
 80  0 public void actionPerformed(ActionEvent e) { chooseColor(); }
 81    });
 82  1181 _button.setText("...");
 83  1181 _button.setMaximumSize(new Dimension(10,10));
 84  1181 _button.setMinimumSize(new Dimension(10,10));
 85   
 86  1181 _colorField = new JTextField();
 87  1181 _colorField.setEditable(false);
 88  1181 _colorField.setHorizontalAlignment(JTextField.CENTER);
 89  1181 _panel = new JPanel(new BorderLayout());
 90  1181 _panel.add(_colorField, BorderLayout.CENTER);
 91  1181 _panel.add(_button, BorderLayout.EAST);
 92  1181 if (_isBackgroundColor) {
 93    // _colorField.setForeground(Color.black);
 94  684 _colorField.setForeground(DrJava.getConfig().getSetting(OptionConstants.DEFINITIONS_NORMAL_COLOR));
 95  684 DrJava.getConfig().addOptionListener(OptionConstants.DEFINITIONS_NORMAL_COLOR,
 96    new OptionListener<Color>() {
 97  0 public void optionChanged(OptionEvent<Color> oe) { _colorField.setForeground(oe.value); }
 98    });
 99    }
 100    else {
 101    // _colorField.setBackground(Color.white);
 102  497 _colorField.setBackground(DrJava.getConfig().getSetting(OptionConstants.DEFINITIONS_BACKGROUND_COLOR));
 103  497 DrJava.getConfig().addOptionListener(OptionConstants.DEFINITIONS_BACKGROUND_COLOR,
 104    new OptionListener<Color>() {
 105  0 public void optionChanged(OptionEvent<Color> oe) {
 106  0 _colorField.setBackground(oe.value);
 107    }
 108    });
 109    }
 110  1181 if (_isBoldText) {
 111  76 _colorField.setFont(_colorField.getFont().deriveFont(Font.BOLD));
 112    }
 113  1181 _color = DrJava.getConfig().getSetting(_option);
 114  1181 _updateField(_color);
 115  1181 setComponent(_panel);
 116    }
 117   
 118    /** Constructor that allows for a tooltip description. */
 119  0 public ColorOptionComponent(ColorOption opt, String text, SwingFrame parent, String description) {
 120  0 this(opt, text, parent, description, false);
 121    }
 122   
 123    /** Constructor that allows for a tooltip description as well as whether or not this is a background color. */
 124  0 public ColorOptionComponent(ColorOption opt, String text, SwingFrame parent, String description, boolean isBackgroundColor)
 125    {
 126  0 this(opt, text, parent, isBackgroundColor);
 127  0 setDescription(description);
 128    }
 129   
 130    /** Constructor that allows for a tooltip description as well as whether or not this is a background color.*/
 131  1178 public ColorOptionComponent(ColorOption opt, String text, SwingFrame parent, String description, boolean isBackgroundColor,
 132    boolean isBoldText) {
 133  1178 this(opt, text, parent, isBackgroundColor, isBoldText);
 134  1178 setDescription(description);
 135    }
 136   
 137    /** Sets the tooltip description text for this option.
 138    * @param description the tooltip text
 139    */
 140  1178 public void setDescription(String description) {
 141  1178 _panel.setToolTipText(description);
 142  1178 _button.setToolTipText(description);
 143  1178 _colorField.setToolTipText(description);
 144  1178 _label.setToolTipText(description);
 145    }
 146   
 147    /** Updates the config object with the new setting.
 148    * @return true if the new value is set successfully
 149    */
 150  4 public boolean updateConfig() {
 151  3 if (!_color.equals(DrJava.getConfig().getSetting(_option))) { DrJava.getConfig().setSetting(_option, _color); }
 152  4 return true;
 153    }
 154   
 155   
 156    /** Displays the given value. */
 157  8 public void setValue(Color value) {
 158  8 _color = value;
 159  8 _updateField(value);
 160    }
 161   
 162    /** Updates the component's field to display the given color. */
 163  1189 private void _updateField(Color c) {
 164  684 if (_isBackgroundColor) _colorField.setBackground(c);
 165  505 else _colorField.setForeground(c);
 166  1189 _colorField.setText(getLabelText() + " (" + _option.format(c) + ")");
 167    }
 168   
 169    /** Shows a color chooser dialog for picking a new color. */
 170  0 public void chooseColor() {
 171  0 Color c = JColorChooser.showDialog(_parent, "Choose '" + getLabelText() + "'", _color);
 172  0 if (c != null) {
 173  0 _color = c;
 174  0 notifyChangeListeners();
 175  0 _updateField(c);
 176    }
 177    }
 178    }