OpenSeSim/src/gui/EditStrategies.java

460 lines
18 KiB
Java

/*
* 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;
import java.awt.Frame;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.logging.Logger;
import javax.swing.JPanel;
import org.json.JSONArray;
import org.json.JSONObject;
import sesim.AutoTraderConfig;
import sesim.AutoTraderGui;
import sesim.AutoTraderInterface;
/**
*
* @author 7u83 <7u83@mail.ru>
*/
public final class EditStrategies extends javax.swing.JDialog {
TreeMap<String, JSONObject> strategies = new TreeMap();
void initComboBox() {
this.reloadStrategyConfigs();
this.jComboBoxStrategySelector.removeAllItems();
/*
ArrayList<String> sn = Globals.tloader.getDefaultStrategyNames();
*/
Iterator<String> i = strategies.keySet().iterator();
while (i.hasNext()) {
this.jComboBoxStrategySelector.addItem(i.next());
}
}
/**
* Creates new form EditTradingStrategies
*/
public EditStrategies(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
this.setLocationRelativeTo(this.getParent());
initComboBox();
}
final String STRATEGYPREFS = "Strategies";
void reloadStrategyConfigs() {
strategies = new TreeMap();
JSONObject cfgs = Globals.getStrategies();
Iterator<String> i = cfgs.keys();
while (i.hasNext()) {
String k = i.next();
JSONObject o = cfgs.getJSONObject(k);
strategies.put(k, o);
}
}
/**
* 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() {
jLabel1 = new javax.swing.JLabel();
jComboBoxStrategySelector = new javax.swing.JComboBox<>();
jScrollPane1 = new javax.swing.JScrollPane();
guiPanel = new javax.swing.JPanel();
defaultGuiPanel = new javax.swing.JPanel();
jLabel2 = new javax.swing.JLabel();
jSaveButton = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton1 = new javax.swing.JButton();
jRemoveButton = new javax.swing.JButton();
jNewButton = new javax.swing.JButton();
jBaseLabel = new javax.swing.JLabel();
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText("No config available");
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
jComboBoxStrategySelector.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
jComboBoxStrategySelector.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jComboBoxStrategySelectorActionPerformed(evt);
}
});
guiPanel.setLayout(new java.awt.BorderLayout());
jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel2.setText("No config available");
jLabel2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
javax.swing.GroupLayout defaultGuiPanelLayout = new javax.swing.GroupLayout(defaultGuiPanel);
defaultGuiPanel.setLayout(defaultGuiPanelLayout);
defaultGuiPanelLayout.setHorizontalGroup(
defaultGuiPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, defaultGuiPanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 612, Short.MAX_VALUE)
.addContainerGap())
);
defaultGuiPanelLayout.setVerticalGroup(
defaultGuiPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(defaultGuiPanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 407, Short.MAX_VALUE)
.addContainerGap())
);
guiPanel.add(defaultGuiPanel, java.awt.BorderLayout.CENTER);
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() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
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);
}
});
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");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(jNewButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.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))
.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)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.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)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jSaveButton)
.addComponent(jButton2)
.addComponent(jButton1)
.addComponent(jRemoveButton)
.addComponent(jNewButton))
.addContainerGap())
);
pack();
}// </editor-fold>//GEN-END:initComponents
AutoTraderGui acgui;
AutoTraderInterface ac;
private void jComboBoxStrategySelectorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxStrategySelectorActionPerformed
String cfglist = Globals.prefs.get(STRATEGYPREFS, "{}");
JSONObject cfgs = new JSONObject(cfglist);
String item = (String) this.jComboBoxStrategySelector.getSelectedItem();
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);
ac = Globals.tloader.getStrategyBase(base);
if (ac == null) {
System.out.print("BASE IST NULL\n");
Globals.LOGGER.info(String.format("Can't load: %s\n", base));
//System.exit(0);
return;
}
ac.putConfig(o);
this.jBaseLabel.setText(base);
acgui = ac.getGui();
guiPanel.removeAll();
if (acgui != null) {
guiPanel.add(acgui, java.awt.BorderLayout.CENTER);
acgui.setVisible(true);
} else {
System.out.printf("Adding default gui panel\n");
guiPanel.add(this.defaultGuiPanel, java.awt.BorderLayout.CENTER);
}
this.revalidate();
System.out.printf("Setted the dings\n", "");
JSONObject jo = ac.getConfig();
if (jo != null) {
System.out.printf("%s\n", jo.toString(2));
// jo = new JSONObject(jo.toString());
}
// System.out.print("Crete auto trader afeter select\n");
// AutoTrader at = ac.createTrader(Globals.se, jo, 100, 100);
this.repaint();
}//GEN-LAST:event_jComboBoxStrategySelectorActionPerformed
private void jSaveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jSaveButtonActionPerformed
if (acgui!=null)
acgui.save();
JSONObject o = ac.getConfig();
String item = (String) this.jComboBoxStrategySelector.getSelectedItem();
// o.put("base", ac.getDisplayName());
o.put("base", ac.getClass().getCanonicalName());
System.out.printf("The big name: %s\n", ac.getClass().getCanonicalName());
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
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;
AutoTraderInterface ac = Globals.tloader.getStrategyBase(sd.result.base);
JSONObject cfg = ac.getConfig();
System.out.printf("Initial cfg %s\n", cfg.toString(2));
cfg.put("base", ac.getClass().getCanonicalName());
Globals.saveStrategy(sd.result.name, cfg);
this.initComboBox();
}//GEN-LAST:event_jNewButtonActionPerformed
/**
* @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) {
java.util.logging.Logger.getLogger(EditStrategies.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(EditStrategies.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(EditStrategies.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(EditStrategies.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>
/* Create and display the dialog */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
EditStrategies dialog = new EditStrategies(new javax.swing.JFrame(), true);
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
private javax.swing.JPanel defaultGuiPanel;
private javax.swing.JPanel guiPanel;
private javax.swing.JLabel jBaseLabel;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JComboBox<String> jComboBoxStrategySelector;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JButton jNewButton;
private javax.swing.JButton jRemoveButton;
private javax.swing.JButton jSaveButton;
private javax.swing.JScrollPane jScrollPane1;
// End of variables declaration//GEN-END:variables
}