KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejtools > adwt > editor > ArrayEditor


1 /*
2  * EJTools, the Enterprise Java Tools
3  *
4  * Distributable under LGPL license.
5  * See terms of license at www.gnu.org.
6  */

7 package org.ejtools.adwt.editor;
8
9 import java.awt.Component JavaDoc;
10 import java.awt.GridLayout JavaDoc;
11 import java.beans.PropertyChangeEvent JavaDoc;
12 import java.beans.PropertyChangeListener JavaDoc;
13 import java.beans.PropertyEditor JavaDoc;
14
15 import javax.swing.JLabel JavaDoc;
16 import javax.swing.JPanel JavaDoc;
17
18 import org.ejtools.beans.CustomPropertyEditorManager;
19
20 import com.dreambean.awt.GenericPropertyCustomizer;
21
22 /**
23  * @author Laurent Etiemble
24  * @version $Revision: 1.7 $
25  */

26 public class ArrayEditor extends GenericEditor
27 {
28    /** Description of the Field */
29    protected Object JavaDoc[] array = new Object JavaDoc[0];
30    /** Description of the Field */
31    protected Class JavaDoc clazz = null;
32    /** Description of the Field */
33    protected JPanel JavaDoc panel = null;
34
35
36    /**
37     * Constructor for ArrayEditor.
38     *
39     * @param clazz Description of the Parameter
40     */

41    public ArrayEditor(Class JavaDoc clazz)
42    {
43       this.clazz = clazz;
44       this.panel = new JPanel JavaDoc();
45    }
46
47
48    /**
49     * @return The customEditor value
50     */

51    public Component JavaDoc getCustomEditor()
52    {
53       return this.panel;
54    }
55
56
57    /**
58     * @param value The new value value
59     */

60    public void setValue(Object JavaDoc value)
61    {
62       super.setValue(value);
63       if (this.value == null)
64       {
65          return;
66       }
67       this.array = (Object JavaDoc[]) this.value;
68       this.buildPanel();
69    }
70
71
72    /**
73     * @return Description of the Return Value
74     */

75    public boolean supportsCustomEditor()
76    {
77       return true;
78    }
79
80
81    /** Description of the Method */
82    private void buildPanel()
83    {
84       this.panel.removeAll();
85
86       PropertyEditor JavaDoc editor = CustomPropertyEditorManager.findEditor(this.clazz);
87       if (editor == null)
88       {
89          editor = CustomPropertyEditorManager.findEditor(String JavaDoc.class);
90       }
91
92       this.panel = new JPanel JavaDoc(new GridLayout JavaDoc(this.array.length, 1));
93       for (int i = 0; i < this.array.length; i++)
94       {
95          try
96          {
97             PropertyEditor JavaDoc peInstance = (PropertyEditor JavaDoc) editor.getClass().newInstance();
98             peInstance.setValue(array[i]);
99
100             Object JavaDoc component;
101
102             if (peInstance.supportsCustomEditor())
103             {
104                component = peInstance.getCustomEditor();
105             }
106             else
107             {
108                String JavaDoc as[] = peInstance.getTags();
109                if (as != null)
110                {
111                   component = new GenericPropertyCustomizer(peInstance, as);
112                }
113                else
114                {
115                   final JLabel JavaDoc label = new JLabel JavaDoc(peInstance.getAsText());
116                   final PropertyEditor JavaDoc pcEditor = peInstance;
117                   component = label;
118                   pcEditor.addPropertyChangeListener(
119                      new PropertyChangeListener JavaDoc()
120                      {
121                         public void propertyChange(PropertyChangeEvent JavaDoc evt)
122                         {
123                            label.setText(pcEditor.getAsText());
124                         }
125                      });
126                }
127             }
128             this.panel.add((Component JavaDoc) component);
129          }
130          catch (Exception JavaDoc exception)
131          {
132             exception.printStackTrace();
133          }
134       }
135
136       this.panel.validate();
137       this.panel.repaint();
138    }
139 }
140
Popular Tags