KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > ui > config > ColorOptionComponent


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2006 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.drjava.ui.config;
35
36 import javax.swing.*;
37 import edu.rice.cs.drjava.config.*;
38 import edu.rice.cs.drjava.*;
39 import java.awt.*;
40 import java.awt.event.*;
41
42 /** Graphical form of a ColorOption.
43  * @version $Id: ColorOptionComponent.java 3901 2006-06-30 05:28:11Z rcartwright $
44  */

45 public class ColorOptionComponent extends OptionComponent<Color> {
46   private JButton _button;
47   private JTextField _colorField;
48   private JPanel _panel;
49   private Color _color;
50   private boolean _isBackgroundColor;
51   private boolean _isBoldText;
52   
53   /** Main constructor for ColorOptionComponent.
54    * @param opt The ColorOption to display
55    * @param text The text to display in the label of the component
56    * @param parent The Frame displaying this component
57    */

58   public ColorOptionComponent (ColorOption opt, String JavaDoc text, Frame parent) {
59     this(opt, text, parent, false);
60   }
61   
62   /** An alternate constructor, allowing the caller to specify whether this color is a background color. If so,
63    * the button will display the color as its background.
64    */

65   public ColorOptionComponent(ColorOption opt, String JavaDoc text, Frame parent, boolean isBackgroundColor) {
66     this(opt, text, parent, isBackgroundColor, false);
67   }
68   
69   public ColorOptionComponent(ColorOption opt, String JavaDoc text, Frame parent, boolean isBackgroundColor, boolean isBoldText)
70   {
71     super(opt, text, parent);
72     _isBackgroundColor = isBackgroundColor;
73     _isBoldText = isBoldText;
74     _button = new JButton();
75     _button.addActionListener(new ActionListener() {
76       public void actionPerformed(ActionEvent e) { chooseColor(); }
77     });
78     _button.setText("...");
79     _button.setMaximumSize(new Dimension(10,10));
80     _button.setMinimumSize(new Dimension(10,10));
81     
82     _colorField = new JTextField();
83     _colorField.setEditable(false);
84     _colorField.setHorizontalAlignment(JTextField.CENTER);
85     _panel = new JPanel(new BorderLayout());
86     _panel.add(_colorField, BorderLayout.CENTER);
87     _panel.add(_button, BorderLayout.EAST);
88     if (_isBackgroundColor) {
89 // _colorField.setForeground(Color.black);
90
_colorField.setForeground(DrJava.getConfig().getSetting(OptionConstants.DEFINITIONS_NORMAL_COLOR));
91       DrJava.getConfig().addOptionListener(OptionConstants.DEFINITIONS_NORMAL_COLOR,
92                                            new OptionListener<Color>() {
93         public void optionChanged(OptionEvent<Color> oe) {
94           _colorField.setForeground(oe.value);
95         }
96       });
97     }
98     else {
99 // _colorField.setBackground(Color.white);
100
_colorField.setBackground(DrJava.getConfig().getSetting(OptionConstants.DEFINITIONS_BACKGROUND_COLOR));
101        DrJava.getConfig().addOptionListener(OptionConstants.DEFINITIONS_BACKGROUND_COLOR,
102                                            new OptionListener<Color>() {
103         public void optionChanged(OptionEvent<Color> oe) {
104           _colorField.setBackground(oe.value);
105         }
106       });
107    }
108     if (_isBoldText) {
109       _colorField.setFont(_colorField.getFont().deriveFont(Font.BOLD));
110     }
111     _color = DrJava.getConfig().getSetting(_option);
112     _updateField(_color);
113   }
114   
115   /** Constructor that allows for a tooltip description. */
116   public ColorOptionComponent(ColorOption opt, String JavaDoc text,
117                               Frame parent, String JavaDoc description) {
118     this(opt, text, parent, description, false);
119   }
120
121   /** Constructor that allows for a tooltip description as well as whether or not this is a background color. */
122   public ColorOptionComponent(ColorOption opt, String JavaDoc text, Frame parent, String JavaDoc description, boolean isBackgroundColor)
123   {
124     this(opt, text, parent, isBackgroundColor);
125     setDescription(description);
126   }
127
128   /** Constructor that allows for a tooltip description as well as whether or not this is a background color.*/
129   public ColorOptionComponent(ColorOption opt, String JavaDoc text, Frame parent, String JavaDoc description, boolean isBackgroundColor,
130                               boolean isBoldText) {
131     this(opt, text, parent, isBackgroundColor, isBoldText);
132     setDescription(description);
133   }
134
135   /** Sets the tooltip description text for this option.
136    * @param description the tooltip text
137    */

138   public void setDescription(String JavaDoc description) {
139     _panel.setToolTipText(description);
140     _button.setToolTipText(description);
141     _colorField.setToolTipText(description);
142     _label.setToolTipText(description);
143   }
144     
145   /** Updates the config object with the new setting.
146    * @return true if the new value is set successfully
147    */

148   public boolean updateConfig() {
149     if (!_color.equals(DrJava.getConfig().getSetting(_option))) {
150       DrJava.getConfig().setSetting(_option, _color);
151     }
152
153     return true;
154   }
155   
156    
157   /** Displays the given value. */
158   public void setValue(Color value) {
159     _color = value;
160     _updateField(value);
161   }
162   
163   /** Updates the component's field to display the given color. */
164   private void _updateField(Color c) {
165     if (_isBackgroundColor) {
166       _colorField.setBackground(c);
167     }
168     else {
169       _colorField.setForeground(c);
170     }
171     _colorField.setText(getLabelText() + " ("+_option.format(c)+")");
172   }
173   
174   /**
175    * Return's this OptionComponent's configurable component.
176    */

177   public JComponent getComponent() { return _panel; }
178   
179   /** Shows a color chooser dialog for picking a new color. */
180   public void chooseColor() {
181     Color c = JColorChooser.showDialog(_parent, "Choose '" + getLabelText() + "'", _color);
182     if (c != null) {
183       _color = c;
184       notifyChangeListeners();
185       _updateField(_color);
186     }
187   }
188   
189 }
Popular Tags