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: NumberProperty.java,v 1.4 2004/09/22 14:32:50 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: * Base class for number properties.
031: *
032: * @author $Author: jesper $
033: * @version $Revision: 1.4 $
034: */
035: public class NumberProperty extends ValueHandlerProperty {
036: private long minValue;
037: private long maxValue;
038: private int preferredDigitCount;
039:
040: /**
041: * Constructor.
042: *
043: * @param group the property group
044: * @param name the property name
045: * @param cl the property type
046: * @param description the property description
047: * @param minValue the smallest value that this property can have
048: * @param maxValue the largest value that this property can have
049: * @param preferredDigitCount the preferred number of digits to allocate space for in an editor for a property value
050: * @param valueHandler handles values for this property
051: */
052: public NumberProperty(PropertyGroup group, String name, Class cl,
053: String description, long minValue, long maxValue,
054: int preferredDigitCount, PropertyValueHandler valueHandler) {
055: super (group, name, cl, description, valueHandler);
056: this .minValue = minValue;
057: this .maxValue = maxValue;
058: this .preferredDigitCount = preferredDigitCount;
059: }
060:
061: /**
062: * Returns the preferred number of digits to allocate space for in an editor for a property value
063: *
064: * @return the preferred number of digits to allocate space for in an editor for a property value
065: */
066: public int getPreferredDigitCount() {
067: return preferredDigitCount;
068: }
069:
070: /**
071: * Returns the smallest value that this property can have.
072: *
073: * @return the smallest value that this property can have
074: */
075: public long getMinValue() {
076: return minValue;
077: }
078:
079: /**
080: * Returns the largest value that this property can have.
081: *
082: * @return the largest value that this property can have
083: */
084: public long getMaxValue() {
085: return maxValue;
086: }
087:
088: /**
089: * Returns the long value of this property in a value container.
090: *
091: * @param valueContainer the value container
092: * @return the long value of this property
093: */
094: public long getLongValue(Object valueContainer) {
095: Object value = getValue(valueContainer);
096: return value == null ? Math.max(0, minValue)
097: : ((Number) getValue(valueContainer)).longValue();
098: }
099:
100: public boolean canBeAssiged(Object value) {
101: if (!super .canBeAssiged(value))
102: return false;
103:
104: long v = ((Number) value).longValue();
105: return minValue <= v && maxValue >= v;
106: }
107:
108: }
|