001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.drjava.ui.config;
038:
039: import javax.swing.*;
040: import edu.rice.cs.drjava.config.*;
041: import edu.rice.cs.drjava.*;
042: import edu.rice.cs.util.swing.FontChooser;
043:
044: import java.awt.*;
045: import java.awt.event.*;
046:
047: /**
048: * The Graphical form of a FontOption.
049: * @version $Id: FontOptionComponent.java 4255 2007-08-28 19:17:37Z mgricken $
050: */
051: public class FontOptionComponent extends OptionComponent<Font> {
052:
053: private JButton _button;
054: private JTextField _fontField;
055: private JPanel _panel;
056: private Font _font;
057:
058: public FontOptionComponent(FontOption opt, String text, Frame parent) {
059: super (opt, text, parent);
060: _button = new JButton();
061: _button.addActionListener(new ActionListener() {
062: public void actionPerformed(ActionEvent e) {
063: chooseFont();
064: }
065: });
066: _button.setText("...");
067: _button.setMaximumSize(new Dimension(10, 10));
068: _button.setMinimumSize(new Dimension(10, 10));
069:
070: _fontField = new JTextField();
071: _fontField.setEditable(false);
072: _fontField.setBackground(Color.white);
073: _fontField.setHorizontalAlignment(JTextField.CENTER);
074: _panel = new JPanel(new BorderLayout());
075: _panel.add(_fontField, BorderLayout.CENTER);
076: _panel.add(_button, BorderLayout.EAST);
077:
078: _font = DrJava.getConfig().getSetting(_option);
079: _updateField(_font);
080: }
081:
082: /**
083: * Constructor that allows for a tooltip description.
084: */
085: public FontOptionComponent(FontOption opt, String text,
086: Frame parent, String description) {
087: this (opt, text, parent);
088: setDescription(description);
089: }
090:
091: /**
092: * Sets the tooltip description text for this option.
093: * @param description the tooltip text
094: */
095: public void setDescription(String description) {
096: _panel.setToolTipText(description);
097: _fontField.setToolTipText(description);
098: _label.setToolTipText(description);
099: }
100:
101: /**
102: * Updates the font field to display the given font.
103: */
104: private void _updateField(Font f) {
105: _fontField.setFont(f);
106: _fontField.setText(_option.format(f));
107: }
108:
109: /**
110: * Return's this OptionComponent's configurable component.
111: */
112: public JComponent getComponent() {
113: return _panel;
114: }
115:
116: /**
117: * Shows a custom font chooser dialog to pick a new font.
118: */
119: public void chooseFont() {
120: String oldText = _fontField.getText();
121: Font f = FontChooser.showDialog(_parent, "Choose '"
122: + getLabelText() + "'", _font);
123: if (f != null) {
124: _font = f;
125: _updateField(_font);
126: if (!oldText.equals(_fontField.getText())) {
127: notifyChangeListeners();
128: }
129: }
130: }
131:
132: /**
133: * Updates the config object with the new setting.
134: * @return true if the new value is set successfully
135: */
136: public boolean updateConfig() {
137: if (!_font.equals(DrJava.getConfig().getSetting(_option))) {
138: DrJava.getConfig().setSetting(_option, _font);
139: }
140: return true;
141: }
142:
143: /**
144: * Displays the given value.
145: */
146: public void setValue(Font value) {
147: _font = value;
148: _updateField(value);
149: }
150: }
|