save dialog now working, current path is rembmered

This commit is contained in:
7u83 2017-04-08 18:46:42 +02:00
parent 6f8f74dd8f
commit 3790b172ef
4 changed files with 115 additions and 34 deletions

View File

@ -1,4 +1,4 @@
#Sat, 08 Apr 2017 12:15:15 +0200 #Sat, 08 Apr 2017 16:28:12 +0200
annotation.processing.enabled=true annotation.processing.enabled=true
annotation.processing.enabled.in.editor=false annotation.processing.enabled.in.editor=false
annotation.processing.processors.list= annotation.processing.processors.list=

View File

@ -27,7 +27,9 @@ package gui;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.nio.file.Files;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Iterator; import java.util.Iterator;
@ -48,6 +50,10 @@ import sesim.AutoTraderLoader;
*/ */
public class Globals { public class Globals {
public static final String SESIM_FILEEXTENSION = "sesim";
public static final String SESIM_APPTITLE = "SeSim - Stock Exchange Simulator";
public interface CfgListener { public interface CfgListener {
void cfgChanged(); void cfgChanged();
@ -80,6 +86,7 @@ public class Globals {
*/ */
public static final class PrefKeys{ public static final class PrefKeys{
public static String WORKDIR = "workdir"; public static String WORKDIR = "workdir";
public static final String CURRENTFILE = "currentfile";
} }
static public Preferences prefs; static public Preferences prefs;
@ -192,10 +199,11 @@ public class Globals {
public static final String SESIMVERSION = "sesim_version"; public static final String SESIMVERSION = "sesim_version";
public static final String STRATEGIES = "strategies"; public static final String STRATEGIES = "strategies";
public static final String TRADERS = "traders"; public static final String TRADERS = "traders";
public static final String SESIM_EXTENSION = "sesim";
} }
static void saveFile(File f) throws FileNotFoundException {
public static void saveFile(File f) throws FileNotFoundException {
JSONObject sobj = new JSONObject(); JSONObject sobj = new JSONObject();
@ -213,4 +221,10 @@ public class Globals {
} }
public static void loadFile(File f) throws IOException {
String s = new String(Files.readAllBytes(f.toPath()));
JSONObject sobj = new JSONObject(s);
}
} }

View File

@ -53,15 +53,17 @@
<Property name="text" type="java.lang.String" value="File"/> <Property name="text" type="java.lang.String" value="File"/>
</Properties> </Properties>
<SubComponents> <SubComponents>
<MenuItem class="javax.swing.JMenuItem" name="jMenuItem1"> <MenuItem class="javax.swing.JMenuItem" name="resetMenuItem">
<Properties> <Properties>
<Property name="mnemonic" type="int" value="110"/> <Property name="mnemonic" type="int" value="114"/>
<Property name="text" type="java.lang.String" value="New"/> <Property name="text" type="java.lang.String" value="Reset"/>
</Properties> </Properties>
<Events> <Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="jMenuItem1ActionPerformed"/> <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="resetMenuItemActionPerformed"/>
</Events> </Events>
</MenuItem> </MenuItem>
<MenuItem class="javax.swing.JPopupMenu$Separator" name="jSeparator5">
</MenuItem>
<MenuItem class="javax.swing.JMenuItem" name="openMenuItem"> <MenuItem class="javax.swing.JMenuItem" name="openMenuItem">
<Properties> <Properties>
<Property name="mnemonic" type="int" value="111"/> <Property name="mnemonic" type="int" value="111"/>
@ -90,6 +92,17 @@
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="saveAsMenuItemActionPerformed"/> <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="saveAsMenuItemActionPerformed"/>
</Events> </Events>
</MenuItem> </MenuItem>
<MenuItem class="javax.swing.JMenuItem" name="closeMenuItem">
<Properties>
<Property name="mnemonic" type="int" value="99"/>
<Property name="text" type="java.lang.String" value="Close"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="closeMenuItemActionPerformed"/>
</Events>
</MenuItem>
<MenuItem class="javax.swing.JPopupMenu$Separator" name="jSeparator4">
</MenuItem>
<MenuItem class="javax.swing.JMenuItem" name="exitMenuItem"> <MenuItem class="javax.swing.JMenuItem" name="exitMenuItem">
<Properties> <Properties>
<Property name="mnemonic" type="int" value="120"/> <Property name="mnemonic" type="int" value="120"/>

View File

@ -67,6 +67,7 @@ public class SeSimApplication extends javax.swing.JFrame {
*/ */
public SeSimApplication() { public SeSimApplication() {
initComponents(); initComponents();
setTitle("");
this.chartSrollPane.setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_NEVER); this.chartSrollPane.setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_NEVER);
this.setLocationRelativeTo(null); this.setLocationRelativeTo(null);
} }
@ -171,10 +172,13 @@ public class SeSimApplication extends javax.swing.JFrame {
statistics1 = new gui.Statistics(); statistics1 = new gui.Statistics();
menuBar = new javax.swing.JMenuBar(); menuBar = new javax.swing.JMenuBar();
fileMenu = new javax.swing.JMenu(); fileMenu = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem(); resetMenuItem = new javax.swing.JMenuItem();
jSeparator5 = new javax.swing.JPopupMenu.Separator();
openMenuItem = new javax.swing.JMenuItem(); openMenuItem = new javax.swing.JMenuItem();
saveMenuItem = new javax.swing.JMenuItem(); saveMenuItem = new javax.swing.JMenuItem();
saveAsMenuItem = new javax.swing.JMenuItem(); saveAsMenuItem = new javax.swing.JMenuItem();
closeMenuItem = new javax.swing.JMenuItem();
jSeparator4 = new javax.swing.JPopupMenu.Separator();
exitMenuItem = new javax.swing.JMenuItem(); exitMenuItem = new javax.swing.JMenuItem();
editMenu = new javax.swing.JMenu(); editMenu = new javax.swing.JMenu();
editExchangeMenuItem = new javax.swing.JMenuItem(); editExchangeMenuItem = new javax.swing.JMenuItem();
@ -328,14 +332,15 @@ public class SeSimApplication extends javax.swing.JFrame {
fileMenu.setMnemonic('f'); fileMenu.setMnemonic('f');
fileMenu.setText("File"); fileMenu.setText("File");
jMenuItem1.setMnemonic('n'); resetMenuItem.setMnemonic('r');
jMenuItem1.setText("New"); resetMenuItem.setText("Reset");
jMenuItem1.addActionListener(new java.awt.event.ActionListener() { resetMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) { public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem1ActionPerformed(evt); resetMenuItemActionPerformed(evt);
} }
}); });
fileMenu.add(jMenuItem1); fileMenu.add(resetMenuItem);
fileMenu.add(jSeparator5);
openMenuItem.setMnemonic('o'); openMenuItem.setMnemonic('o');
openMenuItem.setText("Open"); openMenuItem.setText("Open");
@ -365,6 +370,16 @@ public class SeSimApplication extends javax.swing.JFrame {
}); });
fileMenu.add(saveAsMenuItem); fileMenu.add(saveAsMenuItem);
closeMenuItem.setMnemonic('c');
closeMenuItem.setText("Close");
closeMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
closeMenuItemActionPerformed(evt);
}
});
fileMenu.add(closeMenuItem);
fileMenu.add(jSeparator4);
exitMenuItem.setMnemonic('x'); exitMenuItem.setMnemonic('x');
exitMenuItem.setText("Exit"); exitMenuItem.setText("Exit");
exitMenuItem.addActionListener(new java.awt.event.ActionListener() { exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
@ -598,11 +613,7 @@ public class SeSimApplication extends javax.swing.JFrame {
private void openMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openMenuItemActionPerformed private void openMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openMenuItemActionPerformed
JFileChooser fc = new JFileChooser(); JFileChooser fc = getFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("SeSim Files", "sesim");
fc.setFileFilter(filter);
if (fc.showOpenDialog(this) != JFileChooser.APPROVE_OPTION) { if (fc.showOpenDialog(this) != JFileChooser.APPROVE_OPTION) {
return; return;
} }
@ -616,28 +627,54 @@ public class SeSimApplication extends javax.swing.JFrame {
String workdir = Globals.prefs.get(Globals.PrefKeys.WORKDIR, ""); String workdir = Globals.prefs.get(Globals.PrefKeys.WORKDIR, "");
fc.setCurrentDirectory(new File(workdir)); fc.setCurrentDirectory(new File(workdir));
FileNameExtensionFilter sesim_filter = new FileNameExtensionFilter("SeSim Files", Globals.FileStrings.SESIM_EXTENSION); FileNameExtensionFilter sesim_filter = new FileNameExtensionFilter("SeSim Files", Globals.SESIM_FILEEXTENSION);
fc.setFileFilter(sesim_filter); fc.setFileFilter(sesim_filter);
return fc; return fc;
} }
@Override
public final void setTitle(String filename) {
String name;
name = Globals.SESIM_APPTITLE;
if (!"".equals(filename)) {
name += " (" + filename + ")";
}
super.setTitle(name);
}
private void saveAsMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveAsMenuItemActionPerformed private void saveFile(boolean saveAs) {
JFileChooser fc = getFileChooser(); JFileChooser fc = getFileChooser();
FileFilter sesim_filter = fc.getFileFilter(); FileFilter sesim_filter = fc.getFileFilter();
boolean done = false; while (true) {
while (!done) { String current_file = Globals.prefs.get(Globals.PrefKeys.CURRENTFILE, "");
if (fc.showSaveDialog(this) != JFileChooser.APPROVE_OPTION) { File fobj;
return;
if (saveAs || "".equals(current_file)) {
if (!"".equals(current_file)) {
fobj = new File(current_file);
fc.setSelectedFile(fobj);
fc.setCurrentDirectory(fobj);
}
saveAs = true;
if (fc.showSaveDialog(this) != JFileChooser.APPROVE_OPTION) {
return;
}
} else {
fobj = new File(current_file);
fc.setSelectedFile(fobj);
fc.setCurrentDirectory(fobj);
} }
File f = fc.getSelectedFile(); File f = fc.getSelectedFile();
String workdir = fc.getCurrentDirectory().getAbsolutePath(); String workdir = fc.getCurrentDirectory().getAbsolutePath();
Globals.prefs.put(Globals.PrefKeys.WORKDIR, workdir); Globals.prefs.put(Globals.PrefKeys.WORKDIR, workdir);
String fn = f.getAbsolutePath(); String fn = f.getAbsolutePath();
if (f.exists()) { if (f.exists() && saveAs) {
String s = String.format ("File %s already exists. Do you want to overwrite?",fn); String s = String.format("File %s already exists. Do you want to overwrite?", fn);
int dialogResult = JOptionPane.showConfirmDialog(null, s, "Warning", JOptionPane.YES_NO_OPTION); int dialogResult = JOptionPane.showConfirmDialog(null, s, "Warning", JOptionPane.YES_NO_OPTION);
if (dialogResult != JOptionPane.YES_OPTION) { if (dialogResult != JOptionPane.YES_OPTION) {
continue; continue;
@ -648,13 +685,15 @@ public class SeSimApplication extends javax.swing.JFrame {
FileFilter selected_filter = fc.getFileFilter(); FileFilter selected_filter = fc.getFileFilter();
if (selected_filter == sesim_filter) { if (selected_filter == sesim_filter) {
System.out.printf("Filter", selected_filter.toString()); System.out.printf("Filter", selected_filter.toString());
if (!fn.toLowerCase().endsWith("sesim")) { if (!fn.toLowerCase().endsWith("." + Globals.SESIM_FILEEXTENSION)) {
f = new File(fn + "." + "sesim"); f = new File(fn + "." + Globals.SESIM_FILEEXTENSION);
} }
} }
try { try {
Globals.saveFile(f); Globals.saveFile(f);
Globals.prefs.put(Globals.PrefKeys.CURRENTFILE, fn);
setTitle(f.getName());
return; return;
} catch (Exception ex) { } catch (Exception ex) {
@ -665,7 +704,12 @@ public class SeSimApplication extends javax.swing.JFrame {
} }
}
private void saveAsMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveAsMenuItemActionPerformed
this.saveFile(true);
}//GEN-LAST:event_saveAsMenuItemActionPerformed }//GEN-LAST:event_saveAsMenuItemActionPerformed
private void editExchangeMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editExchangeMenuItemActionPerformed private void editExchangeMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editExchangeMenuItemActionPerformed
@ -675,9 +719,9 @@ public class SeSimApplication extends javax.swing.JFrame {
}//GEN-LAST:event_editExchangeMenuItemActionPerformed }//GEN-LAST:event_editExchangeMenuItemActionPerformed
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed private void resetMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resetMenuItemActionPerformed
// TODO add your handling code here: // TODO add your handling code here:
}//GEN-LAST:event_jMenuItem1ActionPerformed }//GEN-LAST:event_resetMenuItemActionPerformed
private void simMenuStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_simMenuStartActionPerformed private void simMenuStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_simMenuStartActionPerformed
startSim(); startSim();
@ -747,13 +791,19 @@ public class SeSimApplication extends javax.swing.JFrame {
}//GEN-LAST:event_viewTraderListCheckBoxActionPerformed }//GEN-LAST:event_viewTraderListCheckBoxActionPerformed
private void accelSpinnerPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_accelSpinnerPropertyChange private void accelSpinnerPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_accelSpinnerPropertyChange
System.out.printf("Accel Spinner PRop Change\n"); // System.out.printf("Accel Spinner PRop Change\n");
}//GEN-LAST:event_accelSpinnerPropertyChange }//GEN-LAST:event_accelSpinnerPropertyChange
private void saveMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveMenuItemActionPerformed private void saveMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveMenuItemActionPerformed
// TODO add your handling code here: saveFile(false);
}//GEN-LAST:event_saveMenuItemActionPerformed }//GEN-LAST:event_saveMenuItemActionPerformed
private void closeMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeMenuItemActionPerformed
Globals.prefs.put(Globals.PrefKeys.CURRENTFILE, "");
setTitle("");
}//GEN-LAST:event_closeMenuItemActionPerformed
/** /**
* @param args the command line arguments * @param args the command line arguments
* @throws java.lang.IllegalAccessException * @throws java.lang.IllegalAccessException
@ -777,6 +827,7 @@ public class SeSimApplication extends javax.swing.JFrame {
Class<?> c = sesim.Exchange.class; Class<?> c = sesim.Exchange.class;
Globals.prefs = Preferences.userNodeForPackage(c); Globals.prefs = Preferences.userNodeForPackage(c);
Globals.prefs.put(Globals.PrefKeys.CURRENTFILE, "");
Globals.setLookAndFeel(Globals.prefs.get("laf", "Nimbus")); Globals.setLookAndFeel(Globals.prefs.get("laf", "Nimbus"));
@ -804,6 +855,7 @@ public class SeSimApplication extends javax.swing.JFrame {
private gui.MainChart chart; private gui.MainChart chart;
private javax.swing.JScrollPane chartSrollPane; private javax.swing.JScrollPane chartSrollPane;
private gui.Clock clock; private gui.Clock clock;
private javax.swing.JMenuItem closeMenuItem;
private javax.swing.JMenuItem deleteMenuItem; private javax.swing.JMenuItem deleteMenuItem;
private javax.swing.JMenuItem editExchangeMenuItem; private javax.swing.JMenuItem editExchangeMenuItem;
private javax.swing.JMenu editMenu; private javax.swing.JMenu editMenu;
@ -814,7 +866,6 @@ public class SeSimApplication extends javax.swing.JFrame {
private javax.swing.JButton jButton2; private javax.swing.JButton jButton2;
private javax.swing.JCheckBoxMenuItem jCheckBoxMenuItem1; private javax.swing.JCheckBoxMenuItem jCheckBoxMenuItem1;
private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JMenuItem jMenuItem4; private javax.swing.JMenuItem jMenuItem4;
private javax.swing.JMenuItem jMenuItem5; private javax.swing.JMenuItem jMenuItem5;
private javax.swing.JPanel jPanel2; private javax.swing.JPanel jPanel2;
@ -822,6 +873,8 @@ public class SeSimApplication extends javax.swing.JFrame {
private javax.swing.JPopupMenu.Separator jSeparator1; private javax.swing.JPopupMenu.Separator jSeparator1;
private javax.swing.JPopupMenu.Separator jSeparator2; private javax.swing.JPopupMenu.Separator jSeparator2;
private javax.swing.JPopupMenu.Separator jSeparator3; private javax.swing.JPopupMenu.Separator jSeparator3;
private javax.swing.JPopupMenu.Separator jSeparator4;
private javax.swing.JPopupMenu.Separator jSeparator5;
private javax.swing.JSplitPane jSplitPane1; private javax.swing.JSplitPane jSplitPane1;
private javax.swing.JSplitPane jSplitPane2; private javax.swing.JSplitPane jSplitPane2;
private javax.swing.JSplitPane jSplitPane3; private javax.swing.JSplitPane jSplitPane3;
@ -834,6 +887,7 @@ public class SeSimApplication extends javax.swing.JFrame {
private gui.orderbook.OrderBooksHorizontal orderBooksHorizontal1; private gui.orderbook.OrderBooksHorizontal orderBooksHorizontal1;
private javax.swing.JMenuItem pasteMenuItem; private javax.swing.JMenuItem pasteMenuItem;
private gui.orderbook.QuoteVertical quoteVertical1; private gui.orderbook.QuoteVertical quoteVertical1;
private javax.swing.JMenuItem resetMenuItem;
private javax.swing.JButton runButton; private javax.swing.JButton runButton;
private javax.swing.JMenuItem saveAsMenuItem; private javax.swing.JMenuItem saveAsMenuItem;
private javax.swing.JMenuItem saveMenuItem; private javax.swing.JMenuItem saveMenuItem;