KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gcc > properties > FloatProperty


1 /*
2  * Copyright 2004 The Apache Software Foundation or its licensors, as
3  * applicable.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19 package gcc.properties;
20
21 public class FloatProperty extends PropertyType
22 {
23     private float _defaultValue = 0;
24
25     private float _minimumValue = 0;
26
27     private float _maximumValue = Float.MAX_VALUE;
28
29     public FloatProperty(Class componentClass, String propertyName)
30     {
31         super(componentClass, propertyName);
32     }
33
34     public FloatProperty displayName(String displayName)
35     {
36         setDisplayName(displayName);
37         return this;
38     }
39
40     public FloatProperty displayOnlyIf(PropertyType other, String value)
41     {
42         setDisplayOnlyIf(other, value);
43         return this;
44     }
45
46     public FloatProperty description(String description)
47     {
48         setDescription(description);
49         return this;
50     }
51
52     public FloatProperty consoleHelp(String consoleHelp)
53     {
54         setConsoleHelp(consoleHelp);
55         return this;
56     }
57
58     public FloatProperty sortOrder(int sortOrder)
59     {
60         setSortOrder(sortOrder);
61         return this;
62     }
63
64     public FloatProperty defaultValue(float defaultValue)
65     {
66         _defaultValue = defaultValue;
67         return this;
68     }
69
70     public FloatProperty minimumValue(float minimumValue)
71     {
72         _minimumValue = minimumValue;
73         return this;
74     }
75
76     public FloatProperty maximumValue(float maximumValue)
77     {
78         _maximumValue = maximumValue;
79         return this;
80     }
81
82     public float getDefaultValue()
83     {
84         return _defaultValue;
85     }
86
87     public String getDefaultValueAsString()
88     {
89         return String.valueOf(_defaultValue);
90     }
91
92     public float getMinimumValue()
93     {
94         return _minimumValue;
95     }
96
97     public float getMaximumValue()
98     {
99         return _maximumValue;
100     }
101
102     public float getFloat()
103     {
104         return getFloat(null, getComponentProperties());
105     }
106
107     public float getFloat(String instanceName, PropertyMap props)
108     {
109         float n;
110         boolean ok = true;
111         String value = props.getProperty(_propertyName, String.valueOf(_defaultValue));
112         try
113         {
114             n = Float.parseFloat(value);
115         }
116         catch (NumberFormatException ex)
117         {
118             ok = false;
119             n = 0;
120         }
121         if (n < _minimumValue || n > _maximumValue)
122         {
123             ok = false;
124         }
125         if (! ok)
126         {
127             badPropertyValue(instanceName, value, expectedNumberInRange(_minimumValue, _maximumValue));
128         }
129         logPropertyValue(instanceName, value, n == _defaultValue);
130         return n;
131     }
132 }
133
Popular Tags