001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.sql.framework.ui.editor.property.impl;
043:
044: import java.beans.PropertyEditor;
045: import java.beans.PropertyVetoException;
046: import org.netbeans.modules.sql.framework.ui.editor.property.IElement;
047: import org.netbeans.modules.sql.framework.ui.editor.property.INode;
048: import org.netbeans.modules.sql.framework.ui.editor.property.IProperty;
049: import org.netbeans.modules.sql.framework.ui.editor.property.IPropertyCustomizer;
050: import org.netbeans.modules.sql.framework.ui.editor.property.IPropertyEditor;
051: import org.openide.nodes.Node;
052:
053: /**
054: * @author Ritesh Adval
055: * @version $Revision$
056: */
057: public class BasicPropertySupport extends Node.Property implements
058: IProperty, Comparable {
059:
060: private boolean customEditor;
061: private IPropertyCustomizer customizer;
062: private PropertyEditor editor;
063: private String group;
064: private boolean isDefault;
065: private INode parent;
066: private int position;
067: private boolean readOnly = false;
068: private boolean required;
069: private String type;
070: private Object val = null;
071: private Class valueType;
072:
073: public BasicPropertySupport() {
074: this (String.class);
075: }
076:
077: /** Creates a new instance of GUIPropertySupport */
078: @SuppressWarnings(value="unchecked")
079: public BasicPropertySupport(Class valueType) {
080: super (valueType);
081: this .valueType = valueType;
082: }
083:
084: public BasicPropertySupport(String valueType) {
085: this (valueType.getClass());
086: }
087:
088: /**
089: * add a element in the node
090: *
091: * @param element element to add
092: */
093: public void add(IElement element) {
094: }
095:
096: public boolean canRead() {
097: return true;
098: }
099:
100: public boolean canWrite() {
101: return !this .readOnly;
102: }
103:
104: /**
105: * Compares this object with the specified object for order. Returns a negative
106: * integer, zero, or a positive integer as this object is less than, equal to, or
107: * greater than the specified object.
108: * <p>
109: * In the foregoing description, the notation <tt>sgn(</tt> <i>expression </i>
110: * <tt>)</tt> designates the mathematical <i>signum </i> function, which is defined
111: * to return one of <tt>-1</tt>,<tt>0</tt>, or <tt>1</tt> according to
112: * whether the value of <i>expression </i> is negative, zero or positive. The
113: * implementor must ensure <tt>sgn(x.compareTo(y)) ==
114: * -sgn(y.compareTo(x))</tt> for
115: * all <tt>x</tt> and <tt>y</tt>. (This implies that <tt>x.compareTo(y)</tt>
116: * must throw an exception iff <tt>y.compareTo(x)</tt> throws an exception.)
117: * <p>
118: * The implementor must also ensure that the relation is transitive:
119: * <tt>(x.compareTo(y)>0 && y.compareTo(z)>0)</tt> implies
120: * <tt>x.compareTo(z)>0</tt>.
121: * <p>
122: * Finally, the implementer must ensure that <tt>x.compareTo(y)==0</tt> implies that
123: * <tt>sgn(x.compareTo(z)) == sgn(y.compareTo(z))</tt>, for all <tt>z</tt>.
124: * <p>
125: * It is strongly recommended, but <i>not </i> strictly required that
126: * <tt>(x.compareTo(y)==0) == (x.equals(y))</tt>. Generally speaking, any class
127: * that implements the <tt>Comparable</tt> interface and violates this condition
128: * should clearly indicate this fact. The recommended language is "Note: this class
129: * has a natural ordering that is inconsistent with equals."
130: *
131: * @param o the Object to be compared.
132: * @return a negative integer, zero, or a positive integer as this object is less
133: * than, equal to, or greater than the specified object.
134: * @throws ClassCastException if the specified object's type prevents it from being
135: * compared to this Object.
136: */
137: public int compareTo(Object o) {
138: if (!(o instanceof IProperty)) {
139: throw new ClassCastException("Object " + o
140: + " being compared is not same as this object");
141: }
142:
143: IProperty property = (IProperty) o;
144: if (this .getPosition() < property.getPosition()) {
145: return -1;
146: } else if (this .getPosition() > property.getPosition()) {
147: return 1;
148: }
149: return 0;
150: }
151:
152: /**
153: * get the group to which this property belongs
154: *
155: * @return group to which property belongs
156: */
157: public String getGroup() {
158: return this .group;
159: }
160:
161: /**
162: * get the parent element
163: *
164: * @return parent
165: */
166: public INode getParent() {
167: return parent;
168: }
169:
170: /**
171: * get the position where this property should appear in the property sheet gui
172: *
173: * @return position
174: */
175: public int getPosition() {
176: return this .position;
177: }
178:
179: /**
180: * get the property customizer
181: *
182: * @return property customizer
183: */
184: public IPropertyCustomizer getPropertyCustomizer() {
185: return this .customizer;
186: }
187:
188: @Override
189: public PropertyEditor getPropertyEditor() {
190: if (this .editor != null) {
191: return this .editor;
192: }
193: return java.beans.PropertyEditorManager
194: .findEditor(getValueType());
195: }
196:
197: /**
198: * get the tooltip of of element
199: *
200: * @return tooltip
201: */
202: public String getToolTip() {
203: return super .getShortDescription();
204: }
205:
206: /**
207: * get the gui type of this property
208: *
209: * @return gui type
210: */
211: public String getType() {
212: return this .type;
213: }
214:
215: public Object getValue() throws java.lang.IllegalAccessException,
216: java.lang.reflect.InvocationTargetException {
217: return val;
218: }
219:
220: /**
221: * get the type of value of this property
222: *
223: * @return property value type
224: */
225: @Override
226: public Class getValueType() {
227: return valueType;
228: }
229:
230: /**
231: * does this property has a custom editor
232: *
233: * @return whether property has a custom editor
234: */
235: public boolean isCustomEditor() {
236: return customEditor;
237: }
238:
239: /**
240: * Does this property has a default value
241: *
242: * @returrn whether property has a default value
243: */
244: public boolean isDefault() {
245: return this .isDefault;
246: }
247:
248: /**
249: * Is this property read only, if readonly then gui can not edit this property
250: *
251: * @return whether this property is read only
252: */
253: public boolean isReadOnly() {
254: return this .readOnly;
255: }
256:
257: /**
258: * Is this property require if yes then this property should always have a vaule
259: *
260: * @return whether this property is required
261: */
262: public boolean isRequired() {
263: return this .required;
264: }
265:
266: /**
267: * is this value valid
268: *
269: * @return value if valid
270: */
271: public boolean isValid() {
272: try {
273: if (this .getValue() != null) {
274: return true;
275: }
276: } catch (Exception ex) {
277: }
278: return false;
279: }
280:
281: /**
282: * deos this property has a custom editor
283: *
284: * @param editor whether property has a custom editor
285: */
286: public void setCustomEditor(String editor) {
287: customEditor = Boolean.valueOf(editor).booleanValue();
288: }
289:
290: /**
291: * Does this property has a default value
292: *
293: * @returrn whether property has a default value
294: */
295: public void setDefault(boolean def) {
296: this .isDefault = def;
297: }
298:
299: /**
300: * Does this property has a default value
301: *
302: * @returrn whether property has a default value
303: */
304: public void setDefault(String def) {
305: this .isDefault = Boolean.valueOf(def).booleanValue();
306: }
307:
308: /**
309: * set the optional property editor which can be used to edit this property
310: *
311: * @return property editor
312: */
313: public void setEditorClass(String editorClass) {
314: if (editorClass == null || editorClass.trim().equals("")) {
315: return;
316: }
317:
318: try {
319: Class eClass = Class.forName(editorClass);
320: editor = (PropertyEditor) eClass.newInstance();
321: if (editor instanceof IPropertyEditor) {
322: ((IPropertyEditor) editor).setProperty(this );
323: }
324: } catch (Exception ex) {
325: ex.printStackTrace();
326: }
327: }
328:
329: /**
330: * set the group to which this property belongs
331: *
332: * @return group to which property belongs
333: */
334: public void setGroup(String group) {
335: this .group = group;
336: }
337:
338: /**
339: * set the owner object which has this property
340: */
341: public void setOwnerObject(Object obj) {
342: // this.ownerObject = obj;
343: }
344:
345: /**
346: * set parent parent element
347: */
348: public void setParent(INode parent) {
349: this .parent = parent;
350: }
351:
352: /**
353: * set the position where this property should appear in the property sheet gui
354: *
355: * @return position
356: */
357: public void setPosition(String position) {
358: this .position = Integer.parseInt(position);
359: }
360:
361: /**
362: * set the property customizer
363: *
364: * @param customizer customizer
365: */
366: public void setPropertyCustomizer(IPropertyCustomizer customizer) {
367: this .customizer = customizer;
368:
369: if (customizer != null) {
370: PropertyEditor pEditor = customizer.getPropertyEditor();
371: if (pEditor != null) {
372: this .setPropertyEditor(pEditor);
373: }
374: }
375: }
376:
377: /**
378: * set the optional property editor which can be used to edit this property
379: *
380: * @return property editor
381: */
382: public void setPropertyEditor(PropertyEditor editor) {
383: this .editor = editor;
384: if (editor instanceof IPropertyEditor) {
385: ((IPropertyEditor) editor).setProperty(this );
386: }
387: }
388:
389: /**
390: * Is this property read only, if readonly then gui can not edit this property
391: *
392: * @return whether this property is read only
393: */
394: public void setReadOnly(boolean readOnly) {
395: this .readOnly = readOnly;
396: }
397:
398: /**
399: * Is this property read only, if readonly then gui can not edit this property
400: *
401: * @return whether this property is read only
402: */
403: public void setReadOnly(String readOnly) {
404: this .readOnly = Boolean.valueOf(readOnly).booleanValue();
405: }
406:
407: /**
408: * Is this property require if yes then this property should always have a vaule
409: *
410: * @return whether this property is required
411: */
412: public void setRequired(boolean required) {
413: this .required = required;
414: }
415:
416: /**
417: * Is this property require if yes then this property should always have a vaule
418: *
419: * @return whether this property is required
420: */
421: public void setRequired(String required) {
422: this .required = Boolean.valueOf(required).booleanValue();
423: }
424:
425: /**
426: * set the tooltip of the element
427: *
428: * @param tTip tool tip
429: */
430: public void setToolTip(String tTip) {
431: super .setShortDescription(tTip);
432: }
433:
434: /**
435: * get the gui type of this property
436: *
437: * @return gui type
438: */
439: public void setType(String type) {
440: this .type = type;
441: }
442:
443: public void setValue(Object obj)
444: throws java.lang.IllegalAccessException,
445: java.lang.IllegalArgumentException,
446: java.lang.reflect.InvocationTargetException {
447: Object oldVal = this .getValue();
448: try {
449: // first fire vetoable change event
450: ((PropertyGroup) this .getParent()).fireVetoableChangeEvent(
451: this .getName(), oldVal, obj);
452: // then fire VALID_STATE event
453: ((PropertyGroup) this .getParent())
454: .firePropertyChangeEvent();
455: } catch (PropertyVetoException ex) {
456: return;
457: }
458: val = obj;
459:
460: // then fire value change
461: if (!isReadOnly()) {
462: ((PropertyGroup) this .getParent()).firePropertyChangeEvent(
463: this .getName(), oldVal, obj);
464: }
465: }
466:
467: /**
468: * set the type of value of this property
469: *
470: * @return property value type
471: */
472: public void setValueType(String vType) {
473: try {
474: valueType = Class.forName(vType);
475: } catch (ClassNotFoundException ex) {
476: ex.printStackTrace();
477: }
478: }
479: }
|