KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > gui > ColorWellButton


1 /*
2  * ColorWellButton.java - Shows color chooser when clicked
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 2002 Slava Pestov
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  */

22
23 package org.gjt.sp.jedit.gui;
24
25 //{{{ Imports
26
import javax.swing.*;
27 import java.awt.event.*;
28 import java.awt.*;
29 import org.gjt.sp.jedit.jEdit;
30 import org.gjt.sp.jedit.GUIUtilities;
31 import org.gjt.sp.jedit.OperatingSystem;
32 //}}}
33

34 /**
35  * A button that, when clicked, shows a color chooser.
36  *
37  * You can get and set the currently selected color using
38  * {@link #getSelectedColor()} and {@link #setSelectedColor(Color)}.
39  * @author Slava Pestov
40  * @version $Id: ColorWellButton.java 5067 2004-06-28 06:45:27Z spestov $
41  */

42 public class ColorWellButton extends JButton
43 {
44     //{{{ ColorWellButton constructor
45
public ColorWellButton(Color color)
46     {
47         setIcon(new ColorWell(color));
48         setMargin(new Insets(2,2,2,2));
49         addActionListener(new ActionHandler());
50
51         // according to krisk this looks better on OS X...
52
if(OperatingSystem.isMacOSLF())
53             putClientProperty("JButton.buttonType","toolbar");
54     } //}}}
55

56     //{{{ getSelectedColor() method
57
public Color getSelectedColor()
58     {
59         return ((ColorWell)getIcon()).color;
60     } //}}}
61

62     //{{{ setSelectedColor() method
63
public void setSelectedColor(Color color)
64     {
65         ((ColorWell)getIcon()).color = color;
66         repaint();
67     } //}}}
68

69     //{{{ ColorWell class
70
static class ColorWell implements Icon
71     {
72         Color color;
73
74         ColorWell(Color color)
75         {
76             this.color = color;
77         }
78
79         public int getIconWidth()
80         {
81             return 35;
82         }
83
84         public int getIconHeight()
85         {
86             return 10;
87         }
88
89         public void paintIcon(Component c, Graphics g, int x, int y)
90         {
91             if(color == null)
92                 return;
93
94             g.setColor(color);
95             g.fillRect(x,y,getIconWidth(),getIconHeight());
96             g.setColor(color.darker());
97             g.drawRect(x,y,getIconWidth()-1,getIconHeight()-1);
98         }
99     } //}}}
100

101     //{{{ ActionHandler class
102
class ActionHandler implements ActionListener
103     {
104         public void actionPerformed(ActionEvent evt)
105         {
106             JDialog parent = GUIUtilities.getParentDialog(ColorWellButton.this);
107             JDialog dialog;
108             if (parent != null)
109             {
110                 dialog = new ColorPickerDialog(parent,
111                     jEdit.getProperty("colorChooser.title"),
112                     true);
113             }
114             else
115             {
116                 dialog = new ColorPickerDialog(
117                     JOptionPane.getFrameForComponent(
118                     ColorWellButton.this),
119                     jEdit.getProperty("colorChooser.title"),
120                     true);
121             }
122             dialog.pack();
123             dialog.setVisible(true);
124         }
125     } //}}}
126

127     //{{{ ColorPickerDialog class
128
/**
129      * Replacement for the color picker dialog provided with Swing. This version
130      * supports dialog as well as frame parents.
131      * @since jEdit 4.1pre7
132      */

133     private class ColorPickerDialog extends EnhancedDialog implements ActionListener
134     {
135         public ColorPickerDialog(Frame parent, String JavaDoc title, boolean modal)
136         {
137             super(parent,title,modal);
138
139             init();
140         }
141
142         public ColorPickerDialog(Dialog parent, String JavaDoc title, boolean modal)
143         {
144             super(parent,title,modal);
145
146             getContentPane().setLayout(new BorderLayout());
147
148             init();
149         }
150
151         public void ok()
152         {
153             Color c = chooser.getColor();
154             if (c != null)
155                 setSelectedColor(c);
156             setVisible(false);
157         }
158
159         public void cancel()
160         {
161             setVisible(false);
162         }
163
164         public void actionPerformed(ActionEvent evt)
165         {
166             if (evt.getSource() == ok)
167                 ok();
168             else
169                 cancel();
170         }
171
172         //{{{ Private members
173
private JColorChooser chooser;
174         private JButton ok;
175         private JButton cancel;
176
177         private void init()
178         {
179             Color c = getSelectedColor();
180             if(c == null)
181                 chooser = new JColorChooser();
182             else
183                 chooser = new JColorChooser(c);
184
185             getContentPane().add(BorderLayout.CENTER, chooser);
186
187             Box buttons = new Box(BoxLayout.X_AXIS);
188             buttons.add(Box.createGlue());
189
190             ok = new JButton(jEdit.getProperty("common.ok"));
191             ok.addActionListener(this);
192             buttons.add(ok);
193             buttons.add(Box.createHorizontalStrut(6));
194             getRootPane().setDefaultButton(ok);
195             cancel = new JButton(jEdit.getProperty("common.cancel"));
196             cancel.addActionListener(this);
197             buttons.add(cancel);
198             buttons.add(Box.createGlue());
199
200             getContentPane().add(BorderLayout.SOUTH, buttons);
201             pack();
202             setLocationRelativeTo(getParent());
203         } //}}}
204
} //}}}
205
}
206
Popular Tags