KickJava   Java API By Example, From Geeks To Geeks.

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


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 edu.rice.cs.util.swing.FontChooser;
40
41 import java.awt.*;
42 import java.awt.event.*;
43
44 /**
45  * The Graphical form of a FontOption.
46  * @version $Id: FontOptionComponent.java 3901 2006-06-30 05:28:11Z rcartwright $
47  */

48 public class FontOptionComponent extends OptionComponent<Font> {
49   
50   private JButton _button;
51   private JTextField _fontField;
52   private JPanel _panel;
53   private Font _font;
54   
55   public FontOptionComponent(FontOption opt, String JavaDoc text, Frame parent) {
56     super(opt, text, parent);
57     _button = new JButton();
58     _button.addActionListener(new ActionListener() {
59       public void actionPerformed(ActionEvent e) {
60         chooseFont();
61       }
62     });
63     _button.setText("...");
64     _button.setMaximumSize(new Dimension(10,10));
65     _button.setMinimumSize(new Dimension(10,10));
66     
67     _fontField = new JTextField();
68     _fontField.setEditable(false);
69     _fontField.setBackground(Color.white);
70     _fontField.setHorizontalAlignment(JTextField.CENTER);
71     _panel = new JPanel(new BorderLayout());
72     _panel.add(_fontField, BorderLayout.CENTER);
73     _panel.add(_button, BorderLayout.EAST);
74
75     _font = DrJava.getConfig().getSetting(_option);
76     _updateField(_font);
77   }
78   
79   /**
80    * Constructor that allows for a tooltip description.
81    */

82   public FontOptionComponent(FontOption opt, String JavaDoc text,
83                              Frame parent, String JavaDoc description) {
84     this(opt, text, parent);
85     setDescription(description);
86   }
87
88   /**
89    * Sets the tooltip description text for this option.
90    * @param description the tooltip text
91    */

92   public void setDescription(String JavaDoc description) {
93     _panel.setToolTipText(description);
94     _fontField.setToolTipText(description);
95     _label.setToolTipText(description);
96   }
97
98   /**
99    * Updates the font field to display the given font.
100    */

101   private void _updateField(Font f) {
102     _fontField.setFont(f);
103     _fontField.setText(_option.format(f));
104   }
105     
106   /**
107    * Return's this OptionComponent's configurable component.
108    */

109   public JComponent getComponent() {
110     return _panel;
111   }
112   
113   /**
114    * Shows a custom font chooser dialog to pick a new font.
115    */

116   public void chooseFont() {
117     String JavaDoc oldText = _fontField.getText();
118     Font f = FontChooser.showDialog(_parent,
119                                     "Choose '" + getLabelText() + "'",
120                                     _font);
121     if (f != null) {
122       _font = f;
123       _updateField(_font);
124       if (!oldText.equals(_fontField.getText())) {
125         notifyChangeListeners();
126       }
127     }
128   }
129   
130   /**
131    * Updates the config object with the new setting.
132    * @return true if the new value is set successfully
133    */

134   public boolean updateConfig() {
135     if (!_font.equals(DrJava.getConfig().getSetting(_option))) {
136       DrJava.getConfig().setSetting(_option, _font);
137     }
138     return true;
139   }
140   
141    /**
142    * Displays the given value.
143    */

144   public void setValue(Font value) {
145     _font = value;
146     _updateField(value);
147   }
148 }
Popular Tags