001: /*
002: *
003: * JMoney - A Personal Finance Manager
004: * Copyright (c) 2006 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.model2.Currency;
026:
027: import org.eclipse.jface.dialogs.Dialog;
028: import org.eclipse.jface.dialogs.DialogMessageArea;
029: import org.eclipse.jface.dialogs.IDialogConstants;
030: import org.eclipse.jface.dialogs.IMessageProvider;
031: import org.eclipse.swt.SWT;
032: import org.eclipse.swt.graphics.Image;
033: import org.eclipse.swt.graphics.Rectangle;
034: import org.eclipse.swt.layout.GridData;
035: import org.eclipse.swt.layout.GridLayout;
036: import org.eclipse.swt.widgets.Button;
037: import org.eclipse.swt.widgets.Composite;
038: import org.eclipse.swt.widgets.Control;
039: import org.eclipse.swt.widgets.Label;
040: import org.eclipse.swt.widgets.Shell;
041: import org.eclipse.swt.widgets.Text;
042:
043: /**
044: * An input dialog that allows the user to configure the methods for importing statement data
045: * for a particular account.
046: *
047: * @author Nigel Westbury
048: */
049: class RatesDialog extends Dialog {
050:
051: private String title;
052:
053: private RatesTable rates;
054:
055: private Currency currencyForFormatting;
056:
057: private DialogMessageArea messageArea;
058:
059: private RatesEditorControl control;
060:
061: /**
062: * Ok button widget.
063: */
064: private Button okButton;
065:
066: /**
067: * Error message label widget.
068: */
069: private Text errorMessageText;
070:
071: Image errorImage;
072:
073: /**
074: * Creates an input dialog with OK and Cancel buttons. Note that the dialog
075: * will have no visual representation (no widgets) until it is told to open.
076: * <p>
077: * Note that the <code>open</code> method blocks for input dialogs.
078: * </p>
079: *
080: * @param parentShell
081: * the parent shell
082: * @param currency
083: */
084: public RatesDialog(Shell parentShell, String title,
085: RatesTable rates, Currency currencyForFormatting) {
086: super (parentShell);
087:
088: this .title = title;
089: this .rates = rates;
090: this .currencyForFormatting = currencyForFormatting;
091:
092: // Load the error indicator
093: // URL installURL = ReconciliationPlugin.getDefault().getBundle().getEntry("/icons/error.gif");
094: // errorImage = ImageDescriptor.createFromURL(installURL).createImage();
095: }
096:
097: @Override
098: protected void buttonPressed(int buttonId) {
099: if (buttonId == IDialogConstants.OK_ID) {
100:
101: RatesTable newRates = control.getRatesTable();
102: if (newRates == null) {
103: // TODO: Set the error message.
104: return;
105: }
106:
107: rates = newRates;
108: }
109: super .buttonPressed(buttonId);
110: }
111:
112: @Override
113: protected void configureShell(Shell shell) {
114: super .configureShell(shell);
115: shell.setText(title);
116: }
117:
118: @Override
119: public boolean close() {
120: boolean closed = super .close();
121:
122: // Dispose the image
123: if (closed) {
124: // errorImage.dispose();
125: }
126:
127: return closed;
128: }
129:
130: @Override
131: protected void createButtonsForButtonBar(Composite parent) {
132: // create OK and Cancel buttons by default
133: okButton = createButton(parent, IDialogConstants.OK_ID,
134: IDialogConstants.OK_LABEL, true);
135: createButton(parent, IDialogConstants.CANCEL_ID,
136: IDialogConstants.CANCEL_LABEL, false);
137: }
138:
139: @Override
140: protected Control createDialogArea(Composite parent) {
141: Composite composite = (Composite) super
142: .createDialogArea(parent);
143: composite.setLayout(new GridLayout(1, false));
144:
145: // Message label
146: messageArea = new DialogMessageArea();
147: messageArea.createContents(composite);
148:
149: // What are these for?
150: // messageArea.setTitleLayoutData(createMessageAreaData());
151: // messageArea.setMessageLayoutData(createMessageAreaData());
152:
153: Label label = new Label(composite, SWT.WRAP);
154: label
155: .setText("Setup the rates. This allows JMoney to calculate the various commissions and taxes for you. "
156: + "A commission or tax may include a fixed amount and percentages. The percentages may vary based on the amount of the purchase or sale.");
157:
158: GridData messageData = new GridData();
159: Rectangle rect = getShell().getMonitor().getClientArea();
160: messageData.widthHint = rect.width / 2;
161: label.setLayoutData(messageData);
162:
163: control = new RatesEditorControl(composite);
164: control.setRatesTable(rates, currencyForFormatting);
165:
166: applyDialogFont(composite);
167: return composite;
168: }
169:
170: /**
171: * Sets or clears the error message.
172: * If not <code>null</code>, the OK button is disabled.
173: *
174: * @param errorMessage
175: * the error message, or <code>null</code> to clear
176: */
177: public void updateErrorMessage() {
178: String errorMessage = null;
179:
180: // TODO:
181:
182: if (errorMessage == null) {
183: messageArea.clearErrorMessage();
184: } else {
185: messageArea
186: .updateText(errorMessage, IMessageProvider.ERROR);
187: }
188:
189: // errorMessageText.setText(errorMessage == null ? "" : errorMessage); //$NON-NLS-1$
190:
191: // If called during createDialogArea, the okButton
192: // will not have been created yet.
193: if (okButton != null) {
194: okButton.setEnabled(errorMessage == null);
195: }
196: // errorMessageText.getParent().update();
197: }
198:
199: public RatesTable getRates() {
200: return rates;
201: }
202: }
|