KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > businesslogic > ireport > gui > sheet > ColorSelectorPanel


1 /*
2  * Copyright (C) 2005 - 2006 JasperSoft Corporation. All rights reserved.
3  * http://www.jaspersoft.com.
4  *
5  * Unless you have purchased a commercial license agreement from JasperSoft,
6  * the following license terms apply:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as published by
10  * the Free Software Foundation.
11  *
12  * This program is distributed WITHOUT ANY WARRANTY; and without the
13  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
18  * or write to:
19  *
20  * Free Software Foundation, Inc.,
21  * 59 Temple Place - Suite 330,
22  * Boston, MA USA 02111-1307
23  *
24  *
25  *
26  *
27  * ColorSelectorPanel.java
28  *
29  * Created on 5 ottobre 2004, 23.31
30  *
31  */

32
33 package it.businesslogic.ireport.gui.sheet;
34 import java.awt.*;
35 import java.util.*;
36 import javax.swing.*;
37 import javax.swing.border.LineBorder JavaDoc;
38 import it.businesslogic.ireport.util.I18n;
39 /**
40  *
41  * @author Administrator
42  */

43 public class ColorSelectorPanel extends javax.swing.JPanel JavaDoc {
44     
45     private Color color = Color.BLACK;
46     private String JavaDoc value = "[0,0,0]";
47     /** Creates new form ColorSelectorPanel */
48     public ColorSelectorPanel() {
49         initComponents();
50         applyI18n();
51         setColor(null);
52     }
53
54     public String JavaDoc getValue() {
55         return value;
56     }
57
58     public void setValue(Object JavaDoc newValue) {
59         
60         if (newValue == null){
61             this.setColor(null);
62             return;
63         }
64         
65         Color newColor = null;
66         if (newValue instanceof Color) newColor = (Color)newValue;
67         else newColor = parseColorString(""+newValue);
68         
69         if (newColor == null) return;
70         this.setColor(newColor);
71     }
72
73     public Color getColor() {
74         return color;
75     }
76
77     public void setColor(Color color) {
78         this.color = color;
79         if (color == null){
80             this.value=null;
81             jTextFieldColorValue.setText("");
82             this.jPanelColor.setBackground(Color.WHITE);
83             this.jPanelColor.setBorder(new LineBorder JavaDoc(Color.LIGHT_GRAY));
84             this.jPanelColor.invalidate();
85             this.jPanelColor.updateUI();
86         }
87         else
88         {
89             this.jPanelColor.setBorder(new LineBorder JavaDoc(Color.BLACK));
90             this.value = "[" + color.getRed() + "," + color.getGreen() + "," + color.getBlue()+ "]";
91             this.jPanelColor.setBackground(color );
92             this.jPanelColor.invalidate();
93             this.jPanelColor.updateUI();
94             jTextFieldColorValue.setText( getValue() );
95         }
96         
97         
98         fireActionListenerActionPerformed(new java.awt.event.ActionEvent JavaDoc(this,0,""));
99     }
100     
101     public static Color parseColorString(String JavaDoc newValue)
102     {
103         if (newValue == null) return null;
104         
105         newValue = newValue.trim();
106         if (!newValue.startsWith("[") || !newValue.endsWith("]"))
107         {
108             // Try to create the color from a string...
109
java.awt.Color JavaDoc c = java.awt.Color.getColor(newValue);
110             if (c == null && newValue.startsWith("#") && newValue.length() == 7)
111             {
112                 int hr = Integer.parseInt( newValue.substring(1,3), 16);
113                 int hg = Integer.parseInt( newValue.substring(3,5), 16);
114                 int hb = Integer.parseInt( newValue.substring(5,7), 16);
115                 c = new Color(hr,hg,hb);
116                 return c;
117             }
118             
119             return null;
120         }
121         
122         int r = 0;
123         int g = 0;
124         int b = 0;
125         String JavaDoc rgbValues = newValue.substring(1,newValue.length()-1);
126         try {
127         
128             StringTokenizer st = new StringTokenizer(rgbValues, ",",false);
129             r = Integer.parseInt( st.nextToken() );
130             g = Integer.parseInt( st.nextToken() );
131             b = Integer.parseInt( st.nextToken() );
132         } catch (Exception JavaDoc ex) { return null; }
133         
134         Color c = new Color(r,g,b);
135         return c;
136         
137     }
138     
139     
140     /** This method is called from within the constructor to
141      * initialize the form.
142      * WARNING: Do NOT modify this code. The content of this method is
143      * always regenerated by the Form Editor.
144      */

145     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
146
private void initComponents() {
147         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
148
149         jPanelColor = new javax.swing.JPanel JavaDoc();
150         jTextFieldColorValue = new javax.swing.JTextField JavaDoc();
151         jButtonSelect = new javax.swing.JButton JavaDoc();
152
153         setLayout(new java.awt.GridBagLayout JavaDoc());
154
155         setBackground(new java.awt.Color JavaDoc(255, 255, 255));
156         setMinimumSize(new java.awt.Dimension JavaDoc(45, 10));
157         setPreferredSize(new java.awt.Dimension JavaDoc(75, 22));
158         jPanelColor.setLayout(null);
159
160         jPanelColor.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color JavaDoc(0, 0, 0)));
161         jPanelColor.setMaximumSize(new java.awt.Dimension JavaDoc(18, 18));
162         jPanelColor.setMinimumSize(new java.awt.Dimension JavaDoc(18, 8));
163         jPanelColor.setPreferredSize(new java.awt.Dimension JavaDoc(18, 18));
164         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
165         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
166         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
167         gridBagConstraints.weighty = 1.0;
168         gridBagConstraints.insets = new java.awt.Insets JavaDoc(4, 0, 4, 4);
169         add(jPanelColor, gridBagConstraints);
170
171         jTextFieldColorValue.setText("[0,0,0]");
172         jTextFieldColorValue.setBorder(null);
173         jTextFieldColorValue.setPreferredSize(new java.awt.Dimension JavaDoc(100, 14));
174         jTextFieldColorValue.addActionListener(new java.awt.event.ActionListener JavaDoc() {
175             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
176                 jTextFieldColorValueActionPerformed(evt);
177             }
178         });
179         jTextFieldColorValue.addFocusListener(new java.awt.event.FocusAdapter JavaDoc() {
180             public void focusLost(java.awt.event.FocusEvent JavaDoc evt) {
181                 jTextFieldColorValueFocusLost(evt);
182             }
183         });
184
185         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
186         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
187         gridBagConstraints.weightx = 1.0;
188         add(jTextFieldColorValue, gridBagConstraints);
189
190         jButtonSelect.setText("...");
191         jButtonSelect.setMargin(new java.awt.Insets JavaDoc(0, 0, 0, 0));
192         jButtonSelect.setMaximumSize(new java.awt.Dimension JavaDoc(19, 19));
193         jButtonSelect.setMinimumSize(new java.awt.Dimension JavaDoc(19, 10));
194         jButtonSelect.setPreferredSize(new java.awt.Dimension JavaDoc(19, 22));
195         jButtonSelect.addActionListener(new java.awt.event.ActionListener JavaDoc() {
196             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
197                 jButtonSelectActionPerformed(evt);
198             }
199         });
200
201         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
202         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
203         gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
204         gridBagConstraints.weighty = 1.0;
205         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 4, 0, 0);
206         add(jButtonSelect, gridBagConstraints);
207
208     }// </editor-fold>//GEN-END:initComponents
209

210     private void jTextFieldColorValueFocusLost(java.awt.event.FocusEvent JavaDoc evt) {//GEN-FIRST:event_jTextFieldColorValueFocusLost
211
jTextFieldColorValueActionPerformed(null);
212     }//GEN-LAST:event_jTextFieldColorValueFocusLost
213

214     private void jButtonSelectActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jButtonSelectActionPerformed
215

216         Color c =JColorChooser.showDialog(this,"Pick a color...",this.getColor());
217         if (c != null) setColor(c);
218         
219     }//GEN-LAST:event_jButtonSelectActionPerformed
220

221     private void jTextFieldColorValueActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jTextFieldColorValueActionPerformed
222

223         Color newColor = parseColorString(jTextFieldColorValue.getText());
224         if (newColor == null)
225         {
226             this.jTextFieldColorValue.setText(getValue());
227         }
228         if (newColor != null) setColor(newColor);
229         
230     }//GEN-LAST:event_jTextFieldColorValueActionPerformed
231

232     /**
233      * Registers ActionListener to receive events.
234      * @param listener The listener to register.
235      */

236     public synchronized void addActionListener(java.awt.event.ActionListener JavaDoc listener) {
237
238         if (listenerList == null ) {
239             listenerList = new javax.swing.event.EventListenerList JavaDoc();
240         }
241         listenerList.add (java.awt.event.ActionListener JavaDoc.class, listener);
242     }
243
244     /**
245      * Removes ActionListener from the list of listeners.
246      * @param listener The listener to remove.
247      */

248     public synchronized void removeActionListener(java.awt.event.ActionListener JavaDoc listener) {
249
250         listenerList.remove (java.awt.event.ActionListener JavaDoc.class, listener);
251     }
252
253     /**
254      * Notifies all registered listeners about the event.
255      *
256      * @param event The event to be fired
257      */

258     private void fireActionListenerActionPerformed(java.awt.event.ActionEvent JavaDoc event) {
259
260         if (listenerList == null) return;
261         Object JavaDoc[] listeners = listenerList.getListenerList ();
262         for (int i = listeners.length-2; i>=0; i-=2) {
263             if (listeners[i]==java.awt.event.ActionListener JavaDoc.class) {
264                 ((java.awt.event.ActionListener JavaDoc)listeners[i+1]).actionPerformed (event);
265             }
266         }
267     }
268     
269     
270     // Variables declaration - do not modify//GEN-BEGIN:variables
271
private javax.swing.JButton JavaDoc jButtonSelect;
272     private javax.swing.JPanel JavaDoc jPanelColor;
273     private javax.swing.JTextField JavaDoc jTextFieldColorValue;
274     // End of variables declaration//GEN-END:variables
275

276     public void applyI18n(){
277                 // Start autogenerated code ----------------------
278
jButtonSelect.setText(I18n.getString("colorSelectorPanel.buttonSelect","..."));
279                 // End autogenerated code ----------------------
280
}
281 }
282
Popular Tags