OpenSeSim/src/opensesim/gui/orderbook/OrderBookPanel.java

275 lines
8.3 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.
*/
2018-11-30 12:27:41 +01:00
package opensesim.gui.orderbook;
2017-02-13 00:07:50 +01:00
2018-11-30 12:27:41 +01:00
import opensesim.gui.Globals;
import opensesim.gui.util.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;
2018-12-24 15:12:32 +01:00
import java.util.Collection;
2017-04-11 07:56:43 +02:00
import java.util.Timer;
import java.util.TimerTask;
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;
2018-12-24 15:12:32 +01:00
import opensesim.world.GodWorld;
import opensesim.world.Order;
import opensesim.world.TradingAPI;
import opensesim.world.scheduler.Event;
import opensesim.world.scheduler.EventListener;
2017-02-13 00:07:50 +01:00
/**
*
* @author 7u83 <7u83@mail.ru>
*/
2018-12-24 15:12:32 +01:00
public class OrderBookPanel extends javax.swing.JPanel implements EventListener {
2017-02-13 00:07:50 +01:00
DefaultTableModel model;
TableColumn trader_column = null;
2018-12-24 15:12:32 +01:00
TradingAPI api=null;
@Override
public long receive(Event task) {
synchronized (this) {
if (oupdate) {
new_oupdate = true;
return 0;
}
oupdate = true;
}
SwingUtilities.invokeLater(() -> {
oupdater();
});
return 0;
}
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);
}
}
2018-12-24 15:12: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
*/
2018-12-24 15:12:32 +01:00
/* @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();
}
2018-12-24 15:12:32 +01:00
*/
2017-02-13 00:07:50 +01:00
2018-12-24 15:12:32 +01:00
/*
public void setType(OrderType type) {
this.type = type;
2017-02-13 18:26:32 +01:00
Globals.se.addBookReceiver(type, this);
}
2018-12-24 15:12:32 +01:00
*/
GodWorld godworld;
2017-02-13 00:07:50 +01:00
/**
* Creates new form OrderBookNew
2017-02-13 00:07:50 +01:00
*/
2018-12-24 15:12:32 +01:00
public OrderBookPanel(GodWorld godworld) {
2017-02-13 00:07:50 +01:00
initComponents();
2017-02-25 20:06:26 +01:00
2018-12-24 15:12:32 +01:00
if (Globals.world == null) {
return;
}
2018-12-24 15:12:32 +01:00
this.godworld = godworld;
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));
2017-04-08 01:35:13 +02:00
list.getColumnModel().getColumn(2).setCellRenderer(new NummericCellRenderer(0));
2018-12-24 15:12:32 +01:00
// cfgChanged();
2017-02-13 18:26:32 +01:00
// Globals.se.addBookReceiver(Exchange.OrderType.BUYLIMIT, this);
2018-12-24 15:12:32 +01:00
// Globals.addCfgListener(this);
2017-04-11 07:56:43 +02:00
new Timer().schedule(new TimerTask() {
@Override
public void run() {
// System.out.printf("Update order book\n");
// UpdateOrderBook();
}
}, 1000, 1000);
}
2017-02-13 00:07:50 +01:00
boolean oupdate = false;
2017-02-25 20:06:26 +01:00
boolean new_oupdate = false;
2017-04-08 01:35:13 +02:00
long ouctr = 0;
2017-02-25 20:06:26 +01:00
void oupdater() {
2018-12-24 15:12:32 +01:00
// ArrayList<Order> ob = Globals.se.getOrderBook(type, depth);
Collection <Order> ob = api.getOrderBook(Order.Type.BUY);
2017-02-25 20:06:26 +01:00
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++;
}
2017-04-08 01:35:13 +02:00
synchronized (this) {
oupdate = new_oupdate;
new_oupdate = false;
}
if (oupdate) {
SwingUtilities.invokeLater(() -> {
oupdater();
});
}
2017-02-25 20:06:26 +01:00
}
2018-12-24 15:12:32 +01:00
// @Override
2017-04-08 01:35:13 +02:00
public void UpdateOrderBook() {
2017-04-08 01:35:13 +02:00
synchronized (this) {
if (oupdate) {
new_oupdate = true;
return;
}
oupdate = true;
}
2017-02-25 20:06:26 +01:00
SwingUtilities.invokeLater(() -> {
2017-02-25 20:06:26 +01:00
oupdater();
});
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
}