KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > form > editors2 > BorderDesignSupport


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 package org.netbeans.modules.form.editors2;
21
22 import javax.swing.border.*;
23 import java.util.*;
24 import java.beans.*;
25 import java.lang.reflect.*;
26
27 import org.openide.nodes.*;
28 import org.netbeans.modules.form.*;
29 import org.openide.ErrorManager;
30
31 /**
32  * A support class holding metadata for borders (javax.swing.border.Border),
33  * similar to RADComponent.
34  *
35  * @author Tomas Pavek
36  */

37
38 public class BorderDesignSupport implements FormDesignValue
39 {
40     private Border theBorder;
41     private boolean borderNeedsUpdate;
42     private boolean propertiesNeedInit;
43     private CreationDescriptor creationDesc;
44     private FormPropertyContext propertyContext = null;
45     private FormProperty[] properties = null;
46     // -------------------------
47
// constructors
48

49     public BorderDesignSupport(Class JavaDoc borderClass)
50         throws Exception JavaDoc
51     {
52         creationDesc = CreationFactory.getDescriptor(borderClass);
53         if (creationDesc == null) {
54             creationDesc = new CreationDescriptor(borderClass);
55             CreationFactory.registerDescriptor(creationDesc);
56         }
57
58         theBorder = (Border) CreationFactory.createInstance(borderClass);
59     }
60     
61     public BorderDesignSupport(Border border) {
62         creationDesc = CreationFactory.getDescriptor(border.getClass());
63         if (creationDesc == null) {
64             creationDesc = new CreationDescriptor(border.getClass());
65             CreationFactory.registerDescriptor(creationDesc);
66         }
67         setBorder(border);
68     }
69
70     public BorderDesignSupport(BorderDesignSupport borderDesignSupport, FormPropertyContext propertyContext)
71         throws Exception JavaDoc
72     {
73         this(borderDesignSupport.getBorderClass());
74         createProperties();
75         setPropertyContext(propertyContext);
76         int copyMode = FormUtils.CHANGED_ONLY | FormUtils.DISABLE_CHANGE_FIRING;
77             
78         FormUtils.copyProperties(borderDesignSupport.getProperties(),
79                                  this.properties,
80                                  copyMode);
81     }
82
83     // --------------------------
84

85     public FormDesignValue copy(FormProperty formProperty) {
86         FormModel formModel = formProperty.getPropertyContext().getFormModel();
87         try {
88             return new BorderDesignSupport(this, BorderEditor.createFormPropertyContext(formModel));
89         } catch (Exception JavaDoc ex) {
90             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
91         }
92         return null;
93     }
94     
95     public Border getBorder() {
96         if (borderNeedsUpdate)
97             updateBorder();
98         return theBorder;
99     }
100
101     public void setBorder(Border border) {
102         theBorder = border;
103         if (properties != null) {
104             for (int i=0; i < properties.length; i++)
105                 try {
106                     properties[i].reinstateProperty();
107                 }
108                 catch (IllegalAccessException JavaDoc e1) {
109                 }
110                 catch (InvocationTargetException e2) {
111                 }
112             propertiesNeedInit = false;
113         }
114         else propertiesNeedInit = true;
115         borderNeedsUpdate = false;
116     }
117
118     public Class JavaDoc getBorderClass() {
119         return creationDesc.getDescribedClass();
120     }
121
122     public String JavaDoc getDisplayName() {
123         return org.openide.util.Utilities.getShortClassName(theBorder.getClass());
124 // String longName = theBorder.getClass().getName();
125
// int dot = longName.lastIndexOf('.');
126
// return dot < 0 ? longName : longName.substring(dot + 1);
127
}
128
129     /** Sets FormPropertyContext for properties. This should be called before
130      * properties are created or used after property context had changed.
131      */

132     public void setPropertyContext(FormPropertyContext propertyContext) {
133         if (properties != null && this.propertyContext != propertyContext) {
134             for (int i=0; i < properties.length; i++)
135                 if (!properties[i].getValueType().isPrimitive())
136                     properties[i].setPropertyContext(propertyContext);
137         }
138
139         this.propertyContext = propertyContext;
140     }
141
142     // FormPropertyContainer implementation
143
public Node.Property[] getProperties() {
144         if (properties == null)
145             createProperties();
146         return properties;
147     }
148
149     public Node.Property getPropertyOfName(String JavaDoc name) {
150         Node.Property[] props = getProperties();
151         for (int i=0; i < props.length; i++)
152             if (props[i].getName().equals(name))
153                 return props[i];
154
155         return null;
156     }
157
158     private void createProperties() {
159         BeanInfo bInfo;
160         try {
161             bInfo = FormUtils.getBeanInfo(theBorder.getClass());
162         } catch (IntrospectionException ex) {
163             return;
164         }
165         PropertyDescriptor[] props = bInfo.getPropertyDescriptors();
166
167         ArrayList nodeProps = new ArrayList();
168         for (int i = 0; i < props.length; i++) {
169             PropertyDescriptor pd = props[i];
170             if (!pd.isHidden()
171                 && (pd.getWriteMethod() != null
172                     || CreationFactory.containsProperty(creationDesc,
173                                                         pd.getName())))
174             {
175                 BorderProperty prop =
176                     new BorderProperty(pd.getPropertyType().isPrimitive() ?
177                                            null : propertyContext,
178                                        pd);
179
180                 if (propertiesNeedInit)
181                     try {
182                         prop.reinstateProperty();
183                     }
184                     catch (IllegalAccessException JavaDoc e1) {
185                     }
186                     catch (InvocationTargetException e2) {
187                     }
188
189                 nodeProps.add(prop);
190             }
191         }
192         properties = new FormProperty[nodeProps.size()];
193         nodeProps.toArray(properties);
194         propertiesNeedInit = false;
195     }
196
197     public String JavaDoc getJavaInitializationString() {
198         if (properties == null)
199             createProperties();
200
201         CreationDescriptor.Creator creator =
202             creationDesc.findBestCreator(properties,
203                 CreationDescriptor.CHANGED_ONLY | CreationDescriptor.PLACE_ALL);
204
205         return creator.getJavaCreationCode(properties, Border.class);
206     }
207
208     void updateBorder() {
209         if (properties == null)
210             createProperties();
211
212         CreationDescriptor.Creator creator =
213             creationDesc.findBestCreator(properties,
214                 CreationDescriptor.CHANGED_ONLY | CreationDescriptor.PLACE_ALL);
215
216         try {
217             theBorder = (Border) CreationFactory.createInstance(
218                 creationDesc.getDescribedClass(),
219                 properties,
220                 CreationDescriptor.CHANGED_ONLY | CreationDescriptor.PLACE_ALL);
221
222             // set other properties (not used in constructor)
223
FormProperty[] otherProps = CreationFactory.getRemainingProperties(
224                                                          creator, properties);
225             for (int i=0; i < otherProps.length; i++)
226                 otherProps[i].reinstateTarget();
227         }
228         catch (Exception JavaDoc ex) { // should not happen (at least for standard borders)
229
org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, ex);
230         }
231     }
232     
233     public Object JavaDoc getDesignValue() {
234         return getBorder();
235     }
236
237     public String JavaDoc getDescription() {
238         return getDisplayName();
239     }
240
241     // -----------------------
242

243     public class BorderProperty extends FormProperty {
244         private PropertyDescriptor desc;
245
246         public BorderProperty(FormPropertyContext propertyContext,
247                               PropertyDescriptor desc)
248         {
249             super(propertyContext,
250                   desc.getName(),
251                   desc.getPropertyType(),
252                   desc.getDisplayName(),
253                   desc.getShortDescription());
254
255             this.desc = desc;
256
257             if (desc.getWriteMethod() == null)
258                 setAccessType(DETACHED_WRITE);
259             else if (desc.getReadMethod() == null)
260                 setAccessType(DETACHED_READ);
261         }
262
263         public Object JavaDoc getTargetValue()
264             throws IllegalAccessException JavaDoc, InvocationTargetException
265         {
266             Method readMethod = desc.getReadMethod();
267             return readMethod.invoke(theBorder, new Object JavaDoc[0]);
268         }
269
270         public void setTargetValue(Object JavaDoc value)
271             throws IllegalAccessException JavaDoc, IllegalArgumentException JavaDoc,
272                    InvocationTargetException
273         {
274             Method writeMethod = desc.getWriteMethod();
275             writeMethod.invoke(theBorder, new Object JavaDoc[] { value });
276         }
277
278         protected Object JavaDoc getRealValue(Object JavaDoc value) {
279             Object JavaDoc realValue = super.getRealValue(value);
280
281             if (realValue == FormDesignValue.IGNORED_VALUE
282                   && "title".equals(desc.getName())) // NOI18N
283
realValue = ((FormDesignValue)value).getDescription();
284
285             return realValue;
286         }
287
288         public boolean supportsDefaultValue () {
289             return true;
290         }
291
292         public Object JavaDoc getDefaultValue() {
293             Method readMethod = desc.getReadMethod();
294             Object JavaDoc value = null;
295             if (readMethod != null)
296                 try {
297                     value = readMethod.invoke(
298                         BeanSupport.getDefaultInstance(theBorder.getClass()),
299                         new Object JavaDoc[0]);
300                 }
301                 catch (Exception JavaDoc ex) { // do nothing
302
}
303             return value;
304         }
305
306         public PropertyEditor getExpliciteEditor() {
307             try {
308                 return desc.createPropertyEditor(theBorder);
309             }
310             catch (Exception JavaDoc ex) {
311                 ex.printStackTrace();
312                 return null;
313             }
314         }
315     
316     protected Method getWriteMethod() {
317         return desc.getWriteMethod();
318     }
319     
320         protected void propertyValueChanged(Object JavaDoc old, Object JavaDoc current) {
321             super.propertyValueChanged(old, current);
322             borderNeedsUpdate = (getAccessType() & DETACHED_WRITE) != 0;
323         }
324     }
325 }
326
Popular Tags