001: /*--------------------------------------------------------------------------*
002: | Copyright (C) 2006 Christopher Kohlhaas |
003: | |
004: | This program is free software; you can redistribute it and/or modify |
005: | it under the terms of the GNU General Public License as published by the |
006: | Free Software Foundation. A copy of the license has been included with |
007: | these distribution in the COPYING file, if not go to www.fsf.org |
008: | |
009: | As a special exception, you are granted the permissions to link this |
010: | program with every library, which license fulfills the Open Source |
011: | Definition as published by the Open Source Initiative (OSI). |
012: *--------------------------------------------------------------------------*/
013: package org.rapla.components.calendar;
014:
015: import java.awt.ComponentOrientation;
016:
017: /**
018: * The NumberField only accepts integer values.
019: * <strong>Warning!<strong> Currently only Longs are supported.
020: */
021:
022: public class NumberField extends AbstractBlockField {
023: private static final long serialVersionUID = 1L;
024:
025: static char[] m_separators = new char[0];
026: Number m_number;
027: Number m_minimum;
028: Number m_maximum;
029:
030: /**
031: * @return Returns the m_blockStepSize.
032: */
033: public int getBlockStepSize() {
034: return m_blockStepSize;
035: }
036:
037: /**
038: * @param stepSize The m_blockStepSize to set.
039: */
040: public void setBlockStepSize(int stepSize) {
041: m_blockStepSize = stepSize;
042: }
043:
044: /**
045: * @return Returns the m_stepSize.
046: */
047: public int getStepSize() {
048: return m_stepSize;
049: }
050:
051: /**
052: * @param size The m_stepSize to set.
053: */
054: public void setStepSize(int size) {
055: m_stepSize = size;
056: }
057:
058: int m_stepSize;
059: int m_blockStepSize;
060: int m_maxLength;
061: boolean m_isNullPermitted = true;
062:
063: public NumberField() {
064: setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
065: updateColumns();
066: }
067:
068: public NumberField(Number minimum, Number maximum, int stepSize,
069: int blockStepSize) {
070: this ();
071: setStepSize(stepSize);
072: setBlockStepSize(blockStepSize);
073: setMinimum(minimum);
074: setMaximum(maximum);
075: }
076:
077: public void setMinimum(Number minimum) {
078: m_minimum = minimum;
079: updateColumns();
080: }
081:
082: public void setMaximum(Number maximum) {
083: m_maximum = maximum;
084: updateColumns();
085: }
086:
087: public Number getMinimum() {
088: return m_minimum;
089: }
090:
091: public Number getMaximum() {
092: return m_maximum;
093: }
094:
095: private void updateColumns() {
096: if (m_maximum != null && m_minimum != null) {
097: if ((Math.abs(m_maximum.longValue())) > (Math.abs(m_minimum
098: .longValue())) * 10)
099: m_maxLength = m_maximum.toString().length();
100: else
101: m_maxLength = m_minimum.toString().length();
102: setColumns(m_maxLength);
103: } else {
104: m_maxLength = 100;
105: setColumns(4);
106: }
107: }
108:
109: public boolean isNullPermitted() {
110: return m_isNullPermitted;
111: }
112:
113: public void setNullPermitted(boolean isNullPermitted) {
114: m_isNullPermitted = isNullPermitted;
115: if (m_number == null && !isNullPermitted)
116: m_number = new Double(defaultValue());
117: }
118:
119: private long defaultValue() {
120: if (m_minimum != null && m_minimum.longValue() > 0)
121: return m_minimum.longValue();
122: else if (m_maximum != null && m_maximum.longValue() < 0)
123: return m_maximum.longValue();
124: return 0;
125: }
126:
127: public void setNumber(Number number) {
128: updateNumber(number);
129: m_oldText = getText();
130: fireValueChanged();
131: }
132:
133: private void updateNumber(Number number) {
134: m_number = number;
135: String text;
136: if (number != null) {
137: text = String.valueOf(number.longValue());
138: } else {
139: text = "";
140: }
141: setText(text);
142: }
143:
144: public void increase() {
145: changeSelectedBlock(new int[1], 0, "", 1);
146: }
147:
148: public void decrease() {
149: changeSelectedBlock(new int[1], 0, "", -1);
150: }
151:
152: public Number getNumber() {
153: return m_number;
154: }
155:
156: public boolean allowsNegative() {
157: return (m_minimum == null || m_minimum.longValue() < 0);
158: }
159:
160: protected char[] getSeparators() {
161: return m_separators;
162: }
163:
164: protected boolean isSeparator(char c) {
165: return false;
166: }
167:
168: protected void changeSelectedBlock(int[] blocks, int block,
169: String selected, int count) {
170: long longValue = ((m_number != null) ? m_number.longValue()
171: : defaultValue());
172: if (count == 1)
173: longValue = longValue + m_stepSize;
174: if (count == -1)
175: longValue = longValue - m_stepSize;
176: if (count == 10)
177: longValue = longValue + m_blockStepSize;
178: if (count == -10)
179: longValue = longValue - m_blockStepSize;
180:
181: if (m_minimum != null && longValue < m_minimum.longValue())
182: longValue = m_minimum.longValue();
183: if (m_maximum != null && longValue > m_maximum.longValue())
184: longValue = m_maximum.longValue();
185: updateNumber(new Long(longValue));
186: calcBlocks(blocks);
187: markBlock(blocks, block);
188: }
189:
190: public boolean blocksValid() {
191: try {
192: String text = getText();
193: if (text.length() == 0) {
194: if (isNullPermitted())
195: m_number = null;
196: return true;
197: }
198:
199: long newLong = Long.parseLong(text);
200: if ((m_minimum == null || newLong >= m_minimum.longValue())
201: && (m_maximum == null || newLong <= m_maximum
202: .longValue())) {
203: m_number = new Long(newLong);
204: return true;
205: }
206: } catch (NumberFormatException e) {
207: if (isNullPermitted())
208: m_number = null;
209: }
210: return false;
211: }
212:
213: protected int blockCount() {
214: return 1;
215: }
216:
217: protected int maxBlockLength(int block) {
218: return m_maxLength;
219: }
220:
221: protected boolean isValidChar(char c) {
222: return (Character.isDigit(c) || (allowsNegative() && c == '-'));
223: }
224:
225: }
|