OpenSeSim/src/main/java/gui/TraderListPanel.java

183 lines
6.3 KiB
Java
Raw Normal View History

2017-01-17 01:23:59 +01:00
/*
* Copyright (c) 2017, 7u83 <7u83@mail.ru>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package gui;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
2017-02-13 00:07:50 +01:00
2017-02-11 08:36:45 +01:00
import sesim.AutoTraderInterface;
2017-01-17 01:23:59 +01:00
import sesim.Exchange;
import sesim.Exchange.Account;
2017-01-17 01:23:59 +01:00
/**
*
* @author 7u83 <7u83@mail.ru>
*/
public class TraderListPanel extends javax.swing.JPanel {
2017-01-17 01:23:59 +01:00
DefaultTableModel model;
2017-01-17 01:23:59 +01:00
final void updateModel() {
2017-01-22 10:04:50 +01:00
if (Globals.se == null) {
2017-01-17 01:23:59 +01:00
return;
}
if (Globals.se.traders == null) {
return;
}
sesim.Quote q = Globals.se.getLastQuoete();
2017-01-17 01:23:59 +01:00
double price = q == null ? 0 : q.price;
int size = Globals.se.traders.size();
model.setRowCount(size);
for (int i = 0; i < size; i++) {
AutoTraderInterface at = Globals.se.traders.get(i);
Account a = at.getAccount();
model.setValueAt(i, i, 0);
model.setValueAt(at.getName(), i, 1);
model.setValueAt(a.getMoney(), i, 2);
model.setValueAt(a.getShares(), i, 3);
double wealth = a.getShares() * price + a.getMoney();
model.setValueAt(wealth, i, 4);
2017-01-17 01:23:59 +01:00
}
list.getRowSorter().allRowsChanged();
2017-01-17 01:23:59 +01:00
}
TimerTask updater;
2017-01-17 01:23:59 +01:00
/**
* Creates new form TraderListPanel2
*/
public TraderListPanel() {
initComponents();
model = (DefaultTableModel) list.getModel();
updateModel();
2017-01-17 01:23:59 +01:00
Timer timer = new Timer();
updater = new TimerTask() {
2017-01-17 01:23:59 +01:00
@Override
public void run() {
updateModel();
2017-01-17 01:23:59 +01:00
}
};
2017-01-17 01:23:59 +01:00
timer.schedule(updater, 0, 1000);
2017-01-17 01:23:59 +01:00
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
list = new javax.swing.JTable();
2017-01-17 01:23:59 +01:00
list.setAutoCreateRowSorter(true);
list.setModel(new javax.swing.table.DefaultTableModel(
2017-01-17 01:23:59 +01:00
new Object [][] {
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null}
2017-01-17 01:23:59 +01:00
},
new String [] {
"ID", "Name", "Money", "Shares", "Wealth"
2017-01-17 01:23:59 +01:00
}
) {
Class[] types = new Class [] {
java.lang.Long.class, java.lang.String.class, java.lang.Double.class, java.lang.Double.class, java.lang.Double.class
};
boolean[] canEdit = new boolean [] {
false, false, false, false, false
};
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
list.addMouseListener(new java.awt.event.MouseAdapter() {
2017-02-13 00:07:50 +01:00
public void mouseClicked(java.awt.event.MouseEvent evt) {
listMouseClicked(evt);
2017-02-13 00:07:50 +01:00
}
});
jScrollPane1.setViewportView(list);
2017-01-17 01:23:59 +01:00
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 537, Short.MAX_VALUE)
2017-01-17 01:23:59 +01:00
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
2017-01-17 01:23:59 +01:00
);
}// </editor-fold>//GEN-END:initComponents
private void listMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_listMouseClicked
if (evt.getClickCount() == 2) {
int index = list.rowAtPoint(evt.getPoint());
index = list.getRowSorter().convertRowIndexToModel(index);
Integer tid = (Integer) model.getValueAt(index, 0);
System.out.printf("Trader ID %d\n", tid);
JDialog console = Globals.se.traders.get(tid).getGuiConsole();
if (console == null) {
return;
}
console.setVisible(true);
}
}//GEN-LAST:event_listMouseClicked
2017-02-13 00:07:50 +01:00
2017-01-17 01:23:59 +01:00
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable list;
2017-01-17 01:23:59 +01:00
// End of variables declaration//GEN-END:variables
}