2017-01-22 10:04: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;
|
2017-01-22 10:04:50 +01:00
|
|
|
|
2017-01-31 08:04:11 +01:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileNotFoundException;
|
2017-04-08 18:46:42 +02:00
|
|
|
import java.io.IOException;
|
2017-01-31 08:04:11 +01:00
|
|
|
import java.io.PrintWriter;
|
2018-12-01 22:36:57 +01:00
|
|
|
import java.lang.reflect.InvocationTargetException;
|
2018-11-30 12:27:41 +01:00
|
|
|
import java.net.MalformedURLException;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.net.URLClassLoader;
|
2017-04-08 18:46:42 +02:00
|
|
|
import java.nio.file.Files;
|
2017-02-13 09:02:40 +01:00
|
|
|
import java.util.ArrayList;
|
2018-12-01 11:37:25 +01:00
|
|
|
import java.util.Collection;
|
2018-12-01 22:36:57 +01:00
|
|
|
import java.util.Comparator;
|
2017-01-30 22:18:10 +01:00
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.TreeMap;
|
2017-01-31 08:04:11 +01:00
|
|
|
import java.util.logging.Level;
|
2017-01-28 15:34:24 +01:00
|
|
|
import java.util.logging.Logger;
|
2017-01-25 02:52:49 +01:00
|
|
|
import java.util.prefs.Preferences;
|
2017-01-30 22:18:10 +01:00
|
|
|
import javax.swing.JComboBox;
|
2018-11-30 12:27:41 +01:00
|
|
|
import javax.swing.JDialog;
|
2017-02-07 01:53:43 +01:00
|
|
|
import javax.swing.JFrame;
|
2018-11-30 12:27:41 +01:00
|
|
|
import javax.swing.LookAndFeel;
|
2017-01-25 02:52:49 +01:00
|
|
|
import javax.swing.UIManager;
|
2018-12-03 00:03:44 +01:00
|
|
|
import opensesim.world.AbstractAsset;
|
|
|
|
import opensesim.world.World;
|
2018-12-01 22:36:57 +01:00
|
|
|
import opensesim.gui.AssetEditor.AssetEditorPanel;
|
2017-01-30 22:18:10 +01:00
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONObject;
|
2018-11-30 12:27:41 +01:00
|
|
|
import opensesim.old_sesim.AutoTraderLoader;
|
|
|
|
import opensesim.old_sesim.IndicatorLoader;
|
2018-12-01 11:37:25 +01:00
|
|
|
import opensesim.util.XClassLoader.ClassCache;
|
2017-01-25 02:52:49 +01:00
|
|
|
|
2017-01-22 10:04:50 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @author 7u83 <7u83@mail.ru>
|
|
|
|
*/
|
|
|
|
public class Globals {
|
2018-12-01 11:48:54 +01:00
|
|
|
|
2018-11-30 12:27:41 +01:00
|
|
|
public static World world;
|
2017-04-16 16:34:51 +02:00
|
|
|
|
2017-04-08 18:46:42 +02:00
|
|
|
public static final String SESIM_FILEEXTENSION = "sesim";
|
2017-04-08 22:56:32 +02:00
|
|
|
public static final Double SESIM_FILEVERSION = 0.1;
|
2017-04-16 16:34:51 +02:00
|
|
|
|
2018-11-30 12:27:41 +01:00
|
|
|
public static final String SESIM_APPTITLE = "OpenSeSim - Stock Exchange Simulator";
|
2017-03-21 07:53:06 +01:00
|
|
|
|
|
|
|
public interface CfgListener {
|
|
|
|
|
2017-02-13 09:02:40 +01:00
|
|
|
void cfgChanged();
|
|
|
|
}
|
2017-03-21 07:53:06 +01:00
|
|
|
|
|
|
|
static ArrayList<CfgListener> cfg_listeners = new ArrayList<>();
|
|
|
|
|
|
|
|
public static void notifyCfgListeners() {
|
|
|
|
for (CfgListener l : cfg_listeners) {
|
2017-02-13 09:02:40 +01:00
|
|
|
l.cfgChanged();
|
|
|
|
}
|
|
|
|
}
|
2017-03-21 07:53:06 +01:00
|
|
|
|
|
|
|
public static void addCfgListener(CfgListener l) {
|
2017-02-13 09:02:40 +01:00
|
|
|
cfg_listeners.add(l);
|
|
|
|
}
|
2017-03-21 07:53:06 +01:00
|
|
|
|
2017-02-07 01:53:43 +01:00
|
|
|
public static JFrame frame;
|
2017-01-26 01:52:03 +01:00
|
|
|
|
2017-04-16 16:34:51 +02:00
|
|
|
// static final String STRATEGYPREFS = "Strategies";
|
|
|
|
// static final String TRADERPREFS = "Traders";
|
2018-11-30 12:27:41 +01:00
|
|
|
public static final String DEVELSTATUS = "devel_status";
|
2017-02-13 18:26:32 +01:00
|
|
|
public static final String GODMODE = "godmode";
|
2017-01-30 22:18:10 +01:00
|
|
|
|
2018-11-30 12:27:41 +01:00
|
|
|
static public opensesim.old_sesim.Exchange se;
|
2017-01-26 01:52:03 +01:00
|
|
|
|
2017-04-08 12:16:40 +02:00
|
|
|
/**
|
|
|
|
* Defines keys for preferences
|
|
|
|
*/
|
2017-04-16 16:34:51 +02:00
|
|
|
public static final class PrefKeys {
|
2017-04-08 22:56:32 +02:00
|
|
|
|
2018-11-30 12:27:41 +01:00
|
|
|
public static final String XCLASSPATH = "xclasspath";
|
|
|
|
public static final String LAF = "laf";
|
|
|
|
|
2017-04-08 12:16:40 +02:00
|
|
|
public static String WORKDIR = "workdir";
|
2017-04-08 18:46:42 +02:00
|
|
|
public static final String CURRENTFILE = "currentfile";
|
2017-04-16 16:34:51 +02:00
|
|
|
|
2017-04-08 22:56:32 +02:00
|
|
|
public static final String SESIMVERSION = "version";
|
|
|
|
public static final String STRATEGIES = "strategies";
|
|
|
|
public static final String TRADERS = "traders";
|
2018-12-01 23:16:46 +01:00
|
|
|
public static final String ASSETS = "assets";
|
2018-12-02 21:38:17 +01:00
|
|
|
public static final String EXCHANGES = "exchanges";
|
2017-04-08 22:56:32 +02:00
|
|
|
|
2017-04-08 12:16:40 +02:00
|
|
|
}
|
2018-12-01 11:48:54 +01:00
|
|
|
|
|
|
|
public static final class MAX {
|
|
|
|
|
2018-12-01 22:36:57 +01:00
|
|
|
public static final int SYMLEN = 16;
|
2018-11-30 12:27:41 +01:00
|
|
|
public static final int NAMELEN = 64;
|
|
|
|
}
|
2017-04-16 16:34:51 +02:00
|
|
|
|
2018-11-30 12:27:41 +01:00
|
|
|
/**
|
|
|
|
* Preferences
|
|
|
|
*/
|
2017-01-25 02:52:49 +01:00
|
|
|
static public Preferences prefs;
|
2017-03-21 07:53:06 +01:00
|
|
|
|
|
|
|
public static class CfgStrings {
|
|
|
|
|
|
|
|
public static final String GODMODE = "godmode";
|
2018-11-30 12:27:41 +01:00
|
|
|
|
2017-02-13 18:26:32 +01:00
|
|
|
}
|
2017-03-21 07:53:06 +01:00
|
|
|
|
|
|
|
public static String DEFAULT_EXCHANGE_CFG
|
|
|
|
= "{"
|
2017-02-16 18:37:08 +01:00
|
|
|
+ " money_decimals: 2,"
|
|
|
|
+ " shares_decimals: 0"
|
|
|
|
+ "}";
|
2017-01-26 01:52:03 +01:00
|
|
|
|
2018-11-30 12:27:41 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param selected
|
|
|
|
*/
|
|
|
|
public static void setLookAndFeel(String selected) {
|
2017-01-26 01:52:03 +01:00
|
|
|
|
2018-11-30 12:27:41 +01:00
|
|
|
ClassLoader old;
|
|
|
|
old = Globals.setXClassLoader();
|
|
|
|
Logger.getLogger(Globals.class.getName()).log(Level.SEVERE, "Setting LAF:" + selected);
|
2017-01-25 02:52:49 +01:00
|
|
|
UIManager.LookAndFeelInfo[] lafInfo = UIManager.getInstalledLookAndFeels();
|
2017-01-26 01:52:03 +01:00
|
|
|
for (UIManager.LookAndFeelInfo lafInfo1 : lafInfo) {
|
2017-01-25 02:52:49 +01:00
|
|
|
if (lafInfo1.getName().equals(selected)) {
|
|
|
|
String lafClassName = lafInfo1.getClassName();
|
|
|
|
try {
|
2018-11-30 12:27:41 +01:00
|
|
|
Logger.getLogger(Globals.class.getName()).log(Level.SEVERE, "Setting LAF:" + lafClassName);
|
2018-12-01 11:48:54 +01:00
|
|
|
JFrame.setDefaultLookAndFeelDecorated(true);
|
|
|
|
JDialog.setDefaultLookAndFeelDecorated(true);
|
2018-11-30 12:27:41 +01:00
|
|
|
|
2017-01-25 02:52:49 +01:00
|
|
|
UIManager.setLookAndFeel(lafClassName);
|
|
|
|
break;
|
|
|
|
} catch (Exception e) {
|
2018-11-30 12:27:41 +01:00
|
|
|
Logger.getLogger(Globals.class.getName()).log(Level.SEVERE, null, e);
|
2017-01-25 02:52:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-30 12:27:41 +01:00
|
|
|
|
|
|
|
Globals.unsetXClassLoader(old);
|
2017-01-25 02:52:49 +01:00
|
|
|
}
|
2017-01-30 22:18:10 +01:00
|
|
|
|
2017-03-21 07:53:06 +01:00
|
|
|
static AutoTraderLoader tloader;
|
2017-11-25 09:28:01 +01:00
|
|
|
static IndicatorLoader iloader;
|
2017-03-21 07:53:06 +01:00
|
|
|
|
2018-11-30 12:27:41 +01:00
|
|
|
static ArrayList default_pathlist = new ArrayList<>();
|
2017-03-21 07:53:06 +01:00
|
|
|
|
2018-11-30 12:27:41 +01:00
|
|
|
static ArrayList<URL> urllist;
|
|
|
|
|
|
|
|
static public void updateUrlList() {
|
|
|
|
urllist = new ArrayList<>();
|
|
|
|
URL url;
|
|
|
|
try {
|
|
|
|
url = new java.io.File(Globals.class.getProtectionDomain()
|
|
|
|
.getCodeSource()
|
|
|
|
.getLocation()
|
|
|
|
.getPath()).toURI().toURL();
|
|
|
|
urllist.add(url);
|
|
|
|
} catch (MalformedURLException ex) {
|
|
|
|
Logger.getLogger(Globals.class.getName()).log(Level.SEVERE, null, ex);
|
|
|
|
}
|
|
|
|
|
|
|
|
String xpath;
|
|
|
|
xpath = prefs.get(Globals.PrefKeys.XCLASSPATH, null);
|
|
|
|
|
|
|
|
urllist.addAll(opensesim.util.XClassLoader.getXPathUrlList(xpath));
|
|
|
|
}
|
|
|
|
|
|
|
|
static ClassLoader setXClassLoader() {
|
|
|
|
ClassLoader old_classloader = Thread.currentThread().getContextClassLoader();
|
2018-12-01 11:48:54 +01:00
|
|
|
if (urllist == null) {
|
2018-11-30 12:27:41 +01:00
|
|
|
return old_classloader;
|
2018-12-01 11:48:54 +01:00
|
|
|
}
|
2018-11-30 12:27:41 +01:00
|
|
|
URL[] urls = urllist.toArray(new URL[urllist.size()]);
|
|
|
|
URLClassLoader cl;
|
|
|
|
cl = new URLClassLoader(urls, old_classloader);
|
|
|
|
Thread.currentThread().setContextClassLoader(cl);
|
|
|
|
return old_classloader;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void unsetXClassLoader(ClassLoader old_classloader) {
|
|
|
|
Thread.currentThread().setContextClassLoader(old_classloader);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-12-01 22:36:57 +01:00
|
|
|
static public ArrayList<Class<AbstractAsset>> getAvailableAssetsTypes(boolean sort) {
|
|
|
|
|
2018-12-04 20:04:12 +01:00
|
|
|
// if class_cache is not initialized return an empty list
|
|
|
|
if (class_cache == null){
|
|
|
|
return new ArrayList<>();
|
|
|
|
}
|
|
|
|
|
2018-12-01 11:48:54 +01:00
|
|
|
Collection<Class> asset_types_raw;
|
|
|
|
asset_types_raw = class_cache.getClassCollection(AbstractAsset.class);
|
|
|
|
|
|
|
|
ArrayList<Class<AbstractAsset>> asset_types = new ArrayList<>();
|
2018-12-01 22:36:57 +01:00
|
|
|
asset_types_raw.forEach((a) -> {
|
|
|
|
asset_types.add(a);
|
|
|
|
});
|
2018-12-04 15:31:34 +01:00
|
|
|
|
|
|
|
if (!sort) {
|
2018-12-01 22:36:57 +01:00
|
|
|
return asset_types;
|
2018-12-04 15:31:34 +01:00
|
|
|
}
|
2018-12-01 22:36:57 +01:00
|
|
|
|
|
|
|
asset_types.sort(new Comparator<Class<AbstractAsset>>() {
|
|
|
|
@Override
|
|
|
|
public int compare(Class<AbstractAsset> o1, Class<AbstractAsset> o2) {
|
|
|
|
AbstractAsset a1, a2;
|
|
|
|
|
|
|
|
try {
|
|
|
|
// a1 = o1.newInstance();
|
2018-12-04 20:04:12 +01:00
|
|
|
a1 = o1.getConstructor(World.class,JSONObject.class).newInstance(null,null);
|
|
|
|
a2 = o2.getConstructor(World.class,JSONObject.class).newInstance(null,null);
|
2018-12-01 22:36:57 +01:00
|
|
|
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | SecurityException | IllegalArgumentException | InvocationTargetException ex) {
|
|
|
|
Logger.getLogger(AssetEditorPanel.class.getName()).log(Level.SEVERE, null, ex);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
String t1, t2;
|
|
|
|
t1 = a1.getTypeName();
|
|
|
|
t2 = a2.getTypeName();
|
|
|
|
|
|
|
|
return t1.compareToIgnoreCase(t2);
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
2018-12-01 11:48:54 +01:00
|
|
|
|
2018-11-30 12:27:41 +01:00
|
|
|
return asset_types;
|
|
|
|
}
|
|
|
|
|
2018-12-01 22:36:57 +01:00
|
|
|
static public ArrayList<Class<AbstractAsset>> getAvailableAssetsTypes() {
|
|
|
|
return getAvailableAssetsTypes(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
static public Class getClassByName(String name) {
|
|
|
|
return class_cache.getClassByName(name);
|
|
|
|
}
|
|
|
|
|
2018-11-30 12:27:41 +01:00
|
|
|
static public void installLookAndFeels() {
|
2018-12-01 11:37:25 +01:00
|
|
|
Collection<Class> lafs;
|
2018-12-01 11:48:54 +01:00
|
|
|
// lafs = opensesim.util.XClassLoader.getClassesList(urllist, LookAndFeel.class);
|
|
|
|
lafs = class_cache.getClassCollection(LookAndFeel.class);
|
2018-12-04 15:31:34 +01:00
|
|
|
if (lafs == null) {
|
|
|
|
return;
|
|
|
|
}
|
2018-11-30 12:27:41 +01:00
|
|
|
|
|
|
|
ClassLoader currentThreadClassLoader
|
|
|
|
= Thread.currentThread().getContextClassLoader();
|
|
|
|
URL[] urls = urllist.toArray(new URL[urllist.size()]);
|
|
|
|
URLClassLoader cl;
|
|
|
|
cl = new URLClassLoader(urls, currentThreadClassLoader);
|
|
|
|
Thread.currentThread().setContextClassLoader(cl);
|
|
|
|
|
|
|
|
for (Class<?> cls : lafs) {
|
2018-12-01 11:48:54 +01:00
|
|
|
/* System.out.println("Class:");
|
2018-12-01 11:37:25 +01:00
|
|
|
System.out.println(cls.getName());*/
|
2018-11-30 12:27:41 +01:00
|
|
|
|
|
|
|
Class<LookAndFeel> lafc;
|
|
|
|
lafc = (Class<LookAndFeel>) cls;
|
|
|
|
LookAndFeel laf;
|
|
|
|
try {
|
|
|
|
laf = lafc.newInstance();
|
|
|
|
} catch (InstantiationException | IllegalAccessException | InternalError ex) {
|
|
|
|
Logger.getLogger(SeSimApplication.class.getName()).log(Level.SEVERE, null, ex);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-12-01 11:48:54 +01:00
|
|
|
/* System.out.println("LAF:");
|
2018-12-01 11:37:25 +01:00
|
|
|
System.out.println(laf.getName());*/
|
2018-12-01 11:48:54 +01:00
|
|
|
UIManager.installLookAndFeel(laf.getName() + " - " + laf.getDescription(), lafc.getName());
|
2018-11-30 12:27:41 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Thread.currentThread().setContextClassLoader(currentThreadClassLoader);
|
|
|
|
|
|
|
|
// UIManager.installLookAndFeel(laf.getName(), lafc.getName());
|
|
|
|
}
|
2018-12-01 11:48:54 +01:00
|
|
|
|
2018-12-01 11:37:25 +01:00
|
|
|
static ClassCache class_cache;
|
2018-11-30 12:27:41 +01:00
|
|
|
|
|
|
|
static void initGlobals(Class<?> c) {
|
|
|
|
|
|
|
|
prefs = Preferences.userNodeForPackage(c);
|
2018-12-01 11:48:54 +01:00
|
|
|
|
2018-12-04 15:31:34 +01:00
|
|
|
// world = new World();
|
2018-11-30 12:27:41 +01:00
|
|
|
// initialize urllist used by class loader
|
|
|
|
updateUrlList();
|
|
|
|
|
2018-12-01 11:48:54 +01:00
|
|
|
class_cache = new ClassCache(urllist, new Class[]{
|
2018-12-01 11:37:25 +01:00
|
|
|
LookAndFeel.class,
|
|
|
|
AbstractAsset.class
|
|
|
|
});
|
|
|
|
|
2018-12-01 11:48:54 +01:00
|
|
|
installLookAndFeels();
|
|
|
|
|
2018-11-30 12:27:41 +01:00
|
|
|
/*
|
|
|
|
ArrayList<Class<LookAndFeelInfo>> res;
|
|
|
|
|
|
|
|
res = cl.getInstalledClasses(default_pathlist);
|
|
|
|
|
|
|
|
for (Class<LookAndFeelInfo> laf : res) {
|
|
|
|
System.out.print(laf.getCanonicalName());
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
/* System.out.print("PATH LIST\n");
|
|
|
|
System.out.print(default_pathlist);
|
|
|
|
System.out.print("END PL\n");
|
|
|
|
*/
|
|
|
|
tloader = new AutoTraderLoader(default_pathlist);
|
|
|
|
|
|
|
|
iloader = new IndicatorLoader(default_pathlist);
|
|
|
|
|
2017-11-25 09:28:01 +01:00
|
|
|
iloader.getNames();
|
|
|
|
|
|
|
|
// SeSimClassLoader<Indicator> il = new SeSimClassLoader<>(Indicator.class);
|
|
|
|
// il.setDefaultPathList(default_pathlist);
|
|
|
|
// ArrayList<Class<Indicator>> ires = il.getInstalledClasses();
|
2017-03-21 07:53:06 +01:00
|
|
|
}
|
2017-01-30 22:18:10 +01:00
|
|
|
|
2017-03-21 07:53:06 +01:00
|
|
|
static public final Logger LOGGER = Logger.getLogger("com.cauwersin.sesim");
|
2017-01-30 22:18:10 +01:00
|
|
|
|
2018-12-01 23:16:46 +01:00
|
|
|
static public final JSONObject getAssets() {
|
|
|
|
String assets_json = prefs.get(PrefKeys.ASSETS, "{}");
|
|
|
|
return new JSONObject(assets_json);
|
|
|
|
}
|
2018-12-04 15:31:34 +01:00
|
|
|
|
|
|
|
static public final void putAssets(JSONObject assets) {
|
2018-12-01 23:16:46 +01:00
|
|
|
prefs.put(PrefKeys.ASSETS, assets.toString());
|
|
|
|
}
|
2018-12-04 15:31:34 +01:00
|
|
|
|
|
|
|
static public final JSONObject getExchanges() {
|
2018-12-02 21:38:17 +01:00
|
|
|
String json = prefs.get(PrefKeys.EXCHANGES, "{}");
|
|
|
|
return new JSONObject(json);
|
|
|
|
}
|
2018-12-04 15:31:34 +01:00
|
|
|
|
|
|
|
static public final void putExchanges(JSONObject e) {
|
2018-12-02 21:38:17 +01:00
|
|
|
prefs.put(PrefKeys.EXCHANGES, e.toString());
|
|
|
|
}
|
2018-12-04 15:31:34 +01:00
|
|
|
|
2017-01-30 22:18:10 +01:00
|
|
|
static public final JSONArray getTraders() {
|
2018-11-30 12:27:41 +01:00
|
|
|
|
2017-04-08 22:56:32 +02:00
|
|
|
String traders_json = Globals.prefs.get(PrefKeys.TRADERS, "[]");
|
2017-01-30 22:18:10 +01:00
|
|
|
JSONArray traders = new JSONArray(traders_json);
|
|
|
|
return traders;
|
|
|
|
}
|
|
|
|
|
|
|
|
static public final JSONObject getStrategies() {
|
2017-04-08 22:56:32 +02:00
|
|
|
String cfglist = Globals.prefs.get(PrefKeys.STRATEGIES, "{}");
|
2017-01-30 22:18:10 +01:00
|
|
|
JSONObject cfgs = new JSONObject(cfglist);
|
|
|
|
return cfgs;
|
|
|
|
}
|
2017-04-16 16:34:51 +02:00
|
|
|
|
|
|
|
static public final void putStrategies(JSONObject strategies) {
|
2017-04-08 22:56:32 +02:00
|
|
|
Globals.prefs.put(Globals.PrefKeys.STRATEGIES, strategies.toString());
|
|
|
|
}
|
2017-01-31 08:04:11 +01:00
|
|
|
|
2017-04-16 16:34:51 +02:00
|
|
|
static public final void putTraders(JSONArray traders) {
|
2017-04-08 22:56:32 +02:00
|
|
|
Globals.prefs.put(Globals.PrefKeys.TRADERS, traders.toString());
|
|
|
|
}
|
|
|
|
|
2017-01-31 08:04:11 +01:00
|
|
|
static public JSONObject getStrategy(String name) {
|
2017-01-30 22:18:10 +01:00
|
|
|
return getStrategies().getJSONObject(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static public void getStrategiesIntoComboBox(JComboBox comboBox) {
|
|
|
|
TreeMap stm = getStrategiesAsTreeMap();
|
|
|
|
comboBox.removeAllItems();
|
|
|
|
|
|
|
|
Iterator<String> i = stm.keySet().iterator();
|
|
|
|
while (i.hasNext()) {
|
|
|
|
comboBox.addItem(i.next());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static public TreeMap getStrategiesAsTreeMap() {
|
|
|
|
TreeMap 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);
|
|
|
|
}
|
|
|
|
return strategies;
|
|
|
|
}
|
|
|
|
|
|
|
|
static public final void saveStrategy(String name, JSONObject cfg) {
|
|
|
|
JSONObject cfgs = getStrategies();
|
|
|
|
cfgs.put(name, cfg);
|
2017-04-08 22:56:32 +02:00
|
|
|
prefs.put(PrefKeys.STRATEGIES, cfgs.toString());
|
2017-01-30 22:18:10 +01:00
|
|
|
}
|
2017-01-26 01:52:03 +01:00
|
|
|
|
2017-04-08 18:46:42 +02:00
|
|
|
public static void saveFile(File f) throws FileNotFoundException {
|
2017-01-31 08:04:11 +01:00
|
|
|
|
|
|
|
JSONObject sobj = new JSONObject();
|
2017-04-08 10:13:09 +02:00
|
|
|
|
2017-01-31 08:04:11 +01:00
|
|
|
JSONArray traders = getTraders();
|
|
|
|
JSONObject strategies = getStrategies();
|
2017-04-08 10:13:09 +02:00
|
|
|
|
2017-04-08 22:56:32 +02:00
|
|
|
sobj.put(PrefKeys.SESIMVERSION, SESIM_FILEVERSION);
|
|
|
|
sobj.put(PrefKeys.STRATEGIES, strategies);
|
|
|
|
sobj.put(PrefKeys.TRADERS, traders);
|
2017-04-08 10:13:09 +02:00
|
|
|
|
|
|
|
PrintWriter out;
|
|
|
|
out = new PrintWriter(f.getAbsolutePath());
|
|
|
|
out.print(sobj.toString(4));
|
|
|
|
out.close();
|
2017-01-31 08:04:11 +01:00
|
|
|
|
|
|
|
}
|
2017-04-16 16:34:51 +02:00
|
|
|
|
|
|
|
public static void loadString(String s) throws IOException {
|
2017-04-08 18:46:42 +02:00
|
|
|
JSONObject sobj = new JSONObject(s);
|
2017-04-16 16:34:51 +02:00
|
|
|
|
2017-04-08 22:56:32 +02:00
|
|
|
Double version = sobj.getDouble(PrefKeys.SESIMVERSION);
|
2017-04-16 16:34:51 +02:00
|
|
|
if (version > SESIM_FILEVERSION) {
|
2017-04-08 22:56:32 +02:00
|
|
|
throw new IOException("File has wrong version.");
|
|
|
|
}
|
2017-04-16 16:34:51 +02:00
|
|
|
|
2017-04-08 22:56:32 +02:00
|
|
|
JSONArray traders = sobj.getJSONArray(PrefKeys.TRADERS);
|
|
|
|
JSONObject strategies = sobj.getJSONObject(PrefKeys.STRATEGIES);
|
2017-04-16 16:34:51 +02:00
|
|
|
|
2017-04-08 22:56:32 +02:00
|
|
|
putStrategies(strategies);
|
|
|
|
putTraders(traders);
|
2017-04-16 16:34:51 +02:00
|
|
|
|
2017-04-08 18:46:42 +02:00
|
|
|
}
|
2018-12-04 15:31:34 +01:00
|
|
|
|
2017-04-16 16:34:51 +02:00
|
|
|
public static void clearAll() {
|
2017-04-14 07:06:37 +02:00
|
|
|
putStrategies(new JSONObject());
|
|
|
|
putTraders(new JSONArray());
|
|
|
|
}
|
2017-04-16 16:34:51 +02:00
|
|
|
|
2017-04-09 00:40:05 +02:00
|
|
|
public static void loadFile(File f) throws IOException {
|
2017-04-16 16:34:51 +02:00
|
|
|
|
2017-04-09 00:40:05 +02:00
|
|
|
f.getAbsoluteFile();
|
|
|
|
String s;
|
|
|
|
s = new String(Files.readAllBytes(f.toPath()));
|
2017-04-16 16:34:51 +02:00
|
|
|
|
2017-04-09 00:40:05 +02:00
|
|
|
loadString(s);
|
2017-04-16 16:34:51 +02:00
|
|
|
|
2017-04-09 00:40:05 +02:00
|
|
|
}
|
2018-12-04 15:31:34 +01:00
|
|
|
|
|
|
|
public static JSONObject getWorld() {
|
2018-12-04 01:36:00 +01:00
|
|
|
JSONObject cfg = new JSONObject();
|
|
|
|
cfg.put(World.JKEYS.ASSETS, getAssets());
|
2018-12-04 15:31:34 +01:00
|
|
|
cfg.put(World.JKEYS.EXCHANGES, getExchanges());
|
2018-12-04 01:36:00 +01:00
|
|
|
return cfg;
|
|
|
|
}
|
2017-01-31 08:04:11 +01:00
|
|
|
|
2017-01-22 10:04:50 +01:00
|
|
|
}
|