📄 portfoliomanagementjpanel.java
字号:
for(Stock stock : stocks) {
m.displayHistoryChart(stock);
}
}
});
popup.add(menuItem);
popup.addSeparator();
menuItem = new JMenuItem("Delete", this.getImageIcon("/images/16x16/editdelete.png"));
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
PortfolioManagementJPanel.this.deteleSelectedTreeTableRow();
}
});
popup.add(menuItem);
}
return popup;
}
private void editSellTransaction(Transaction newTransaction, Transaction oldTransaction) {
assert(newTransaction.getContract().getType() == Contract.Type.Sell);
assert(oldTransaction.getContract().getType() == Contract.Type.Sell);
final SellPortfolioTreeTableModel portfolioTreeTableModel = (SellPortfolioTreeTableModel)sellTreeTable.getTreeTableModel();
portfolioTreeTableModel.editTransaction(newTransaction, oldTransaction);
}
private void editBuyTransaction(Transaction newTransaction, Transaction oldTransaction) {
assert(newTransaction.getContract().getType() == Contract.Type.Buy);
assert(oldTransaction.getContract().getType() == Contract.Type.Buy);
final BuyPortfolioTreeTableModel portfolioTreeTableModel = (BuyPortfolioTreeTableModel)buyTreeTable.getTreeTableModel();
portfolioTreeTableModel.editTransaction(newTransaction, oldTransaction);
}
private void addBuyTransaction(Transaction transaction) {
assert(transaction.getContract().getType() == Contract.Type.Buy);
final BuyPortfolioTreeTableModel portfolioTreeTableModel = (BuyPortfolioTreeTableModel)buyTreeTable.getTreeTableModel();
portfolioTreeTableModel.addTransaction(transaction);
this.realTimeStockMonitor.addStockCode(transaction.getContract().getStock().getCode());
}
private void addSellTransaction(Transaction transaction) {
assert(transaction.getContract().getType() == Contract.Type.Sell);
final SellPortfolioTreeTableModel portfolioTreeTableModel = (SellPortfolioTreeTableModel)sellTreeTable.getTreeTableModel();
portfolioTreeTableModel.addTransaction(transaction);
}
private void updateRealTimeStockMonitorAccordingToBuyPortfolioTreeTableModel() {
if(this.realTimeStockMonitor == null) return;
final BuyPortfolioTreeTableModel portfolioTreeTableModel = (BuyPortfolioTreeTableModel)buyTreeTable.getTreeTableModel();
if(portfolioTreeTableModel != null) {
this.buyTreeTable.setTreeTableModel(portfolioTreeTableModel);
Portfolio portfolio = (Portfolio)portfolioTreeTableModel.getRoot();
final int count = portfolio.getChildCount();
for(int i=0; i<count; i++) {
TransactionSummary transactionSummary = (TransactionSummary)portfolio.getChildAt(i);
if(transactionSummary.getChildCount() <= 0) continue;
final Transaction transaction = (Transaction)transactionSummary.getChildAt(0);
this.realTimeStockMonitor.addStockCode(transaction.getContract().getStock().getCode());
}
}
}
private List<Stock> getSelectedStock(JXTreeTable treeTable) {
final TreePath[] treePaths = treeTable.getTreeSelectionModel().getSelectionPaths();
List<Stock> stocks = new ArrayList<Stock>();
Set<Code> c = new HashSet<Code>();
if(treePaths == null) {
return Collections.unmodifiableList(stocks);
}
for(TreePath treePath : treePaths) {
final Object lastPathComponent = treePath.getLastPathComponent();
if(lastPathComponent instanceof TransactionSummary) {
final TransactionSummary transactionSummary = (TransactionSummary)lastPathComponent;
assert(transactionSummary.getChildCount() > 0);
final Transaction transaction = (Transaction)transactionSummary.getChildAt(0);
final Stock stock = transaction.getContract().getStock();
final Code code = stock.getCode();
if(c.contains(code)) continue;
stocks.add(stock);
c.add(code);
}
else if(lastPathComponent instanceof Transaction) {
final Transaction transaction = (Transaction)lastPathComponent;
final Stock stock = transaction.getContract().getStock();
final Code code = stock.getCode();
if(c.contains(code)) continue;
stocks.add(stock);
c.add(code);
}
}
return Collections.unmodifiableList(stocks);
}
public void initPortfolio() {
final Country country = MainFrame.getJStockOptions().getCountry();
boolean sellReadSuccess = false;
boolean buyReadSuccess = false;
try {
File f = new File(org.yccheok.jstock.gui.Utils.getUserDataDirectory() + country + File.separator + "config" + File.separator + "buyportfolio.xml");
XStream xStream = new XStream();
InputStream inputStream = new java.io.FileInputStream(f);
final Object obj = xStream.fromXML(inputStream);
if(obj instanceof BuyPortfolioTreeTableModel) {
final BuyPortfolioTreeTableModel portfolioTreeTableModel = (BuyPortfolioTreeTableModel)obj;
this.buyTreeTable.setTreeTableModel(portfolioTreeTableModel);
buyReadSuccess = true;
}
}
catch(java.io.FileNotFoundException exp) {
log.error("", exp);
}
catch(com.thoughtworks.xstream.core.BaseException exp) {
log.error("", exp);
}
try {
File f = new File(org.yccheok.jstock.gui.Utils.getUserDataDirectory() + country + File.separator + "config" + File.separator + "sellportfolio.xml");
XStream xStream = new XStream();
InputStream inputStream = new java.io.FileInputStream(f);
final Object obj = xStream.fromXML(inputStream);
if(obj instanceof SellPortfolioTreeTableModel) {
final SellPortfolioTreeTableModel portfolioTreeTableModel = (SellPortfolioTreeTableModel)obj;
this.sellTreeTable.setTreeTableModel(portfolioTreeTableModel);
sellReadSuccess = true;
}
}
catch(java.io.FileNotFoundException exp) {
log.error("", exp);
}
catch(com.thoughtworks.xstream.core.BaseException exp) {
log.error("", exp);
}
if(buyReadSuccess == false) buyTreeTable.setTreeTableModel(new BuyPortfolioTreeTableModel());
if(sellReadSuccess == false) sellTreeTable.setTreeTableModel(new SellPortfolioTreeTableModel());
updateRealTimeStockMonitorAccordingToBuyPortfolioTreeTableModel();
updateWealthHeader();
}
public boolean savePortfolio() {
final Country country = MainFrame.getJStockOptions().getCountry();
if(Utils.createCompleteDirectoryHierarchyIfDoesNotExist(org.yccheok.jstock.gui.Utils.getUserDataDirectory() + country + File.separator + "config") == false)
{
return false;
}
File f = new File(org.yccheok.jstock.gui.Utils.getUserDataDirectory() + country + File.separator + "config" + File.separator + "buyportfolio.xml");
XStream xStream = new XStream();
boolean status = true;
try {
OutputStream outputStream = new FileOutputStream(f);
final BuyPortfolioTreeTableModel portfolioTreeTableModel = (BuyPortfolioTreeTableModel)buyTreeTable.getTreeTableModel();
xStream.toXML(portfolioTreeTableModel, outputStream);
}
catch(java.io.FileNotFoundException exp) {
log.error("", exp);
status = false;
}
catch(com.thoughtworks.xstream.core.BaseException exp) {
log.error("", exp);
status = false;
}
f = new File(org.yccheok.jstock.gui.Utils.getUserDataDirectory() + country + File.separator + "config" + File.separator + "sellportfolio.xml");
try {
OutputStream outputStream = new FileOutputStream(f);
final SellPortfolioTreeTableModel portfolioTreeTableModel = (SellPortfolioTreeTableModel)sellTreeTable.getTreeTableModel();
xStream.toXML(portfolioTreeTableModel, outputStream);
}
catch(java.io.FileNotFoundException exp) {
log.error("", exp);
status = false;
}
catch(com.thoughtworks.xstream.core.BaseException exp) {
log.error("", exp);
status = false;
}
return status;
}
public void initRealTimeStockMonitor(java.util.List<StockServerFactory> stockServerFactories) {
if(realTimeStockMonitor != null) {
final RealTimeStockMonitor oldRealTimeStockMonitor = realTimeStockMonitor;
Utils.getZoombiePool().execute(new Runnable() {
public void run() {
log.info("Prepare to shut down " + oldRealTimeStockMonitor + "...");
oldRealTimeStockMonitor.clearStockCodes();
oldRealTimeStockMonitor.dettachAll();
oldRealTimeStockMonitor.stop();
log.info("Shut down " + oldRealTimeStockMonitor + " peacefully.");
}
});
}
realTimeStockMonitor = new RealTimeStockMonitor(4, 20, MainFrame.getJStockOptions().getScanningSpeed());
for(StockServerFactory factory : stockServerFactories) {
realTimeStockMonitor.addStockServerFactory(factory);
}
realTimeStockMonitor.attach(this.realTimeStockMonitorObserver);
updateRealTimeStockMonitorAccordingToBuyPortfolioTreeTableModel();
}
// This is the workaround to overcome Erasure by generics. We are unable to make MainFrame to
// two observers at the same time.
private org.yccheok.jstock.engine.Observer<RealTimeStockMonitor, java.util.List<Stock>> getRealTimeStockMonitorObserver() {
return new org.yccheok.jstock.engine.Observer<RealTimeStockMonitor, java.util.List<Stock>>() {
public void update(RealTimeStockMonitor monitor, java.util.List<Stock> stocks)
{
PortfolioManagementJPanel.this.update(monitor, stocks);
}
};
}
public void update(RealTimeStockMonitor monitor, final java.util.List<Stock> stocks) {
final BuyPortfolioTreeTableModel portfolioTreeTableModel = (BuyPortfolioTreeTableModel)buyTreeTable.getTreeTableModel();
for(Stock stock : stocks) {
if(false == portfolioTreeTableModel.updateStockLastPrice(stock)) {
this.realTimeStockMonitor.removeStockCode(stock.getCode());
}
}
updateWealthHeader();
}
private void updateWealthHeader() {
final BuyPortfolioTreeTableModel buyPortfolioTreeTableModel = (BuyPortfolioTreeTableModel)this.buyTreeTable.getTreeTableModel();
final SellPortfolioTreeTableModel sellPortfolioTreeTableModel = (SellPortfolioTreeTableModel)this.sellTreeTable.getTreeTableModel();
final double share = buyPortfolioTreeTableModel.getCurrentValue();
final double cash = sellPortfolioTreeTableModel.getNetSellingValue();
final double paperProfit = buyPortfolioTreeTableModel.getNetGainLossValue();
final double paperProfitPercentage = buyPortfolioTreeTableModel.getNetGainLossPercentage();
final double realizedProfit = sellPortfolioTreeTableModel.getNetGainLossValue();
final double realizedProfitPercentage = sellPortfolioTreeTableModel.getNetGainLossPercentage();
final java.text.NumberFormat numberFormat = java.text.NumberFormat.getInstance();
numberFormat.setMaximumFractionDigits(2);
numberFormat.setMinimumFractionDigits(2);
final String _share = numberFormat.format(share);
final String _cash = numberFormat.format(cash);
final String _paperProfit = numberFormat.format(paperProfit);
final String _paperProfitPercentage = numberFormat.format(paperProfitPercentage);
final String _realizedProfit = numberFormat.format(realizedProfit);
final String _realizedProfitPercentage = numberFormat.format(realizedProfitPercentage);
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
jLabel2.setText(_share);
jLabel4.setText(_cash);
jLabel6.setText(_paperProfit + " (" + _paperProfitPercentage + "%)");
jLabel8.setText(_realizedProfit + " (" + _realizedProfitPercentage + "%)");
jLabel2.setForeground(Utils.getColor(share, 0.0));
jLabel4.setForeground(Utils.getColor(cash, 0.0));
jLabel6.setForeground(Utils.getColor(paperProfit, 0.0));
jLabel8.setForeground(Utils.getColor(realizedProfit, 0.0));
}
});
}
public void softStart() {
if(realTimeStockMonitor == null) return;
realTimeStockMonitor.softStart();
}
public void softStop() {
if(realTimeStockMonitor == null) return;
realTimeStockMonitor.softStop();
}
private static final Log log = LogFactory.getLog(PortfolioManagementJPanel.class);
private RealTimeStockMonitor realTimeStockMonitor = null;
private org.yccheok.jstock.engine.Observer<RealTimeStockMonitor, java.util.List<Stock>> realTimeStockMonitorObserver = this.getRealTimeStockMonitorObserver();
// Variables declaration - do not modify//GEN-BEGIN:variables
private org.jdesktop.swingx.JXTreeTable buyTreeTable;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JPanel jPanel4;
private javax.swing.JPanel jPanel5;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JSplitPane jSplitPane1;
private org.jdesktop.swingx.JXTreeTable sellTreeTable;
// End of variables declaration//GEN-END:variables
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -