Coverage Report - edu.rice.cs.drjava.ui.config.ColorOptionComponent
 
Classes in this File Line Coverage Branch Coverage Complexity
ColorOptionComponent
73%
45/61
70%
7/10
1.357
ColorOptionComponent$1
66%
2/3
N/A
1.357
ColorOptionComponent$2
66%
2/3
N/A
1.357
ColorOptionComponent$3
50%
2/4
N/A
1.357
 
 1  181102
 /*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  0
   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  
   public ColorOptionComponent (ColorOption opt, String text, SwingFrame parent) {
 63  0
     this(opt, text, parent, false);
 64  0
   }
 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  
   public ColorOptionComponent(ColorOption opt, String text, SwingFrame parent, boolean isBackgroundColor) {
 70  0
     this(opt, text, parent, isBackgroundColor, false);
 71  0
   }
 72  
   
 73  
   public ColorOptionComponent(ColorOption opt, String text, SwingFrame parent, boolean isBackgroundColor, boolean isBoldText)
 74  
   {
 75  181102
     super(opt, text, parent);
 76  181102
     _isBackgroundColor = isBackgroundColor;
 77  181102
     _isBoldText = isBoldText;
 78  181102
     _button = new JButton();
 79  181102
     _button.addActionListener(new ActionListener() {
 80  0
       public void actionPerformed(ActionEvent e) { chooseColor(); }
 81  
     });
 82  181102
     _button.setText("...");
 83  181102
     _button.setMaximumSize(new Dimension(10,10));
 84  181102
     _button.setMinimumSize(new Dimension(10,10));
 85  
     
 86  181102
     _colorField = new JTextField();
 87  181102
     _colorField.setEditable(false);
 88  181102
     _colorField.setHorizontalAlignment(JTextField.CENTER);
 89  181102
     _panel = new JPanel(new BorderLayout());
 90  181102
     _panel.add(_colorField, BorderLayout.CENTER);
 91  181102
     _panel.add(_button, BorderLayout.EAST);
 92  181102
     if (_isBackgroundColor) {
 93  
 //      _colorField.setForeground(Color.black);
 94  105156
       _colorField.setForeground(DrJava.getConfig().getSetting(OptionConstants.DEFINITIONS_NORMAL_COLOR));
 95  210312
       DrJava.getConfig().addOptionListener(OptionConstants.DEFINITIONS_NORMAL_COLOR,
 96  105156
                                            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  75946
       _colorField.setBackground(DrJava.getConfig().getSetting(OptionConstants.DEFINITIONS_BACKGROUND_COLOR));
 103  151892
        DrJava.getConfig().addOptionListener(OptionConstants.DEFINITIONS_BACKGROUND_COLOR,
 104  75946
                                            new OptionListener<Color>() {
 105  
         public void optionChanged(OptionEvent<Color> oe) {
 106  0
           _colorField.setBackground(oe.value);
 107  0
         }
 108  
       });
 109  
    }
 110  181102
     if (_isBoldText) {
 111  11684
       _colorField.setFont(_colorField.getFont().deriveFont(Font.BOLD));
 112  
     }
 113  181102
     _color = DrJava.getConfig().getSetting(_option);
 114  181102
     _updateField(_color);
 115  181102
     setComponent(_panel);
 116  181102
   }
 117  
   
 118  
   /** Constructor that allows for a tooltip description. */
 119  
   public ColorOptionComponent(ColorOption opt, String text, SwingFrame parent, String description) {
 120  0
     this(opt, text, parent, description, false);
 121  0
   }
 122  
 
 123  
   /** Constructor that allows for a tooltip description as well as whether or not this is a background color. */
 124  
   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  0
   }
 129  
 
 130  
   /** Constructor that allows for a tooltip description as well as whether or not this is a background color.*/
 131  
   public ColorOptionComponent(ColorOption opt, String text, SwingFrame parent, String description, boolean isBackgroundColor, 
 132  
                               boolean isBoldText) {
 133  181102
     this(opt, text, parent, isBackgroundColor, isBoldText);
 134  181102
     setDescription(description);
 135  181102
   }
 136  
 
 137  
   /** Sets the tooltip description text for this option.
 138  
    *  @param description the tooltip text
 139  
    */
 140  
   public void setDescription(String description) {
 141  181102
     _panel.setToolTipText(description);
 142  181102
     _button.setToolTipText(description);
 143  181102
     _colorField.setToolTipText(description);
 144  181102
     _label.setToolTipText(description);
 145  181102
   }
 146  
     
 147  
   /** Updates the config object with the new setting.
 148  
    *  @return true if the new value is set successfully
 149  
    */
 150  
   public boolean updateConfig() {
 151  2449
     if (!_color.equals(DrJava.getConfig().getSetting(_option))) { DrJava.getConfig().setSetting(_option, _color); }
 152  2449
     return true;
 153  
   }
 154  
   
 155  
    
 156  
   /** Displays the given value. */
 157  
   public void setValue(Color value) {
 158  22196
     _color = value;
 159  22196
     _updateField(value);
 160  22196
   }
 161  
   
 162  
   /** Updates the component's field to display the given color. */
 163  
   private void _updateField(Color c) {
 164  203298
     if (_isBackgroundColor) _colorField.setBackground(c);
 165  85254
     else _colorField.setForeground(c);
 166  203298
     _colorField.setText(getLabelText() + " (" + _option.format(c) + ")");
 167  203298
   }
 168  
   
 169  
   /** Shows a color chooser dialog for picking a new color. */
 170  
   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  0
   }
 178  
 }