KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > ui > nodes > schema > properties > BaseSchemaProperty


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 /*
21  * BooleanProperty.java
22  *
23  * Created on January 5, 2006, 3:21 PM
24  *
25  */

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

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

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

113     public BaseSchemaProperty(SchemaComponent component,
114             Class JavaDoc valueType,
115             Method JavaDoc getter,
116             Method JavaDoc setter,
117             String JavaDoc property,
118             String JavaDoc propDispName,
119             String JavaDoc propDesc,
120             Class JavaDoc propEditorClass) {
121         super(component,valueType,getter,setter);
122         super.setName(property);
123         super.setDisplayName(propDispName);
124         super.setShortDescription(propDesc);
125         if(propEditorClass!=null)
126             super.setPropertyEditorClass(propEditorClass);
127     }
128     
129     /**
130      * This api determines if this property is editable
131      * @return Returns true if the property is editable, false otherwise.
132      */

133     @Override JavaDoc
134     public boolean canWrite() {
135         // Check for null model since component may have been removed.
136
SchemaModel model = getComponent().getModel();
137         return XAMUtils.isWritable(model);
138     }
139     
140     // Methods to support restore to default
141
/**
142      * This api determines if this property has default value.
143      * If the property value is null, its considered as default value.
144      * Subclasses can override if different behaviour expected.
145      * @return Returns true if the property is default value, false otherwise.
146      */

147     @Override JavaDoc
148     public boolean isDefaultValue () {
149         try {
150             return getValue()==null;
151         } catch (IllegalArgumentException JavaDoc ex) {
152         } catch (InvocationTargetException JavaDoc ex) {
153         } catch (IllegalAccessException JavaDoc ex) {
154         }
155         return false;
156     }
157
158     /**
159      * This api determines if this property supports resetting default value.
160      * This returns true always.
161      * Subclasses can override if different behaviour expected.
162      */

163     @Override JavaDoc
164     public boolean supportsDefaultValue () {
165         return true;
166     }
167
168     /**
169      * This api resets the property to its default value.
170      * It sets property value to null which is considered as default value.
171      * Subclasses can override if different behaviour expected.
172      */

173     @Override JavaDoc
174     public void restoreDefaultValue () throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
175         setValue(null);
176     }
177
178     protected SchemaComponent getComponent() {
179         return (SchemaComponent)instance;
180     }
181
182     protected void setComponent(SchemaComponent sc) {
183         instance = sc;
184     }
185
186     /**
187      * Helper method to convert the first letter of a string to uppercase.
188      * And prefix the string with some next string.
189      */

190     public static String JavaDoc firstLetterToUpperCase(String JavaDoc s, String JavaDoc pref) {
191         switch (s.length()) {
192             case 0:
193                 return pref;
194                 
195             case 1:
196                 return pref + Character.toUpperCase(s.charAt(0));
197                 
198             default:
199                 return pref + Character.toUpperCase(s.charAt(0)) + s.substring(1);
200         }
201     }
202     
203 }
Popular Tags