001: /*
002: * Copyright (C) 2005 - 2008 JasperSoft Corporation. All rights reserved.
003: * http://www.jaspersoft.com.
004: *
005: * Unless you have purchased a commercial license agreement from JasperSoft,
006: * the following license terms apply:
007: *
008: * This program is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License version 2 as published by
010: * the Free Software Foundation.
011: *
012: * This program is distributed WITHOUT ANY WARRANTY; and without the
013: * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
014: * See the GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
018: * or write to:
019: *
020: * Free Software Foundation, Inc.,
021: * 59 Temple Place - Suite 330,
022: * Boston, MA USA 02111-1307
023: *
024: *
025: *
026: *
027: * ColorSelectorPanel.java
028: *
029: * Created on 5 ottobre 2004, 23.31
030: *
031: */
032:
033: package it.businesslogic.ireport.gui.sheet;
034:
035: import java.awt.*;
036: import java.util.*;
037: import javax.swing.*;
038: import javax.swing.border.LineBorder;
039: import it.businesslogic.ireport.util.I18n;
040:
041: /**
042: *
043: * @author Administrator
044: */
045: public class ColorSelectorPanel extends javax.swing.JPanel {
046:
047: private Color color = Color.BLACK;
048: private String value = "#000000";
049: private ImageIcon noColorIcon = new javax.swing.ImageIcon(
050: getClass().getResource(
051: "/it/businesslogic/ireport/icons/nocolor.png"));
052:
053: private boolean init = false;
054:
055: /** Creates new form ColorSelectorPanel */
056: public ColorSelectorPanel() {
057: initComponents();
058: applyI18n();
059: setColor(null);
060: }
061:
062: public String getValue() {
063: return value;
064: }
065:
066: public void setValue(Object newValue) {
067:
068: if (newValue == null) {
069: this .setColor(null);
070: return;
071: }
072:
073: Color newColor = null;
074: if (newValue instanceof Color)
075: newColor = (Color) newValue;
076: else
077: newColor = parseColorString("" + newValue);
078:
079: if (newColor == null)
080: return;
081: this .setColor(newColor);
082: }
083:
084: public Color getColor() {
085: return color;
086: }
087:
088: public void setColor(Color color) {
089: this .color = color;
090:
091: jLabelNoColor.setIcon(color == null ? noColorIcon : null);
092:
093: if (color == null) {
094: this .value = null;
095: jTextFieldColorValue.setText("");
096: this .jPanelColor.setBackground(Color.WHITE);
097: this .jPanelColor
098: .setBorder(new LineBorder(Color.LIGHT_GRAY));
099: this .jPanelColor.invalidate();
100: this .jPanelColor.updateUI();
101: } else {
102: this .jPanelColor.setBorder(new LineBorder(Color.BLACK));
103: this .value = HexColorChooserPanel.getEncodedColor(color);
104: this .jPanelColor.setBackground(color);
105: this .jPanelColor.invalidate();
106: this .jPanelColor.updateUI();
107: jTextFieldColorValue.setText(HexColorChooserPanel
108: .getEncodedColor(color));
109: }
110:
111: fireActionListenerActionPerformed(new java.awt.event.ActionEvent(
112: this , 0, ""));
113: }
114:
115: public static Color parseColorString(String newValue) {
116: if (newValue == null)
117: return null;
118:
119: newValue = newValue.trim();
120: //System.out.println("Evaluating: " + newValue);
121:
122: if (!newValue.startsWith("[") || !newValue.endsWith("]")) {
123: // Try to create the color from a string...
124: java.awt.Color c = java.awt.Color.getColor(newValue);
125: if (c != null)
126: return c;
127: if (c == null
128: && newValue.matches(MaskedPlainDocument.COLOR_MASK)) {
129: if (newValue.startsWith("#")) {
130: newValue = newValue.substring(1);
131: }
132: if (newValue.length() >= 6) {
133: try {
134: int hr = Integer.parseInt(newValue.substring(0,
135: 2), 16);
136: int hg = Integer.parseInt(newValue.substring(2,
137: 4), 16);
138: int hb = Integer.parseInt(newValue.substring(4,
139: 6), 16);
140: c = new Color(hr, hg, hb);
141: } catch (Exception ex) {
142: }
143: }
144: }
145: return c;
146: }
147:
148: int r = 0;
149: int g = 0;
150: int b = 0;
151: String rgbValues = newValue.substring(1, newValue.length() - 1);
152: try {
153:
154: StringTokenizer st = new StringTokenizer(rgbValues, ",",
155: false);
156: r = Integer.parseInt(st.nextToken());
157: g = Integer.parseInt(st.nextToken());
158: b = Integer.parseInt(st.nextToken());
159: } catch (Exception ex) {
160: return null;
161: }
162:
163: Color c = new Color(r, g, b);
164: return c;
165:
166: }
167:
168: /** This method is called from within the constructor to
169: * initialize the form.
170: * WARNING: Do NOT modify this code. The content of this method is
171: * always regenerated by the Form Editor.
172: */
173: // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
174: private void initComponents() {
175: java.awt.GridBagConstraints gridBagConstraints;
176:
177: jPanelColor = new javax.swing.JPanel();
178: jLabelNoColor = new javax.swing.JLabel();
179: jTextFieldColorValue = new javax.swing.JTextField();
180: jButtonSelect = new javax.swing.JButton();
181:
182: setBackground(new java.awt.Color(255, 255, 255));
183: setMinimumSize(new java.awt.Dimension(45, 10));
184: setPreferredSize(new java.awt.Dimension(75, 22));
185: setLayout(new java.awt.GridBagLayout());
186:
187: jPanelColor.setBorder(javax.swing.BorderFactory
188: .createLineBorder(new java.awt.Color(0, 0, 0)));
189: jPanelColor.setMaximumSize(new java.awt.Dimension(18, 18));
190: jPanelColor.setMinimumSize(new java.awt.Dimension(18, 8));
191: jPanelColor.setPreferredSize(new java.awt.Dimension(18, 18));
192: jPanelColor.setLayout(new java.awt.BorderLayout());
193:
194: jLabelNoColor
195: .setIcon(new javax.swing.ImageIcon(
196: getClass()
197: .getResource(
198: "/it/businesslogic/ireport/icons/nocolor.png"))); // NOI18N
199: jPanelColor.add(jLabelNoColor, java.awt.BorderLayout.CENTER);
200:
201: gridBagConstraints = new java.awt.GridBagConstraints();
202: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
203: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
204: gridBagConstraints.weighty = 1.0;
205: gridBagConstraints.insets = new java.awt.Insets(4, 0, 4, 4);
206: add(jPanelColor, gridBagConstraints);
207:
208: jTextFieldColorValue.setText("[0,0,0]");
209: jTextFieldColorValue.setBorder(null);
210: jTextFieldColorValue.setPreferredSize(new java.awt.Dimension(
211: 100, 14));
212: jTextFieldColorValue
213: .addActionListener(new java.awt.event.ActionListener() {
214: public void actionPerformed(
215: java.awt.event.ActionEvent evt) {
216: jTextFieldColorValueActionPerformed(evt);
217: }
218: });
219: jTextFieldColorValue
220: .addFocusListener(new java.awt.event.FocusAdapter() {
221: public void focusLost(java.awt.event.FocusEvent evt) {
222: jTextFieldColorValueFocusLost(evt);
223: }
224: });
225: gridBagConstraints = new java.awt.GridBagConstraints();
226: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
227: gridBagConstraints.weightx = 1.0;
228: add(jTextFieldColorValue, gridBagConstraints);
229:
230: jButtonSelect.setText("...");
231: jButtonSelect.setMargin(new java.awt.Insets(0, 0, 0, 0));
232: jButtonSelect.setMaximumSize(new java.awt.Dimension(19, 19));
233: jButtonSelect.setMinimumSize(new java.awt.Dimension(19, 10));
234: jButtonSelect.setPreferredSize(new java.awt.Dimension(19, 22));
235: jButtonSelect
236: .addActionListener(new java.awt.event.ActionListener() {
237: public void actionPerformed(
238: java.awt.event.ActionEvent evt) {
239: jButtonSelectActionPerformed(evt);
240: }
241: });
242: gridBagConstraints = new java.awt.GridBagConstraints();
243: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
244: gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
245: gridBagConstraints.weighty = 1.0;
246: gridBagConstraints.insets = new java.awt.Insets(0, 4, 0, 0);
247: add(jButtonSelect, gridBagConstraints);
248: }// </editor-fold>//GEN-END:initComponents
249:
250: private void jTextFieldColorValueFocusLost(
251: java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextFieldColorValueFocusLost
252: jTextFieldColorValueActionPerformed(null);
253: }//GEN-LAST:event_jTextFieldColorValueFocusLost
254:
255: private void jButtonSelectActionPerformed(
256: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSelectActionPerformed
257:
258: Color c = HexColorChooserPanel.showDialog(this ,
259: "Pick a color!...", this .getColor());
260: if (c != null)
261: setColor(c);
262:
263: }//GEN-LAST:event_jButtonSelectActionPerformed
264:
265: private void jTextFieldColorValueActionPerformed(
266: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldColorValueActionPerformed
267:
268: Color newColor = parseColorString(jTextFieldColorValue
269: .getText());
270: if (newColor == null) {
271: this .jTextFieldColorValue.setText(getValue());
272: }
273: if (newColor != null)
274: setColor(newColor);
275:
276: }//GEN-LAST:event_jTextFieldColorValueActionPerformed
277:
278: /**
279: * Registers ActionListener to receive events.
280: * @param listener The listener to register.
281: */
282: public synchronized void addActionListener(
283: java.awt.event.ActionListener listener) {
284:
285: if (listenerList == null) {
286: listenerList = new javax.swing.event.EventListenerList();
287: }
288: listenerList.add(java.awt.event.ActionListener.class, listener);
289: }
290:
291: /**
292: * Removes ActionListener from the list of listeners.
293: * @param listener The listener to remove.
294: */
295: public synchronized void removeActionListener(
296: java.awt.event.ActionListener listener) {
297:
298: listenerList.remove(java.awt.event.ActionListener.class,
299: listener);
300: }
301:
302: /**
303: * Notifies all registered listeners about the event.
304: *
305: * @param event The event to be fired
306: */
307: private void fireActionListenerActionPerformed(
308: java.awt.event.ActionEvent event) {
309:
310: if (listenerList == null)
311: return;
312: Object[] listeners = listenerList.getListenerList();
313: for (int i = listeners.length - 2; i >= 0; i -= 2) {
314: if (listeners[i] == java.awt.event.ActionListener.class) {
315: ((java.awt.event.ActionListener) listeners[i + 1])
316: .actionPerformed(event);
317: }
318: }
319: }
320:
321: // Variables declaration - do not modify//GEN-BEGIN:variables
322: private javax.swing.JButton jButtonSelect;
323: private javax.swing.JLabel jLabelNoColor;
324: private javax.swing.JPanel jPanelColor;
325: private javax.swing.JTextField jTextFieldColorValue;
326:
327: // End of variables declaration//GEN-END:variables
328:
329: public void applyI18n() {
330: // Start autogenerated code ----------------------
331: jButtonSelect.setText(I18n.getString(
332: "colorSelectorPanel.buttonSelect", "..."));
333: // End autogenerated code ----------------------
334: }
335:
336: public boolean isInit() {
337: return init;
338: }
339:
340: public void setInit(boolean init) {
341: this.init = init;
342: }
343: }
|