OpenSeSim/src/gui/EditAutoTraderList.java

254 lines
9.4 KiB
Java
Raw Normal View History

2017-01-25 00:24:53 +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;
2017-01-26 01:52:03 +01:00
import java.io.IOException;
2017-01-25 02:52:49 +01:00
import java.util.ArrayList;
import java.util.HashMap;
2017-01-26 01:52:03 +01:00
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
2017-02-02 18:39:55 +01:00
import javax.swing.JLabel;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
2017-01-25 00:24:53 +01:00
import javax.swing.table.AbstractTableModel;
2017-01-30 16:52:55 +01:00
import javax.swing.table.DefaultTableCellRenderer;
2017-01-25 00:24:53 +01:00
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
2017-01-25 02:52:49 +01:00
import org.json.JSONArray;
import org.json.JSONObject;
2017-01-26 01:52:03 +01:00
import sesim.AutoTraderConfig;
2017-01-25 00:24:53 +01:00
/**
*
* @author 7u83 <7u83@mail.ru>
*/
public class EditAutoTraderList extends javax.swing.JPanel {
2017-01-30 16:52:55 +01:00
private String getColumnHeader(int i) {
JTableHeader th = list.getTableHeader();
return (String) th.getColumnModel().getColumn(i).getHeaderValue();
}
2017-01-25 00:24:53 +01:00
void save() {
DefaultTableModel model = (DefaultTableModel) list.getModel();
JTableHeader th = list.getTableHeader();
2017-01-30 16:52:55 +01:00
/*
2017-01-25 00:24:53 +01:00
for (int i = 0; i < model.getColumnCount(); i++) {
2017-01-25 02:52:49 +01:00
String hw = (String) th.getColumnModel().getColumn(i).getHeaderValue();
2017-01-25 00:24:53 +01:00
}
2017-01-30 16:52:55 +01:00
*/
2017-01-25 02:52:49 +01:00
JSONArray ja = new JSONArray();
2017-01-25 00:24:53 +01:00
for (int i = 0; i < model.getRowCount(); i++) {
2017-01-25 02:52:49 +01:00
JSONObject jo = new JSONObject();
2017-01-25 00:24:53 +01:00
for (int x = 0; x < model.getColumnCount(); x++) {
Object cw = model.getValueAt(i, x);
2017-02-06 08:14:05 +01:00
//System.out.printf("CWSaver: %s\n",cw.getClass().toString());
2017-01-25 02:52:49 +01:00
if (cw != null) {
jo.put((String) th.getColumnModel().getColumn(x).getHeaderValue(), cw.toString());
2017-01-30 16:52:55 +01:00
}
2017-01-25 00:24:53 +01:00
}
2017-01-25 02:52:49 +01:00
ja.put(jo);
2017-01-25 00:24:53 +01:00
}
2017-01-25 02:52:49 +01:00
Globals.prefs.put("Traders", ja.toString());
2017-01-30 16:52:55 +01:00
}
final void load() {
2017-01-30 22:18:10 +01:00
JSONArray traders = Globals.getTraders();
DefaultTableModel model = (DefaultTableModel) list.getModel();
model.setRowCount(traders.length());
2017-02-06 08:14:05 +01:00
2017-01-30 22:18:10 +01:00
for (int row = 0; row < traders.length(); row++) {
2017-01-30 16:52:55 +01:00
JSONObject rowobj = traders.getJSONObject(row);
for (int col = 0; col < list.getColumnCount(); col++) {
String h = this.getColumnHeader(col);
2017-03-19 16:07:38 +01:00
// System.out.printf("Doing stuff for %s\n", h);
2017-02-06 08:14:05 +01:00
String val = null;
try {
val = rowobj.getString(h);
} catch (Exception e) {
continue;
}
2017-01-30 16:52:55 +01:00
System.out.printf("Want to set (%d,%d): %s\n", row, col, val);
//list.getModel().setValueAt(val, row, col);
Class cl = list.getModel().getColumnClass(col);
System.out.printf("The Class is: %s\n", cl.getName());
Object cv = new Object();
if (cl == Double.class) {
cv = rowobj.getDouble(h);
}
if (cl == String.class) {
cv = rowobj.getString(h);
}
if (cl == Integer.class) {
cv = rowobj.getInt(h);
}
if (cl == Boolean.class) {
cv = rowobj.getBoolean(h);
}
2017-02-06 08:14:05 +01:00
if (cl == Object.class) {
cv = rowobj.getString(h);
2017-01-30 22:18:10 +01:00
}
2017-01-30 16:52:55 +01:00
list.getModel().setValueAt(cv, row, col);
}
}
2017-01-25 00:24:53 +01:00
}
2017-03-19 16:07:38 +01:00
DefaultTableModel model;
private double getFairValue() {
Double money = 0.0;
Double shares = 0.0;
for (int r = 0; r < model.getRowCount(); r++) {
Boolean e = (Boolean) list.getValueAt(r, list.getColumn("Enabled").getModelIndex());
if (!e) {
continue;
}
money += (Double) list.getValueAt(r, list.getColumn("Money").getModelIndex());
shares += (Double) list.getValueAt(r, list.getColumn("Shares").getModelIndex());
System.out.printf("Row: %d %f %f\n", r, money, shares);
}
return money / shares;
}
2017-01-25 00:24:53 +01:00
/**
* Creates new form NewJPanel
*/
public EditAutoTraderList() {
initComponents();
2017-02-06 08:14:05 +01:00
if (Globals.se == null) {
2017-01-30 22:18:10 +01:00
return;
2017-02-06 08:14:05 +01:00
}
2017-01-25 00:24:53 +01:00
2017-01-30 16:52:55 +01:00
this.load();
2017-01-26 01:52:03 +01:00
JComboBox comboBox = new JComboBox();
2017-01-30 16:52:55 +01:00
2017-02-06 08:14:05 +01:00
Globals.getStrategiesIntoComboBox(comboBox);
2017-01-30 16:52:55 +01:00
2017-03-19 16:07:38 +01:00
model = (DefaultTableModel) list.getModel();
2017-01-26 01:52:03 +01:00
list.getColumnModel().getColumn(2).setCellEditor(new DefaultCellEditor(comboBox));
2017-01-30 16:52:55 +01:00
// list.getColumnModel().getColumn(2).setCellRenderer(new javax.swing.table.DefaultTableCellRenderer());
// model.setRowCount(3);
list.setRowHeight(30);
2017-02-06 08:14:05 +01:00
2017-02-02 18:39:55 +01:00
list.getModel().addTableModelListener((TableModelEvent e) -> {
2017-03-19 16:07:38 +01:00
this.summary.setText(String.format("Fair Value: %.5f", this.getFairValue()));
2017-02-06 08:14:05 +01:00
2017-02-02 18:39:55 +01:00
});
2017-03-19 16:07:38 +01:00
this.summary.setText(String.format("Fair Value: %.5f", this.getFairValue()));
2017-01-30 22:18:10 +01:00
}
2017-02-06 08:14:05 +01:00
void add() {
DefaultTableModel model = (DefaultTableModel) list.getModel();
model.setRowCount(model.getRowCount() + 1);
2017-01-25 00:24:53 +01:00
}
2017-02-06 08:14:05 +01:00
2017-03-19 16:07:38 +01:00
// JLabel summary = null;
2017-01-25 00:24:53 +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-03-19 16:07:38 +01:00
summary = new javax.swing.JLabel();
2017-01-25 00:24:53 +01:00
2017-01-30 16:52:55 +01:00
list.setAutoCreateRowSorter(true);
2017-01-25 00:24:53 +01:00
list.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
2017-01-26 01:52:03 +01:00
{"Alice", new Integer(1), "", new Double(10000.0), new Double(100.0), new Boolean(true)},
{"Bob", new Integer(1), null, new Double(1000.0), new Double(100.0), new Boolean(true)}
2017-01-25 00:24:53 +01:00
},
new String [] {
"Name", "Count", "Strategy", "Money", "Shares", "Enabled"
}
) {
Class[] types = new Class [] {
2017-01-26 01:52:03 +01:00
java.lang.String.class, java.lang.Integer.class, java.lang.Object.class, java.lang.Double.class, java.lang.Double.class, java.lang.Boolean.class
2017-01-25 00:24:53 +01:00
};
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
});
jScrollPane1.setViewportView(list);
2017-03-19 16:07:38 +01:00
summary.setText("jLabel1");
2017-01-25 00:24:53 +01:00
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
2017-03-19 16:07:38 +01:00
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 446, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(summary, javax.swing.GroupLayout.PREFERRED_SIZE, 264, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE)))
2017-01-25 00:24:53 +01:00
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
2017-03-19 16:07:38 +01:00
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 257, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(summary)
.addContainerGap(16, Short.MAX_VALUE))
2017-01-25 00:24:53 +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;
2017-03-19 16:07:38 +01:00
private javax.swing.JLabel summary;
2017-01-25 00:24:53 +01:00
// End of variables declaration//GEN-END:variables
}