full featured save as dialog

This commit is contained in:
7u83 2017-04-08 12:16:40 +02:00
parent 1a32edd373
commit 6f8f74dd8f
3 changed files with 95 additions and 51 deletions

View File

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

View File

@ -75,6 +75,13 @@ public class Globals {
static public sesim.Exchange se;
/**
* Defines keys for preferences
*/
public static final class PrefKeys{
public static String WORKDIR = "workdir";
}
static public Preferences prefs;
public static class CfgStrings {
@ -185,6 +192,7 @@ public class Globals {
public static final String SESIMVERSION = "sesim_version";
public static final String STRATEGIES = "strategies";
public static final String TRADERS = "traders";
public static final String SESIM_EXTENSION = "sesim";
}
static void saveFile(File f) throws FileNotFoundException {

View File

@ -45,6 +45,7 @@ import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import static javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER;
import javax.swing.UIManager;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import org.json.JSONArray;
import org.json.JSONObject;
@ -595,39 +596,76 @@ public class SeSimApplication extends javax.swing.JFrame {
private final LoggerDialog log_d = new LoggerDialog(this, false);
private void openMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openMenuItemActionPerformed
}//GEN-LAST:event_openMenuItemActionPerformed
private void saveAsMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveAsMenuItemActionPerformed
JFileChooser fc = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("SeSim Files", "sesim");
fc.setFileFilter(filter);
if (fc.showSaveDialog(this) != JFileChooser.APPROVE_OPTION) {
if (fc.showOpenDialog(this) != JFileChooser.APPROVE_OPTION) {
return;
}
File f = fc.getSelectedFile();
}//GEN-LAST:event_openMenuItemActionPerformed
String[] e = ((FileNameExtensionFilter) fc.getFileFilter()).getExtensions();
// initialize a JFileChose with worging directory and extension
private JFileChooser getFileChooser() {
JFileChooser fc = new JFileChooser();
String fn = f.getAbsolutePath();
String workdir = Globals.prefs.get(Globals.PrefKeys.WORKDIR, "");
fc.setCurrentDirectory(new File(workdir));
if (!f.getAbsolutePath().endsWith(e[0])) {
f = new File(f.getAbsolutePath() + "." + e[0]);
}
FileNameExtensionFilter sesim_filter = new FileNameExtensionFilter("SeSim Files", Globals.FileStrings.SESIM_EXTENSION);
fc.setFileFilter(sesim_filter);
return fc;
}
try{
Globals.saveFile(f);
}
catch (Exception ex){
JOptionPane.showMessageDialog(this, "Can't save file "+ex.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
private void saveAsMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveAsMenuItemActionPerformed
JFileChooser fc = getFileChooser();
FileFilter sesim_filter = fc.getFileFilter();
boolean done = false;
while (!done) {
if (fc.showSaveDialog(this) != JFileChooser.APPROVE_OPTION) {
return;
}
File f = fc.getSelectedFile();
String workdir = fc.getCurrentDirectory().getAbsolutePath();
Globals.prefs.put(Globals.PrefKeys.WORKDIR, workdir);
String fn = f.getAbsolutePath();
if (f.exists()) {
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);
if (dialogResult != JOptionPane.YES_OPTION) {
continue;
}
}
FileFilter selected_filter = fc.getFileFilter();
if (selected_filter == sesim_filter) {
System.out.printf("Filter", selected_filter.toString());
if (!fn.toLowerCase().endsWith("sesim")) {
f = new File(fn + "." + "sesim");
}
}
try {
Globals.saveFile(f);
return;
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "Can't save file " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
}//GEN-LAST:event_saveAsMenuItemActionPerformed
private void editExchangeMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editExchangeMenuItemActionPerformed
@ -682,29 +720,29 @@ public class SeSimApplication extends javax.swing.JFrame {
TraderListDialog tld = null;
private void viewTraderListCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_viewTraderListCheckBoxActionPerformed
javax.swing.SwingUtilities.invokeLater(()->{
javax.swing.SwingUtilities.invokeLater(() -> {
System.out.printf("Trwindow: %s\n", Boolean.toString(this.viewTraderListCheckBox.getState()));
if (this.viewTraderListCheckBox.getState()) {
if (tld == null) {
tld = new TraderListDialog(this, false);
tld.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
viewTraderListCheckBox.setState(false);
System.out.printf("Set menu false\n");
}
});
System.out.printf("Trwindow: %s\n", Boolean.toString(this.viewTraderListCheckBox.getState()));
if (this.viewTraderListCheckBox.getState()) {
if (tld == null) {
tld = new TraderListDialog(this, false);
tld.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
viewTraderListCheckBox.setState(false);
System.out.printf("Set menu false\n");
}
});
}
tld.setVisible(true);
} else if (tld != null) {
System.out.printf("Set visible = false\n");
tld.setVisible(false);
}
tld.setVisible(true);
} else if (tld != null) {
System.out.printf("Set visible = false\n");
tld.setVisible(false);
}
});
});
}//GEN-LAST:event_viewTraderListCheckBoxActionPerformed
@ -726,15 +764,13 @@ javax.swing.SwingUtilities.invokeLater(()->{
System.out.printf("Main called\n");
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[]gs = ge.getScreenDevices();
for (GraphicsDevice d:gs){
System.out.printf("ID %s\n",d.getIDstring());
GraphicsDevice[] gs = ge.getScreenDevices();
for (GraphicsDevice d : gs) {
System.out.printf("ID %s\n", d.getIDstring());
}
//System.exit(0);
Globals.initGlobals();
//System.exit(0);
Globals.se = new Exchange();
@ -752,9 +788,9 @@ javax.swing.SwingUtilities.invokeLater(()->{
System.out.printf("In run method now\n");
String x = new java.io.File(SeSimApplication.class.getProtectionDomain()
.getCodeSource()
.getLocation()
.getPath()).toString(); //.getName();
.getCodeSource()
.getLocation()
.getPath()).toString(); //.getName();
System.out.printf("Creating Application\n");
new SeSimApplication().setVisible(true);