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: package com.sun.jsfcl.std.property;
042:
043: import com.sun.jsfcl.util.ComponentBundle;
044: import com.sun.rave.designtime.DesignProperty;
045: import org.openide.ErrorManager;
046:
047: /**
048: * @author eric, jdeva
049: *
050: * @deprecated
051: */
052: public abstract class NumberPropertyEditor extends
053: AbstractPropertyEditor {
054:
055: protected boolean isValueString;
056:
057: protected static final ComponentBundle bundle = ComponentBundle
058: .getBundle(NumberPropertyEditor.class);
059:
060: /**
061: * The following attributes are read from BeanInfo to check whether
062: * the user typed-in value is within the allowed range
063: */
064: public static final String MIN_VALUE_KEY = "minValue"; // NOI18N
065: public static final String MAX_VALUE_KEY = "maxValue"; // NOI18N
066: public static final String UNSET_VALUE_KEY = "unsetValue"; // NOI18N
067:
068: public static final String MIN_VALUE_PROP_KEY = "minValueProperty"; // NOI18N
069: public static final String MAX_VALUE_PROP_KEY = "maxValueProperty"; // NOI18N
070: public static final String UNSET_VALUE_PROP_KEY = "unsetValueProperty"; // NOI18N
071:
072: Comparable minValue = null, maxValue = null, unsetValue = null;
073: String minValueProperty = null, maxValueProperty = null,
074: unsetValueProperty = null;
075:
076: public void attachToNewDesignProperty() {
077: super .attachToNewDesignProperty();
078: isValueString = getDesignProperty().getPropertyDescriptor()
079: .getPropertyType() == String.class;
080:
081: /**
082: * Read the min/max/unset values or property names from which we have to
083: * get their values from the property descriptor
084: *
085: */
086: Object temp;
087: ErrorManager em = ErrorManager.getDefault();
088: String errMsg = "jsfcl.NumberPropertyEditor: The Property Descriptor for "
089: + // NOI18N
090: getDesignProperty().getPropertyDescriptor().getName()
091: + " has incorrect value for "; // NOI18N
092:
093: try {
094: temp = getDesignProperty().getPropertyDescriptor()
095: .getValue(MIN_VALUE_KEY);
096: if (temp != null)
097: minValue = (Comparable) temp;
098: } catch (ClassCastException exc) {
099: em.log(ErrorManager.ERROR, errMsg + MIN_VALUE_KEY);
100: }
101:
102: try {
103: temp = getDesignProperty().getPropertyDescriptor()
104: .getValue(MAX_VALUE_KEY);
105: if (temp != null)
106: maxValue = (Comparable) temp;
107: } catch (ClassCastException exc) {
108: em.log(ErrorManager.ERROR, errMsg + MAX_VALUE_KEY);
109: }
110:
111: try {
112: temp = getDesignProperty().getPropertyDescriptor()
113: .getValue(UNSET_VALUE_KEY);
114: if (temp != null)
115: unsetValue = (Comparable) temp;
116: } catch (ClassCastException exc) {
117: //Use the default value if something wrong is specified in BeanInfo
118: em.log(ErrorManager.ERROR, errMsg + UNSET_VALUE_KEY);
119: }
120:
121: try {
122: temp = getDesignProperty().getPropertyDescriptor()
123: .getValue(MIN_VALUE_PROP_KEY);
124: if (temp != null)
125: minValueProperty = (String) temp;
126: } catch (ClassCastException exc) {
127: //Use the default value if something wrong is specified in BeanInfo
128: em.log(ErrorManager.ERROR, errMsg + MIN_VALUE_PROP_KEY);
129: }
130:
131: try {
132: temp = getDesignProperty().getPropertyDescriptor()
133: .getValue(MAX_VALUE_PROP_KEY);
134: if (temp != null)
135: maxValueProperty = (String) temp;
136: } catch (ClassCastException exc) {
137: em.log(ErrorManager.ERROR, errMsg + MAX_VALUE_PROP_KEY);
138: }
139:
140: try {
141: temp = getDesignProperty().getPropertyDescriptor()
142: .getValue(UNSET_VALUE_PROP_KEY);
143: if (temp != null)
144: unsetValueProperty = (String) temp;
145: } catch (ClassCastException exc) {
146: em.log(ErrorManager.ERROR, errMsg + UNSET_VALUE_PROP_KEY);
147: }
148: }
149:
150: public Object getUnsetValue() {
151: try {
152: if (unsetValueProperty != null) {
153: DesignProperty temp = liveProperty.getDesignBean()
154: .getProperty(unsetValueProperty);
155: if (temp != null)
156: return (Comparable) temp.getValue();
157: }
158: } catch (ClassCastException exc) {
159: ErrorManager em = ErrorManager.getDefault();
160: em.log(ErrorManager.ERROR,
161: "jsfcl.NumberPropertyEditor: value of "
162: + // NOI18N
163: getDesignProperty().getPropertyDescriptor()
164: .getName()
165: + " is not of Comparable type"); // NOI18N
166: }
167:
168: return unsetValue;
169: }
170:
171: public String getAsText() {
172: Object value = getValue();
173: if (value == null || value.equals(getUnsetValue())) {
174: return ""; //NOI18N
175: }
176: return value.toString();
177: }
178:
179: public String getJavaInitializationString() {
180: // the Java rep and human rep are NOT the same for numbers, "" is not a valid Java string,
181: // so 0 must be used.
182: Object value = getValue();
183: String result = value != null ? value.toString() : "0";
184: String suffix = getJavaInitializationStringSuffix();
185: if (suffix != null) {
186: result += suffix;
187: }
188: return result;
189: }
190:
191: public String getJavaInitializationStringSuffix() {
192: return null;
193: }
194:
195: public abstract Object parseString(String string)
196: throws NumberFormatException;
197:
198: public void setAsText(String string)
199: throws java.lang.IllegalArgumentException {
200: string = string.trim();
201: Object value = getUnsetValue();
202: boolean unset = true;
203: if (string.length() > 0) {
204: try {
205: value = parseString(string);
206: } catch (NumberFormatException nfe) {
207: String errMsg = bundle.getMessage("valueInvalid", //NOI18N
208: getDesignProperty().getPropertyDescriptor()
209: .getName());
210: throw new LocalizedMessageRuntimeException(errMsg, nfe);
211: }
212: checkRange(value);
213: unset = false;
214: }
215: if (isValueString) {
216: if (value == null) {
217: setValue(null);
218: } else {
219: setValue(String.valueOf(value));
220: }
221: } else {
222: setValue(value);
223: }
224: if (unset) {
225: unsetProperty();
226: }
227: }
228:
229: public void checkRange(Object value)
230: throws IllegalArgumentException {
231: Comparable min = minValue;
232: Comparable max = maxValue;
233: String errMsg = null;
234:
235: /**
236: * Get the min/max values from other property of the bean if property names
237: * are specified in the BeanInfo
238: *
239: */
240: ErrorManager em = ErrorManager.getDefault();
241: try {
242: if (minValueProperty != null) {
243: DesignProperty minProp;
244: minProp = liveProperty.getDesignBean().getProperty(
245: minValueProperty);
246: if (minProp != null) {
247: min = (Comparable) minProp.getValue();
248: Comparable minUnset = (Comparable) minProp
249: .getPropertyDescriptor().getValue(
250: UNSET_VALUE_KEY);
251: //We should use the other property value only if the user has changed it
252: if (min.compareTo(minUnset) == 0)
253: min = minValue;
254: }
255: }
256: } catch (ClassCastException exc) {
257: em.log(ErrorManager.ERROR,
258: "jsfcl.NumberPropertyEditor: value of "
259: + // NOI18N
260: getDesignProperty().getPropertyDescriptor()
261: .getName()
262: + " is not of Comparable type"); // NOI18N
263: }
264:
265: try {
266: if (maxValueProperty != null) {
267: DesignProperty maxProp;
268: maxProp = liveProperty.getDesignBean().getProperty(
269: maxValueProperty);
270: if (maxProp != null) {
271: max = (Comparable) maxProp.getValue();
272: Comparable maxUnset = (Comparable) maxProp
273: .getPropertyDescriptor().getValue(
274: UNSET_VALUE_KEY);
275: //We should use the other property value only if the user has changed it
276: if (max.compareTo(maxUnset) == 0)
277: max = maxValue;
278: }
279: }
280: } catch (ClassCastException exc) {
281: em.log(ErrorManager.ERROR,
282: "jsfcl.NumberPropertyEditor: value of "
283: + // NOI18N
284: getDesignProperty().getPropertyDescriptor()
285: .getName()
286: + " is not of Comparable type"); // NOI18N
287: }
288:
289: /*
290: * Make sure the user typed-in value is within the allowed range
291: */
292: int result = min.compareTo(value);
293: if (result > 0) {
294: errMsg = bundle.getMessage("valueHigher", //NOI18N
295: getDesignProperty().getPropertyDescriptor()
296: .getName(), min);
297: throw new IllegalArgumentException(errMsg);
298: }
299:
300: result = max.compareTo(value);
301: if (result < 0) {
302: errMsg = bundle.getMessage("valueLower", //NOI18N
303: getDesignProperty().getPropertyDescriptor()
304: .getName(), max);
305: throw new IllegalArgumentException(errMsg);
306: }
307: }
308: }
|