001: /*
002: *
003: * JMoney - A Personal Finance Manager
004: * Copyright (c) 2004 Nigel Westbury <westbury@users.sourceforge.net>
005: *
006: *
007: * This program is free software; you can redistribute it and/or modify
008: * it under the terms of the GNU General Public License as published by
009: * the Free Software Foundation; either version 2 of the License, or
010: * (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
020: *
021: */
022:
023: package net.sf.jmoney.fields;
024:
025: import java.util.Comparator;
026:
027: import net.sf.jmoney.model2.ExtendableObject;
028: import net.sf.jmoney.model2.IPropertyControl;
029: import net.sf.jmoney.model2.IPropertyControlFactory;
030: import net.sf.jmoney.model2.ScalarPropertyAccessor;
031:
032: import org.eclipse.swt.widgets.Composite;
033: import org.eclipse.swt.widgets.Control;
034: import org.eclipse.swt.widgets.Text;
035:
036: /**
037: * A control factory for integer properties.
038: *
039: * @author Nigel Westbury
040: */
041: public class IntegerControlFactory implements
042: IPropertyControlFactory<Integer> {
043:
044: public IPropertyControl createPropertyControl(Composite parent,
045: ScalarPropertyAccessor<Integer> propertyAccessor) {
046: return new IntegerEditor(parent, 0, propertyAccessor);
047: }
048:
049: public String formatValueForMessage(
050: ExtendableObject extendableObject,
051: ScalarPropertyAccessor<? extends Integer> propertyAccessor) {
052: Integer value = extendableObject
053: .getPropertyValue(propertyAccessor);
054: return value.toString();
055: }
056:
057: public String formatValueForTable(
058: ExtendableObject extendableObject,
059: ScalarPropertyAccessor<? extends Integer> propertyAccessor) {
060: Integer value = extendableObject
061: .getPropertyValue(propertyAccessor);
062: return value.toString();
063: }
064:
065: public Integer getDefaultValue() {
066: return 0;
067: }
068:
069: public boolean isEditable() {
070: return true;
071: }
072:
073: public Comparator<Integer> getComparator() {
074: return new Comparator<Integer>() {
075: public int compare(Integer value1, Integer value2) {
076: return value1.compareTo(value2);
077: }
078: };
079: }
080: }
081:
082: /**
083: * A property control to handle generic integer input.
084: *
085: * @author Nigel Westbury
086: */
087: class IntegerEditor implements IPropertyControl {
088:
089: private ExtendableObject extendableObject;
090:
091: private ScalarPropertyAccessor<Integer> propertyAccessor;
092:
093: private Text propertyControl;
094:
095: public IntegerEditor(Composite parent,
096: ScalarPropertyAccessor<Integer> propertyAccessor) {
097: propertyControl = new Text(parent, 0);
098: this .propertyAccessor = propertyAccessor;
099: }
100:
101: public IntegerEditor(Composite parent, int style,
102: ScalarPropertyAccessor<Integer> propertyAccessor) {
103: propertyControl = new Text(parent, style);
104: this .propertyAccessor = propertyAccessor;
105: }
106:
107: public void load(ExtendableObject object) {
108: extendableObject = object;
109:
110: if (object == null) {
111: propertyControl.setText("");
112: } else {
113: Integer value = object.getPropertyValue(propertyAccessor);
114: propertyControl.setText(value == null ? "" : value
115: .toString());
116: }
117: propertyControl.setEnabled(object != null);
118: }
119:
120: public void save() {
121: try {
122: Integer value = Integer.getInteger(propertyControl
123: .getText());
124: extendableObject.setPropertyValue(propertyAccessor, value);
125: } catch (NumberFormatException e) {
126: // TODO: Is there a better way of handling this?
127: // For time being, just don't update the property.
128: }
129: }
130:
131: /* (non-Javadoc)
132: * @see net.sf.jmoney.model2.IPropertyControl#getControl()
133: */
134: public Control getControl() {
135: return propertyControl;
136: }
137:
138: }
|