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: * NonNegativeIntegerProperty.java
044: *
045: * Created on January 5, 2006, 3:21 PM
046: *
047: */
048:
049: package org.netbeans.modules.xml.schema.ui.nodes.schema.properties;
050:
051: import java.lang.reflect.InvocationTargetException;
052: import org.netbeans.modules.xml.schema.model.SchemaComponent;
053: import org.openide.ErrorManager;
054: import org.openide.util.NbBundle;
055:
056: /**
057: * This class provides property support for properties having
058: * non negative integer values(value>=0).
059: * This class supports the Integer type properties.
060: * Inner class Primitive supports int type properties.
061: * Inner class PrimitivePositive supports int type properties
062: * which have positive values (value>0).
063: * @author Ajit Bhate
064: */
065: public class NonNegativeIntegerProperty extends BaseSchemaProperty {
066:
067: /**
068: * Creates a new instance of NonNegativeIntegerProperty.
069: *
070: *
071: * @param component The schema component which property belongs to.
072: * @param property The property name.
073: * @param propDispName The display name of the property.
074: * @param propDesc Short description about the property.
075: * @param isPrimitive distinguish between int and Integer. temporary property
076: * Assumes that the property editor is default editor for Integer.
077: * If special editor needed, subclasses and instances must set it explicitly.
078: * @throws java.lang.NoSuchMethodException If no getter and setter for the property are found
079: */
080: public NonNegativeIntegerProperty(SchemaComponent component,
081: String property, String dispName, String desc)
082: throws NoSuchMethodException {
083: this (component, Integer.class, property, dispName, desc);
084: }
085:
086: protected NonNegativeIntegerProperty(SchemaComponent component,
087: Class type, String property, String dispName, String desc)
088: throws NoSuchMethodException {
089: super (component, type, property, dispName, desc, null);
090: }
091:
092: protected int getLowerLimit() {
093: return 0;
094: }
095:
096: protected int getDefaultValue() {
097: return 1;
098: }
099:
100: /**
101: * The getValue method never returns null.
102: * So this api is overridden to use super call instead
103: */
104: @Override
105: public boolean isDefaultValue() {
106: try {
107: return super .getValue() == null;
108: } catch (IllegalArgumentException ex) {
109: } catch (InvocationTargetException ex) {
110: } catch (IllegalAccessException ex) {
111: }
112: return false;
113: }
114:
115: /**
116: * Overridden to return default value if null.
117: */
118: @Override
119: public Object getValue() throws IllegalAccessException,
120: InvocationTargetException {
121: Object o = super .getValue();
122: return o == null ? getDefaultValue() : o;
123: }
124:
125: /**
126: * This method sets the value of the property.
127: * Overridden to validate positive values.
128: */
129: @Override
130: public void setValue(Object o) throws IllegalAccessException,
131: InvocationTargetException {
132: if (o instanceof Integer) {
133: int newVal = ((Integer) o).intValue();
134: if (newVal < getLowerLimit()) {
135: String msg = NbBundle.getMessage(
136: NonNegativeIntegerProperty.class,
137: "MSG_Neg_Int_Value", o); //NOI18N
138: IllegalArgumentException iae = new IllegalArgumentException(
139: msg);
140: ErrorManager.getDefault().annotate(iae,
141: ErrorManager.USER, msg, msg, null,
142: new java.util.Date());
143: throw iae;
144: }
145: }
146: super .setValue(o);
147: }
148:
149: /**
150: * Supports properties with non negative int value(>=0)
151: */
152: public static class Primitive extends NonNegativeIntegerProperty {
153: public Primitive(SchemaComponent component, String property,
154: String dispName, String desc)
155: throws NoSuchMethodException {
156: super (component, int.class, property, dispName, desc);
157: }
158:
159: /**
160: * Overridden to return false always
161: */
162: @Override
163: public boolean supportsDefaultValue() {
164: return false;
165: }
166:
167: }
168:
169: /**
170: * Supports properties with non negative int value(>0)
171: */
172: public static class PrimitivePositive extends Primitive {
173: public PrimitivePositive(SchemaComponent component,
174: String property, String dispName, String desc)
175: throws NoSuchMethodException {
176: super (component, property, dispName, desc);
177: }
178:
179: @Override
180: protected int getLowerLimit() {
181: return 1;
182: }
183: }
184:
185: }
|