KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > share > config > ui > ConfigProperty


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /*
21  * ConfigProperty.java
22  *
23  * Created on August 17, 2001, 1:48 PM
24  */

25
26 package org.netbeans.modules.j2ee.sun.share.config.ui;
27
28 import java.beans.*;
29 import java.util.*;
30
31 import javax.enterprise.deploy.spi.DConfigBean JavaDoc;
32
33 import org.openide.nodes.*;
34
35 import org.netbeans.modules.j2ee.sun.share.config.ConfigBeanStorage;
36
37
38 public class ConfigProperty {
39
40     public static Node.Property getBraindead(BeanDescriptor descrip) {
41         return new Braindead(descrip);
42     }
43
44     public static Node.Property getProperty(Object JavaDoc bean,PropertyDescriptor property) {
45         if(property instanceof IndexedPropertyDescriptor)
46             return new Complex(bean,(IndexedPropertyDescriptor) property);
47         return new Simple(bean,property);
48     }
49     
50     public static Node.Property getFixedProperty(ConfigBeanStorage cbs, String JavaDoc xpath) {
51         return new Fixed(cbs,xpath);
52     }
53     
54     public static PropertyEditor getEditor(Object JavaDoc bean,PropertyDescriptor property,Class JavaDoc type) {
55         PropertyEditor ed = null;
56         try {
57             ed = (PropertyEditor) property.getPropertyEditorClass().newInstance();
58         } catch (Exception JavaDoc e) {}
59         if(ed == null)
60             ed = PropertyEditorManager.findEditor(type);
61         if(ed == null)
62             ed = new ConfigPropertyEditor(bean,type);
63         // System.out.println("Created editor " + ed);
64
ed.setValue(bean);
65         // System.out.println("Set value to " + bean);
66
return ed;
67     }
68     
69     
70     private static class Braindead extends PropertySupport.WriteOnly {
71         
72         Object JavaDoc obj = null;
73         Class JavaDoc customizer;
74         
75         public Braindead(BeanDescriptor descrip) {
76             super(descrip.getName(),descrip.getBeanClass(),descrip.getDisplayName(),descrip.getShortDescription());
77             customizer = descrip.getCustomizerClass();
78             System.err.println("bean customizer class " + customizer);
79         }
80         
81         public void setValue(Object JavaDoc obj) throws java.lang.IllegalAccessException JavaDoc, java.lang.IllegalArgumentException JavaDoc, java.lang.reflect.InvocationTargetException JavaDoc {
82             this.obj = obj;
83         }
84         
85         public PropertyEditor getPropertyEditor() {
86             return new PropertyEditorSupport() {
87                 public String JavaDoc getAsText() {return "No text";}
88                 public boolean supportsCustomEditor() { return true; }
89                 public synchronized java.awt.Component JavaDoc getCustomEditor() {
90                     try {
91                         Customizer foo = (Customizer) customizer.newInstance();
92                         foo.setObject(obj);
93                         return (java.awt.Component JavaDoc) foo;
94                     } catch (Exception JavaDoc e) {
95                         return null;
96                     }
97                 }
98             };
99         }
100         
101     }
102     
103     private static class Simple extends PropertySupport.Reflection {
104         
105         PropertyDescriptor property;
106         
107         Simple(Object JavaDoc bean,PropertyDescriptor property) {
108             super(bean,property.getPropertyType(),
109             property.getReadMethod(),property.getWriteMethod());
110             this.property = property;
111             // System.err.println("Simple property " + instance);
112
}
113         
114         public PropertyEditor getPropertyEditor() {
115             // System.err.println("Editor for Simple property " + instance);
116
return ConfigProperty.getEditor(instance,property,property.getPropertyType());
117         }
118         
119         public String JavaDoc getName() {
120             return property.getName();
121         }
122         
123         public String JavaDoc getDisplayName() {
124             return property.getDisplayName();
125         }
126         
127         public String JavaDoc getShortDescription() {
128             return property.getShortDescription();
129         }
130         
131         public Object JavaDoc getValue() throws IllegalArgumentException JavaDoc, IllegalAccessException JavaDoc, java.lang.reflect.InvocationTargetException JavaDoc {
132             Object JavaDoc obj = super.getValue();
133             if(obj == null)
134                 try {
135                     obj = property.getPropertyType().newInstance();
136                     setValue(obj);
137                 } catch (InstantiationException JavaDoc ie) {
138                     // throw new java.lang.reflect.InvocationTargetException(ie);
139
// PENDING should report the error but return gracefully
140
}
141             return obj;
142         }
143         
144     }
145     
146     private static class Complex extends IndexedPropertySupport {
147         
148         IndexedPropertyDescriptor property;
149         
150         Complex(Object JavaDoc bean,IndexedPropertyDescriptor descriptor) {
151             super(bean,descriptor.getPropertyType(),descriptor.getIndexedPropertyType(),
152             descriptor.getReadMethod(),descriptor.getWriteMethod(),
153             descriptor.getIndexedReadMethod(),descriptor.getIndexedWriteMethod());
154             property = descriptor;
155             // System.err.println("Array property " + instance);
156
}
157         
158         public PropertyEditor getIndexedPropertyEditor() {
159             // System.err.println("Editor for Array property " + instance);
160
return ConfigProperty.getEditor(instance,property,property.getIndexedPropertyType());
161         }
162         
163         public String JavaDoc getName() {
164             return property.getName();
165         }
166         
167         public String JavaDoc getDisplayName() {
168             return property.getDisplayName();
169         }
170         
171         public void setValue(Object JavaDoc obj) throws IllegalArgumentException JavaDoc, IllegalAccessException JavaDoc, java.lang.reflect.InvocationTargetException JavaDoc {
172             if(obj.getClass().isArray()) {
173                 Object JavaDoc[] arr = (Object JavaDoc[]) obj;
174                 for(int i = 0 ; i < arr.length; i++)
175                     if(arr[i] == null)
176                         try {
177                             arr[i] = property.getIndexedPropertyType().newInstance();
178                         } catch (InstantiationException JavaDoc ie) {
179                             // throw new java.lang.reflect.InvocationTargetException(ie);//"Blewed up");
180
// should return gracefully
181
}
182             }
183             super.setValue(obj);
184         }
185     }
186     
187     // A fixed array where the only editable portion is the bean properties
188
// of an element of the array.
189
private static class Fixed extends IndexedPropertySupport {
190         ConfigBeanStorage cbs;
191         String JavaDoc xpath;
192         Collection objs = new HashSet();
193         Class JavaDoc cl = DConfigBean JavaDoc.class;
194         // should be able to handle changing arr array
195
Fixed(ConfigBeanStorage cbs, String JavaDoc xpath) {
196             super(cbs.getConfigBean(),(new DConfigBean JavaDoc[0]).getClass(),DConfigBean JavaDoc.class,
197             null,null,null,null);
198             this.cbs = cbs; this.xpath = xpath;
199             String JavaDoc name = xpath.substring(xpath.lastIndexOf("/")+1);
200             setName(name);
201             setDisplayName(name);
202             // System.err.println("Fixed property " + instance);
203
}
204         
205         public PropertyEditor getIndexedPropertyEditor() {
206             // System.err.println("Editor for Fixed property " + instance);
207
// System.out.println("Getting indexed editor");
208
return ConfigProperty.getEditor(instance,null,cl);
209         }
210         
211         public boolean canIndexedRead() { return true; }
212         public boolean canIndexedWrite() { return false; }
213         public boolean canRead() { return true; }
214         public boolean canWrite() { return false; }
215         
216         public Object JavaDoc getIndexedValue(int index) {
217             // System.out.println("Getting indexed value");
218
return ((ConfigBeanStorage)objs.toArray()[index]).getConfigBean();
219         }
220         
221         public Object JavaDoc getValue() {
222             // System.out.println("Getting array value");
223
Object JavaDoc[] arr = objs.toArray();
224             DConfigBean JavaDoc[] cb = new DConfigBean JavaDoc[arr.length];
225             for(int i = 0; i < cb.length; i++)
226                 cb[i] = ((ConfigBeanStorage)arr[i]).getConfigBean();
227             return cb;
228         }
229         
230         public void addElement(ConfigBeanStorage elem) {
231             cl = elem.getConfigBean().getClass();
232             objs.add(elem);
233         }
234         
235         public void removeElement(ConfigBeanStorage elem) {
236             objs.remove(elem);
237         }
238     }
239 }
240
Popular Tags