[eclipsefinance-subversion] SF.net SVN: eclipsefinance: [18] trunk/net.sf.eclipsefinance.core/src/n
Status: Pre-Alpha
Brought to you by:
yukio7
|
From: <yu...@us...> - 2006-09-20 17:05:12
|
Revision: 18
http://svn.sourceforge.net/eclipsefinance/?rev=18&view=rev
Author: yukio7
Date: 2006-09-20 10:04:54 -0700 (Wed, 20 Sep 2006)
Log Message:
-----------
start of transaction
Modified Paths:
--------------
trunk/net.sf.eclipsefinance.core/src/net/sf/eclipsefinance/core/ui/views/AccountView.java
trunk/net.sf.eclipsefinance.core/src/net/sf/eclipsefinance/core/ui/views/CoreFinanceAdapterFactory.java
trunk/net.sf.eclipsefinance.core/src/net/sf/eclipsefinance/core/ui/views/CorePerspective.java
Added Paths:
-----------
trunk/net.sf.eclipsefinance.core/src/net/sf/eclipsefinance/core/model/Transaction.java
trunk/net.sf.eclipsefinance.core/src/net/sf/eclipsefinance/core/ui/
trunk/net.sf.eclipsefinance.core/src/net/sf/eclipsefinance/core/ui/dialogs/
trunk/net.sf.eclipsefinance.core/src/net/sf/eclipsefinance/core/ui/dialogs/TransactionDialog.java
trunk/net.sf.eclipsefinance.core/src/net/sf/eclipsefinance/core/ui/views/
trunk/net.sf.eclipsefinance.core/src/net/sf/eclipsefinance/core/ui/views/TransactionsView.java
Removed Paths:
-------------
trunk/net.sf.eclipsefinance.core/src/net/sf/eclipsefinance/core/views/
Added: trunk/net.sf.eclipsefinance.core/src/net/sf/eclipsefinance/core/model/Transaction.java
===================================================================
--- trunk/net.sf.eclipsefinance.core/src/net/sf/eclipsefinance/core/model/Transaction.java (rev 0)
+++ trunk/net.sf.eclipsefinance.core/src/net/sf/eclipsefinance/core/model/Transaction.java 2006-09-20 17:04:54 UTC (rev 18)
@@ -0,0 +1,83 @@
+/*
+ * Copyright (c) 2004-2006 Eclipse Finance.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Francois Cottet - initial API and implementation
+ */
+
+package net.sf.eclipsefinance.core.model;
+
+import java.math.BigDecimal;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+
+public class Transaction {
+
+ private Date date = null;
+ private Integer id;
+ private String memo = null;
+ private BigDecimal amount = new BigDecimal("0");
+ Map params = new HashMap();
+
+ public Transaction() {
+ }
+
+ public Transaction(Integer id) {
+ this.id =id;
+ }
+
+ public Transaction(Date date, String memo, BigDecimal amount) {
+ this.date = date;
+ this.date = date;
+ this.amount = amount;
+ }
+
+ public String getMemo() {
+ return this.memo;
+ }
+
+ public void setMemo(String memo) {
+ this.memo = memo;
+ }
+
+ public Integer getId() {
+ return this.id;
+ }
+
+ public Date getDate() {
+ return this.date;
+ }
+
+ public void setDate(Date date) {
+ this.date = date;
+ }
+
+ public BigDecimal getAmount() {
+ return this.amount;
+ }
+
+ public void setAmount(BigDecimal amount) {
+ this.amount = amount;
+ }
+
+ public Map getParams() {
+ return this.params;
+ }
+
+ public void setParams(Map params) {
+ this.params = params;
+ }
+
+ public void setParam(String key, String value) {
+ params.put(key, value);
+ }
+
+ public String getParam(String key) {
+ return (String)params.get(key);
+ }
+}
Added: trunk/net.sf.eclipsefinance.core/src/net/sf/eclipsefinance/core/ui/dialogs/TransactionDialog.java
===================================================================
--- trunk/net.sf.eclipsefinance.core/src/net/sf/eclipsefinance/core/ui/dialogs/TransactionDialog.java (rev 0)
+++ trunk/net.sf.eclipsefinance.core/src/net/sf/eclipsefinance/core/ui/dialogs/TransactionDialog.java 2006-09-20 17:04:54 UTC (rev 18)
@@ -0,0 +1,366 @@
+/*
+ * Copyright (c) 2004-2006 Marco Maccaferri and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Marco Maccaferri - initial API and implementation
+ */
+
+package net.sf.eclipsefinance.core.ui.dialogs;
+
+import java.text.NumberFormat;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.Date;
+import java.util.Iterator;
+import java.util.List;
+
+import net.sourceforge.eclipsetrader.core.CorePlugin;
+import net.sourceforge.eclipsetrader.core.db.Account;
+import net.sourceforge.eclipsetrader.core.db.Security;
+import net.sourceforge.eclipsetrader.core.db.Transaction;
+
+import org.eclipse.jface.dialogs.IDialogConstants;
+import org.eclipse.jface.dialogs.TitleAreaDialog;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.FocusAdapter;
+import org.eclipse.swt.events.FocusEvent;
+import org.eclipse.swt.events.ModifyEvent;
+import org.eclipse.swt.events.ModifyListener;
+import org.eclipse.swt.events.SelectionAdapter;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Button;
+import org.eclipse.swt.widgets.Combo;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Label;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.swt.widgets.Spinner;
+import org.eclipse.swt.widgets.Text;
+
+public class TransactionDialog extends TitleAreaDialog
+{
+ Account account;
+ Security security;
+ int defaultQuantity = 1;
+ Transaction transaction;
+ Text dateText;
+ Combo accountCombo;
+ Combo securityCombo;
+ Button buyButton;
+ Button sellButton;
+ Spinner quantitySpinner;
+ Spinner priceSpinner;
+ Spinner expensesSpinner;
+ Text totalText;
+ SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
+ NumberFormat nf = NumberFormat.getInstance();
+
+ public TransactionDialog(Account account, Shell parentShell)
+ {
+ super(parentShell);
+ this.account = account;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
+ */
+ protected void configureShell(Shell newShell)
+ {
+ super.configureShell(newShell);
+ newShell.setText("Transaction");
+
+ nf.setGroupingUsed(true);
+ nf.setMinimumIntegerDigits(1);
+ nf.setMinimumFractionDigits(2);
+ nf.setMaximumFractionDigits(2);
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
+ */
+ protected Control createDialogArea(Composite parent)
+ {
+ Composite content = new Composite(parent, SWT.NONE);
+ GridLayout gridLayout = new GridLayout(2, false);
+ content.setLayout(gridLayout);
+ content.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true));
+
+ Label label = new Label(content, SWT.NONE);
+ label.setText("Date / Time");
+ label.setLayoutData(new GridData(125, SWT.DEFAULT));
+ dateText = new Text(content, SWT.BORDER);
+ dateText.setLayoutData(new GridData(125, SWT.DEFAULT));
+ dateText.addFocusListener(new FocusAdapter() {
+ public void focusLost(FocusEvent e)
+ {
+ try {
+ Date date = df.parse(dateText.getText());
+ dateText.setText(df.format(date));
+ } catch(Exception e1) {
+ }
+ }
+ });
+
+ label = new Label(content, SWT.NONE);
+ label.setText("Account");
+ label.setLayoutData(new GridData(125, SWT.DEFAULT));
+ accountCombo = new Combo(content, SWT.READ_ONLY);
+ accountCombo.setLayoutData(new GridData(GridData.FILL, GridData.BEGINNING, true, false));
+ accountCombo.addSelectionListener(new SelectionAdapter() {
+ public void widgetSelected(SelectionEvent e)
+ {
+ updateTotals();
+ }
+ });
+
+ label = new Label(content, SWT.NONE);
+ label.setText("Security");
+ label.setLayoutData(new GridData(125, SWT.DEFAULT));
+ securityCombo = new Combo(content, SWT.READ_ONLY);
+ securityCombo.setLayoutData(new GridData(GridData.FILL, GridData.BEGINNING, true, false));
+ securityCombo.setVisibleItemCount(25);
+ securityCombo.addSelectionListener(new SelectionAdapter() {
+ public void widgetSelected(SelectionEvent e)
+ {
+ Security security = (Security)securityCombo.getData(securityCombo.getText());
+ if (security != null && security.getQuote() != null)
+ {
+ priceSpinner.setSelection((int)Math.round(security.getQuote().getLast() * Math.pow(10, priceSpinner.getDigits())));
+ updateTotals();
+ }
+ }
+ });
+
+ label = new Label(content, SWT.NONE);
+ label.setLayoutData(new GridData(125, SWT.DEFAULT));
+ Composite group = new Composite(content, SWT.NONE);
+ gridLayout = new GridLayout(2, false);
+ gridLayout.marginWidth = gridLayout.marginHeight = 0;
+ group.setLayout(gridLayout);
+ buyButton = new Button(group, SWT.RADIO);
+ buyButton.setText("Buy");
+ buyButton.addSelectionListener(new SelectionAdapter() {
+ public void widgetSelected(SelectionEvent e)
+ {
+ updateTotals();
+ }
+ });
+ sellButton = new Button(group, SWT.RADIO);
+ sellButton.setText("Sell");
+ sellButton.addSelectionListener(new SelectionAdapter() {
+ public void widgetSelected(SelectionEvent e)
+ {
+ updateTotals();
+ }
+ });
+
+ label = new Label(content, SWT.NONE);
+ label.setText("Quantity");
+ label.setLayoutData(new GridData(125, SWT.DEFAULT));
+ quantitySpinner = new Spinner(content, SWT.BORDER);
+ quantitySpinner.setMinimum(1);
+ quantitySpinner.setMaximum(999999);
+ quantitySpinner.setSelection(Math.abs(defaultQuantity));
+ quantitySpinner.setLayoutData(new GridData(60, SWT.DEFAULT));
+ quantitySpinner.addSelectionListener(new SelectionAdapter() {
+ public void widgetSelected(SelectionEvent e)
+ {
+ updateTotals();
+ }
+ });
+
+ label = new Label(content, SWT.NONE);
+ label.setText("Price");
+ label.setLayoutData(new GridData(125, SWT.DEFAULT));
+ priceSpinner = new Spinner(content, SWT.BORDER);
+ priceSpinner.setMinimum(0);
+ priceSpinner.setMaximum(999999999);
+ priceSpinner.setDigits(4);
+ priceSpinner.setIncrement(100);
+ priceSpinner.setSelection(0);
+ priceSpinner.setLayoutData(new GridData(60, SWT.DEFAULT));
+ priceSpinner.addSelectionListener(new SelectionAdapter() {
+ public void widgetSelected(SelectionEvent e)
+ {
+ updateTotals();
+ }
+ });
+
+ label = new Label(content, SWT.NONE);
+ label.setText("Expenses");
+ label.setLayoutData(new GridData(125, SWT.DEFAULT));
+ expensesSpinner = new Spinner(content, SWT.BORDER);
+ expensesSpinner.setMinimum(0);
+ expensesSpinner.setMaximum(99999999);
+ expensesSpinner.setDigits(2);
+ expensesSpinner.setIncrement(1);
+ expensesSpinner.setSelection(0);
+ expensesSpinner.setLayoutData(new GridData(60, SWT.DEFAULT));
+
+ label = new Label(content, SWT.NONE);
+ label.setText("Total");
+ label.setLayoutData(new GridData(125, SWT.DEFAULT));
+ totalText = new Text(content, SWT.BORDER|SWT.READ_ONLY|SWT.RIGHT);
+ totalText.setEnabled(false);
+ totalText.setLayoutData(new GridData(60, SWT.DEFAULT));
+
+ List list = CorePlugin.getRepository().allAccounts();
+ Collections.sort(list, new Comparator() {
+ public int compare(Object arg0, Object arg1)
+ {
+ return ((Account)arg0).getDescription().compareTo(((Account)arg1).getDescription());
+ }
+ });
+ for (Iterator iter = list.iterator(); iter.hasNext(); )
+ {
+ Account s = (Account)iter.next();
+ accountCombo.add(s.getDescription());
+ accountCombo.setData(s.getDescription(), s);
+ }
+ if (account != null)
+ accountCombo.setText(account.getDescription());
+
+ list = CorePlugin.getRepository().allSecurities();
+ Collections.sort(list, new Comparator() {
+ public int compare(Object arg0, Object arg1)
+ {
+ return ((Security)arg0).getDescription().compareTo(((Security)arg1).getDescription());
+ }
+ });
+ for (Iterator iter = list.iterator(); iter.hasNext(); )
+ {
+ Security s = (Security)iter.next();
+ securityCombo.add(s.getDescription());
+ securityCombo.setData(s.getDescription(), s);
+ }
+
+ if (transaction != null)
+ {
+ setTitle("Edit a Transaction");
+ setMessage("Enter the details of the transaction to edit");
+ dateText.setText(df.format(transaction.getDate()));
+ securityCombo.setText(transaction.getSecurity().getDescription());
+ buyButton.setSelection(transaction.getQuantity() >= 0);
+ sellButton.setSelection(transaction.getQuantity() < 0);
+ quantitySpinner.setSelection(Math.abs(transaction.getQuantity()));
+ priceSpinner.setSelection((int)Math.round(transaction.getPrice() * Math.pow(10, priceSpinner.getDigits())));
+ expensesSpinner.setSelection((int)Math.round(transaction.getExpenses() * Math.pow(10, expensesSpinner.getDigits())));
+ }
+ else
+ {
+ setTitle("Create a new Transaction");
+ setMessage("Enter the details of the transaction to create");
+ dateText.setText(df.format(Calendar.getInstance().getTime()));
+ buyButton.setSelection(defaultQuantity >= 0);
+ sellButton.setSelection(defaultQuantity < 0);
+ }
+
+ if (security != null)
+ {
+ securityCombo.setText(security.getDescription());
+ if (security.getQuote() != null)
+ priceSpinner.setSelection((int)Math.round(security.getQuote().getLast() * Math.pow(10, priceSpinner.getDigits())));
+ quantitySpinner.setFocus();
+ }
+ else
+ securityCombo.setFocus();
+
+ updateTotals();
+
+ dateText.addModifyListener(new ModifyListener() {
+ public void modifyText(ModifyEvent e)
+ {
+ try {
+ df.parse(dateText.getText());
+ setErrorMessage(null);
+ getButton(IDialogConstants.OK_ID).setEnabled(true);
+ } catch(Exception e1) {
+ setErrorMessage("Invalid date and time format");
+ getButton(IDialogConstants.OK_ID).setEnabled(false);
+ }
+ }
+ });
+
+ return super.createDialogArea(parent);
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jface.window.Window#open()
+ */
+ public int open(Transaction transaction)
+ {
+ this.transaction = transaction;
+ this.security = transaction.getSecurity();
+ return super.open();
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jface.window.Window#open()
+ */
+ public int open(Security security)
+ {
+ this.security = security;
+ return super.open();
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jface.dialogs.Dialog#okPressed()
+ */
+ protected void okPressed()
+ {
+ if (transaction != null)
+ account.getTransactions().remove(transaction);
+
+ transaction = new Transaction();
+ transaction.setDate(Calendar.getInstance().getTime());
+ try {
+ transaction.setDate(df.parse(dateText.getText()));
+ } catch(Exception e) {
+ CorePlugin.logException(e);
+ }
+ transaction.setSecurity((Security)securityCombo.getData(securityCombo.getText()));
+ if (buyButton.getSelection())
+ transaction.setQuantity(quantitySpinner.getSelection());
+ else
+ transaction.setQuantity(-quantitySpinner.getSelection());
+ transaction.setPrice(priceSpinner.getSelection() / Math.pow(10, priceSpinner.getDigits()));
+ transaction.setExpenses(expensesSpinner.getSelection() / Math.pow(10, expensesSpinner.getDigits()));
+
+ account.getTransactions().add(transaction);
+ CorePlugin.getRepository().save(account);
+
+ super.okPressed();
+ }
+
+ void updateTotals()
+ {
+ int quantity = quantitySpinner.getSelection();
+ double price = priceSpinner.getSelection() / Math.pow(10, priceSpinner.getDigits());
+ double total = quantity * price;
+
+ double expenses = account.getExpenses(security, quantity, price);
+ if (transaction != null)
+ expenses = transaction.getExpenses();
+ else
+ expensesSpinner.setSelection((int)Math.round(expenses * Math.pow(10, expensesSpinner.getDigits())));
+ if (buyButton.getSelection())
+ total += expenses;
+ else
+ total -= expenses;
+
+ totalText.setText(nf.format(total));
+ }
+
+ public void setDefaultQuantity(int defaultQuantity)
+ {
+ this.defaultQuantity = defaultQuantity;
+ }
+}
Copied: trunk/net.sf.eclipsefinance.core/src/net/sf/eclipsefinance/core/ui/views (from rev 13, trunk/net.sf.eclipsefinance.core/src/net/sf/eclipsefinance/core/views)
Modified: trunk/net.sf.eclipsefinance.core/src/net/sf/eclipsefinance/core/ui/views/AccountView.java
===================================================================
--- trunk/net.sf.eclipsefinance.core/src/net/sf/eclipsefinance/core/views/AccountView.java 2006-08-08 20:04:49 UTC (rev 13)
+++ trunk/net.sf.eclipsefinance.core/src/net/sf/eclipsefinance/core/ui/views/AccountView.java 2006-09-20 17:04:54 UTC (rev 18)
@@ -1,4 +1,4 @@
-package net.sf.eclipsefinance.core.views;
+package net.sf.eclipsefinance.core.ui.views;
import net.sf.eclipsefinance.core.model.Account;
import net.sf.eclipsefinance.core.model.Bank;
Modified: trunk/net.sf.eclipsefinance.core/src/net/sf/eclipsefinance/core/ui/views/CoreFinanceAdapterFactory.java
===================================================================
--- trunk/net.sf.eclipsefinance.core/src/net/sf/eclipsefinance/core/views/CoreFinanceAdapterFactory.java 2006-08-08 20:04:49 UTC (rev 13)
+++ trunk/net.sf.eclipsefinance.core/src/net/sf/eclipsefinance/core/ui/views/CoreFinanceAdapterFactory.java 2006-09-20 17:04:54 UTC (rev 18)
@@ -1,4 +1,4 @@
-package net.sf.eclipsefinance.core.views;
+package net.sf.eclipsefinance.core.ui.views;
import java.util.List;
Modified: trunk/net.sf.eclipsefinance.core/src/net/sf/eclipsefinance/core/ui/views/CorePerspective.java
===================================================================
--- trunk/net.sf.eclipsefinance.core/src/net/sf/eclipsefinance/core/views/CorePerspective.java 2006-08-08 20:04:49 UTC (rev 13)
+++ trunk/net.sf.eclipsefinance.core/src/net/sf/eclipsefinance/core/ui/views/CorePerspective.java 2006-09-20 17:04:54 UTC (rev 18)
@@ -1,6 +1,6 @@
-package net.sf.eclipsefinance.core.views;
+package net.sf.eclipsefinance.core.ui.views;
-import net.sf.eclipsefinance.core.views.AccountView;
+import net.sf.eclipsefinance.core.ui.views.AccountView;
import org.eclipse.ui.IPageLayout;
import org.eclipse.ui.IPerspectiveFactory;
Added: trunk/net.sf.eclipsefinance.core/src/net/sf/eclipsefinance/core/ui/views/TransactionsView.java
===================================================================
--- trunk/net.sf.eclipsefinance.core/src/net/sf/eclipsefinance/core/ui/views/TransactionsView.java (rev 0)
+++ trunk/net.sf.eclipsefinance.core/src/net/sf/eclipsefinance/core/ui/views/TransactionsView.java 2006-09-20 17:04:54 UTC (rev 18)
@@ -0,0 +1,415 @@
+/*
+ * Copyright (c) 2004-2006 Marco Maccaferri and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Marco Maccaferri - initial API and implementation
+ */
+
+package net.sf.eclipsefinance.core.ui.views;
+
+import java.security.Security;
+import java.text.NumberFormat;
+import java.text.SimpleDateFormat;
+import java.util.Comparator;
+import java.util.Observable;
+import java.util.Observer;
+
+import net.sf.eclipsefinance.core.model.Account;
+import net.sf.eclipsefinance.core.model.Transaction;
+
+import org.eclipse.jface.action.IMenuListener;
+import org.eclipse.jface.action.IMenuManager;
+import org.eclipse.jface.action.MenuManager;
+import org.eclipse.jface.action.Separator;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.dnd.DND;
+import org.eclipse.swt.dnd.DropTarget;
+import org.eclipse.swt.dnd.DropTargetEvent;
+import org.eclipse.swt.dnd.DropTargetListener;
+import org.eclipse.swt.dnd.Transfer;
+import org.eclipse.swt.events.DisposeEvent;
+import org.eclipse.swt.events.DisposeListener;
+import org.eclipse.swt.events.MouseAdapter;
+import org.eclipse.swt.events.MouseEvent;
+import org.eclipse.swt.events.SelectionAdapter;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.graphics.Color;
+import org.eclipse.swt.graphics.Point;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Table;
+import org.eclipse.swt.widgets.TableColumn;
+import org.eclipse.swt.widgets.TableItem;
+import org.eclipse.ui.IWorkbenchActionConstants;
+import org.eclipse.ui.part.ViewPart;
+import org.eclipse.ui.part.PageBookView.SelectionProvider;
+
+public class TransactionsView extends ViewPart implements ICollectionObserver
+{
+ public static final String VIEW_ID = "net.sourceforge.eclipsetrader.views.transactions";
+ private Account account;
+ private Table table;
+ private Color evenForeground = new Color(null, 0, 0, 0);
+ private Color evenBackground = new Color(null, 255, 255, 255);
+ private Color oddForeground = new Color(null, 0, 0, 0);
+ private Color oddBackground = new Color(null, 240, 240, 240);
+ private Color negativeForeground = new Color(null, 240, 0, 0);
+ private Color positiveForeground = new Color(null, 0, 192, 0);
+ private NumberFormat nf = NumberFormat.getInstance();
+ private NumberFormat pf = NumberFormat.getInstance();
+ private SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
+// private Action deleteAction = new DeleteTransactionAction(this);
+// private Action propertiesAction = new TransactionSettingsAction(this);
+ private Comparator comparator = new Comparator() {
+ public int compare(Object arg0, Object arg1)
+ {
+ return ((Transaction)arg0).getDate().compareTo(((Transaction)arg1).getDate());
+ }
+ };
+
+ public TransactionsView()
+ {
+ nf.setGroupingUsed(true);
+ nf.setMinimumIntegerDigits(1);
+ nf.setMinimumFractionDigits(2);
+ nf.setMaximumFractionDigits(2);
+
+ pf.setGroupingUsed(true);
+ pf.setMinimumIntegerDigits(1);
+ pf.setMinimumFractionDigits(4);
+ pf.setMaximumFractionDigits(4);
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
+ */
+ public void createPartControl(Composite parent)
+ {
+ Composite content = new Composite(parent, SWT.NONE);
+ GridLayout gridLayout = new GridLayout();
+ gridLayout.marginWidth = gridLayout.marginHeight = 0;
+ content.setLayout(gridLayout);
+
+ table = new Table(content, SWT.FULL_SELECTION|SWT.MULTI);
+ table.setHeaderVisible(true);
+ table.setLinesVisible(false);
+ table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
+ table.addSelectionListener(new SelectionAdapter() {
+ public void widgetSelected(SelectionEvent e)
+ {
+ updateSelection();
+ }
+ });
+ table.addMouseListener(new MouseAdapter() {
+ public void mouseDown(MouseEvent e)
+ {
+ if (table.getItem(new Point(e.x, e.y)) == null)
+ {
+ table.deselectAll();
+ updateSelection();
+ }
+ }
+ public void mouseDoubleClick(MouseEvent e)
+ {
+ TableItem[] selection = table.getSelection();
+ if (selection.length == 1)
+ {
+ Transaction transaction = (Transaction)selection[0].getData();
+ TransactionDialog dlg = new TransactionDialog(account, getViewSite().getShell());
+ dlg.open(transaction);
+ }
+ }
+ });
+ TableColumn column = new TableColumn(table, SWT.NONE);
+ column.setWidth(0);
+ column.setResizable(false);
+
+ column = new TableColumn(table, SWT.RIGHT);
+ column.setText("Date / Time");
+ column = new TableColumn(table, SWT.LEFT);
+ column.setText("Code");
+ column = new TableColumn(table, SWT.LEFT);
+ column.setText("Description");
+ column = new TableColumn(table, SWT.LEFT);
+ column.setText("Operation");
+ column = new TableColumn(table, SWT.RIGHT);
+ column.setText("Quantity");
+ column = new TableColumn(table, SWT.RIGHT);
+ column.setText("Price");
+ column = new TableColumn(table, SWT.RIGHT);
+ column.setText("Expenses");
+ column = new TableColumn(table, SWT.RIGHT);
+ column.setText("Total");
+
+ for (int i = 1; i < table.getColumnCount(); i++)
+ table.getColumn(i).pack();
+
+ getSite().setSelectionProvider(new SelectionProvider());
+
+ // Drag and drop support
+ DropTarget target = new DropTarget(parent, DND.DROP_COPY|DND.DROP_MOVE);
+ target.setTransfer(new Transfer[] { SecurityTransfer.getInstance() });
+ target.addDropListener(new DropTargetListener() {
+ public void dragEnter(DropTargetEvent event)
+ {
+ event.detail = DND.DROP_COPY;
+ }
+
+ public void dragOver(DropTargetEvent event)
+ {
+ event.feedback = DND.FEEDBACK_SELECT | DND.FEEDBACK_SCROLL;
+ }
+
+ public void dragOperationChanged(DropTargetEvent event)
+ {
+ }
+
+ public void dragLeave(DropTargetEvent event)
+ {
+ }
+
+ public void dropAccept(DropTargetEvent event)
+ {
+ }
+
+ public void drop(DropTargetEvent event)
+ {
+ event.detail = DND.DROP_COPY;
+
+ if (SecurityTransfer.getInstance().isSupportedType(event.currentDataType))
+ {
+ Security[] securities = (Security[]) event.data;
+ for (int i = 0; i < securities.length; i++)
+ {
+ TransactionDialog dlg = new TransactionDialog(account, getViewSite().getShell());
+ dlg.open(securities[i]);
+ }
+ }
+ }
+ });
+
+ MenuManager menuMgr = new MenuManager("#popupMenu", "popupMenu"); //$NON-NLS-1$ //$NON-NLS-2$
+ menuMgr.setRemoveAllWhenShown(true);
+ menuMgr.addMenuListener(new IMenuListener() {
+ public void menuAboutToShow(IMenuManager menuManager)
+ {
+ menuManager.add(new Separator("top")); //$NON-NLS-1$
+ menuManager.add(new TransactionAction(TransactionsView.this));
+ menuManager.add(new Separator());
+ menuManager.add(deleteAction);
+ menuManager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
+ menuManager.add(propertiesAction);
+ menuManager.add(new Separator("bottom")); //$NON-NLS-1$
+ }
+ });
+ table.setMenu(menuMgr.createContextMenu(table));
+ getSite().registerContextMenu(menuMgr, getSite().getSelectionProvider());
+
+ account = (Account)CorePlugin.getRepository().load(Account.class, new Integer(Integer.parseInt(getViewSite().getSecondaryId())));
+ if (account != null)
+ {
+ setTitleToolTip(account.getDescription());
+ setContentDescription(account.getDescription());
+ getSite().getSelectionProvider().setSelection(new AccountSelection(account));
+
+ parent.getDisplay().asyncExec(new Runnable() {
+ public void run()
+ {
+ updateView();
+ }
+ });
+
+ account.getTransactions().addCollectionObserver(this);
+ }
+ else
+ getSite().getSelectionProvider().setSelection(new NullSelection());
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
+ */
+ public void setFocus()
+ {
+ table.getParent().setFocus();
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.ui.part.WorkbenchPart#dispose()
+ */
+ public void dispose()
+ {
+ if (account != null)
+ account.getTransactions().removeCollectionObserver(this);
+ super.dispose();
+ }
+
+ private void updateView()
+ {
+ table.setRedraw(false);
+
+ Object[] items = account.getTransactions().toArray();
+ for (int i = 0; i < items.length; i++)
+ {
+ TableItem tableItem = new TransactionTableItem((Transaction)items[i], table, SWT.NONE);
+ tableItem.setBackground(((i & 1) == 1) ? oddBackground : evenBackground);
+ tableItem.setForeground(((i & 1) == 1) ? oddForeground : evenForeground);
+ }
+
+ table.setRedraw(true);
+
+ for (int i = 1; i < table.getColumnCount(); i++)
+ table.getColumn(i).pack();
+ }
+
+ private void updateSelection()
+ {
+ TableItem[] selection = table.getSelection();
+ deleteAction.setEnabled(selection.length != 0);
+ propertiesAction.setEnabled(selection.length == 1);
+ }
+
+ /* (non-Javadoc)
+ * @see net.sourceforge.eclipsetrader.core.ICollectionObserver#itemAdded(java.lang.Object)
+ */
+ public void itemAdded(final Object o)
+ {
+ table.getDisplay().asyncExec(new Runnable() {
+ public void run()
+ {
+ if (!table.isDisposed())
+ {
+ TableItem[] items = table.getItems();
+ for (int i = 0; i < items.length; i++)
+ {
+ if (comparator.compare(o, items[i].getData()) < 0)
+ {
+ new TransactionTableItem((Transaction)o, table, SWT.NONE, i);
+ for (i = 0; i < table.getItemCount(); i++)
+ {
+ table.getItem(i).setBackground(((i & 1) == 1) ? oddBackground : evenBackground);
+ table.getItem(i).setForeground(((i & 1) == 1) ? oddForeground : evenForeground);
+ }
+ for (i = 1; i < table.getColumnCount(); i++)
+ table.getColumn(i).pack();
+ return;
+ }
+ }
+ new TransactionTableItem((Transaction)o, table, SWT.NONE);
+ for (int i = 0; i < table.getItemCount(); i++)
+ {
+ table.getItem(i).setBackground(((i & 1) == 1) ? oddBackground : evenBackground);
+ table.getItem(i).setForeground(((i & 1) == 1) ? oddForeground : evenForeground);
+ }
+ for (int i = 1; i < table.getColumnCount(); i++)
+ table.getColumn(i).pack();
+ }
+ }
+ });
+ }
+
+ /* (non-Javadoc)
+ * @see net.sourceforge.eclipsetrader.core.ICollectionObserver#itemRemoved(java.lang.Object)
+ */
+ public void itemRemoved(final Object o)
+ {
+ table.getDisplay().asyncExec(new Runnable() {
+ public void run()
+ {
+ if (!table.isDisposed())
+ {
+ TableItem[] items = table.getItems();
+ for (int i = 0; i < items.length; i++)
+ {
+ if (o.equals(items[i].getData()))
+ items[i].dispose();
+ }
+ for (int i = 0; i < table.getItemCount(); i++)
+ {
+ table.getItem(i).setBackground(((i & 1) == 1) ? oddBackground : evenBackground);
+ table.getItem(i).setForeground(((i & 1) == 1) ? oddForeground : evenForeground);
+ }
+ }
+ }
+ });
+ }
+
+ public Account getAccount()
+ {
+ return account;
+ }
+
+ public Table getTable()
+ {
+ return table;
+ }
+
+ private class TransactionTableItem extends TableItem implements Observer
+ {
+ private Transaction transaction;
+
+ public TransactionTableItem(Transaction transaction, Table parent, int style, int index)
+ {
+ super(parent, style, index);
+ init(transaction);
+ }
+
+ public TransactionTableItem(Transaction transaction, Table parent, int style)
+ {
+ super(parent, style);
+ init(transaction);
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.swt.widgets.TableItem#checkSubclass()
+ */
+ protected void checkSubclass()
+ {
+ }
+
+ private void init(Transaction transaction)
+ {
+ this.transaction = transaction;
+ setData(transaction);
+ update();
+
+ transaction.addObserver(this);
+ addDisposeListener(new DisposeListener() {
+ public void widgetDisposed(DisposeEvent e)
+ {
+ TransactionTableItem.this.transaction.deleteObserver(TransactionTableItem.this);
+ }
+ });
+ }
+
+ private void update()
+ {
+ setText(1, df.format(transaction.getDate()));
+ setText(2, transaction.getSecurity().getCode());
+ setText(3, transaction.getSecurity().getDescription());
+ setText(4, transaction.getQuantity() >= 0 ? "Buy" : "Sell");
+ setText(5, String.valueOf(Math.abs(transaction.getQuantity())));
+ setText(6, pf.format(transaction.getPrice()));
+ setText(7, nf.format(transaction.getExpenses()));
+ setText(8, nf.format(transaction.getAmount()));
+ setForeground(8, transaction.getAmount() >= 0 ? positiveForeground : negativeForeground);
+ }
+
+ /* (non-Javadoc)
+ * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
+ */
+ public void update(Observable o, Object arg)
+ {
+ getDisplay().asyncExec(new Runnable() {
+ public void run()
+ {
+ update();
+ }
+ });
+ }
+ }
+}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|