KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > abe > nodes > properties > NonNegativeIntegerProperty


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  * NonNegativeIntegerProperty.java
22  *
23  * Created on January 5, 2006, 3:21 PM
24  *
25  */

26
27 package org.netbeans.modules.xml.schema.abe.nodes.properties;
28
29 import java.lang.reflect.InvocationTargetException JavaDoc;
30 import org.netbeans.modules.xml.axi.AXIComponent;
31 import org.openide.ErrorManager;
32 import org.openide.util.NbBundle;
33
34
35 /**
36  * This class provides property support for properties having
37  * non negative integer values(value>=0).
38  * This class supports the Integer type properties.
39  * Inner class Primitive supports int type properties.
40  * Inner class PrimitivePositive supports int type properties
41  * which have positive values (value>0).
42  * @author Ajit Bhate
43  */

44 public class NonNegativeIntegerProperty extends BaseABENodeProperty {
45     
46     /**
47      * Creates a new instance of NonNegativeIntegerProperty.
48      *
49      *
50      * @param component The schema component which property belongs to.
51      * @param property The property name.
52      * @param propDispName The display name of the property.
53      * @param propDesc Short description about the property.
54      * @param isPrimitive distinguish between int and Integer. temporary property
55      * Assumes that the property editor is default editor for Integer.
56      * If special editor needed, subclasses and instances must set it explicitly.
57      * @throws java.lang.NoSuchMethodException If no getter and setter for the property are found
58      */

59     public NonNegativeIntegerProperty(AXIComponent component, String JavaDoc property, String JavaDoc dispName,
60             String JavaDoc desc) throws NoSuchMethodException JavaDoc {
61         this(component,Integer JavaDoc.class,property,dispName,desc);
62     }
63     
64     protected NonNegativeIntegerProperty(AXIComponent component, Class JavaDoc type,
65             String JavaDoc property, String JavaDoc dispName, String JavaDoc desc)
66             throws NoSuchMethodException JavaDoc {
67         super(component,type,property,dispName,desc,null);
68     }
69     
70     protected int getLowerLimit() {
71         return 0;
72     }
73
74     protected int getDefaultValue() {
75         return 1;
76     }
77
78     /**
79      * The getValue method never returns null.
80      * So this api is overridden to use super call instead
81      */

82     @Override JavaDoc
83     public boolean isDefaultValue() {
84         try {
85             return super.getValue()==null;
86         } catch (IllegalArgumentException JavaDoc ex) {
87         } catch (InvocationTargetException JavaDoc ex) {
88         } catch (IllegalAccessException JavaDoc ex) {
89         }
90         return false;
91     }
92
93     /**
94      * Overridden to return default value if null.
95      */

96     @Override JavaDoc
97     public Object JavaDoc getValue() throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
98         Object JavaDoc o = super.getValue();
99         return o==null?getDefaultValue():o;
100     }
101
102     /**
103      * This method sets the value of the property.
104      * Overridden to validate positive values.
105      */

106     @Override JavaDoc
107     public void setValue(Object JavaDoc o) throws
108             IllegalAccessException JavaDoc, InvocationTargetException JavaDoc{
109         if (o instanceof Integer JavaDoc){
110             int newVal = ((Integer JavaDoc)o).intValue();
111             if(newVal<getLowerLimit()){
112                 String JavaDoc msg = NbBundle.getMessage(NonNegativeIntegerProperty.class, "MSG_Neg_Int_Value", o); //NOI18N
113
IllegalArgumentException JavaDoc iae = new IllegalArgumentException JavaDoc(msg);
114                 ErrorManager.getDefault().annotate(iae, ErrorManager.USER,
115                         msg, msg, null, new java.util.Date JavaDoc());
116                 throw iae;
117             }
118         }
119         super.setValue(o);
120     }
121     
122     /**
123      * Supports properties with non negative int value(>=0)
124      */

125     public static class Primitive extends NonNegativeIntegerProperty {
126         public Primitive(AXIComponent component, String JavaDoc property, String JavaDoc dispName,
127                 String JavaDoc desc) throws NoSuchMethodException JavaDoc {
128             super(component,int.class,property,dispName,desc);
129         }
130
131         /**
132          * Overridden to return false always
133          */

134         @Override JavaDoc
135         public boolean supportsDefaultValue() {
136             return false;
137         }
138         
139     }
140     
141     /**
142      * Supports properties with non negative int value(>0)
143      */

144     public static class PrimitivePositive extends Primitive {
145         public PrimitivePositive(AXIComponent component, String JavaDoc property, String JavaDoc dispName,
146                 String JavaDoc desc) throws NoSuchMethodException JavaDoc {
147             super(component,property,dispName,desc);
148         }
149         @Override JavaDoc
150         protected int getLowerLimit() {
151             return 1;
152         }
153     }
154     
155 }
156
Popular Tags