001: /*
002: * Copyright (c) 2000, Jacob Smullyan.
003: *
004: * This is part of SkunkDAV, a WebDAV client. See http://skunkdav.sourceforge.net/
005: * for the latest version.
006: *
007: * SkunkDAV is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License as published
009: * by the Free Software Foundation; either version 2, or (at your option)
010: * any later version.
011: *
012: * SkunkDAV is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with SkunkDAV; see the file COPYING. If not, write to the Free
019: * Software Foundation, 59 Temple Place - Suite 330, Boston, MA
020: * 02111-1307, USA.
021: */
022:
023: package org.skunk.swing;
024:
025: import java.awt.Color;
026: import java.awt.Dimension;
027: import java.awt.event.ActionEvent;
028: import java.awt.event.ActionListener;
029: import javax.swing.JButton;
030: import javax.swing.JColorChooser;
031: import javax.swing.JDialog;
032:
033: /**
034: * a button subclass that displays a color and has a built-in
035: * action listener that opens a JColorChooser to select a different one.
036: * @author Jacob Smullyan ($Author: smulloni $) <smulloni@krinst.org>
037: * @version $Revision: 1.2 $
038: */
039: public class ColorChoiceButton extends JButton {
040: public static final String COLOR_PROPERTY = "color";
041: private String dialogTitle = "Choose color";
042: private JColorChooser chooser;
043:
044: public ColorChoiceButton(Color defaultColor, Dimension size) {
045: super ();
046: setMinimumSize(size);
047: setPreferredSize(size);
048: setMaximumSize(size);
049: setBackground(defaultColor);
050: chooser = new JColorChooser();
051: addActionListener(new ActionListener() {
052: public void actionPerformed(ActionEvent ae) {
053: final ColorChoiceButton button = (ColorChoiceButton) ae
054: .getSource();
055: chooser.setColor(button.getColor());
056: ActionListener anotherListener = new ActionListener() {
057: public void actionPerformed(ActionEvent ae2) {
058: button.setColor(ColorChoiceButton.this .chooser
059: .getColor());
060: }
061: };
062: JDialog dialog = JColorChooser.createDialog(button,
063: button.getColorChooserDialogTitle(), true,
064: ColorChoiceButton.this .chooser,
065: anotherListener, null);
066: dialog.setLocationRelativeTo(button);
067: dialog.show();
068: }
069: });
070: }
071:
072: public ColorChoiceButton(Color c) {
073: this (c, new Dimension(63, 28));
074: }
075:
076: public ColorChoiceButton() {
077: this (Color.white, new Dimension(63, 28));
078: }
079:
080: public final Color getColor() {
081: return getBackground();
082: }
083:
084: public final void setColor(Color c) {
085: Color oldColor = getColor();
086: setBackground(c);
087: fireStateChanged();
088: firePropertyChange(COLOR_PROPERTY, oldColor, c);
089: }
090:
091: public String getColorChooserDialogTitle() {
092: return dialogTitle;
093: }
094:
095: public void setColorChooserDialogTitle(String dialogTitle) {
096: this .dialogTitle = dialogTitle;
097: }
098:
099: public static void main(String[] args) {
100: javax.swing.JFrame frame = new javax.swing.JFrame();
101: javax.swing.JPanel panel = new javax.swing.JPanel();
102: panel.setLayout(new java.awt.FlowLayout());
103: panel.add(new ColorChoiceButton(Color.green));
104: panel.setPreferredSize(new Dimension(300, 300));
105: frame.setContentPane(panel);
106: frame.addWindowListener(new java.awt.event.WindowAdapter() {
107: public void windowClosing(java.awt.event.WindowEvent woo) {
108: System.exit(0);
109: }
110: });
111: frame.pack();
112: frame.setVisible(true);
113: }
114: }
115:
116: /* $Log: ColorChoiceButton.java,v $
117: /* Revision 1.2 2001/02/06 00:11:18 smulloni
118: /* struggle, perhaps futile, with the TextEditorCustomizer and other implicated
119: /* classes
120: /*
121: /* Revision 1.1 2001/02/02 23:30:33 smulloni
122: /* adding customization features to the text editor.
123: /* */
|