OpenSeSim/src/gui/Globals.java

184 lines
5.7 KiB
Java
Raw Normal View History

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.
*/
package gui;
2017-01-31 08:04:11 +01:00
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
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;
2017-02-07 01:53:43 +01:00
import javax.swing.JFrame;
2017-01-25 02:52:49 +01:00
import javax.swing.UIManager;
2017-01-30 22:18:10 +01:00
import org.json.JSONArray;
import org.json.JSONObject;
2017-01-26 01:52:03 +01:00
import sesim.AutoTraderLoader;
2017-01-25 02:52:49 +01:00
2017-01-22 10:04:50 +01:00
/**
*
* @author 7u83 <7u83@mail.ru>
*/
public class Globals {
2017-02-07 01:53:43 +01:00
public interface CfgListener{
void cfgChanged();
}
static ArrayList <CfgListener> cfg_listeners = new ArrayList<>();
public static void notifyCfgListeners(){
for (CfgListener l : cfg_listeners){
l.cfgChanged();
}
}
public static void addCfgListener(CfgListener l){
cfg_listeners.add(l);
}
2017-02-07 01:53:43 +01:00
public static JFrame frame;
2017-01-26 01:52:03 +01:00
2017-01-30 22:18:10 +01:00
static final String STRATEGYPREFS = "Strategies";
static final String TRADERPREFS = "Traders";
2017-02-02 18:39:55 +01:00
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
2017-01-22 10:04:50 +01:00
static public sesim.Exchange se;
2017-01-26 01:52:03 +01:00
2017-01-25 02:52:49 +01:00
static public Preferences prefs;
2017-02-13 18:26:32 +01:00
public static class CfgStrings{
public static final String GODMODE = "godmode";
}
2017-02-16 18:37:08 +01:00
public static String DEFAULT_EXCHANGE_CFG =
"{"
+ " money_decimals: 2,"
+ " shares_decimals: 0"
+ "}";
2017-02-13 18:26:32 +01:00
//CfgStrings
2017-01-26 01:52:03 +01:00
static void setLookAndFeel(String selected) {
2017-01-25 02:52:49 +01:00
try {
2017-01-26 01:52:03 +01:00
String look = "com.seaglasslookandfeel.SeaGlassLookAndFeel";
Class.forName(look);
UIManager.installLookAndFeel("Sea Glass", look);
} catch (ClassNotFoundException e) {
}
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 {
UIManager.setLookAndFeel(lafClassName);
2017-01-26 01:52:03 +01:00
// UIManager.setLookAndFeel("com.seaglasslookandfeel.SeaGlassLookAndFeel");
2017-01-25 02:52:49 +01:00
break;
} catch (Exception e) {
}
}
}
}
2017-01-30 22:18:10 +01:00
static AutoTraderLoader tloader = new AutoTraderLoader();
2017-01-28 15:34:24 +01:00
static final Logger LOGGER = Logger.getLogger("com.cauwersin.sesim");
2017-01-30 22:18:10 +01:00
static public final JSONArray getTraders() {
String traders_json = Globals.prefs.get(TRADERPREFS, "[]");
JSONArray traders = new JSONArray(traders_json);
return traders;
}
static public final JSONObject getStrategies() {
String cfglist = Globals.prefs.get(STRATEGYPREFS, "{}");
JSONObject cfgs = new JSONObject(cfglist);
return cfgs;
}
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);
prefs.put(STRATEGYPREFS, cfgs.toString());
}
2017-01-26 01:52:03 +01:00
2017-01-31 08:04:11 +01:00
static void saveFile(File f) {
JSONObject sobj = new JSONObject();
JSONArray traders = getTraders();
JSONObject strategies = getStrategies();
sobj.put("strategies", strategies);
sobj.put("traders", traders);
try {
PrintWriter out = new PrintWriter(f.getAbsolutePath());
out.print(sobj.toString(4));
out.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(Globals.class.getName()).log(Level.SEVERE, null, ex);
}
}
2017-01-22 10:04:50 +01:00
}