KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > abe > nodes > properties > BaseABENodeProperty


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.schema.abe.nodes.properties;
21
22 import java.lang.reflect.InvocationTargetException JavaDoc;
23 import java.lang.reflect.Method JavaDoc;
24 import org.netbeans.modules.xml.axi.AXIComponent;
25 import org.netbeans.modules.xml.schema.abe.nodes.ABEAbstractNode;
26 import org.netbeans.modules.xml.schema.model.SchemaModel;
27 import org.netbeans.modules.xml.xam.ui.XAMUtils;
28 import org.openide.nodes.PropertySupport;
29
30 /**
31  * The Base class for schema component properties.
32  * It extends PropertySupport.Reflection
33  * In general all ABE node properties allow restoring to default,
34  * so this class provides skeleton impl of the required methods.
35  * Subclasses can override these methods for different behaviour.
36  * The canWrite method is overridden to mark properties as uneditable,
37  * if schema component belongs to read only model.
38  * This class provides constructors which create properties with localized user
39  * friendly names and description and customized editor classes.
40  * They can be changed after creation, using the setter methods.
41  * This class can be instantiated, but will be subclassed for specific type of properties.
42  * For example the BooleanProperty class subclasses this class to support
43  * boolean properties.
44  * Example usage:
45  * <CODE>
46  * Property myProp = new BaseABENodeProperty(
47  * myComp, // schema component
48  * String.class // value type
49  * "myProperty", // property name
50  * "My property", // display name
51  * "My property Description", // descr
52  * MyPropertyEditor.class // if default value is false
53  * );
54  * </CODE>
55  *
56  * @author Ayub Khan
57  */

58 public class BaseABENodeProperty extends PropertySupport.Reflection {
59     
60     /**
61      * Creates a new instance of BaseABENodeProperty.
62      *
63      * @param component The schema component which property belongs to.
64      * @param valueType The class type of the property.
65      * @param property The property name.
66      * @param propDispName The display name of the property.
67      * @param propDesc Short description about the property.
68      * @param propEditorClass The property editor class
69      * if the property needs special property editor.
70      * If no property editor class is provided default editor
71      * (for type of property) will be used.
72      * The property editor class must provide a default constructor.
73      * Subclasses can also override
74      * getPropertyEditor method to provide property editor.
75      * @throws java.lang.NoSuchMethodException If no getter and setter for the property are found
76      */

77     public BaseABENodeProperty(Object JavaDoc component,
78             Class JavaDoc valueType,
79             String JavaDoc property,
80             String JavaDoc propDispName,
81             String JavaDoc propDesc,
82             Class JavaDoc propEditorClass)
83             throws NoSuchMethodException JavaDoc {
84         super(component,valueType,property);
85         super.setName(property);
86         super.setDisplayName(propDispName);
87         super.setShortDescription(propDesc);
88         if(propEditorClass!=null)
89             super.setPropertyEditorClass(propEditorClass);
90     }
91     
92     /**
93      * Creates a new instance of BaseABENodeProperty.
94      *
95      * @param component The schema component which property belongs to.
96      * @param valueType The class type of the property.
97      * @param property The property name.
98      * @param propDispName The display name of the property.
99      * @param propDesc Short description about the property.
100      * @param propEditorClass The property editor class
101      * if the property needs special property editor.
102      * If no property editor class is provided default editor
103      * (for type of property) will be used.
104      * The property editor class must provide a default constructor.
105      * Subclasses can also override
106      * getPropertyEditor method to provide property editor.
107      * @throws java.lang.NoSuchMethodException If no getter and setter for the property are found
108      */

109     public BaseABENodeProperty(Object JavaDoc component,
110             Class JavaDoc valueType,
111             String JavaDoc property,
112             String JavaDoc propDispName,
113             String JavaDoc propDesc)
114             throws NoSuchMethodException JavaDoc {
115         this(component, valueType, property, propDispName, propDesc, null);
116     }
117     
118     /**
119      * Creates a new instance of BaseABENodeProperty.
120      *
121      * @param component The schema component which property belongs to.
122      * @param valueType The class type of the property.
123      * @param getter The property getter method.
124      * @param setter The property setter method.
125      * @param property The property name.
126      * @param propDispName The display name of the property.
127      * @param propDesc Short description about the property.
128      * @param propEditorClass The property editor class if the property needs
129      * special property editor. If no property editor
130      * class is provided default editor
131      * (for type of property) will be used.
132      * The property editor class must provide a
133      * default constructor. Subclasses can also override
134      * getPropertyEditor method to provide property editor.
135      */

136     public BaseABENodeProperty(Object JavaDoc component,
137             Class JavaDoc valueType,
138             Method JavaDoc getter,
139             Method JavaDoc setter,
140             String JavaDoc property,
141             String JavaDoc propDispName,
142             String JavaDoc propDesc,
143             Class JavaDoc propEditorClass) {
144         super(component,valueType,getter,setter);
145         super.setName(property);
146         super.setDisplayName(propDispName);
147         super.setShortDescription(propDesc);
148         if(propEditorClass!=null)
149             super.setPropertyEditorClass(propEditorClass);
150     }
151     
152     /**
153      * This api determines if this property is editable
154      * @return Returns true if the property is editable, false otherwise.
155      */

156     @Override JavaDoc
157     public boolean canWrite() {
158         // Check for null model since component may have been removed.
159
AXIComponent c = getComponent();
160         if(c == null || (c.getModel() == null))
161             return false;
162         
163         AXIComponent o = c.getOriginal();
164         if(c != o && c.isReadOnly()) {
165             return false;
166         }
167         
168         SchemaModel model = c.getModel().getSchemaModel();
169         return XAMUtils.isWritable(model);
170     }
171     
172     // Methods to support restore to default
173
/**
174      * This api determines if this property has default value.
175      * If the property value is null, its considered as default value.
176      * Subclasses can override if different behaviour expected.
177      * @return Returns true if the property is default value, false otherwise.
178      */

179     @Override JavaDoc
180     public boolean isDefaultValue () {
181         try {
182             return getValue()==null;
183         } catch (IllegalArgumentException JavaDoc ex) {
184         } catch (InvocationTargetException JavaDoc ex) {
185         } catch (IllegalAccessException JavaDoc ex) {
186         }
187         return false;
188     }
189
190     /**
191      * This api determines if this property supports resetting default value.
192      * This returns true always.
193      * Subclasses can override if different behaviour expected.
194      */

195     @Override JavaDoc
196     public boolean supportsDefaultValue () {
197         return true;
198     }
199
200     /**
201      * This api resets the property to its default value.
202      * It sets property value to null which is considered as default value.
203      * Subclasses can override if different behaviour expected.
204      */

205     @Override JavaDoc
206     public void restoreDefaultValue () throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
207         setValue(null);
208     }
209
210     protected AXIComponent getComponent() {
211         if(instance instanceof ABEAbstractNode){
212             //this is a node so return the axi component associated with the node
213
return ((ABEAbstractNode)instance).getAXIComponent();
214         }
215         return (AXIComponent) instance;
216     }
217
218     protected void setComponent(AXIComponent sc) {
219         instance = sc;
220     }
221
222     /**
223      * Helper method to convert the first letter of a string to uppercase.
224      * And prefix the string with some next string.
225      */

226     public static String JavaDoc firstLetterToUpperCase(String JavaDoc s, String JavaDoc pref) {
227         switch (s.length()) {
228             case 0:
229                 return pref;
230                 
231             case 1:
232                 return pref + Character.toUpperCase(s.charAt(0));
233                 
234             default:
235                 return pref + Character.toUpperCase(s.charAt(0)) + s.substring(1);
236         }
237     }
238     
239 }
Popular Tags