OpenSeSim/src/gui/orderbook/OrderBook.java

221 lines
7.1 KiB
Java
Raw Normal View History

2017-02-13 00:07:50 +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.
*/
2017-02-13 18:26:32 +01:00
package gui.orderbook;
2017-02-13 00:07:50 +01:00
2017-02-13 18:26:32 +01:00
import gui.Globals;
import gui.Globals.CfgListener;
2017-02-26 09:54:31 +01:00
import gui.tools.NummericCellRenderer;
2017-02-25 20:06:26 +01:00
import java.awt.Component;
import java.text.DecimalFormat;
2017-02-13 00:07:50 +01:00
import java.util.ArrayList;
2017-02-25 20:06:26 +01:00
import javax.swing.JTable;
import javax.swing.SwingUtilities;
2017-02-25 20:06:26 +01:00
import javax.swing.table.DefaultTableCellRenderer;
2017-02-13 00:07:50 +01:00
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
2017-02-13 00:07:50 +01:00
import sesim.Exchange;
import sesim.Exchange.Order;
2017-02-13 18:26:32 +01:00
import sesim.Exchange.OrderType;
2017-02-13 00:07:50 +01:00
/**
*
* @author 7u83 <7u83@mail.ru>
*/
public class OrderBook extends javax.swing.JPanel implements Exchange.BookReceiver, CfgListener {
2017-02-13 00:07:50 +01:00
DefaultTableModel model;
TableColumn trader_column = null;
2017-02-25 20:06:26 +01:00
class Renderer extends DefaultTableCellRenderer {
private final DecimalFormat formatter = new DecimalFormat("#.0000");
Renderer() {
super();
this.setHorizontalAlignment(RIGHT);
}
@Override
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column) {
// First format the cell value as required
value = formatter.format((Number) value);
// And pass it on to parent class
return super.getTableCellRendererComponent(
table, value, isSelected, hasFocus, row, column);
}
}
2017-02-13 18:26:32 +01:00
OrderType type = OrderType.BUYLIMIT;
int depth = 40;
2017-02-13 00:07:50 +01:00
public void setGodMode(boolean on) {
TableColumnModel tcm = list.getColumnModel();
if (on) {
if (list.getColumnCount() == 3) {
return;
}
tcm.addColumn(trader_column);
tcm.moveColumn(2, 0);
2017-02-25 20:06:26 +01:00
} else {
if (list.getColumnCount() == 2) {
return;
}
tcm.removeColumn(tcm.getColumn(0));
2017-02-13 00:07:50 +01:00
}
}
2017-02-13 18:26:32 +01:00
/**
* Bla
*/
@Override
2017-02-13 18:26:32 +01:00
public final void cfgChanged() {
boolean gm = Globals.prefs.get(Globals.CfgStrings.GODMODE, "false").equals("true");
setGodMode(gm);
list.invalidate();
list.repaint();
}
2017-02-13 00:07:50 +01:00
public void setType(OrderType type) {
this.type = type;
2017-02-13 18:26:32 +01:00
Globals.se.addBookReceiver(type, this);
}
2017-02-13 00:07:50 +01:00
/**
* Creates new form OrderBookNew
2017-02-13 00:07:50 +01:00
*/
public OrderBook() {
2017-02-13 00:07:50 +01:00
initComponents();
2017-02-25 20:06:26 +01:00
if (Globals.se == null) {
return;
}
model = (DefaultTableModel) this.list.getModel();
trader_column = list.getColumnModel().getColumn(0);
2017-02-26 09:54:31 +01:00
list.getColumnModel().getColumn(1).setCellRenderer(new NummericCellRenderer(3));
list.getColumnModel().getColumn(2).setCellRenderer(new NummericCellRenderer(0));
cfgChanged();
2017-02-13 18:26:32 +01:00
// Globals.se.addBookReceiver(Exchange.OrderType.BUYLIMIT, this);
Globals.addCfgListener(this);
}
2017-02-13 00:07:50 +01:00
boolean oupdate = false;
2017-02-25 20:06:26 +01:00
boolean new_oupdate = false;
void oupdater() {
ArrayList<Order> ob = Globals.se.getOrderBook(type, depth);
model.setRowCount(ob.size());
int row = 0;
for (Order ob1 : ob) {
model.setValueAt(ob1.getAccount().getOwner().getName(), row, 0);
model.setValueAt(ob1.getLimit(), row, 1);
model.setValueAt(ob1.getVolume(), row, 2);
row++;
}
oupdate = false;
}
@Override
public synchronized void UpdateOrderBook() {
if (oupdate) {
2017-02-25 20:06:26 +01:00
new_oupdate=true;
return;
}
2017-02-25 20:06:26 +01:00
oupdate = true;
SwingUtilities.invokeLater(() -> {
2017-02-25 20:06:26 +01:00
oupdater();
});
2017-02-25 20:06:26 +01:00
2017-02-13 00:07:50 +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();
list.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
2017-02-20 23:40:01 +01:00
2017-02-13 00:07:50 +01:00
},
new String [] {
"Trader", "Price", "Volume"
2017-02-13 00:07:50 +01:00
}
) {
Class[] types = new Class [] {
java.lang.String.class, java.lang.Double.class, java.lang.Double.class
2017-02-13 00:07:50 +01:00
};
boolean[] canEdit = new boolean [] {
false, false, false
2017-02-13 00:07:50 +01:00
};
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
jScrollPane1.setViewportView(list);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
2017-02-13 18:26:32 +01:00
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 389, Short.MAX_VALUE)
2017-02-13 00:07:50 +01:00
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
2017-02-13 18:26:32 +01:00
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 284, Short.MAX_VALUE)
2017-02-13 00:07:50 +01:00
);
}// </editor-fold>//GEN-END:initComponents
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable list;
// End of variables declaration//GEN-END:variables
2017-02-13 00:07:50 +01:00
}