OpenSeSim/src/gui/EditStrategies.java

460 lines
18 KiB
Java
Raw Normal View History

2017-01-26 01:52:03 +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-29 22:21:12 +01:00
import java.awt.Frame;
2017-01-26 01:52:03 +01:00
import java.util.ArrayList;
2017-01-29 22:21:12 +01:00
import java.util.HashMap;
import java.util.Iterator;
import java.util.SortedMap;
import java.util.TreeMap;
2017-01-28 15:34:24 +01:00
import java.util.logging.Logger;
2017-01-27 23:34:00 +01:00
import javax.swing.JPanel;
2017-01-28 15:34:24 +01:00
import org.json.JSONArray;
import org.json.JSONObject;
2017-02-13 00:07:50 +01:00
2017-01-26 01:52:03 +01:00
import sesim.AutoTraderConfig;
2017-01-28 15:34:24 +01:00
import sesim.AutoTraderGui;
2017-02-11 08:36:45 +01:00
import sesim.AutoTraderInterface;
2017-01-26 01:52:03 +01:00
/**
*
* @author 7u83 <7u83@mail.ru>
*/
2017-01-29 22:21:12 +01:00
public final class EditStrategies extends javax.swing.JDialog {
2017-01-30 22:18:10 +01:00
2017-01-29 22:21:12 +01:00
TreeMap<String, JSONObject> strategies = new TreeMap();
void initComboBox() {
this.reloadStrategyConfigs();
this.jComboBoxStrategySelector.removeAllItems();
2017-01-30 22:18:10 +01:00
/*
ArrayList<String> sn = Globals.tloader.getDefaultStrategyNames();
*/
2017-01-29 22:21:12 +01:00
Iterator<String> i = strategies.keySet().iterator();
while (i.hasNext()) {
this.jComboBoxStrategySelector.addItem(i.next());
2017-01-30 22:18:10 +01:00
}
2017-01-29 22:21:12 +01:00
}
2017-01-26 01:52:03 +01:00
/**
* Creates new form EditTradingStrategies
*/
2017-01-28 15:34:24 +01:00
public EditStrategies(java.awt.Frame parent, boolean modal) {
2017-01-26 01:52:03 +01:00
super(parent, modal);
initComponents();
2017-01-28 15:34:24 +01:00
this.setLocationRelativeTo(this.getParent());
2017-01-27 23:34:00 +01:00
2017-01-29 22:21:12 +01:00
initComboBox();
2017-01-27 23:34:00 +01:00
2017-01-26 01:52:03 +01:00
}
2017-01-27 23:34:00 +01:00
2017-01-28 15:34:24 +01:00
2017-02-11 08:36:45 +01:00
2017-01-29 22:21:12 +01:00
final String STRATEGYPREFS = "Strategies";
2017-01-28 15:34:24 +01:00
2017-01-29 22:21:12 +01:00
void reloadStrategyConfigs() {
strategies = new TreeMap();
2017-01-30 22:18:10 +01:00
JSONObject cfgs = Globals.getStrategies();
2017-01-29 22:21:12 +01:00
Iterator<String> i = cfgs.keys();
while (i.hasNext()) {
String k = i.next();
JSONObject o = cfgs.getJSONObject(k);
strategies.put(k, o);
}
2017-01-28 15:34:24 +01:00
}
2017-01-26 01:52:03 +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() {
2017-01-29 22:21:12 +01:00
jLabel1 = new javax.swing.JLabel();
2017-01-28 15:34:24 +01:00
jComboBoxStrategySelector = new javax.swing.JComboBox<>();
2017-01-29 22:21:12 +01:00
jScrollPane1 = new javax.swing.JScrollPane();
2017-01-27 23:34:00 +01:00
guiPanel = new javax.swing.JPanel();
defaultGuiPanel = new javax.swing.JPanel();
2017-01-30 22:18:10 +01:00
jLabel2 = new javax.swing.JLabel();
2017-01-29 22:21:12 +01:00
jSaveButton = new javax.swing.JButton();
2017-01-28 15:34:24 +01:00
jButton2 = new javax.swing.JButton();
2017-01-29 22:21:12 +01:00
jButton1 = new javax.swing.JButton();
jRemoveButton = new javax.swing.JButton();
2017-01-30 22:18:10 +01:00
jNewButton = new javax.swing.JButton();
jBaseLabel = new javax.swing.JLabel();
2017-01-29 22:21:12 +01:00
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText("No config available");
2017-01-26 01:52:03 +01:00
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
2017-01-28 15:34:24 +01:00
jComboBoxStrategySelector.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
jComboBoxStrategySelector.addActionListener(new java.awt.event.ActionListener() {
2017-01-26 01:52:03 +01:00
public void actionPerformed(java.awt.event.ActionEvent evt) {
2017-01-28 15:34:24 +01:00
jComboBoxStrategySelectorActionPerformed(evt);
2017-01-26 01:52:03 +01:00
}
});
2017-01-27 23:34:00 +01:00
guiPanel.setLayout(new java.awt.BorderLayout());
2017-01-30 22:18:10 +01:00
jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel2.setText("No config available");
jLabel2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
2017-01-27 23:34:00 +01:00
javax.swing.GroupLayout defaultGuiPanelLayout = new javax.swing.GroupLayout(defaultGuiPanel);
defaultGuiPanel.setLayout(defaultGuiPanelLayout);
defaultGuiPanelLayout.setHorizontalGroup(
defaultGuiPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
2017-01-30 22:18:10 +01:00
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, defaultGuiPanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 612, Short.MAX_VALUE)
.addContainerGap())
2017-01-26 01:52:03 +01:00
);
2017-01-27 23:34:00 +01:00
defaultGuiPanelLayout.setVerticalGroup(
defaultGuiPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
2017-01-30 22:18:10 +01:00
.addGroup(defaultGuiPanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 407, Short.MAX_VALUE)
.addContainerGap())
2017-01-26 01:52:03 +01:00
);
2017-01-27 23:34:00 +01:00
guiPanel.add(defaultGuiPanel, java.awt.BorderLayout.CENTER);
2017-01-26 01:52:03 +01:00
2017-01-29 22:21:12 +01:00
jScrollPane1.setViewportView(guiPanel);
jSaveButton.setMnemonic('s');
jSaveButton.setText("Save");
jSaveButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jSaveButtonActionPerformed(evt);
}
});
jButton2.setMnemonic('c');
jButton2.setText("Close");
jButton2.addActionListener(new java.awt.event.ActionListener() {
2017-01-28 15:34:24 +01:00
public void actionPerformed(java.awt.event.ActionEvent evt) {
2017-01-29 22:21:12 +01:00
jButton2ActionPerformed(evt);
2017-01-28 15:34:24 +01:00
}
});
2017-01-29 22:21:12 +01:00
jButton1.setMnemonic('a');
jButton1.setText("Save as ...");
jRemoveButton.setMnemonic('r');
jRemoveButton.setText("Remove");
jRemoveButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jRemoveButtonActionPerformed(evt);
}
});
2017-01-28 15:34:24 +01:00
2017-01-30 22:18:10 +01:00
jNewButton.setMnemonic('n');
jNewButton.setText("New");
jNewButton.setToolTipText("");
jNewButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jNewButtonActionPerformed(evt);
}
});
jBaseLabel.setText("base");
2017-01-26 01:52:03 +01:00
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
2017-01-30 22:18:10 +01:00
.addContainerGap()
2017-01-26 01:52:03 +01:00
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
2017-01-30 22:18:10 +01:00
.addComponent(jScrollPane1)
2017-01-26 01:52:03 +01:00
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
2017-01-30 22:18:10 +01:00
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(jNewButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
2017-01-29 22:21:12 +01:00
.addComponent(jRemoveButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jSaveButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2)
.addGap(9, 9, 9))
2017-01-30 22:18:10 +01:00
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jBaseLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 263, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jComboBoxStrategySelector, javax.swing.GroupLayout.PREFERRED_SIZE, 361, javax.swing.GroupLayout.PREFERRED_SIZE)))
2017-01-26 01:52:03 +01:00
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
2017-01-30 22:18:10 +01:00
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jComboBoxStrategySelector, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jBaseLabel))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 425, Short.MAX_VALUE)
.addGap(12, 12, 12)
2017-01-28 15:34:24 +01:00
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
2017-01-29 22:21:12 +01:00
.addComponent(jSaveButton)
.addComponent(jButton2)
2017-01-28 15:34:24 +01:00
.addComponent(jButton1)
2017-01-30 22:18:10 +01:00
.addComponent(jRemoveButton)
.addComponent(jNewButton))
2017-01-26 01:52:03 +01:00
.addContainerGap())
);
pack();
}// </editor-fold>//GEN-END:initComponents
2017-01-29 22:21:12 +01:00
AutoTraderGui acgui;
2017-02-11 08:36:45 +01:00
AutoTraderInterface ac;
2017-01-29 22:21:12 +01:00
2017-01-28 15:34:24 +01:00
private void jComboBoxStrategySelectorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxStrategySelectorActionPerformed
2017-01-29 22:21:12 +01:00
String cfglist = Globals.prefs.get(STRATEGYPREFS, "{}");
JSONObject cfgs = new JSONObject(cfglist);
String item = (String) this.jComboBoxStrategySelector.getSelectedItem();
2017-02-26 03:01:24 +01:00
2017-01-29 22:21:12 +01:00
if (item == null) {
return;
}
String base = null;
JSONObject o = null;
try {
o = (JSONObject) cfgs.get(item);
System.out.printf("Have got a jason obj %s\n", o.toString(9));
System.out.print("--------------------------------------\n");
base = o.getString("base");
} catch (Exception e) {
System.out.printf("Exception return\n", "");
base = item;
}
System.out.printf("Base %s\n", base);
2017-01-30 22:18:10 +01:00
ac = Globals.tloader.getStrategyBase(base);
2017-01-27 23:34:00 +01:00
if (ac == null) {
2017-01-29 22:21:12 +01:00
System.out.print("BASE IST NULL\n");
2017-01-30 22:18:10 +01:00
Globals.LOGGER.info(String.format("Can't load: %s\n", base));
//System.exit(0);
2017-01-29 22:21:12 +01:00
2017-01-27 23:34:00 +01:00
return;
}
2017-01-29 22:21:12 +01:00
ac.putConfig(o);
2017-01-30 22:18:10 +01:00
this.jBaseLabel.setText(base);
2017-01-29 22:21:12 +01:00
2017-01-28 15:34:24 +01:00
acgui = ac.getGui();
2017-01-27 23:34:00 +01:00
guiPanel.removeAll();
2017-01-28 15:34:24 +01:00
if (acgui != null) {
2017-01-27 23:34:00 +01:00
2017-01-28 15:34:24 +01:00
guiPanel.add(acgui, java.awt.BorderLayout.CENTER);
acgui.setVisible(true);
2017-01-27 23:34:00 +01:00
} else {
2017-02-07 01:53:43 +01:00
System.out.printf("Adding default gui panel\n");
2017-01-27 23:34:00 +01:00
guiPanel.add(this.defaultGuiPanel, java.awt.BorderLayout.CENTER);
}
this.revalidate();
System.out.printf("Setted the dings\n", "");
2017-01-28 15:34:24 +01:00
JSONObject jo = ac.getConfig();
if (jo != null) {
System.out.printf("%s\n", jo.toString(2));
// jo = new JSONObject(jo.toString());
2017-01-29 22:21:12 +01:00
2017-01-28 15:34:24 +01:00
}
// System.out.print("Crete auto trader afeter select\n");
// AutoTrader at = ac.createTrader(Globals.se, jo, 100, 100);
2017-01-28 15:34:24 +01:00
2017-01-27 23:34:00 +01:00
this.repaint();
2017-01-28 15:34:24 +01:00
}//GEN-LAST:event_jComboBoxStrategySelectorActionPerformed
2017-01-29 22:21:12 +01:00
private void jSaveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jSaveButtonActionPerformed
2017-02-07 01:53:43 +01:00
if (acgui!=null)
acgui.save();
2017-01-29 22:21:12 +01:00
JSONObject o = ac.getConfig();
String item = (String) this.jComboBoxStrategySelector.getSelectedItem();
2017-01-30 22:18:10 +01:00
// o.put("base", ac.getDisplayName());
o.put("base", ac.getClass().getCanonicalName());
System.out.printf("The big name: %s\n", ac.getClass().getCanonicalName());
2017-01-29 22:21:12 +01:00
JSONObject o0 = strategies.get(item);
if (o0 == null) {
StrategySaveDialog d = new StrategySaveDialog((Frame) this.getParent(), true);
d.setLabelAndName(item, "");
d.setVisible(true);
if (d.getName().equals("")) {
return;
}
item = item + " - " + d.getName();
}
String cfglist = Globals.prefs.get(STRATEGYPREFS, "{}");
JSONObject cfgs = new JSONObject(cfglist);
cfgs.put(item, o);
Globals.prefs.put(STRATEGYPREFS, cfgs.toString());
//this.reloadStrategyConfigs();
this.initComboBox();
this.jComboBoxStrategySelector.setSelectedItem(item);
//this.jComboBoxStrategySelector.
System.out.printf("Saving item: %s\nJSON %s\n", item, o.toString(3));
}//GEN-LAST:event_jSaveButtonActionPerformed
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
this.dispose();
}//GEN-LAST:event_jButton2ActionPerformed
private void jRemoveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRemoveButtonActionPerformed
System.out.print("Hello - remove button\n");
String selected = (String) this.jComboBoxStrategySelector.getSelectedItem();
System.out.printf("Selected: %s\n", selected);
String cfglist = Globals.prefs.get(STRATEGYPREFS, "{}");
JSONObject cfgs = new JSONObject(cfglist);
//System.out.printf("CFG vorher: %s\n", cfgs.toString(3));
cfgs.remove(selected);
// System.out.printf("Neue nachher: %s\n", cfgs.toString(3));
Globals.prefs.put(STRATEGYPREFS, cfgs.toString());
this.initComboBox();
}//GEN-LAST:event_jRemoveButtonActionPerformed
2017-01-26 01:52:03 +01:00
2017-01-30 22:18:10 +01:00
private void jNewButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jNewButtonActionPerformed
NewStrategyDialog sd = new NewStrategyDialog((Frame) this.getParent(),true);
sd.setVisible(true);
if (sd.result==null)
return;
2017-02-11 08:36:45 +01:00
AutoTraderInterface ac = Globals.tloader.getStrategyBase(sd.result.base);
2017-01-30 22:18:10 +01:00
JSONObject cfg = ac.getConfig();
2017-02-07 01:53:43 +01:00
System.out.printf("Initial cfg %s\n", cfg.toString(2));
cfg.put("base", ac.getClass().getCanonicalName());
2017-01-30 22:18:10 +01:00
Globals.saveStrategy(sd.result.name, cfg);
this.initComboBox();
}//GEN-LAST:event_jNewButtonActionPerformed
2017-01-26 01:52:03 +01:00
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
2017-01-28 15:34:24 +01:00
java.util.logging.Logger.getLogger(EditStrategies.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
2017-01-26 01:52:03 +01:00
} catch (InstantiationException ex) {
2017-01-28 15:34:24 +01:00
java.util.logging.Logger.getLogger(EditStrategies.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
2017-01-26 01:52:03 +01:00
} catch (IllegalAccessException ex) {
2017-01-28 15:34:24 +01:00
java.util.logging.Logger.getLogger(EditStrategies.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
2017-01-26 01:52:03 +01:00
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
2017-01-28 15:34:24 +01:00
java.util.logging.Logger.getLogger(EditStrategies.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
2017-01-26 01:52:03 +01:00
}
//</editor-fold>
2017-01-28 15:34:24 +01:00
//</editor-fold>
2017-01-26 01:52:03 +01:00
/* Create and display the dialog */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
2017-01-28 15:34:24 +01:00
EditStrategies dialog = new EditStrategies(new javax.swing.JFrame(), true);
2017-01-26 01:52:03 +01:00
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
dialog.setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
2017-01-27 23:34:00 +01:00
private javax.swing.JPanel defaultGuiPanel;
private javax.swing.JPanel guiPanel;
2017-01-30 22:18:10 +01:00
private javax.swing.JLabel jBaseLabel;
2017-01-28 15:34:24 +01:00
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JComboBox<String> jComboBoxStrategySelector;
2017-01-27 23:34:00 +01:00
private javax.swing.JLabel jLabel1;
2017-01-30 22:18:10 +01:00
private javax.swing.JLabel jLabel2;
private javax.swing.JButton jNewButton;
2017-01-29 22:21:12 +01:00
private javax.swing.JButton jRemoveButton;
private javax.swing.JButton jSaveButton;
private javax.swing.JScrollPane jScrollPane1;
2017-01-26 01:52:03 +01:00
// End of variables declaration//GEN-END:variables
}