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 java.text.MessageFormat;
026:
027: import net.sf.jmoney.model2.ExtendableObject;
028: import net.sf.jmoney.model2.IPropertyControl;
029: import net.sf.jmoney.model2.ScalarPropertyAccessor;
030:
031: import org.eclipse.jface.dialogs.Dialog;
032: import org.eclipse.swt.SWT;
033: import org.eclipse.swt.events.SelectionAdapter;
034: import org.eclipse.swt.events.SelectionEvent;
035: import org.eclipse.swt.widgets.Button;
036: import org.eclipse.swt.widgets.Composite;
037: import org.eclipse.swt.widgets.Control;
038:
039: /**
040: * Editor class for a table of progressive rates. For example,
041: * the commission rates or the stamp duty chargable on stock
042: * purchase and sales. (Actually, commission rates are regressive,
043: * and go down in percentage terms as the amount goes up. This
044: * editor allows rates to be entered that go up or down as the
045: * amount goes up.
046: *
047: * @author Nigel Westbury
048: */
049: public class RatesEditor implements IPropertyControl {
050:
051: private StockAccount account = null;
052:
053: private ScalarPropertyAccessor<RatesTable> ratesPropertyAccessor;
054:
055: private Button propertyControl;
056:
057: public RatesEditor(Composite parent,
058: ScalarPropertyAccessor<RatesTable> propertyAccessor) {
059: this .ratesPropertyAccessor = propertyAccessor;
060:
061: propertyControl = new Button(parent, SWT.PUSH);
062: propertyControl.setText("Setup...");
063: propertyControl.addSelectionListener(new SelectionAdapter() {
064: @Override
065: public void widgetSelected(SelectionEvent e) {
066:
067: Object[] messageArgs = new Object[] { "commission/tax",
068: account.getName() };
069: String title = new MessageFormat(
070: "Setup {0} rates for {1}", java.util.Locale.US)
071: .format(messageArgs);
072:
073: RatesDialog dialog = new RatesDialog(propertyControl
074: .getShell(), title, account
075: .getPropertyValue(ratesPropertyAccessor),
076: account.getCurrency());
077: if (dialog.open() == Dialog.OK) {
078: account.setPropertyValue(ratesPropertyAccessor,
079: dialog.getRates());
080: }
081: }
082: });
083: }
084:
085: /**
086: * Load the control with the value from the given account.
087: */
088: public void load(ExtendableObject object) {
089: account = (StockAccount) object;
090:
091: // propertyControl.setRatesTable();
092:
093: propertyControl.setEnabled(true);
094: }
095:
096: /**
097: * Save the value from the control back into the account object.
098: *
099: * Editors may update the property on a regular basis, not just when
100: * the framework calls the <code>save</code> method. However, the only time
101: * that editors must update the property is when the framework calls this method.
102: *
103: * The framework should never call this method when no account is selected
104: * so we can assume that <code>account</code> is not null.
105: */
106: public void save() {
107: // RatesTable ratesTable = propertyControl.getRatesTable();
108: // account.setPropertyValue(ratesPropertyAccessor, ratesTable);
109: }
110:
111: /**
112: * @return The underlying SWT widget.
113: */
114: public Control getControl() {
115: return propertyControl;
116: }
117: }
|