📄 buyportfoliotreetablemodel.java
字号:
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* Copyright (C) 2008 Cheok YanCheng <yccheok@yahoo.com>
*/
package org.yccheok.jstock.gui;
import java.text.SimpleDateFormat;
import org.jdesktop.swingx.treetable.*;
import org.yccheok.jstock.portfolio.*;
import javax.swing.tree.TreePath;
import org.yccheok.jstock.engine.Code;
/**
*
* @author Owner
*/
public class BuyPortfolioTreeTableModel extends AbstractPortfolioTreeTableModel {
private java.util.Map<Code, Double> stockLastPrice = new java.util.HashMap<Code, Double>();
public double getLastPrice(Code code) {
Object price = stockLastPrice.get(code);
if(price == null) return 0.0;
return (Double)price;
}
// Names of the columns.
private static final String[] cNames = {
"Stock",
"Date",
"Units",
"Purchase Price",
"Current Price",
"Purchase Value",
"Current Value",
"Gain/Loss Price",
"Gain/Loss Value",
"Gain/Loss %",
"Broker",
"Clearing Fee",
"Stamp Duty",
"Net Purchase Value",
"Net Gain/Loss Value",
"Net Gain/Loss %"
};
// Types of the columns.
private static final Class[] cTypes = {
TreeTableModel.class,
org.yccheok.jstock.engine.SimpleDate.class,
Integer.class,
Double.class,
Double.class,
Double.class,
Double.class,
Double.class,
Double.class,
Double.class,
Double.class,
Double.class,
Double.class,
Double.class,
Double.class,
Double.class
};
public boolean updateStockLastPrice(org.yccheok.jstock.engine.Stock stock) {
boolean status = false;
stockLastPrice.put(stock.getCode(), stock.getLastPrice());
final Portfolio portfolio = (Portfolio)getRoot();
final int count = portfolio.getChildCount();
TransactionSummary transactionSummary = null;
for(int i=0; i<count; i++) {
transactionSummary = (TransactionSummary)portfolio.getChildAt(i);
assert(transactionSummary.getChildCount() > 0);
final Transaction transaction = (Transaction)transactionSummary.getChildAt(0);
if(true == transaction.getContract().getStock().getCode().equals(stock.getCode())) {
break;
}
}
if(null == transactionSummary) return status;
final int num = transactionSummary.getChildCount();
for(int i=0; i<num; i++) {
final Transaction transaction = (Transaction)transactionSummary.getChildAt(i);
this.modelSupport.fireChildChanged(new TreePath(getPathToRoot(transaction)), i, transaction);
status = true;
}
fireTreeTableNodeChanged(transactionSummary);
fireTreeTableNodeChanged(getRoot());
return status;
}
private double getCurrentValue(Transaction transaction) {
final Code code = transaction.getContract().getStock().getCode();
final Double lastPrice = this.stockLastPrice.get(code);
if(lastPrice == null) return 0.0;
return lastPrice * transaction.getQuantity();
}
public double getCurrentValue(TransactionSummary transactionSummary) {
final Transaction transaction = (Transaction)transactionSummary.getChildAt(0);
final Code code = transaction.getContract().getStock().getCode();
final Double lastPrice = this.stockLastPrice.get(code);
if(lastPrice == null) return 0.0;
return lastPrice * transactionSummary.getQuantity();
}
private double getCurrentPrice(Transaction transaction) {
final Code code = transaction.getContract().getStock().getCode();
final Double lastPrice = this.stockLastPrice.get(code);
if(lastPrice == null) return 0.0;
return lastPrice;
}
public double getCurrentPrice(TransactionSummary transactionSummary) {
final Transaction transaction = (Transaction)transactionSummary.getChildAt(0);
final Code code = transaction.getContract().getStock().getCode();
final Double lastPrice = this.stockLastPrice.get(code);
if(lastPrice == null) return 0.0;
return lastPrice;
}
private double getGainLossValue(Portfolio portfolio) {
return getCurrentValue(portfolio) - portfolio.getTotal();
}
private double getGainLossPercentage(Portfolio portfolio) {
if(portfolio.getTotal() == 0) return 0.0;
return (getCurrentValue(portfolio) - portfolio.getTotal()) / portfolio.getTotal() * 100.0;
}
public double getGainLossPercentage(TransactionSummary transactionSummary) {
if(transactionSummary.getTotal() == 0) return 0.0;
return (getCurrentValue(transactionSummary) - transactionSummary.getTotal()) / transactionSummary.getTotal() * 100.0;
}
public double getNetGainLossValue(TransactionSummary transactionSummary) {
return getCurrentValue(transactionSummary) - transactionSummary.getNetTotal();
}
public double getNetGainLossPercentage(TransactionSummary transactionSummary) {
if(transactionSummary.getTotal() == 0) return 0.0;
return (getCurrentValue(transactionSummary) - transactionSummary.getNetTotal()) / transactionSummary.getNetTotal() * 100.0;
}
private double getGainLossPercentage(Transaction transaction) {
if(transaction.getTotal() == 0) return 0.0;
return (getCurrentValue(transaction) - transaction.getTotal()) / transaction.getTotal() * 100.0;
}
private double getNetGainLossValue(Transaction transaction) {
return getCurrentValue(transaction) - transaction.getNetTotal();
}
private double getNetGainLossPercentage(Transaction transaction) {
if(transaction.getNetTotal() == 0) return 0.0;
return (getCurrentValue(transaction) - transaction.getNetTotal()) / transaction.getNetTotal() * 100.0;
}
public double getNetGainLossValue() {
return getNetGainLossValue((Portfolio)getRoot());
}
private double getNetGainLossValue(Portfolio portfolio) {
return getCurrentValue(portfolio) - portfolio.getNetTotal();
}
public double getNetGainLossPercentage() {
return getNetGainLossPercentage((Portfolio)getRoot());
}
private double getNetGainLossPercentage(Portfolio portfolio) {
if(portfolio.getNetTotal() == 0) return 0.0;
return (getCurrentValue(portfolio) - portfolio.getNetTotal()) / portfolio.getNetTotal() * 100.0;
}
public double getCurrentValue() {
return this.getCurrentValue((Portfolio)getRoot());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -