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: * SheetProperty.java
028: *
029: * Created on 4 ottobre 2004, 22.08
030: *
031: */
032:
033: package it.businesslogic.ireport.gui.sheet;
034:
035: import com.lowagie.text.Font;
036: import java.awt.event.FocusEvent;
037: import java.util.*;
038: import javax.swing.*;
039: import it.businesslogic.ireport.gui.event.*;
040: import java.awt.Color;
041: import java.awt.event.FocusListener;
042:
043: /**
044: *
045: * @author Administrator
046: */
047: public class SheetProperty implements java.awt.event.ActionListener {
048:
049: public static final int STRING = 0;
050: public static final int NUMBER = 1;
051: public static final int INTEGER = 6;
052: public static final int COMBOBOX = 2;
053: public static final int COLOR = 3;
054: public static final int BOOLEAN = 4;
055: public static final int COMBOBOX_NK = 5; // Combobox number key
056: public static final int PASSWORD = 7;
057: public static final int CATEGORY_LABEL = 99;
058: public static final int TEXT = 8;
059:
060: protected String name = "";
061: protected String keyName = "";
062: protected String group = "";
063: protected int type;
064: private java.util.Vector tags = null;
065: protected Object defaultValue = null;
066: protected Object value = null;
067:
068: private boolean readOnly = false;
069:
070: private boolean setting = false;
071:
072: protected JComponent component = null;
073:
074: private JComponent labelComponent = null;
075:
076: private boolean showResetButton = true;
077:
078: private java.awt.Color labelColor = java.awt.Color.BLACK;
079:
080: private Vector currentSelection = new Vector();
081:
082: private String labelError = null;
083: private static final Color errorColor = Color.RED.darker();
084: private static final ImageIcon errorIcon = new javax.swing.ImageIcon(
085: SheetProperty.class
086: .getResource("/it/businesslogic/ireport/icons/problems/error.png"));
087:
088: /**
089: * Utility field used by event firing mechanism.
090: */
091: private javax.swing.event.EventListenerList listenerList = null;
092:
093: public SheetProperty(String keyName, String name, int type) {
094: this (keyName, name, type, null);
095: }
096:
097: /** Creates a new instance of SheetProperty */
098: public SheetProperty(String keyName, String name, int type,
099: Object defaultValue) {
100: this .setName(name);
101: this .setKeyName(keyName);
102: this .setDefaultValue(defaultValue);
103: setType(type);
104: tags = new Vector();
105: }
106:
107: public SheetProperty(String keyName, String name, String groupname,
108: int type, Object defaultValue) {
109: this .setName(name);
110: this .setKeyName(keyName);
111: this .setDefaultValue(defaultValue);
112: setType(type);
113: tags = new Vector();
114: }
115:
116: public String getName() {
117: return name;
118: }
119:
120: public void setName(String name) {
121: this .name = name;
122: }
123:
124: public int getType() {
125: return type;
126: }
127:
128: public void setType(int type) {
129: this .type = type;
130: }
131:
132: public java.util.Vector getTags() {
133: return tags;
134: }
135:
136: public void setTags(java.util.Vector tags) {
137: this .tags = tags;
138: }
139:
140: public void setTags(Tag[] tags_array) {
141:
142: this .tags = new Vector();
143:
144: for (int i = 0; i < tags_array.length; ++i) {
145: tags.add(tags_array[i]);
146: }
147: }
148:
149: public JComponent getEditor() {
150:
151: if (component != null)
152: return component;
153:
154: if (this .getType() == NUMBER) {
155: component = new it.businesslogic.ireport.gui.JNumberField();
156: component.setBorder(null);
157:
158: ((it.businesslogic.ireport.gui.JNumberField) component)
159: .addActionListener(this );
160:
161: } else if (this .getType() == INTEGER) {
162: component = new it.businesslogic.ireport.gui.JNumberField();
163: component.setBorder(null);
164: try {
165: ((it.businesslogic.ireport.gui.JNumberField) component)
166: .setInteger(true);
167: } catch (Exception ex) {
168: }
169:
170: ((it.businesslogic.ireport.gui.JNumberField) component)
171: .addActionListener(this );
172:
173: } else if (this .getType() == COMBOBOX
174: || this .getType() == COMBOBOX_NK) {
175: component = new JComboBox(this .getTags());
176: component.setBorder(null);
177:
178: //((JComboBox)component).setEditor(new SimpleComboBoxEditor() );
179:
180: for (int i = 0; i < component.getComponentCount(); ++i) {
181: try {
182: Object obj = component.getComponent(i);
183: //System.out.println( obj );
184: if (obj != null
185: && obj
186: .getClass()
187: .getMethod(
188: "setBorder",
189: new Class[] { javax.swing.border.Border.class }) != null) {
190: java.lang.reflect.Method mtd = obj
191: .getClass()
192: .getMethod(
193: "setBorder",
194: new Class[] { javax.swing.border.Border.class });
195: mtd.invoke(obj, new Object[] { null });
196: }
197: } catch (Exception ex) {
198: }
199: }
200:
201: ((JComboBox) component).addActionListener(this );
202: } else if (this .getType() == BOOLEAN) {
203: component = new JCheckBox("");
204: ((JCheckBox) component).addActionListener(this );
205: } else if (this .getType() == COLOR) {
206: component = new ColorSelectorPanel();
207: ((ColorSelectorPanel) component).addActionListener(this );
208: } else if (this .getType() == CATEGORY_LABEL) {
209: component = new JPanel();
210: component.setBackground(java.awt.Color.LIGHT_GRAY);
211: } else if (this .getType() == PASSWORD) {
212: component = new JPasswordField();
213: component.setBorder(null);
214: ((JTextField) component).getDocument().addDocumentListener(
215: new javax.swing.event.DocumentListener() {
216: public void changedUpdate(
217: javax.swing.event.DocumentEvent evt) {
218: actionPerformed(null);
219: }
220:
221: public void insertUpdate(
222: javax.swing.event.DocumentEvent evt) {
223: actionPerformed(null);
224: }
225:
226: public void removeUpdate(
227: javax.swing.event.DocumentEvent evt) {
228: actionPerformed(null);
229: }
230: });
231: } else if (this .getType() == TEXT) {
232: component = new JTextField();
233: component.setBorder(null);
234: // Value changed of return and focusLost...
235: ((JTextField) component).addActionListener(this );
236: ((JTextField) component)
237: .addFocusListener(new FocusListener() {
238: public void focusGained(FocusEvent e) {
239: ((JTextField) e.getComponent()).selectAll();
240: }
241:
242: public void focusLost(FocusEvent e) {
243: actionPerformed(null);
244: }
245: });
246:
247: } else {
248: // default (STRING)
249: component = new JTextField();
250: component.setBorder(null);
251: ((JTextField) component).getDocument().addDocumentListener(
252: new javax.swing.event.DocumentListener() {
253: public void changedUpdate(
254: javax.swing.event.DocumentEvent evt) {
255: actionPerformed(null);
256: }
257:
258: public void insertUpdate(
259: javax.swing.event.DocumentEvent evt) {
260: actionPerformed(null);
261: }
262:
263: public void removeUpdate(
264: javax.swing.event.DocumentEvent evt) {
265: actionPerformed(null);
266: }
267: });
268: }
269:
270: return component;
271:
272: }
273:
274: public Object getEditorValue(JComponent component) {
275: if (this .getType() == NUMBER) {
276: return new Double(
277: ((it.businesslogic.ireport.gui.JNumberField) component)
278: .getValue());
279: } else if (this .getType() == INTEGER) {
280: return new Integer(
281: (int) ((it.businesslogic.ireport.gui.JNumberField) component)
282: .getValue());
283: } else if (this .getType() == COMBOBOX) {
284: Object obj = ((JComboBox) component).getSelectedItem();
285: if (obj != null) {
286: if (obj instanceof Tag) {
287: return ((Tag) obj).getValue();
288: } else {
289: return obj;
290: }
291: }
292: return null;
293: } else if (this .getType() == COMBOBOX_NK) {
294: Object obj = ((JComboBox) component).getSelectedItem();
295: if (obj != null) {
296: return new Integer(((Tag) obj).getValue() + "");
297: }
298: return null;
299: } else if (this .getType() == BOOLEAN) {
300: return new Boolean(((JCheckBox) component).isSelected());
301: } else if (this .getType() == BOOLEAN) {
302: return new String(((JPasswordField) component)
303: .getPassword());
304: } else if (this .getType() == COLOR) {
305: return ((ColorSelectorPanel) component).getValue();
306: }
307:
308: return ((JTextField) component).getText();
309: }
310:
311: public synchronized void setEditorValue(JComponent component,
312: Object obj) {
313:
314: String str = "" + obj;
315:
316: component.setForeground(Color.BLACK);
317:
318: try {
319:
320: setSetting(true);
321: if (this .getType() == NUMBER) {
322: if (obj == null)
323: str = "0";
324: ((it.businesslogic.ireport.gui.JNumberField) component)
325: .setValue(Double.parseDouble(str));
326:
327: if (obj == null)
328: ((it.businesslogic.ireport.gui.JNumberField) component)
329: .setForeground(Color.LIGHT_GRAY);
330:
331: } else if (this .getType() == INTEGER) {
332: if (obj == null)
333: str = "0";
334: ((it.businesslogic.ireport.gui.JNumberField) component)
335: .setValue(Integer.parseInt(str));
336: if (obj == null)
337: ((it.businesslogic.ireport.gui.JNumberField) component)
338: .setForeground(Color.LIGHT_GRAY);
339: } else if (this .getType() == COMBOBOX) {
340: if (obj == null) {
341: setSetting(false);
342: if (getTags().size() > 0
343: && ((Tag) getTags().elementAt(0))
344: .getValue() == null) {
345: ((JComboBox) component).setSelectedIndex(0);
346: }
347: return;
348: }
349: boolean found = false;
350: for (int i = 0; i < getTags().size(); ++i) {
351:
352: Tag t = (Tag) getTags().elementAt(i);
353: if (t.getValue() != null
354: && t.getValue().equals(str)) {
355: found = true;
356: ((JComboBox) component).setSelectedIndex(i);
357: break;
358: }
359: }
360: if (!found && ((JComboBox) component).isEditable()) {
361: ((JComboBox) component).setSelectedItem(obj);
362: }
363: } else if (this .getType() == COMBOBOX_NK) {
364: if (obj == null) {
365: setSetting(false);
366: if (getTags().size() > 0) {
367: ((JComboBox) component).setSelectedIndex(0);
368: }
369: return;
370: }
371: for (int i = 0; i < getTags().size(); ++i) {
372: Tag t = (Tag) getTags().elementAt(i);
373: if ((t.getValue() + "").equals(str)) {
374: ((JComboBox) component).setSelectedIndex(i);
375: break;
376: }
377: }
378: } else if (this .getType() == BOOLEAN) {
379: if (obj == null) {
380: ((JCheckBox) component).setSelected(false);
381: setSetting(false);
382: return;
383: }
384: ((JCheckBox) component).setSelected(str.equals("true"));
385: } else if (this .getType() == COLOR) {
386: ((ColorSelectorPanel) component).setValue(obj);
387: } else if (this .getType() == PASSWORD) {
388: if (obj == null) {
389: ((JPasswordField) component).setText("");
390: setSetting(false);
391: return;
392: }
393: ((JPasswordField) component).setText(str);
394: } else {
395: if (obj == null) {
396: ((JTextField) component).setText("");
397: setSetting(false);
398: return;
399: }
400: ((JTextField) component).setText(str);
401: }
402: } catch (Exception ex) {
403: ex.printStackTrace();
404: } finally {
405: setSetting(false);
406: }
407:
408: }
409:
410: public String getKeyName() {
411: return keyName;
412: }
413:
414: public void setKeyName(String keyName) {
415: this .keyName = keyName;
416: }
417:
418: public Object getDefaultValue() {
419: return defaultValue;
420: }
421:
422: public void setDefaultValue(Object defaultValue) {
423: this .defaultValue = defaultValue;
424: }
425:
426: public String getGroup() {
427: return group;
428: }
429:
430: public void setGroup(String group) {
431: this .group = group;
432: }
433:
434: public Object getValue() {
435: return value;
436: }
437:
438: public void setValue(Object value) {
439:
440: this .value = value;
441: this .setEditorValue(getEditor(),
442: (value == null) ? getDefaultValue() : value);
443: //updateLabel();
444:
445: }
446:
447: public void updateLabel() {
448:
449: if (this .getType() != CATEGORY_LABEL) {
450: try {
451: if (getLabelComponent() != null) {
452:
453: //String bold = (value == null || this.isReadOnly() || !isShowResetButton()) ? "" : "<html><b>";
454:
455: boolean needBold = !(value == null
456: || this .isReadOnly() || !isShowResetButton());
457:
458: java.awt.Font f = getLabelComponent().getFont();
459:
460: if (f.isBold() && !needBold) {
461: ((JLabel) getLabelComponent()).setFont(f
462: .deriveFont(Font.NORMAL));
463: } else if (!f.isBold() && needBold) {
464: ((JLabel) getLabelComponent()).setFont(f
465: .deriveFont(Font.BOLD));
466: }
467:
468: // The table is used to avoid word wrap
469: //((JLabel)getLabelComponent()).setText("<html><table><tr><td nowrap>" + bold + this.getName() + "</td></tr></table></html>");
470: ((JLabel) getLabelComponent()).setText(this
471: .getName());
472:
473: if (this .isReadOnly()) {
474: getLabelComponent().setEnabled(false);
475: } else {
476: getLabelComponent().setEnabled(true);
477: }
478:
479: if (labelError != null) {
480: ((JLabel) getLabelComponent())
481: .setIcon(errorIcon);
482: getLabelComponent().setForeground(errorColor);
483: getLabelComponent().setToolTipText(labelError);
484: } else {
485: ((JLabel) getLabelComponent()).setIcon(null);
486: getLabelComponent().setForeground(
487: getLabelColor());
488: getLabelComponent().setToolTipText(getName());
489: }
490:
491: getLabelComponent().updateUI();
492: }
493: } catch (Exception ex) {
494:
495: }
496: }
497: }
498:
499: public void actionPerformed(java.awt.event.ActionEvent event) {
500: if (isSetting())
501: return;
502: getEditor().setForeground(Color.BLACK);
503: Object new_value = getEditorValue(this .getEditor());
504: if (new_value != null && new_value.equals(value))
505: return;
506:
507: Object oldValue = value;
508: value = new_value;
509:
510: if (oldValue == null || new_value == null) {
511: updateLabel();
512: }
513:
514: fireSheetPropertyValueChangedListenerSheetPropertyValueChanged(new SheetPropertyValueChangedEvent(
515: getKeyName(), oldValue, new_value, this ));
516:
517: if (getType() == TEXT || getType() == NUMBER) {
518: FocusManager.getCurrentManager().clearGlobalFocusOwner();
519: }
520: }
521:
522: /**
523: * Registers SheetPropertyValueChangedListener to receive events.
524: * @param listener The listener to register.
525: */
526: public synchronized void addSheetPropertyValueChangedListener(
527: it.businesslogic.ireport.gui.event.SheetPropertyValueChangedListener listener) {
528:
529: if (listenerList == null) {
530: listenerList = new javax.swing.event.EventListenerList();
531: }
532: listenerList
533: .add(
534: it.businesslogic.ireport.gui.event.SheetPropertyValueChangedListener.class,
535: listener);
536: }
537:
538: /**
539: * Removes SheetPropertyValueChangedListener from the list of listeners.
540: * @param listener The listener to remove.
541: */
542: public synchronized void removeSheetPropertyValueChangedListener(
543: it.businesslogic.ireport.gui.event.SheetPropertyValueChangedListener listener) {
544:
545: listenerList
546: .remove(
547: it.businesslogic.ireport.gui.event.SheetPropertyValueChangedListener.class,
548: listener);
549: }
550:
551: /**
552: * Notifies all registered listeners about the event.
553: *
554: * @param event The event to be fired
555: */
556: public void fireSheetPropertyValueChangedListenerSheetPropertyValueChanged(
557: it.businesslogic.ireport.gui.event.SheetPropertyValueChangedEvent event) {
558:
559: if (listenerList == null)
560: return;
561: Object[] listeners = listenerList.getListenerList();
562: for (int i = listeners.length - 2; i >= 0; i -= 2) {
563: if (listeners[i] == it.businesslogic.ireport.gui.event.SheetPropertyValueChangedListener.class) {
564: ((it.businesslogic.ireport.gui.event.SheetPropertyValueChangedListener) listeners[i + 1])
565: .sheetPropertyValueChanged(event);
566: }
567: }
568: }
569:
570: public boolean isReadOnly() {
571: return readOnly;
572: }
573:
574: public void setReadOnly(boolean readOnly) {
575: this .readOnly = readOnly;
576: getEditor().setEnabled(!readOnly);
577: }
578:
579: public JComponent getLabelComponent() {
580: return labelComponent;
581: }
582:
583: public void setLabelComponent(JComponent labelComponent) {
584: this .labelComponent = labelComponent;
585: }
586:
587: public boolean isSetting() {
588: return setting;
589: }
590:
591: public void setSetting(boolean setting) {
592: this .setting = setting;
593: }
594:
595: public boolean isShowResetButton() {
596: return showResetButton;
597: }
598:
599: public void setShowResetButton(boolean showResetButton) {
600: this .showResetButton = showResetButton;
601: }
602:
603: public java.awt.Color getLabelColor() {
604: return labelColor;
605: }
606:
607: public void setLabelColor(java.awt.Color labelColor) {
608: this .labelColor = labelColor;
609: }
610:
611: public Vector getCurrentSelection() {
612: return currentSelection;
613: }
614:
615: public void setCurrentSelection(Vector currentSelection) {
616: this .currentSelection = currentSelection;
617: }
618:
619: public String getLabelError() {
620: return labelError;
621: }
622:
623: public void setLabelError(String labelError) {
624: this.labelError = labelError;
625: }
626: }
|