001: /*
002: * @(#)SpinnerPointModel.java 4/8/2007
003: *
004: * Copyright 2002 - 2007 JIDE Software Inc. All rights reserved.
005: */
006:
007: package com.jidesoft.spinner;
008:
009: import javax.swing.*;
010: import java.awt.*;
011:
012: /**
013: * @author Nako Ruru
014: */
015: public class SpinnerPointModel extends AbstractSpinnerModel {
016:
017: public final static int FIELD_X = 0;
018: public final static int FIELD_Y = 1;
019:
020: private Point point;
021: private int field = FIELD_X;
022:
023: /**
024: * Create a default <code>SpinnerPointModel</code>
025: */
026: public SpinnerPointModel() {
027: this (null);
028: }
029:
030: /**
031: * Create a <code>SpinnerPointModel</code> with a specified <code>Point</code>
032: *
033: * @param point this specified<code>Point</code>
034: */
035: public SpinnerPointModel(Point point) {
036: this .point = point == null ? new Point() : point;
037: }
038:
039: /**
040: * The <i>current element</i> of the sequence. This element is usually
041: * displayed by the <code>editor</code> part of a <code>JSpinner</code>.
042: *
043: * @return the current spinner value.
044: * @see #setValue
045: */
046: public Object getValue() {
047: return point;
048: }
049:
050: /**
051: * Changes current value of the model, typically this value is displayed
052: * by the <code>editor</code> part of a <code>JSpinner</code>.
053: * If the <code>SpinnerModel</code> implementation doesn't support
054: * the specified value then an <code>IllegalArgumentException</code>
055: * is thrown. For example a <code>SpinnerModel</code> for numbers might
056: * only support values that are integer multiples of ten. In
057: * that case, <code>model.setValue(new Number(11))</code>
058: * would throw an exception.
059: *
060: * @param value new value
061: * @throws IllegalArgumentException if <code>value</code> isn't allowed
062: * @see #getValue
063: */
064: public void setValue(Object value) {
065: setPoint((Point) value);
066: }
067:
068: /**
069: * The <i>current element</i> of the sequence. This element is usually
070: * displayed by the <code>editor</code> part of a <code>JSpinner</code>.
071: *
072: * @return the current spinner value.
073: * @see #setPoint(Point)
074: * @see #getValue()
075: */
076: public Point getPoint() {
077: return point;
078: }
079:
080: /**
081: * @param point
082: */
083: public void setPoint(Point point) {
084: if (!this .point.equals(point)) {
085: this .point = point;
086: fireStateChanged();
087: }
088: }
089:
090: /**
091: * @return
092: */
093: public int getField() {
094: return field;
095: }
096:
097: /**
098: * @param field
099: */
100: public void setField(int field) {
101: this .field = field;
102: }
103:
104: /**
105: * Return the object in the sequence that comes after the object returned
106: * by <code>getValue()</code>. If the end of the sequence has been reached
107: * then return null. Calling this method does not effect <code>value</code>.
108: *
109: * @return the next legal value or null if one doesn't exist
110: * @see #getValue
111: * @see #getPreviousValue
112: */
113: public Object getNextValue() {
114: Point p = (Point) point.clone();
115: if (field == FIELD_X) {
116: p.x++;
117: } else {
118: p.y++;
119: }
120: return p;
121: }
122:
123: /**
124: * Return the object in the sequence that comes before the object returned
125: * by <code>getValue()</code>. If the end of the sequence has been reached then
126: * return null. Calling this method does not effect <code>value</code>.
127: *
128: * @return the previous legal value or null if one doesn't exist
129: * @see #getValue
130: * @see #getNextValue
131: */
132: public Object getPreviousValue() {
133: Point p = (Point) point.clone();
134: if (field == FIELD_X) {
135: p.x--;
136: } else {
137: p.y--;
138: }
139: return p;
140: }
141: }
|