KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gcc > properties > DoubleProperty


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