KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > form > RADProperty


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 package org.netbeans.modules.form;
22
23 import java.beans.*;
24 import java.lang.reflect.*;
25 import org.openide.ErrorManager;
26
27 import org.netbeans.modules.form.editors.*;
28 import org.netbeans.modules.form.fakepeer.FakePeerSupport;
29
30 /**
31  * Implementation of properties for (meta)components (class RADComponent).
32  * RADComponent is used to get the component instance and
33  * PropertyDescriptor provides read and write methods to get and set
34  * property values.
35  *
36  * @author Tomas Pavek
37  */

38 public class RADProperty extends FormProperty {
39
40     public static final String JavaDoc SYNTH_PREFIX = "$$$_"; // NOI18N
41
public static final String JavaDoc SYNTH_PRE_CODE = SYNTH_PREFIX + PROP_PRE_CODE + "_"; // NOI18N
42
public static final String JavaDoc SYNTH_POST_CODE = SYNTH_PREFIX + PROP_POST_CODE + "_"; // NOI18N
43

44     private RADComponent component;
45     private PropertyDescriptor desc;
46
47     RADProperty(RADComponent metacomp, PropertyDescriptor propdesc) {
48         super(new RADPropertyContext(metacomp),
49               propdesc.getName(),
50               propdesc.getPropertyType(),
51               propdesc.getDisplayName(),
52               propdesc.getShortDescription());
53
54         component = metacomp;
55         desc = propdesc;
56
57         if (desc.getWriteMethod() == null)
58             setAccessType(NO_WRITE);
59         else if (desc.getReadMethod() == null)
60             setAccessType(DETACHED_READ);
61     }
62
63     public RADComponent getRADComponent() {
64         return component;
65     }
66
67     public PropertyDescriptor getPropertyDescriptor() {
68         return desc;
69     }
70
71     // -------------------------------
72

73     public Object JavaDoc getTargetValue() throws IllegalAccessException JavaDoc,
74                                           InvocationTargetException {
75         Method readMethod = desc.getReadMethod();
76         if (readMethod == null) {
77             throw new IllegalAccessException JavaDoc("Not a readable property: "+desc.getName()); // NOI18N
78
}
79         return readMethod.invoke(component.getBeanInstance(), new Object JavaDoc[0]);
80     }
81
82     public void setTargetValue(Object JavaDoc value) throws IllegalAccessException JavaDoc,
83                                                  IllegalArgumentException JavaDoc,
84                                                  InvocationTargetException {
85         Method writeMethod = desc.getWriteMethod();
86         if (writeMethod == null) {
87             throw new IllegalAccessException JavaDoc("Not a writeable property: "+desc.getName()); // NOI18N
88
}
89
90         Object JavaDoc beanInstance = component.getBeanInstance();
91
92         // Ugly hack for Scrollbar - Scrollbar.setOrientation(...) method tries
93
// to re-create the (native) peer, which we cannot allow. So we detach
94
// the peer first before calling the method. This is the only place
95
// where we can do it. It could be probably done for all AWT
96
// components, but I don't know about any other which would need it.
97
java.awt.peer.ComponentPeer scrollbarPeerHack =
98             "setOrientation".equals(writeMethod.getName()) // NOI18N
99
&& beanInstance instanceof java.awt.Scrollbar JavaDoc ?
100             FakePeerSupport.detachFakePeer((java.awt.Component JavaDoc)beanInstance)
101             : null;
102
103         try {
104             // invoke the setter method
105
writeMethod.invoke(component.getBeanInstance(),
106                                new Object JavaDoc[] { value });
107         }
108         catch (InvocationTargetException ex) {
109             // annotate exception
110
String JavaDoc message = FormUtils.getFormattedBundleString(
111                 "MSG_ERR_WRITING_TO_PROPERTY", // NOI18N
112
new Object JavaDoc[] { getDisplayName() });
113
114             Throwable JavaDoc tex = ex.getTargetException();
115             if(tex instanceof IllegalArgumentException JavaDoc) {
116                 ErrorManager.getDefault().annotate(
117                     tex, ErrorManager.WARNING, null,
118                     message, null, null);
119                 throw (IllegalArgumentException JavaDoc) tex;
120             } else if(tex instanceof IllegalAccessException JavaDoc) {
121                 ErrorManager.getDefault().annotate(
122                     tex, ErrorManager.WARNING, null,
123                     message, null, null);
124                 throw (IllegalAccessException JavaDoc) tex;
125             } else if(value==null && tex instanceof NullPointerException JavaDoc) {
126                 IllegalArgumentException JavaDoc iae = new IllegalArgumentException JavaDoc();
127                 ErrorManager.getDefault().annotate(
128                     iae, ErrorManager.WARNING, null,
129                     message, null, null);
130                 throw iae;
131             }
132             
133             ErrorManager.getDefault().annotate(
134                 ex, ErrorManager.WARNING, null,
135                 message, null, null);
136
137             throw ex;
138         }
139
140         if (scrollbarPeerHack != null) // restore the Scrollbar's fake peer
141
FakePeerSupport.attachFakePeer((java.awt.Component JavaDoc)beanInstance,
142                                            scrollbarPeerHack);
143     }
144
145     public void setValue(Object JavaDoc value) throws IllegalAccessException JavaDoc,
146                                               IllegalArgumentException JavaDoc,
147                                               InvocationTargetException {
148         super.setValue(value);
149
150         component.debugChangedValues(); // do we need this??
151
}
152
153     protected Object JavaDoc getRealValue(Object JavaDoc value) {
154         Object JavaDoc realValue = super.getRealValue(value);
155
156         if (realValue == FormDesignValue.IGNORED_VALUE
157               && component.getBeanInstance() instanceof java.awt.Component JavaDoc
158               && "text".equals(desc.getName())) // NOI18N
159
realValue = ((FormDesignValue)value).getDescription();
160
161         return realValue;
162     }
163
164     public boolean supportsDefaultValue() {
165         return BeanSupport.NO_VALUE != BeanSupport.getDefaultPropertyValue(
166                                                    component.getBeanInstance(),
167                                                    getName());
168     }
169
170     public Object JavaDoc getDefaultValue() {
171         return BeanSupport.getDefaultPropertyValue(component.getBeanInstance(),
172                                                    getName());
173     }
174
175     // ----------
176

177     public boolean canWrite() {
178          return component.isReadOnly() ? false : super.canWrite();
179     }
180
181     // ----------
182

183     public PropertyEditor getExpliciteEditor() {
184         PropertyEditor prEd = null;
185
186         PropertyDescriptor descriptor = getPropertyDescriptor();
187         if (descriptor.getPropertyType() == Integer.TYPE
188             && ("mnemonic".equals(descriptor.getName()) // NOI18N
189
|| "displayedMnemonic".equals(descriptor.getName()))) { // NOI18N
190
prEd = new MnemonicEditor();
191         } else {
192                 prEd = createEnumEditor(descriptor);
193         }
194
195         if (prEd == null) {
196             try {
197                 prEd = desc.createPropertyEditor(component.getBeanInstance());
198             }
199             catch (Exception JavaDoc ex) {
200                 org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, ex);
201             }
202         }
203
204         return prEd;
205     }
206
207     protected PropertyEditor createEnumEditor(PropertyDescriptor descriptor) {
208         Object JavaDoc[] enumerationValues;
209
210         if (!"debugGraphicsOptions".equals(descriptor.getName()) // NOI18N
211
|| !javax.swing.JComponent JavaDoc.class.isAssignableFrom(
212                                               component.getBeanClass()))
213         { // get the enumeration values by standard means
214
enumerationValues = (Object JavaDoc[])
215                                 descriptor.getValue("enumerationValues"); // NOI18N
216
}
217         else { // hack: debugGraphicsOptions is problematic because its
218
// default value (0) does not correspond to any of the
219
// enumerated constants (NONE_OPTION is -1)
220
enumerationValues = new Object JavaDoc[] {
221                 "NONE_OPTION", new Integer JavaDoc(-1), "DebugGraphics.NONE_OPTION", // NOI18N
222
"NO_CHANGES", new Integer JavaDoc(0), "0", // NOI18N
223
"LOG_OPTION", new Integer JavaDoc(1), "DebugGraphics.LOG_OPTION", // NOI18N
224
"FLASH_OPTION", new Integer JavaDoc(2), "DebugGraphics.FLASH_OPTION", // NOI18N
225
"BUFFERED_OPTION", new Integer JavaDoc(4), "DebugGraphics.BUFFERED_OPTION" }; // NOI18N
226
}
227
228         if (enumerationValues == null
229             && "defaultCloseOperation".equals(descriptor.getName()) // NOI18N
230
&& (javax.swing.JDialog JavaDoc.class.isAssignableFrom(
231                                            component.getBeanClass())
232                 || javax.swing.JInternalFrame JavaDoc.class.isAssignableFrom(
233                                            component.getBeanClass())))
234         { // hack: enumeration definition is missing in standard Swing
235
// for JDialog and JInternalFrame defaultCloseOperation property
236
enumerationValues = new Object JavaDoc[] {
237                 "DISPOSE_ON_CLOSE", new Integer JavaDoc(2), // NOI18N
238
"WindowConstants.DISPOSE_ON_CLOSE", // NOI18N
239
"DO_NOTHING_ON_CLOSE", new Integer JavaDoc(0), // NOI18N
240
"WindowConstants.DO_NOTHING_ON_CLOSE", // NOI18N
241
"HIDE_ON_CLOSE", new Integer JavaDoc(1), // NOI18N
242
"WindowConstants.HIDE_ON_CLOSE" }; // NOI18N
243
}
244
245         return enumerationValues != null ?
246                  new EnumEditor(enumerationValues) : null;
247     }
248
249     protected Method getWriteMethod() {
250     return desc.getWriteMethod();
251     }
252     
253     public void setPreCode(String JavaDoc value) {
254         if ((preCode == null && value != null)
255                 || (preCode != null && !preCode.equals(value))) {
256             Object JavaDoc old = preCode;
257             preCode = value;
258             if (isChangeFiring() && component.getFormModel() != null)
259                 component.getFormModel().fireSyntheticPropertyChanged(
260                     component, SYNTH_PRE_CODE + getName(), old, value);
261         }
262     }
263
264     public void setPostCode(String JavaDoc value) {
265         if ((postCode == null && value != null)
266                 || (postCode != null && !postCode.equals(value))) {
267             Object JavaDoc old = postCode;
268             postCode = value;
269             if (isChangeFiring() && component.getFormModel() != null)
270                 component.getFormModel().fireSyntheticPropertyChanged(
271                     component, SYNTH_POST_CODE + getName(), old, value);
272         }
273     }
274
275     // ----------------------------------
276

277 /* protected void firePropertyValueChange(Object old, Object current) {
278         super.firePropertyValueChange(old, current);
279
280         if (isChangeFiring() && component.getFormModel() != null)
281             component.getFormModel().fireComponentPropertyChanged(component,
282                                                   desc.getName(), old, current);
283     }
284
285     protected void fireCurrentEditorChange(PropertyEditor old, PropertyEditor current) {
286         super.fireCurrentEditorChange(old, current);
287
288         if (isChangeFiring() && component.getFormModel() != null)
289             component.getFormModel().fireComponentPropertyChanged(component,
290                                                   desc.getName(), null, null);
291     } */

292
293     // -------------------
294
// innerclasses
295

296     static class RADPropertyContext extends FormPropertyContext.DefaultSupport {
297         RADComponent component;
298
299         RADPropertyContext(RADComponent comp) {
300             component = comp;
301         }
302
303         public FormModel getFormModel() {
304             return component.getFormModel();
305         }
306     }
307
308     // Descriptor for fake-properties (not real, design-time only) that
309
// need to pretend they are of certain type although without both
310
// getter and setter. Used e.g. by ButtonGroupProperty.
311
static class FakePropertyDescriptor extends PropertyDescriptor {
312         Class JavaDoc propType;
313
314         FakePropertyDescriptor(String JavaDoc name, Class JavaDoc type) throws IntrospectionException {
315             super(name,null,null);
316             propType = type;
317         }
318
319         public Class JavaDoc getPropertyType() {
320             return propType;
321         }
322     }
323 }
324
Popular Tags