KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > modeler > diagrammer > ColorChooserDialog


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, is permitted
5  * provided that the following conditions are met:
6  * - Redistributions of source code must retain the above copyright notice, this list of conditions
7  * and the following disclaimer.
8  * - Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * - All advertising materials mentioning features or use of this software must display the
12  * following acknowledgment: "This product includes Djeneric."
13  * - Products derived from this software may not be called "Djeneric" nor may
14  * "Djeneric" appear in their names without prior written permission of Genimen BV.
15  * - Redistributions of any form whatsoever must retain the following acknowledgment: "This
16  * product includes Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.tools.modeler.diagrammer;
31
32 import java.awt.BorderLayout JavaDoc;
33 import java.awt.Color JavaDoc;
34 import java.awt.Dimension JavaDoc;
35 import java.awt.FlowLayout JavaDoc;
36 import java.awt.Frame JavaDoc;
37 import java.awt.Toolkit JavaDoc;
38 import java.awt.event.ActionEvent JavaDoc;
39 import java.awt.event.MouseEvent JavaDoc;
40 import java.awt.event.WindowEvent JavaDoc;
41
42 import javax.swing.JButton JavaDoc;
43 import javax.swing.JColorChooser JavaDoc;
44 import javax.swing.JComponent JavaDoc;
45 import javax.swing.JDialog JavaDoc;
46 import javax.swing.JLabel JavaDoc;
47 import javax.swing.JPanel JavaDoc;
48 import javax.swing.event.ChangeEvent JavaDoc;
49 import javax.swing.event.ChangeListener JavaDoc;
50
51 import com.genimen.djeneric.language.Messages;
52 import com.genimen.djeneric.ui.DjVerticalFlowLayout;
53 import com.genimen.djeneric.ui.Util;
54 import com.genimen.djeneric.util.DjLogger;
55
56 public class ColorChooserDialog extends JDialog JavaDoc
57 {
58   private static final long serialVersionUID = 1L;
59   static Color JavaDoc _storedExtentBackground = new Color JavaDoc(58, 110, 165);
60   static Color JavaDoc _storedPropertyForeground = Color.lightGray;
61   static Color JavaDoc _storedExtentNameForeground = Color.white;
62
63   JPanel JavaDoc panel1 = new JPanel JavaDoc();
64   BorderLayout JavaDoc borderLayout1 = new BorderLayout JavaDoc();
65   JColorChooser JavaDoc _colorChooser = new JColorChooser JavaDoc(Color.white);
66   JPanel JavaDoc jPanel1 = new JPanel JavaDoc();
67   BorderLayout JavaDoc borderLayout2 = new BorderLayout JavaDoc();
68   JPanel JavaDoc jPanel2 = new JPanel JavaDoc();
69   JButton JavaDoc _butOk = new JButton JavaDoc();
70   JButton JavaDoc _butCancel = new JButton JavaDoc();
71   JPanel JavaDoc jPanel4 = new JPanel JavaDoc();
72   FlowLayout JavaDoc flowLayout1 = new FlowLayout JavaDoc();
73   JPanel JavaDoc _tableBackGround = new JPanel JavaDoc();
74   JLabel JavaDoc _tableColor = new JLabel JavaDoc();
75   JLabel JavaDoc _columnColor = new JLabel JavaDoc();
76   DjVerticalFlowLayout verticalFlowLayout1 = new DjVerticalFlowLayout();
77
78   JComponent JavaDoc _selected = _tableBackGround;
79   JPanel JavaDoc jPanel3 = new JPanel JavaDoc();
80   JButton JavaDoc _butStore = new JButton JavaDoc();
81   JButton JavaDoc _butRecall = new JButton JavaDoc();
82   boolean _canceled = true;
83
84   public ColorChooserDialog(Frame JavaDoc frame)
85   {
86     super(frame, Messages.getString("ColorChooserDialog.ChooseColor"), true);
87     try
88     {
89       jbInit();
90
91       pack();
92
93       Dimension JavaDoc screenSize = Toolkit.getDefaultToolkit().getScreenSize();
94       Dimension JavaDoc frameSize = getSize();
95       setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
96
97     }
98     catch (Exception JavaDoc ex)
99     {
100       DjLogger.log(ex);
101     }
102   }
103
104   void jbInit() throws Exception JavaDoc
105   {
106     panel1.setLayout(borderLayout1);
107     jPanel1.setLayout(borderLayout2);
108     _butOk.setText(Messages.getString("global.Ok"));
109     _butOk.addActionListener(new java.awt.event.ActionListener JavaDoc()
110     {
111       public void actionPerformed(ActionEvent JavaDoc e)
112       {
113         _butOk_actionPerformed(e);
114       }
115     });
116     _butCancel.setText(Messages.getString("global.Cancel"));
117     _butCancel.addActionListener(new java.awt.event.ActionListener JavaDoc()
118     {
119       public void actionPerformed(ActionEvent JavaDoc e)
120       {
121         _butCancel_actionPerformed(e);
122       }
123     });
124
125     // Circumvent a bug in a previous 1.4 JDK: set a custom panel, but add it manually to the dialog
126
JPanel JavaDoc dummy = new JPanel JavaDoc();
127     dummy.setPreferredSize(new Dimension JavaDoc(1, 1));
128     _colorChooser.setPreviewPanel(dummy);
129
130     jPanel4.setLayout(flowLayout1);
131     _tableColor.setText(Messages.getString("ColorChooserDialog.ExtentName"));
132     _tableColor.addMouseListener(new java.awt.event.MouseAdapter JavaDoc()
133     {
134       public void mouseClicked(MouseEvent JavaDoc e)
135       {
136         _tableColor_mouseClicked(e);
137       }
138     });
139     _columnColor.setText(Messages.getString("ColorChooserDialog.PropertyName"));
140     _columnColor.addMouseListener(new java.awt.event.MouseAdapter JavaDoc()
141     {
142       public void mouseClicked(MouseEvent JavaDoc e)
143       {
144         _columnColor_mouseClicked(e);
145       }
146     });
147     _tableBackGround.setLayout(verticalFlowLayout1);
148     verticalFlowLayout1.setAlignment(FlowLayout.CENTER);
149     verticalFlowLayout1.setHorizontalFill(false);
150     _tableBackGround.setBackground(new Color JavaDoc(125, 125, 255));
151     _tableBackGround.setPreferredSize(new Dimension JavaDoc(150, 60));
152     _tableBackGround.addMouseListener(new java.awt.event.MouseAdapter JavaDoc()
153     {
154       public void mouseClicked(MouseEvent JavaDoc e)
155       {
156         _tableBackGround_mouseClicked(e);
157       }
158     });
159     _butStore.setText(Messages.getString("ColorChooserDialog.Store"));
160     _butStore.addActionListener(new java.awt.event.ActionListener JavaDoc()
161     {
162       public void actionPerformed(ActionEvent JavaDoc e)
163       {
164         _butStore_actionPerformed(e);
165       }
166     });
167     _butRecall.setText(Messages.getString("ColorChooserDialog.Recall"));
168     _butRecall.addActionListener(new java.awt.event.ActionListener JavaDoc()
169     {
170       public void actionPerformed(ActionEvent JavaDoc e)
171       {
172         _butRecall_actionPerformed(e);
173       }
174     });
175     this.addWindowListener(new java.awt.event.WindowAdapter JavaDoc()
176     {
177       public void windowOpened(WindowEvent JavaDoc e)
178       {
179         this_windowOpened(e);
180       }
181     });
182
183     getContentPane().add(panel1);
184     // Uncomment the following only for design purposes!
185
// It causes a run-time exception under JDK1.4
186
panel1.add(jPanel4, BorderLayout.SOUTH);
187
188     jPanel4.add(_tableBackGround, null);
189     _tableBackGround.add(_tableColor, null);
190
191     panel1.add(_colorChooser, BorderLayout.CENTER);
192
193     this.getContentPane().add(jPanel1, BorderLayout.SOUTH);
194     jPanel1.add(jPanel2, BorderLayout.EAST);
195     jPanel2.add(_butCancel, null);
196     jPanel2.add(_butOk, null);
197     jPanel1.add(jPanel3, BorderLayout.WEST);
198     jPanel3.add(_butStore, null);
199     _tableBackGround.add(_columnColor, null);
200     jPanel3.add(_butRecall, null);
201
202     _colorChooser.getSelectionModel().addChangeListener(new ChangeListener JavaDoc()
203     {
204       public void stateChanged(ChangeEvent JavaDoc e)
205       {
206         updateColorChooser();
207       }
208     });
209     Util.sizeButtons(jPanel1);
210   }
211
212   public Color JavaDoc setExtentColor()
213   {
214     return _tableBackGround.getBackground();
215   }
216
217   public Color JavaDoc getPropertyColor()
218   {
219     return _columnColor.getForeground();
220   }
221
222   public Color JavaDoc getNameColor()
223   {
224     return _tableColor.getForeground();
225   }
226
227   public Color JavaDoc getExtentColor()
228   {
229     return _tableBackGround.getBackground();
230   }
231
232   public void setExtentColor(Color JavaDoc c)
233   {
234     _tableBackGround.setBackground(c);
235     _tableColor.setBackground(c);
236     _columnColor.setBackground(c);
237   }
238
239   public void setNameColor(Color JavaDoc c)
240   {
241     _tableColor.setForeground(c);
242   }
243
244   public void setPropertyColor(Color JavaDoc c)
245   {
246     _columnColor.setForeground(c);
247   }
248
249   void _tableBackGround_mouseClicked(MouseEvent JavaDoc e)
250   {
251     _selected = _tableBackGround;
252     _colorChooser.setColor(_tableBackGround.getBackground());
253   }
254
255   void _tableColor_mouseClicked(MouseEvent JavaDoc e)
256   {
257     _selected = _tableColor;
258     _colorChooser.setColor(_selected.getForeground());
259   }
260
261   void _columnColor_mouseClicked(MouseEvent JavaDoc e)
262   {
263     _selected = _columnColor;
264     _colorChooser.setColor(_selected.getForeground());
265   }
266
267   public void updateColorChooser()
268   {
269     if (_selected == _tableBackGround)
270     {
271       _tableBackGround.setBackground(_colorChooser.getColor());
272       _tableColor.setBackground(_colorChooser.getColor());
273       _columnColor.setBackground(_colorChooser.getColor());
274     }
275     else _selected.setForeground(_colorChooser.getColor());
276   }
277
278   void _butStore_actionPerformed(ActionEvent JavaDoc e)
279   {
280     _storedExtentBackground = getExtentColor();
281     _storedPropertyForeground = getPropertyColor();
282     _storedExtentNameForeground = getNameColor();
283   }
284
285   void _butRecall_actionPerformed(ActionEvent JavaDoc e)
286   {
287     setExtentColor(_storedExtentBackground);
288     setPropertyColor(_storedPropertyForeground);
289     setNameColor(_storedExtentNameForeground);
290
291     _selected = _tableBackGround;
292     _colorChooser.setColor(getExtentColor());
293   }
294
295   void _butOk_actionPerformed(ActionEvent JavaDoc e)
296   {
297     _canceled = false;
298     setVisible(false);
299   }
300
301   void _butCancel_actionPerformed(ActionEvent JavaDoc e)
302   {
303     setVisible(false);
304   }
305
306   public boolean isCancelled()
307   {
308     return _canceled;
309   }
310
311   void this_windowOpened(WindowEvent JavaDoc e)
312   {
313     _colorChooser.setColor(getExtentColor());
314   }
315
316 }
Popular Tags