KickJava   Java API By Example, From Geeks To Geeks.

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


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  * SheetProperty.java
28  *
29  * Created on 4 ottobre 2004, 22.08
30  *
31  */

32
33 package it.businesslogic.ireport.gui.sheet;
34 import com.lowagie.text.Font;
35 import java.util.*;
36 import javax.swing.*;
37 import it.businesslogic.ireport.gui.event.*;
38 import java.awt.Color JavaDoc;
39 /**
40  *
41  * @author Administrator
42  */

43 public class SheetProperty implements java.awt.event.ActionListener JavaDoc {
44     
45     public static final int STRING = 0;
46     public static final int NUMBER = 1;
47     public static final int INTEGER = 6;
48     public static final int COMBOBOX = 2;
49     public static final int COLOR = 3;
50     public static final int BOOLEAN = 4;
51     public static final int COMBOBOX_NK = 5; // Combobox number key
52
public static final int PASSWORD = 7;
53     public static final int CATEGORY_LABEL = 99;
54         
55     protected String JavaDoc name = "";
56     protected String JavaDoc keyName = "";
57     protected String JavaDoc group = "";
58     protected int type;
59     private java.util.Vector JavaDoc tags = null;
60     protected Object JavaDoc defaultValue = null;
61     protected Object JavaDoc value = null;
62     
63     private boolean readOnly = false;
64     
65     private boolean setting = false;
66     
67     protected JComponent component = null;
68     
69     private JComponent labelComponent = null;
70     
71     private boolean showResetButton = true;
72     
73     private java.awt.Color JavaDoc labelColor = java.awt.Color.BLACK;
74     
75     private Vector currentSelection = new Vector();
76     
77     
78     
79     /**
80      * Utility field used by event firing mechanism.
81      */

82     private javax.swing.event.EventListenerList JavaDoc listenerList = null;
83     
84     public SheetProperty(String JavaDoc keyName, String JavaDoc name, int type) {
85        this(keyName,name,type,null);
86     }
87     /** Creates a new instance of SheetProperty */
88     public SheetProperty(String JavaDoc keyName, String JavaDoc name, int type, Object JavaDoc defaultValue) {
89         this.setName( name );
90         this.setKeyName( keyName );
91         this.setDefaultValue( defaultValue );
92         setType(type);
93         tags = new Vector();
94     }
95     
96     public SheetProperty(String JavaDoc keyName, String JavaDoc name,String JavaDoc groupname, int type, Object JavaDoc defaultValue) {
97         this.setName( name );
98         this.setKeyName( keyName );
99         this.setDefaultValue( defaultValue );
100         setType(type);
101         tags = new Vector();
102     }
103
104     public String JavaDoc getName() {
105         return name;
106     }
107
108     public void setName(String JavaDoc name) {
109         this.name = name;
110     }
111
112     public int getType() {
113         return type;
114     }
115
116     public void setType(int type) {
117         this.type = type;
118     }
119
120     public java.util.Vector JavaDoc getTags() {
121         return tags;
122     }
123
124     public void setTags(java.util.Vector JavaDoc tags) {
125         this.tags = tags;
126     }
127     
128     public void setTags(Tag[] tags_array) {
129          
130         this.tags = new Vector();
131         
132         for (int i=0; i<tags_array.length; ++i)
133         {
134             tags.add(tags_array[i]);
135         }
136     }
137     
138     public JComponent getEditor()
139     {
140         
141         if (component != null) return component;
142         
143         if (this.getType() == NUMBER)
144         {
145             component = new it.businesslogic.ireport.gui.JNumberField();
146             component.setBorder(null);
147             
148             ((it.businesslogic.ireport.gui.JNumberField)component).addActionListener( this );
149         
150         }
151         else if (this.getType() == INTEGER)
152         {
153             component = new it.businesslogic.ireport.gui.JNumberField();
154             component.setBorder(null);
155             try {
156             ((it.businesslogic.ireport.gui.JNumberField)component).setInteger(true);
157             } catch (Exception JavaDoc ex){}
158             
159             ((it.businesslogic.ireport.gui.JNumberField)component).addActionListener( this );
160         
161         }
162         else if (this.getType() == COMBOBOX || this.getType() == COMBOBOX_NK)
163         {
164             component = new JComboBox(this.getTags());
165             component.setBorder(null);
166             
167             //((JComboBox)component).setEditor(new SimpleComboBoxEditor() );
168

169             for (int i=0; i< component.getComponentCount(); ++i)
170             {
171                 try {
172                         Object JavaDoc obj = component.getComponent(i);
173                         //System.out.println( obj );
174
if (obj != null && obj.getClass().getMethod("setBorder", new Class JavaDoc[]{javax.swing.border.Border JavaDoc.class}) != null)
175                         {
176                             java.lang.reflect.Method JavaDoc mtd = obj.getClass().getMethod("setBorder", new Class JavaDoc[]{javax.swing.border.Border JavaDoc.class});
177                             mtd.invoke(obj, new Object JavaDoc[]{null});
178                         }
179                 } catch (Exception JavaDoc ex) { }
180             }
181             
182             ((JComboBox)component).addActionListener( this );
183         }
184         else if (this.getType() == BOOLEAN)
185         {
186             component = new JCheckBox("");
187             ((JCheckBox)component).addActionListener( this );
188         }
189         else if (this.getType() == COLOR)
190         {
191             component = new ColorSelectorPanel();
192             ((ColorSelectorPanel)component).addActionListener( this );
193         }
194         else if (this.getType() == CATEGORY_LABEL)
195         {
196             component = new JPanel();
197             component.setBackground( java.awt.Color.LIGHT_GRAY);
198         }
199         else if (this.getType() == PASSWORD)
200         {
201             component = new JPasswordField();
202             component.setBorder(null);
203             ((JTextField)component).getDocument().addDocumentListener( new javax.swing.event.DocumentListener JavaDoc() {
204             public void changedUpdate(javax.swing.event.DocumentEvent JavaDoc evt) {
205                 actionPerformed(null);
206             }
207             public void insertUpdate(javax.swing.event.DocumentEvent JavaDoc evt) {
208                 actionPerformed(null);
209             }
210             public void removeUpdate(javax.swing.event.DocumentEvent JavaDoc evt) {
211                 actionPerformed(null);
212             }
213         });
214         }
215         else
216         {
217             // default (STRING)
218
component = new JTextField();
219             component.setBorder(null);
220             ((JTextField)component).getDocument().addDocumentListener( new javax.swing.event.DocumentListener JavaDoc() {
221             public void changedUpdate(javax.swing.event.DocumentEvent JavaDoc evt) {
222                 actionPerformed(null);
223             }
224             public void insertUpdate(javax.swing.event.DocumentEvent JavaDoc evt) {
225                 actionPerformed(null);
226             }
227             public void removeUpdate(javax.swing.event.DocumentEvent JavaDoc evt) {
228                 actionPerformed(null);
229             }
230         });
231         }
232         
233         
234         return component;
235         
236     }
237     
238     public Object JavaDoc getEditorValue(JComponent component)
239     {
240        if (this.getType() == NUMBER)
241         {
242            return new Double JavaDoc(((it.businesslogic.ireport.gui.JNumberField)component).getValue());
243         }
244        else if (this.getType() == INTEGER)
245         {
246             return new Integer JavaDoc( (int)((it.businesslogic.ireport.gui.JNumberField)component).getValue());
247         }
248         else if (this.getType() == COMBOBOX)
249         {
250            Object JavaDoc obj = ((JComboBox)component).getSelectedItem();
251            if (obj != null)
252            {
253                if (obj instanceof Tag)
254                {
255                     return ((Tag)obj).getValue();
256                }
257                else
258                {
259                    return obj;
260                }
261            }
262            return null;
263         }
264        else if (this.getType() == COMBOBOX_NK)
265         {
266            Object JavaDoc obj = ((JComboBox)component).getSelectedItem();
267            if (obj != null)
268            {
269                return new Integer JavaDoc(((Tag)obj).getValue()+"");
270            }
271            return null;
272         }
273         else if (this.getType() == BOOLEAN)
274         {
275             return new Boolean JavaDoc(((JCheckBox)component).isSelected());
276         }
277         else if (this.getType() == BOOLEAN)
278         {
279             return new String JavaDoc( ((JPasswordField)component).getPassword() );
280         }
281         else if (this.getType() == COLOR)
282         {
283             return ((ColorSelectorPanel)component).getValue();
284         }
285        
286         return ((JTextField)component).getText();
287     }
288     
289     public synchronized void setEditorValue(JComponent component, Object JavaDoc obj)
290     {
291         
292         String JavaDoc str = ""+obj;
293         
294         component.setForeground(Color.BLACK );
295         
296         try {
297             
298         setSetting(true);
299         if (this.getType() == NUMBER)
300         {
301            if (obj == null) str ="0";
302             ((it.businesslogic.ireport.gui.JNumberField)component).setValue(Double.parseDouble(str) );
303             
304            if (obj == null) ((it.businesslogic.ireport.gui.JNumberField)component).setForeground( Color.LIGHT_GRAY);
305            
306         }
307        else if (this.getType() == INTEGER)
308         {
309            if (obj == null) str ="0";
310             ((it.businesslogic.ireport.gui.JNumberField)component).setValue(Integer.parseInt(str) );
311             if (obj == null) ((it.businesslogic.ireport.gui.JNumberField)component).setForeground( Color.LIGHT_GRAY);
312         }
313         else if (this.getType() == COMBOBOX)
314         {
315            if (obj == null) {
316                 setSetting(false);
317                 if (getTags().size() > 0 && ((Tag)getTags().elementAt(0)).getValue() == null)
318                 {
319                     ((JComboBox)component).setSelectedIndex(0);
320                 }
321                 return;
322            }
323            boolean found = false;
324            for (int i=0; i<getTags().size(); ++i)
325            {
326                 
327                 Tag t = (Tag)getTags().elementAt(i);
328                 if (t.getValue() != null && t.getValue().equals(str))
329                 {
330                     found = true;
331                     ((JComboBox)component).setSelectedIndex(i);
332                     break;
333                 }
334            }
335            if (!found && ((JComboBox)component).isEditable())
336            {
337                ((JComboBox)component).setSelectedItem(obj);
338            }
339         }
340         else if (this.getType() == COMBOBOX_NK)
341         {
342             if (obj == null) { setSetting(false);
343                 if (getTags().size() > 0)
344                 {
345                     ((JComboBox)component).setSelectedIndex(0);
346                 }
347                 return;
348             }
349             for (int i=0; i<getTags().size(); ++i)
350             {
351                 Tag t = (Tag)getTags().elementAt(i);
352                 if ((t.getValue()+"").equals(str))
353                 {
354                     ((JComboBox)component).setSelectedIndex(i);
355                     break;
356                 }
357             }
358         }
359         else if (this.getType() == BOOLEAN)
360         {
361             if (obj == null) { ((JCheckBox)component).setSelected(false); setSetting(false); return;}
362             ((JCheckBox)component).setSelected(str.equals("true"));
363         }
364         else if (this.getType() == COLOR)
365         {
366             ((ColorSelectorPanel)component).setValue(obj);
367         }
368         else if (this.getType() == PASSWORD)
369         {
370             if (obj == null) { ((JPasswordField)component).setText(""); setSetting(false); return;}
371             ((JPasswordField)component).setText(str);
372         }
373         else
374         {
375             if (obj == null) { ((JTextField)component).setText(""); setSetting(false); return;}
376             ((JTextField)component).setText(str);
377         }
378         } catch (Exception JavaDoc ex) {
379             ex.printStackTrace();
380         }
381         finally {
382             setSetting(false);
383         }
384         
385     }
386
387     public String JavaDoc getKeyName() {
388         return keyName;
389     }
390
391     public void setKeyName(String JavaDoc keyName) {
392         this.keyName = keyName;
393     }
394
395     public Object JavaDoc getDefaultValue() {
396         return defaultValue;
397     }
398
399     public void setDefaultValue(Object JavaDoc defaultValue) {
400         this.defaultValue = defaultValue;
401     }
402
403     public String JavaDoc getGroup() {
404         return group;
405     }
406
407     public void setGroup(String JavaDoc group) {
408         this.group = group;
409     }
410     
411     public Object JavaDoc getValue() {
412         return value;
413     }
414
415     public void setValue(Object JavaDoc value) {
416         
417         this.value = value;
418         this.setEditorValue(getEditor(), (value == null) ? getDefaultValue() : value);
419         updateLabel();
420         
421     }
422     
423     public void updateLabel()
424     {
425         
426         if (this.getType() != CATEGORY_LABEL)
427         {
428             try {
429                 if (getLabelComponent() != null)
430                 {
431                     
432                     java.awt.Font JavaDoc f = getLabelComponent().getFont();
433                     java.awt.Font JavaDoc f2 = new java.awt.Font JavaDoc( f.getName(), (value == null || this.isReadOnly() || !isShowResetButton()) ? 0 : java.awt.Font.BOLD, f.getSize());
434                     getLabelComponent().setFont(f2);
435                     
436                     getLabelComponent().setForeground( getLabelColor());
437                     
438                     if (this.isReadOnly())
439                     {
440                         getLabelComponent().setEnabled( false );
441                     }
442                     else
443                     {
444                          getLabelComponent().setEnabled( true );
445                     }
446                     
447                     
448                     getLabelComponent().updateUI();
449                 }
450             } catch (Exception JavaDoc ex)
451             {
452
453             }
454         }
455     }
456     
457     public void actionPerformed(java.awt.event.ActionEvent JavaDoc event)
458     {
459         if (isSetting()) return;
460         getEditor().setForeground( Color.BLACK);
461         Object JavaDoc new_value = getEditorValue( this.getEditor() );
462         if (new_value != null && new_value.equals(value)) return;
463                 
464         Object JavaDoc oldValue = value;
465         value = new_value;
466         
467         if(oldValue == null || new_value == null)
468         {
469             updateLabel();
470         }
471         
472         fireSheetPropertyValueChangedListenerSheetPropertyValueChanged(
473             new SheetPropertyValueChangedEvent(getKeyName(),oldValue,new_value, this));
474     }
475
476     /**
477      * Registers SheetPropertyValueChangedListener to receive events.
478      * @param listener The listener to register.
479      */

480     public synchronized void addSheetPropertyValueChangedListener(it.businesslogic.ireport.gui.event.SheetPropertyValueChangedListener listener) {
481
482         if (listenerList == null ) {
483             listenerList = new javax.swing.event.EventListenerList JavaDoc();
484         }
485         listenerList.add (it.businesslogic.ireport.gui.event.SheetPropertyValueChangedListener.class, listener);
486     }
487
488     /**
489      * Removes SheetPropertyValueChangedListener from the list of listeners.
490      * @param listener The listener to remove.
491      */

492     public synchronized void removeSheetPropertyValueChangedListener(it.businesslogic.ireport.gui.event.SheetPropertyValueChangedListener listener) {
493
494         listenerList.remove (it.businesslogic.ireport.gui.event.SheetPropertyValueChangedListener.class, listener);
495     }
496
497     /**
498      * Notifies all registered listeners about the event.
499      *
500      * @param event The event to be fired
501      */

502     public void fireSheetPropertyValueChangedListenerSheetPropertyValueChanged(it.businesslogic.ireport.gui.event.SheetPropertyValueChangedEvent event) {
503
504         if (listenerList == null) return;
505         Object JavaDoc[] listeners = listenerList.getListenerList ();
506         for (int i = listeners.length-2; i>=0; i-=2) {
507             if (listeners[i]==it.businesslogic.ireport.gui.event.SheetPropertyValueChangedListener.class) {
508                 ((it.businesslogic.ireport.gui.event.SheetPropertyValueChangedListener)listeners[i+1]).sheetPropertyValueChanged (event);
509             }
510         }
511     }
512
513     public boolean isReadOnly() {
514         return readOnly;
515     }
516
517     public void setReadOnly(boolean readOnly) {
518         this.readOnly = readOnly;
519         getEditor().setEnabled(!readOnly);
520     }
521
522     public JComponent getLabelComponent() {
523         return labelComponent;
524     }
525
526     public void setLabelComponent(JComponent labelComponent) {
527         this.labelComponent = labelComponent;
528     }
529
530     public boolean isSetting() {
531         return setting;
532     }
533
534     public void setSetting(boolean setting) {
535         this.setting = setting;
536     }
537
538     public boolean isShowResetButton() {
539         return showResetButton;
540     }
541
542     public void setShowResetButton(boolean showResetButton) {
543         this.showResetButton = showResetButton;
544     }
545
546     public java.awt.Color JavaDoc getLabelColor() {
547         return labelColor;
548     }
549
550     public void setLabelColor(java.awt.Color JavaDoc labelColor) {
551         this.labelColor = labelColor;
552     }
553
554     public Vector getCurrentSelection() {
555         return currentSelection;
556     }
557
558     public void setCurrentSelection(Vector currentSelection) {
559         this.currentSelection = currentSelection;
560     }
561     
562 }
563
Popular Tags