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.stocks;
024:
025: import net.sf.jmoney.fields.AmountControlFactory;
026: import net.sf.jmoney.fields.AmountEditor;
027: import net.sf.jmoney.fields.CurrencyControlFactory;
028: import net.sf.jmoney.fields.IntegerControlFactory;
029: import net.sf.jmoney.model2.Commodity;
030: import net.sf.jmoney.model2.CommodityInfo;
031: import net.sf.jmoney.model2.Currency;
032: import net.sf.jmoney.model2.ExtendableObject;
033: import net.sf.jmoney.model2.ExtendablePropertySet;
034: import net.sf.jmoney.model2.IExtendableObjectConstructors;
035: import net.sf.jmoney.model2.IObjectKey;
036: import net.sf.jmoney.model2.IPropertyControl;
037: import net.sf.jmoney.model2.IPropertyControlFactory;
038: import net.sf.jmoney.model2.IPropertySetInfo;
039: import net.sf.jmoney.model2.IReferenceControlFactory;
040: import net.sf.jmoney.model2.IValues;
041: import net.sf.jmoney.model2.ListKey;
042: import net.sf.jmoney.model2.PropertySet;
043: import net.sf.jmoney.model2.ReferencePropertyAccessor;
044: import net.sf.jmoney.model2.ScalarPropertyAccessor;
045: import net.sf.jmoney.model2.SessionChangeAdapter;
046:
047: import org.eclipse.swt.widgets.Composite;
048:
049: /**
050: * The class descriptor for the Bond class.
051: * <P>
052: * @author Nigel Westbury
053: *
054: */
055: public class BondInfo implements IPropertySetInfo {
056:
057: private static ExtendablePropertySet<Bond> propertySet = PropertySet
058: .addDerivedFinalPropertySet(Bond.class, "Bond",
059: CommodityInfo.getPropertySet(),
060: new IExtendableObjectConstructors<Bond>() {
061:
062: public Bond construct(IObjectKey objectKey,
063: ListKey parentKey) {
064: return new Bond(objectKey, parentKey);
065: }
066:
067: public Bond construct(IObjectKey objectKey,
068: ListKey parentKey, IValues values) {
069: return new Bond(
070: objectKey,
071: parentKey,
072: values.getScalarValue(CommodityInfo
073: .getNameAccessor()),
074: values
075: .getReferencedObjectKey(BondInfo
076: .getCurrencyAccessor()),
077: values
078: .getScalarValue(BondInfo
079: .getRedemptionValueAccessor()),
080: values.getScalarValue(BondInfo
081: .getInterestRateAccessor()),
082: values);
083: }
084: });
085:
086: private static ReferencePropertyAccessor<Currency> currencyAccessor;
087: private static ScalarPropertyAccessor<Long> redemptionValueAccessor;
088: private static ScalarPropertyAccessor<Integer> interestRateAccessor;
089:
090: public PropertySet registerProperties() {
091:
092: IPropertyControlFactory<Integer> integerControlFactory = new IntegerControlFactory();
093: IReferenceControlFactory<Bond, Currency> currencyControlFactory = new CurrencyControlFactory<Bond>() {
094: public IObjectKey getObjectKey(Bond parentObject) {
095: return parentObject.currencyKey;
096: }
097: };
098:
099: IPropertyControlFactory<Long> amountControlFactory = new AmountControlFactory() {
100:
101: @Override
102: protected Commodity getCommodity(
103: ExtendableObject extendableObject) {
104: /*
105: * All properties in this object that are amounts are in the
106: * currency in which this bond is denominated. We therefore
107: * return the currency in which this bond is denominated.
108: */
109: return ((Bond) extendableObject).getCurrency();
110: }
111:
112: public IPropertyControl createPropertyControl(
113: Composite parent,
114: ScalarPropertyAccessor<Long> propertyAccessor) {
115: final AmountEditor editor = new AmountEditor(parent,
116: propertyAccessor, this );
117:
118: // The format of the amount will change if either
119: // the currency property of this bond changes or if
120: // any of the properties in the currency change.
121: editor.setListener(new SessionChangeAdapter() {
122: @Override
123: public void objectChanged(
124: ExtendableObject changedObject,
125: ScalarPropertyAccessor changedProperty,
126: Object oldValue, Object newValue) {
127: Bond bond = (Bond) editor.getObject();
128: if (bond == null) {
129: return;
130: }
131: // Has the currency property of the bond changed?
132: if (changedObject == bond
133: && changedProperty == BondInfo
134: .getCurrencyAccessor()) {
135: editor.updateCommodity(bond.getCurrency());
136: }
137: // If any property in the commodity object changed then
138: // the format of the amount might also change.
139: if (changedObject == bond.getCurrency()) {
140: editor.updateCommodity(bond.getCurrency());
141: }
142: }
143: });
144:
145: return editor;
146: }
147: };
148:
149: currencyAccessor = propertySet
150: .addProperty("currency", StocksPlugin
151: .getResourceString("PropertyDesc.currency"),
152: Currency.class, 2, 20, currencyControlFactory,
153: null);
154: redemptionValueAccessor = propertySet
155: .addProperty(
156: "redemptionValue",
157: StocksPlugin
158: .getResourceString("PropertyDesc.redemptionValue"),
159: Long.class, 2, 20, amountControlFactory, null);
160: interestRateAccessor = propertySet
161: .addProperty(
162: "interestRate",
163: StocksPlugin
164: .getResourceString("PropertyDesc.interestRate"),
165: Integer.class, 1, 15, integerControlFactory,
166: null);
167:
168: return propertySet;
169: }
170:
171: /**
172: * @return
173: */
174: public static ReferencePropertyAccessor<Currency> getCurrencyAccessor() {
175: return currencyAccessor;
176: }
177:
178: /**
179: * @return
180: */
181: public static ScalarPropertyAccessor<Long> getRedemptionValueAccessor() {
182: return redemptionValueAccessor;
183: }
184:
185: /**
186: * @return
187: */
188: public static ScalarPropertyAccessor<Integer> getInterestRateAccessor() {
189: return interestRateAccessor;
190: }
191: }
|