01: /*
02: *
03: * JMoney - A Personal Finance Manager
04: * Copyright (c) 2004 Nigel Westbury <westbury@users.sourceforge.net>
05: *
06: *
07: * This program is free software; you can redistribute it and/or modify
08: * it under the terms of the GNU General Public License as published by
09: * the Free Software Foundation; either version 2 of the License, or
10: * (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public License
18: * along with this program; if not, write to the Free Software
19: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20: *
21: */
22:
23: package net.sf.jmoney.fields;
24:
25: import net.sf.jmoney.model2.Commodity;
26: import net.sf.jmoney.model2.ExtendableObject;
27: import net.sf.jmoney.model2.PropertyControlFactory;
28: import net.sf.jmoney.model2.ScalarPropertyAccessor;
29:
30: /**
31: * A control factory to edit an amount of a commodity.
32: *
33: * @author Nigel Westbury
34: * @author Johann Gyger
35: */
36: public abstract class AmountControlFactory extends
37: PropertyControlFactory<Long> {
38:
39: @Override
40: public String formatValueForMessage(
41: ExtendableObject extendableObject,
42: ScalarPropertyAccessor<? extends Long> propertyAccessor) {
43: Long amount = extendableObject
44: .getPropertyValue(propertyAccessor);
45: if (amount == null) {
46: return "none";
47: } else {
48: return "'"
49: + getCommodity(extendableObject).format(
50: amount.longValue()) + "'";
51: }
52: }
53:
54: @Override
55: public String formatValueForTable(
56: ExtendableObject extendableObject,
57: ScalarPropertyAccessor<? extends Long> propertyAccessor) {
58: Long amount = extendableObject
59: .getPropertyValue(propertyAccessor);
60: if (amount == null) {
61: return "";
62: } else {
63: // TODO: clean this up when we have a plan for incomplete data.
64: // For time being, use the default currency for formatting if the user
65: // has not set the currency for the account.
66: Commodity commodity = getCommodity(extendableObject);
67: if (commodity == null) {
68: commodity = extendableObject.getSession()
69: .getDefaultCurrency();
70: }
71: return commodity.format(amount.longValue());
72: }
73: }
74:
75: public Long getDefaultValue() {
76: return 0L;
77: }
78:
79: public boolean isEditable() {
80: return true;
81: }
82:
83: protected abstract Commodity getCommodity(ExtendableObject object);
84: }
|