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: /*
043: * AdvancedFacetProperty.java
044: *
045: * Created on April 19, 2006, 11:53 AM
046: *
047: */
048:
049: package org.netbeans.modules.xml.schema.ui.nodes.schema.properties;
050:
051: import java.lang.reflect.InvocationTargetException;
052: import org.openide.ErrorManager;
053: import org.openide.util.NbBundle;
054: import org.netbeans.modules.xml.schema.model.SchemaComponent;
055: import org.netbeans.modules.xml.schema.model.SimpleTypeRestriction;
056: import org.netbeans.modules.xml.schema.model.TotalDigits;
057: import org.netbeans.modules.xml.schema.ui.basic.editors.StringEditor;
058: import org.netbeans.modules.xml.schema.ui.nodes.categorized.newtype.SchemaComponentCreator;
059: import org.netbeans.modules.xml.xam.ui.XAMUtils;
060:
061: /**
062: *
063: * @author Ajit Bhate
064: */
065: public class AdvancedFacetProperty extends BaseSchemaProperty {
066:
067: private Class<? extends SchemaComponent> facetType;
068:
069: private SimpleTypeRestriction parent;
070:
071: /** Creates a new instance of AdvancedFacetProperty */
072: public AdvancedFacetProperty(SimpleTypeRestriction parent,
073: SchemaComponent facet,
074: Class<? extends SchemaComponent> facetType,
075: Class valueType, String property, String propDispName,
076: String propDesc, Class propEditorClass)
077: throws NoSuchMethodException {
078: super (facet, valueType,
079: facetType.getMethod(BaseSchemaProperty
080: .firstLetterToUpperCase(property, "get"),
081: new Class[0]), facetType.getMethod(
082: BaseSchemaProperty.firstLetterToUpperCase(
083: property, "set"),
084: new Class[] { valueType }), property,
085: propDispName, propDesc,
086: valueType.equals(int.class) ? StringEditor.class
087: : propEditorClass);
088: super .setName(propDispName);
089: this .parent = parent;
090: this .facetType = facetType;
091: }
092:
093: public void setValue(Object val) throws IllegalAccessException,
094: IllegalArgumentException, InvocationTargetException {
095: if (getComponent() == null && val == null)
096: return;
097: if (val == null) {
098: parent.getModel().removeChildComponent(getComponent());
099: setComponent(null);
100: return;
101: }
102: if (super .getValueType().equals(int.class)) {
103: int lowerLimit = TotalDigits.class
104: .isAssignableFrom(facetType) ? 1 : 0;
105: int newVal = lowerLimit - 1;
106: try {
107: val = Integer.valueOf((String) val);
108: newVal = ((Integer) val).intValue();
109: } catch (Exception e) {
110: // do nothing iae is thrown in such case
111: }
112: if (newVal < lowerLimit) {
113: String msg = NbBundle.getMessage(
114: AdvancedFacetProperty.class,
115: "MSG_Neg_Int_Value", val); //NOI18N
116: IllegalArgumentException iae = new IllegalArgumentException(
117: msg);
118: ErrorManager.getDefault().annotate(iae,
119: ErrorManager.USER, msg, msg, null,
120: new java.util.Date());
121: throw iae;
122: }
123: }
124: if (getComponent() == null) {
125: SchemaComponent sc = createFacet();
126: setComponent(sc);
127: try {
128: super .setValue(val);
129: parent.getModel().addChildComponent(parent,
130: getComponent(), -1);
131: } catch (IllegalAccessException iae) {
132: setComponent(null);
133: throw iae;
134: } catch (IllegalArgumentException iae) {
135: setComponent(null);
136: throw iae;
137: } catch (InvocationTargetException ite) {
138: setComponent(null);
139: throw ite;
140: } catch (Exception e) {
141: setComponent(null);
142: assert false;
143: }
144: } else {
145: super .setValue(val);
146: }
147: }
148:
149: public Object getValue() throws IllegalAccessException,
150: IllegalArgumentException, InvocationTargetException {
151: if (getComponent() == null)
152: return null;
153: if (super .getValueType().equals(int.class)) {
154: return super .getValue().toString();
155: }
156: return super .getValue();
157: }
158:
159: /**
160: * This api determines if this property is editable
161: * @return Returns true if the property is editable, false otherwise.
162: */
163: public boolean canWrite() {
164: if (getComponent() == null) {
165: return XAMUtils.isWritable(parent.getModel());
166: }
167: return super .canWrite();
168: }
169:
170: private SchemaComponent createFacet() {
171: return SchemaComponentCreator.createComponent(parent.getModel()
172: .getFactory(), facetType);
173: }
174: }
|