001: /*
002: * ColorWellButton.java - Shows color chooser when clicked
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 2002 Slava Pestov
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022:
023: package org.gjt.sp.jedit.gui;
024:
025: //{{{ Imports
026: import javax.swing.*;
027: import java.awt.event.*;
028: import java.awt.*;
029: import org.gjt.sp.jedit.jEdit;
030: import org.gjt.sp.jedit.GUIUtilities;
031: import org.gjt.sp.jedit.OperatingSystem;
032:
033: //}}}
034:
035: /**
036: * A button that, when clicked, shows a color chooser.
037: *
038: * You can get and set the currently selected color using
039: * {@link #getSelectedColor()} and {@link #setSelectedColor(Color)}.
040: * @author Slava Pestov
041: * @version $Id: ColorWellButton.java 5067 2004-06-28 06:45:27Z spestov $
042: */
043: public class ColorWellButton extends JButton {
044: //{{{ ColorWellButton constructor
045: public ColorWellButton(Color color) {
046: setIcon(new ColorWell(color));
047: setMargin(new Insets(2, 2, 2, 2));
048: addActionListener(new ActionHandler());
049:
050: // according to krisk this looks better on OS X...
051: if (OperatingSystem.isMacOSLF())
052: putClientProperty("JButton.buttonType", "toolbar");
053: } //}}}
054:
055: //{{{ getSelectedColor() method
056: public Color getSelectedColor() {
057: return ((ColorWell) getIcon()).color;
058: } //}}}
059:
060: //{{{ setSelectedColor() method
061: public void setSelectedColor(Color color) {
062: ((ColorWell) getIcon()).color = color;
063: repaint();
064: } //}}}
065:
066: //{{{ ColorWell class
067: static class ColorWell implements Icon {
068: Color color;
069:
070: ColorWell(Color color) {
071: this .color = color;
072: }
073:
074: public int getIconWidth() {
075: return 35;
076: }
077:
078: public int getIconHeight() {
079: return 10;
080: }
081:
082: public void paintIcon(Component c, Graphics g, int x, int y) {
083: if (color == null)
084: return;
085:
086: g.setColor(color);
087: g.fillRect(x, y, getIconWidth(), getIconHeight());
088: g.setColor(color.darker());
089: g.drawRect(x, y, getIconWidth() - 1, getIconHeight() - 1);
090: }
091: } //}}}
092:
093: //{{{ ActionHandler class
094: class ActionHandler implements ActionListener {
095: public void actionPerformed(ActionEvent evt) {
096: JDialog parent = GUIUtilities
097: .getParentDialog(ColorWellButton.this );
098: JDialog dialog;
099: if (parent != null) {
100: dialog = new ColorPickerDialog(parent, jEdit
101: .getProperty("colorChooser.title"), true);
102: } else {
103: dialog = new ColorPickerDialog(JOptionPane
104: .getFrameForComponent(ColorWellButton.this ),
105: jEdit.getProperty("colorChooser.title"), true);
106: }
107: dialog.pack();
108: dialog.setVisible(true);
109: }
110: } //}}}
111:
112: //{{{ ColorPickerDialog class
113: /**
114: * Replacement for the color picker dialog provided with Swing. This version
115: * supports dialog as well as frame parents.
116: * @since jEdit 4.1pre7
117: */
118: private class ColorPickerDialog extends EnhancedDialog implements
119: ActionListener {
120: public ColorPickerDialog(Frame parent, String title,
121: boolean modal) {
122: super (parent, title, modal);
123:
124: init();
125: }
126:
127: public ColorPickerDialog(Dialog parent, String title,
128: boolean modal) {
129: super (parent, title, modal);
130:
131: getContentPane().setLayout(new BorderLayout());
132:
133: init();
134: }
135:
136: public void ok() {
137: Color c = chooser.getColor();
138: if (c != null)
139: setSelectedColor(c);
140: setVisible(false);
141: }
142:
143: public void cancel() {
144: setVisible(false);
145: }
146:
147: public void actionPerformed(ActionEvent evt) {
148: if (evt.getSource() == ok)
149: ok();
150: else
151: cancel();
152: }
153:
154: //{{{ Private members
155: private JColorChooser chooser;
156: private JButton ok;
157: private JButton cancel;
158:
159: private void init() {
160: Color c = getSelectedColor();
161: if (c == null)
162: chooser = new JColorChooser();
163: else
164: chooser = new JColorChooser(c);
165:
166: getContentPane().add(BorderLayout.CENTER, chooser);
167:
168: Box buttons = new Box(BoxLayout.X_AXIS);
169: buttons.add(Box.createGlue());
170:
171: ok = new JButton(jEdit.getProperty("common.ok"));
172: ok.addActionListener(this );
173: buttons.add(ok);
174: buttons.add(Box.createHorizontalStrut(6));
175: getRootPane().setDefaultButton(ok);
176: cancel = new JButton(jEdit.getProperty("common.cancel"));
177: cancel.addActionListener(this );
178: buttons.add(cancel);
179: buttons.add(Box.createGlue());
180:
181: getContentPane().add(BorderLayout.SOUTH, buttons);
182: pack();
183: setLocationRelativeTo(getParent());
184: } //}}}
185: } //}}}
186: }
|