001: /*
002: * Copyright (C) 2004 NNL Technology AB
003: * Visit www.infonode.net for information about InfoNode(R)
004: * products and how to contact NNL Technology AB.
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
019: * MA 02111-1307, USA.
020: */
021:
022: // $Id: FloatProperty.java,v 1.5 2005/02/16 11:28:15 jesper Exp $
023: package net.infonode.properties.types;
024:
025: import net.infonode.properties.base.PropertyGroup;
026: import net.infonode.properties.util.PropertyValueHandler;
027: import net.infonode.properties.util.ValueHandlerProperty;
028:
029: /**
030: * A float property.
031: *
032: * @author $Author: jesper $
033: * @version $Revision: 1.5 $
034: */
035: public class FloatProperty extends ValueHandlerProperty {
036: private float minValue;
037: private float maxValue;
038: private int preferredDigitCount;
039: private float preferredDelta;
040:
041: /**
042: * Constructor.
043: * Creates an unbounded float property.
044: *
045: * @param group the property group
046: * @param name the property name
047: * @param description the property description
048: * @param valueHandler handles values for this property
049: */
050: public FloatProperty(PropertyGroup group, String name,
051: String description, PropertyValueHandler valueHandler) {
052: this (group, name, description, valueHandler, Float.MIN_VALUE,
053: Float.MAX_VALUE);
054: }
055:
056: /**
057: * Constructor.
058: *
059: * @param group the property group
060: * @param name the property name
061: * @param description the property description
062: * @param valueHandler handles values for this property
063: * @param minValue the smallest value that this property can have
064: * @param maxValue the largest value that this property can have
065: */
066: public FloatProperty(PropertyGroup group, String name,
067: String description, PropertyValueHandler valueHandler,
068: float minValue, float maxValue) {
069: this (group, name, description, valueHandler, minValue,
070: maxValue, 6, 0.1f);
071: }
072:
073: /**
074: * Constructor.
075: *
076: * @param group the property group
077: * @param name the property name
078: * @param description the property description
079: * @param valueHandler handles values for this property
080: * @param minValue the smallest value that this property can have
081: * @param maxValue the largest value that this property can have
082: * @param preferredDigitCount the preferred number of digits to allocate space for in an editor for a property value
083: * @param preferredDelta the preferred amount to increase and decrease a property value by
084: */
085: public FloatProperty(PropertyGroup group, String name,
086: String description, PropertyValueHandler valueHandler,
087: float minValue, float maxValue, int preferredDigitCount,
088: float preferredDelta) {
089: super (group, name, Float.class, description, valueHandler);
090: this .minValue = minValue;
091: this .maxValue = maxValue;
092: this .preferredDigitCount = preferredDigitCount;
093: this .preferredDelta = preferredDelta;
094: }
095:
096: /**
097: * Returns the preferred amount to increase and decrease a property value by.
098: *
099: * @return the preferred amount to increase and decrease a property value by
100: */
101: public float getPreferredDelta() {
102: return preferredDelta;
103: }
104:
105: /**
106: * Returns the smallest value that this property can have.
107: *
108: * @return the smallest value that this property can have
109: */
110: public float getMinValue() {
111: return minValue;
112: }
113:
114: /**
115: * Returns the largest value that this property can have.
116: *
117: * @return the largest value that this property can have
118: */
119: public float getMaxValue() {
120: return maxValue;
121: }
122:
123: /**
124: * Returns the preferred number of digits to allocate space for in an editor for a property value.
125: *
126: * @return the preferred number of digits to allocate space for in an editor for a property value
127: */
128: public int getPreferredDigitCount() {
129: return preferredDigitCount;
130: }
131:
132: /**
133: * Returns the float value of this property in a value container.
134: *
135: * @param valueContainer the value container
136: * @return the float value of this property
137: */
138: public float get(Object valueContainer) {
139: Object value = getValue(valueContainer);
140: return value == null ? 0 : ((Number) getValue(valueContainer))
141: .floatValue();
142: }
143:
144: /**
145: * Sets the float value of this property in a value container.
146: *
147: * @param valueContainer the value container
148: * @param value the float value
149: */
150: public void set(Object valueContainer, float value) {
151: setValue(valueContainer, new Float(value));
152: }
153:
154: public boolean canBeAssiged(Object value) {
155: if (!super .canBeAssiged(value))
156: return false;
157:
158: float v = ((Number) value).floatValue();
159: return v >= minValue && v <= maxValue;
160: }
161: }
|