Orderbook updates are synchronized now.

No "Out of Index Exeptions" aynmore
This commit is contained in:
7u83 2017-02-20 06:01:39 +01:00
parent 6719f9a7f5
commit 0603bbc0d3

View File

@ -28,6 +28,7 @@ package gui.orderbook;
import gui.Globals; import gui.Globals;
import gui.Globals.CfgListener; import gui.Globals.CfgListener;
import java.util.ArrayList; import java.util.ArrayList;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel; import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn; import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel; import javax.swing.table.TableColumnModel;
@ -43,52 +44,52 @@ public class OrderBook extends javax.swing.JPanel implements Exchange.BookReceiv
DefaultTableModel model; DefaultTableModel model;
TableColumn trader_column = null; TableColumn trader_column = null;
OrderType type = OrderType.BUYLIMIT; OrderType type = OrderType.BUYLIMIT;
int depth=40; int depth = 40;
public void setGodMode(boolean on) { public void setGodMode(boolean on) {
TableColumnModel tcm = list.getColumnModel(); TableColumnModel tcm = list.getColumnModel();
if (on){ if (on) {
if (list.getColumnCount()==3){ if (list.getColumnCount() == 3) {
return; return;
} }
tcm.addColumn(trader_column); tcm.addColumn(trader_column);
tcm.moveColumn(2, 0); tcm.moveColumn(2, 0);
return; return;
} } else {
else{ if (list.getColumnCount() == 2) {
if (list.getColumnCount()==2){
return; return;
} }
tcm.removeColumn(tcm.getColumn(0)); tcm.removeColumn(tcm.getColumn(0));
} }
} }
/** /**
* Bla * Bla
*/ */
@Override @Override
public final void cfgChanged() { public final void cfgChanged() {
boolean gm = Globals.prefs.get(Globals.CfgStrings.GODMODE, "false").equals("true"); boolean gm = Globals.prefs.get(Globals.CfgStrings.GODMODE, "false").equals("true");
System.out.printf("GM %s\n",gm?"true":"false"); System.out.printf("GM %s\n", gm ? "true" : "false");
setGodMode(gm); setGodMode(gm);
list.invalidate(); list.invalidate();
list.repaint(); list.repaint();
} }
public void setType(OrderType type){ public void setType(OrderType type) {
this.type=type; this.type = type;
Globals.se.addBookReceiver(type, this); Globals.se.addBookReceiver(type, this);
} }
/** /**
* Creates new form OrderBookNew * Creates new form OrderBookNew
*/ */
public OrderBook() { public OrderBook() {
initComponents(); initComponents();
if (Globals.se==null) if (Globals.se == null) {
return; return;
}
model = (DefaultTableModel) this.list.getModel(); model = (DefaultTableModel) this.list.getModel();
trader_column = list.getColumnModel().getColumn(0); trader_column = list.getColumnModel().getColumn(0);
cfgChanged(); cfgChanged();
@ -96,9 +97,31 @@ public class OrderBook extends javax.swing.JPanel implements Exchange.BookReceiv
Globals.addCfgListener(this); Globals.addCfgListener(this);
} }
boolean oupdate = false;
@Override @Override
public void UpdateOrderBook() { public synchronized void UpdateOrderBook() {
ArrayList<Order> ob = Globals.se.getOrderBook(type, depth);
if (oupdate) {
return;
}
oupdate = true;
SwingUtilities.invokeLater(() -> {
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;
});
/* ArrayList<Order> ob = Globals.se.getOrderBook(type, depth);
model.setRowCount(ob.size()); model.setRowCount(ob.size());
int row = 0; int row = 0;
for (Order ob1 : ob) { for (Order ob1 : ob) {
@ -107,7 +130,7 @@ public class OrderBook extends javax.swing.JPanel implements Exchange.BookReceiv
model.setValueAt(ob1.getVolume(), row, 2); model.setValueAt(ob1.getVolume(), row, 2);
row++; row++;
} }
*/
} }
/** /**
@ -168,5 +191,4 @@ public class OrderBook extends javax.swing.JPanel implements Exchange.BookReceiv
private javax.swing.JTable list; private javax.swing.JTable list;
// End of variables declaration//GEN-END:variables // End of variables declaration//GEN-END:variables
} }