001: /*
002: *
003: * JMoney - A Personal Finance Manager
004: * Copyright (c) 2002 Johann Gyger <johann.gyger@switzerland.org>
005: * Copyright (c) 2004 Nigel Westbury <westbury@users.sourceforge.net>
006: *
007: *
008: * This program is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License as published by
010: * the Free Software Foundation; either version 2 of the License, or
011: * (at your option) any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: *
022: */
023:
024: package net.sf.jmoney.model2;
025:
026: import net.sf.jmoney.JMoneyPlugin;
027:
028: /**
029: * An implementation of the IncomeExpenseAccount interface
030: */
031: public class IncomeExpenseAccount extends Account {
032:
033: protected IListManager<IncomeExpenseAccount> subAccounts;
034:
035: /**
036: * True if the entries in this account can be in any account,
037: * false if all the entries must be in the same currency
038: * (in which case the currency property must be set).
039: */
040: private boolean multiCurrency = true;
041:
042: /**
043: * The currency in which all entries in this account are denominated.
044: * This property is not applicable if multiCurrency is true.
045: */
046: protected IObjectKey currencyKey = null;
047:
048: private String fullAccountName = null;
049:
050: public IncomeExpenseAccount(IObjectKey objectKey, ListKey parent,
051: String name,
052: IListManager<IncomeExpenseAccount> subAccounts,
053: boolean multiCurrency, IObjectKey currencyKey,
054: IValues extensionValues) {
055: super (objectKey, parent, name, extensionValues);
056:
057: this .subAccounts = subAccounts;
058: this .multiCurrency = multiCurrency;
059: this .currencyKey = currencyKey;
060: }
061:
062: public IncomeExpenseAccount(IObjectKey objectKey, ListKey parentKey) {
063: super (objectKey, parentKey);
064: this .name = JMoneyPlugin
065: .getResourceString("Account.newAccount");
066: this .subAccounts = objectKey
067: .constructListManager(IncomeExpenseAccountInfo
068: .getSubAccountAccessor());
069: }
070:
071: @Override
072: protected String getExtendablePropertySetId() {
073: return "net.sf.jmoney.category";
074: }
075:
076: @Override
077: public String getFullAccountName() {
078: if (fullAccountName == null) {
079: fullAccountName = name;
080: Account ancestorCategory = getParent();
081: while (ancestorCategory != null) {
082: fullAccountName = ancestorCategory.getName() + ":"
083: + fullAccountName;
084: ancestorCategory = ancestorCategory.getParent();
085: }
086: }
087: return fullAccountName;
088: }
089:
090: // TODO: use debugger to see if this version is called
091: // when the Method object references the version in the
092: // abstract base class. If not then this is broke.
093: @Override
094: public void setName(String name) {
095: super .setName(name);
096: fullAccountName = null;
097: }
098:
099: public boolean isMultiCurrency() {
100: return multiCurrency;
101: }
102:
103: public Currency getCurrency() {
104: return currencyKey == null ? null : (Currency) currencyKey
105: .getObject();
106: }
107:
108: public void setMultiCurrency(boolean multiCurrency) {
109: boolean oldMultiCurrency = this .multiCurrency;
110: this .multiCurrency = multiCurrency;
111: /* No, this is not good code to have here. Firstly we set the currency field but we
112: * don't call processPropertyChange, so no listeners are told, the property may not be
113: * stored in the database etc.
114: *
115: * But also, changing properties other than the property set by the user is bad news.
116: * It is upto the user to ensure the properties are set to valid values. We really need
117: * to get the dependency code working, so the currency property is properly indicated
118: * as being appropriate only if not multi-currency.
119: *
120: // When turning off multi-currency, we must set an appropriate
121: // currency.
122: if (!multiCurrency && currencyKey == null) {
123: // TODO: Look at the entries in the account and set the
124: // account as appropriate.
125: // For time being, set to default currency.
126: currencyKey = getSession().getDefaultCurrency().getObjectKey();
127: }
128: */
129: // Notify the change manager.
130: processPropertyChange(IncomeExpenseAccountInfo
131: .getMultiCurrencyAccessor(), new Boolean(
132: oldMultiCurrency), new Boolean(multiCurrency));
133:
134: }
135:
136: public void setCurrency(Currency currency) {
137: Currency oldCurrency = getCurrency();
138: this .currencyKey = currency.getObjectKey();
139:
140: // Notify the change manager.
141: processPropertyChange(IncomeExpenseAccountInfo
142: .getCurrencyAccessor(), oldCurrency, currency);
143: }
144:
145: /**
146: * @return Commodity represented by the amount in the given entry
147: */
148: @Override
149: public Commodity getCommodity(Entry entry) {
150: // Income and expense accounts may be either single currency or
151: // multiple currency.
152: if (!multiCurrency) {
153: return getCurrency();
154: } else {
155: // This is a multi-currency account, so we look to the entry.
156: return entry.getIncomeExpenseCurrency();
157: }
158: }
159:
160: @Override
161: public ObjectCollection<IncomeExpenseAccount> getSubAccountCollection() {
162: return new ObjectCollection<IncomeExpenseAccount>(subAccounts,
163: this , IncomeExpenseAccountInfo.getSubAccountAccessor());
164: }
165:
166: /**
167: * Creates an sub-income/expense category to this income/expense category.
168: *
169: * @return a new sub-account
170: */
171: public IncomeExpenseAccount createSubAccount() {
172: return getSubAccountCollection().createNewElement(
173: IncomeExpenseAccountInfo.getPropertySet());
174: }
175: }
|