Save As now catches exeptions

This commit is contained in:
7u83 2017-04-08 10:13:09 +02:00
parent 6b53f931bc
commit 1a32edd373
4 changed files with 41 additions and 18 deletions

View File

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

View File

@ -127,7 +127,7 @@ public class Globals {
.getPath()).toString();
pathlist.add(dp);
LOGGER.info(String.format("Path %s",dp));
LOGGER.info(String.format("Path %s", dp));
tloader = new AutoTraderLoader(pathlist);
}
@ -180,20 +180,28 @@ public class Globals {
prefs.put(STRATEGYPREFS, cfgs.toString());
}
static void saveFile(File f) {
public static class FileStrings {
public static final String SESIMVERSION = "sesim_version";
public static final String STRATEGIES = "strategies";
public static final String TRADERS = "traders";
}
static void saveFile(File f) throws FileNotFoundException {
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);
}
sobj.put(FileStrings.SESIMVERSION, "0.1");
sobj.put(FileStrings.STRATEGIES, strategies);
sobj.put(FileStrings.TRADERS, traders);
PrintWriter out;
out = new PrintWriter(f.getAbsolutePath());
out.print(sobj.toString(4));
out.close();
}

View File

@ -76,6 +76,9 @@
<Property name="mnemonic" type="int" value="115"/>
<Property name="text" type="java.lang.String" value="Save"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="saveMenuItemActionPerformed"/>
</Events>
</MenuItem>
<MenuItem class="javax.swing.JMenuItem" name="saveAsMenuItem">
<Properties>

View File

@ -42,6 +42,7 @@ import java.util.logging.Logger;
import java.util.prefs.Preferences;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import static javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER;
import javax.swing.UIManager;
import javax.swing.filechooser.FileNameExtensionFilter;
@ -346,6 +347,11 @@ public class SeSimApplication extends javax.swing.JFrame {
saveMenuItem.setMnemonic('s');
saveMenuItem.setText("Save");
saveMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
saveMenuItemActionPerformed(evt);
}
});
fileMenu.add(saveMenuItem);
saveAsMenuItem.setMnemonic('a');
@ -607,18 +613,20 @@ public class SeSimApplication extends javax.swing.JFrame {
String[] e = ((FileNameExtensionFilter) fc.getFileFilter()).getExtensions();
System.out.printf("Abs: %s\n", f.getAbsoluteFile());
String fn = f.getAbsolutePath();
if (!f.getAbsolutePath().endsWith(e[0])) {
f = new File(f.getAbsolutePath() + "." + e[0]);
}
Globals.saveFile(f);
System.out.printf("Sel File: %s \n", f.getAbsolutePath());
try{
Globals.saveFile(f);
}
catch (Exception ex){
JOptionPane.showMessageDialog(this, "Can't save file "+ex.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
}
}//GEN-LAST:event_saveAsMenuItemActionPerformed
@ -704,6 +712,10 @@ javax.swing.SwingUtilities.invokeLater(()->{
System.out.printf("Accel Spinner PRop Change\n");
}//GEN-LAST:event_accelSpinnerPropertyChange
private void saveMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveMenuItemActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_saveMenuItemActionPerformed
/**
* @param args the command line arguments
* @throws java.lang.IllegalAccessException