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.reconciliation;
024:
025: import net.sf.jmoney.model2.EntryInfo;
026: import net.sf.jmoney.model2.ExtendableObject;
027: import net.sf.jmoney.model2.ExtensionPropertySet;
028: import net.sf.jmoney.model2.IExtensionObjectConstructors;
029: import net.sf.jmoney.model2.IPropertyControl;
030: import net.sf.jmoney.model2.IPropertyControlFactory;
031: import net.sf.jmoney.model2.IPropertySetInfo;
032: import net.sf.jmoney.model2.IValues;
033: import net.sf.jmoney.model2.PropertyControlFactory;
034: import net.sf.jmoney.model2.PropertySet;
035: import net.sf.jmoney.model2.ScalarPropertyAccessor;
036:
037: import org.eclipse.swt.SWT;
038: import org.eclipse.swt.widgets.Composite;
039: import org.eclipse.swt.widgets.Control;
040: import org.eclipse.swt.widgets.Label;
041: import org.eclipse.swt.widgets.Text;
042:
043: /**
044: * Provides the metadata for the extra properties added to each
045: * entry by this plug-in.
046: *
047: * @author Nigel Westbury
048: */
049: public class ReconciliationEntryInfo implements IPropertySetInfo {
050:
051: private static ExtensionPropertySet<ReconciliationEntry> propertySet = PropertySet
052: .addExtensionPropertySet(
053: ReconciliationEntry.class,
054: EntryInfo.getPropertySet(),
055: new IExtensionObjectConstructors<ReconciliationEntry>() {
056:
057: public ReconciliationEntry construct(
058: ExtendableObject extendedObject) {
059: return new ReconciliationEntry(
060: extendedObject);
061: }
062:
063: public ReconciliationEntry construct(
064: ExtendableObject extendedObject,
065: IValues values) {
066: return new ReconciliationEntry(
067: extendedObject,
068: values
069: .getScalarValue(getStatusAccessor()),
070: values
071: .getScalarValue(getStatementAccessor()),
072: values
073: .getScalarValue(getUniqueIdAccessor()));
074: }
075: });
076:
077: private static ScalarPropertyAccessor<Integer> statusAccessor = null;
078: private static ScalarPropertyAccessor<BankStatement> statementAccessor = null;
079: private static ScalarPropertyAccessor<String> uniqueIdAccessor = null;
080:
081: public PropertySet registerProperties() {
082: class NonEditableTextControlFactory extends
083: PropertyControlFactory<String> {
084:
085: public IPropertyControl createPropertyControl(
086: Composite parent,
087: final ScalarPropertyAccessor<String> propertyAccessor) {
088:
089: // Property is not editable
090: final Label control = new Label(parent, SWT.NONE);
091: return new IPropertyControl() {
092:
093: public Control getControl() {
094: return control;
095: }
096:
097: public void load(ExtendableObject object) {
098: String text = object
099: .getPropertyValue(propertyAccessor);
100: if (text == null) {
101: control.setText("");
102: } else {
103: control.setText(text);
104: }
105: }
106:
107: public void save() {
108: /*
109: * The property is not editable so there is nothing
110: * to do here.
111: */
112: }
113: };
114: }
115:
116: @Override
117: public String formatValueForMessage(
118: ExtendableObject extendableObject,
119: ScalarPropertyAccessor<? extends String> propertyAccessor) {
120: String value = extendableObject
121: .getPropertyValue(propertyAccessor);
122: return (value == null) ? "<blank>" : value;
123: }
124:
125: @Override
126: public String formatValueForTable(
127: ExtendableObject extendableObject,
128: ScalarPropertyAccessor<? extends String> propertyAccessor) {
129: String value = extendableObject
130: .getPropertyValue(propertyAccessor);
131: return (value == null) ? "" : value;
132: }
133:
134: public String getDefaultValue() {
135: return null;
136: }
137:
138: public boolean isEditable() {
139: return true;
140: }
141: }
142:
143: IPropertyControlFactory<BankStatement> statementControlFactory = new PropertyControlFactory<BankStatement>() {
144: public IPropertyControl createPropertyControl(
145: Composite parent,
146: final ScalarPropertyAccessor<BankStatement> propertyAccessor) {
147: final Text control = new Text(parent, SWT.NONE);
148: return new IPropertyControl() {
149:
150: private ExtendableObject object;
151:
152: public Control getControl() {
153: return control;
154: }
155:
156: public void load(ExtendableObject object) {
157: this .object = object;
158: BankStatement statement = object
159: .getPropertyValue(propertyAccessor);
160: if (statement == null) {
161: control.setText("");
162: } else {
163: control.setText(statement.toString());
164: }
165: }
166:
167: public void save() {
168: // TODO: make this more robust.
169: // And the control is better if a Combo.
170: String text = control.getText();
171: BankStatement value;
172: if (text.length() == 0) {
173: value = null;
174: } else {
175: value = new BankStatement(control.getText());
176: }
177: object
178: .setPropertyValue(propertyAccessor,
179: value);
180: }
181: };
182: }
183:
184: @Override
185: public String formatValueForMessage(
186: ExtendableObject extendableObject,
187: ScalarPropertyAccessor<? extends BankStatement> propertyAccessor) {
188: BankStatement statement = extendableObject
189: .getPropertyValue(propertyAccessor);
190: if (statement == null) {
191: return "unreconciled";
192: } else {
193: return statement.toString();
194: }
195: }
196:
197: @Override
198: public String formatValueForTable(
199: ExtendableObject extendableObject,
200: ScalarPropertyAccessor<? extends BankStatement> propertyAccessor) {
201: BankStatement statement = extendableObject
202: .getPropertyValue(propertyAccessor);
203: if (statement == null) {
204: return "unreconciled";
205: } else {
206: return statement.toString();
207: }
208: }
209:
210: public BankStatement getDefaultValue() {
211: // By default, not on any statement (unreconciled)
212: return null;
213: }
214:
215: public boolean isEditable() {
216: return true;
217: }
218: };
219:
220: // TODO: correct localized text:
221: statusAccessor = propertySet.addProperty("status",
222: ReconciliationPlugin
223: .getResourceString("Entry.statusShort"),
224: Integer.class, 1, 30, new StatusControlFactory(), null);
225: statementAccessor = propertySet.addProperty("statement",
226: ReconciliationPlugin
227: .getResourceString("Entry.statementShort"),
228: BankStatement.class, 1, 80, statementControlFactory,
229: null);
230: uniqueIdAccessor = propertySet.addProperty("uniqueId",
231: ReconciliationPlugin
232: .getResourceString("Entry.uniqueIdShort"),
233: String.class, 1, 80,
234: new NonEditableTextControlFactory(), null);
235:
236: return propertySet;
237: }
238:
239: /**
240: * @return
241: */
242: public static ExtensionPropertySet<ReconciliationEntry> getPropertySet() {
243: return propertySet;
244: }
245:
246: /**
247: * @return
248: */
249: public static ScalarPropertyAccessor<Integer> getStatusAccessor() {
250: return statusAccessor;
251: }
252:
253: /**
254: * @return
255: */
256: public static ScalarPropertyAccessor<BankStatement> getStatementAccessor() {
257: return statementAccessor;
258: }
259:
260: /**
261: * @return
262: */
263: public static ScalarPropertyAccessor<String> getUniqueIdAccessor() {
264: return uniqueIdAccessor;
265: }
266: }
|