KickJava   Java API By Example, From Geeks To Geeks.

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


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  * AdvancedFacetProperty.java
22  *
23  * Created on April 19, 2006, 11:53 AM
24  *
25  */

26
27 package org.netbeans.modules.xml.schema.ui.nodes.schema.properties;
28
29 import java.lang.reflect.InvocationTargetException JavaDoc;
30 import org.openide.ErrorManager;
31 import org.openide.util.NbBundle;
32 import org.netbeans.modules.xml.schema.model.SchemaComponent;
33 import org.netbeans.modules.xml.schema.model.SimpleTypeRestriction;
34 import org.netbeans.modules.xml.schema.model.TotalDigits;
35 import org.netbeans.modules.xml.schema.ui.basic.editors.StringEditor;
36 import org.netbeans.modules.xml.schema.ui.nodes.categorized.newtype.SchemaComponentCreator;
37 import org.netbeans.modules.xml.xam.ui.XAMUtils;
38
39 /**
40  *
41  * @author Ajit Bhate
42  */

43 public class AdvancedFacetProperty extends BaseSchemaProperty {
44     
45     private Class JavaDoc<? extends SchemaComponent> facetType;
46     
47     private SimpleTypeRestriction parent;
48     
49     /** Creates a new instance of AdvancedFacetProperty */
50     public AdvancedFacetProperty(SimpleTypeRestriction parent,
51             SchemaComponent facet,
52             Class JavaDoc<? extends SchemaComponent> facetType,
53             Class JavaDoc valueType,
54             String JavaDoc property,
55             String JavaDoc propDispName,
56             String JavaDoc propDesc,
57             Class JavaDoc propEditorClass)
58             throws NoSuchMethodException JavaDoc {
59         super(facet,
60                 valueType,
61                 facetType.getMethod(BaseSchemaProperty.
62                 firstLetterToUpperCase(property, "get"), new Class JavaDoc[0]),
63                 facetType.getMethod(BaseSchemaProperty.
64                 firstLetterToUpperCase(property, "set"), new Class JavaDoc[]{valueType}),
65                 property,propDispName,propDesc,
66                 valueType.equals(int.class)?StringEditor.class:propEditorClass);
67         super.setName(propDispName);
68         this.parent = parent;
69         this.facetType = facetType;
70     }
71     
72     public void setValue(Object JavaDoc val) throws IllegalAccessException JavaDoc,
73             IllegalArgumentException JavaDoc, InvocationTargetException JavaDoc {
74         if(getComponent()==null && val == null) return;
75         if(val==null) {
76             parent.getModel().removeChildComponent(getComponent());
77             setComponent(null);
78             return;
79         }
80         if (super.getValueType().equals(int.class)) {
81             int lowerLimit = TotalDigits.class.isAssignableFrom(facetType)?1:0;
82             int newVal = lowerLimit-1;
83             try {
84                 val = Integer.valueOf((String JavaDoc)val);
85                 newVal = ((Integer JavaDoc)val).intValue();
86             } catch (Exception JavaDoc e) {
87                 // do nothing iae is thrown in such case
88
}
89             if(newVal<lowerLimit) {
90                 String JavaDoc msg = NbBundle.getMessage(AdvancedFacetProperty.class,
91                         "MSG_Neg_Int_Value", val); //NOI18N
92
IllegalArgumentException JavaDoc iae = new IllegalArgumentException JavaDoc(msg);
93                 ErrorManager.getDefault().annotate(iae, ErrorManager.USER,
94                         msg, msg, null, new java.util.Date JavaDoc());
95                 throw iae;
96             }
97         }
98         if(getComponent()==null) {
99             SchemaComponent sc = createFacet();
100             setComponent(sc);
101             try {
102                 super.setValue(val);
103                 parent.getModel().addChildComponent(parent,getComponent(),-1);
104             } catch (IllegalAccessException JavaDoc iae) {
105                 setComponent(null);
106                 throw iae;
107             } catch (IllegalArgumentException JavaDoc iae) {
108                 setComponent(null);
109                 throw iae;
110             } catch (InvocationTargetException JavaDoc ite) {
111                 setComponent(null);
112                 throw ite;
113             } catch (Exception JavaDoc e) {
114                 setComponent(null);
115                 assert false;
116             }
117         } else {
118             super.setValue(val);
119         }
120     }
121     
122     public Object JavaDoc getValue() throws IllegalAccessException JavaDoc,
123             IllegalArgumentException JavaDoc, InvocationTargetException JavaDoc {
124         if(getComponent()==null) return null;
125         if (super.getValueType().equals(int.class)) {
126             return super.getValue().toString();
127         }
128         return super.getValue();
129     }
130     
131     /**
132      * This api determines if this property is editable
133      * @return Returns true if the property is editable, false otherwise.
134      */

135     public boolean canWrite() {
136         if(getComponent()==null) {
137             return XAMUtils.isWritable(parent.getModel());
138         }
139         return super.canWrite();
140     }
141     
142     
143     private SchemaComponent createFacet() {
144         return SchemaComponentCreator.createComponent(
145                 parent.getModel().getFactory(), facetType);
146     }
147 }
148
Popular Tags