001: /*
002: *
003: * JMoney - A Personal Finance Manager
004: * Copyright (c) 2007 Nigel Westbury <westbury@users.sf.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.entrytable;
024:
025: import net.sf.jmoney.model2.Commodity;
026:
027: import org.eclipse.core.runtime.Assert;
028: import org.eclipse.swt.SWT;
029: import org.eclipse.swt.events.FocusListener;
030: import org.eclipse.swt.widgets.Composite;
031: import org.eclipse.swt.widgets.Control;
032: import org.eclipse.swt.widgets.Label;
033:
034: public class BalanceColumn extends
035: IndividualBlock<EntryData, BaseEntryRowControl> {
036:
037: private class BalanceCellControl implements
038: ICellControl<EntryData>, IBalanceChangeListener {
039: private final Label balanceLabel;
040: private EntryData entyData = null;
041:
042: private BalanceCellControl(Label balanceLabel) {
043: this .balanceLabel = balanceLabel;
044: }
045:
046: public Control getControl() {
047: return balanceLabel;
048: }
049:
050: public void load(EntryData entryData) {
051: this .entyData = entryData;
052: balanceLabel.setText(commodityForFormatting
053: .format(entryData.getBalance()
054: + entryData.getEntry().getAmount()));
055: }
056:
057: public void save() {
058: // Not editable so nothing to do
059: }
060:
061: public void setFocusListener(FocusListener controlFocusListener) {
062: // Nothing to do
063: }
064:
065: public void balanceChanged() {
066: /*
067: * The balance in the EntryData object has changed. This happens if
068: * the amount in a previous entry changes, or a previous entry is
069: * inserted or removed.
070: */
071: balanceLabel.setText(commodityForFormatting.format(entyData
072: .getBalance()
073: + entyData.getEntry().getAmount()));
074: }
075: }
076:
077: private Commodity commodityForFormatting;
078:
079: public BalanceColumn(Commodity commodityForFormatting) {
080: super ("Balance", 70, 2);
081: Assert.isNotNull(commodityForFormatting);
082: this .commodityForFormatting = commodityForFormatting;
083: }
084:
085: public int compare(EntryData entryData1, EntryData entryData2) {
086: // Entries lists cannot be sorted based on the balance.
087: // The caller should not do this.
088: throw new RuntimeException(
089: "internal error - attempt to sort on balance");
090: }
091:
092: @Override
093: public ICellControl<EntryData> createCellControl(Composite parent,
094: BaseEntryRowControl rowControl) {
095: final Label balanceLabel = new Label(parent, SWT.TRAIL);
096:
097: BalanceCellControl cellControl = new BalanceCellControl(
098: balanceLabel);
099:
100: rowControl.addBalanceChangeListener(cellControl);
101:
102: return cellControl;
103: }
104:
105: public String getId() {
106: return "balance"; //$NON-NLS-1$
107: }
108: }
|