Add load/store to configurations.

refs #31
This commit is contained in:
Fabian Becker 2014-11-03 16:08:38 +01:00
parent 70dd824d13
commit a08655a60f
4 changed files with 18 additions and 10 deletions

View File

@ -126,7 +126,7 @@ public class JTextoutputFrame implements JTextoutputFrameInterface, ActionListen
if (src == clearItem) { if (src == clearItem) {
textArea.setText(null); textArea.setText(null);
} else if (src == saveItem) { } else if (src == saveItem) {
FileTools.saveObjectWithFileChooser(frame, textArea.getText()); FileTools.saveObjectWithFileChooser(frame, textArea.getText(), null);
} else { } else {
System.err.println("Unknown popup component (JTextoutputFrame)!"); System.err.println("Unknown popup component (JTextoutputFrame)!");
} }

View File

@ -6,8 +6,11 @@ import eva2.tools.BasicResourceLoader;
import eva2.tools.EVAHELP; import eva2.tools.EVAHELP;
import eva2.tools.SerializedObject; import eva2.tools.SerializedObject;
import eva2.util.annotation.Description; import eva2.util.annotation.Description;
import eva2.yaml.BeanSerializer;
import javax.swing.*; import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.plaf.basic.BasicComboBoxRenderer; import javax.swing.plaf.basic.BasicComboBoxRenderer;
import java.awt.*; import java.awt.*;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
@ -110,7 +113,8 @@ public class OptimizationEditorPanel extends JPanel implements ItemListener {
openButton.addActionListener(new ActionListener() { openButton.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(final ActionEvent event) { public void actionPerformed(final ActionEvent event) {
Object object = FileTools.openObject(openButton, genericObjectEditor.getClassType()); FileFilter filter = new FileNameExtensionFilter("YAML file", "yml", "yaml");
Object object = FileTools.openObject(openButton, genericObjectEditor.getClassType(), filter);
if (object != null) { if (object != null) {
// setValue takes care of: Making sure obj is of right type, // setValue takes care of: Making sure obj is of right type,
// and firing property change. // and firing property change.
@ -128,7 +132,8 @@ public class OptimizationEditorPanel extends JPanel implements ItemListener {
saveButton.addActionListener(new ActionListener() { saveButton.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(final ActionEvent event) { public void actionPerformed(final ActionEvent event) {
FileTools.saveObjectWithFileChooser(saveButton, genericObjectEditor.getValue()); FileFilter filter = new FileNameExtensionFilter("YAML file", "yml", "yaml");
FileTools.saveObjectWithFileChooser(saveButton, BeanSerializer.serializeObject(genericObjectEditor.getValue()), filter);
} }
}); });

View File

@ -3,7 +3,9 @@ package eva2.optimization.tools;
import eva2.gui.BeanInspector; import eva2.gui.BeanInspector;
import eva2.tools.BasicResourceLoader; import eva2.tools.BasicResourceLoader;
import eva2.tools.StringTools; import eva2.tools.StringTools;
import org.yaml.snakeyaml.Yaml;
import javax.swing.filechooser.FileFilter;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.io.*; import java.io.*;
@ -112,12 +114,13 @@ public class FileTools {
* @param parentComponent the parent component * @param parentComponent the parent component
* @param object The object to save. * @param object The object to save.
*/ */
public static boolean saveObjectWithFileChooser(Component parentComponent, Object object) { public static boolean saveObjectWithFileChooser(Component parentComponent, Object object, FileFilter filter) {
int returnVal; int returnVal;
File sFile; File sFile;
boolean finished = false; boolean finished = false;
do { do {
JFileChooser fc = createFileChooser(); JFileChooser fc = createFileChooser();
fc.setFileFilter(filter);
returnVal = fc.showSaveDialog(parentComponent); returnVal = fc.showSaveDialog(parentComponent);
if (returnVal == JFileChooser.APPROVE_OPTION) { if (returnVal == JFileChooser.APPROVE_OPTION) {
sFile = fc.getSelectedFile(); sFile = fc.getSelectedFile();
@ -169,16 +172,15 @@ public class FileTools {
* *
* @return the loaded object, or null if the operation was cancelled * @return the loaded object, or null if the operation was cancelled
*/ */
public static Object openObject(Component parentComponent, Class clazz) { public static Object openObject(Component parentComponent, Class clazz, FileFilter filter) {
Object obj = null; Object obj = null;
JFileChooser fc = createFileChooser(); JFileChooser fc = createFileChooser();
fc.setFileFilter(filter);
int returnVal = fc.showOpenDialog(parentComponent); int returnVal = fc.showOpenDialog(parentComponent);
if (returnVal == JFileChooser.APPROVE_OPTION) { if (returnVal == JFileChooser.APPROVE_OPTION) {
File selected = fc.getSelectedFile(); File selected = fc.getSelectedFile();
try { try {
ObjectInputStream oi = new ObjectInputStream(new BufferedInputStream(new FileInputStream(selected))); obj = new Yaml().load(new FileInputStream(selected.getAbsolutePath()));
obj = oi.readObject();
oi.close();
if (!clazz.isAssignableFrom(obj.getClass())) { if (!clazz.isAssignableFrom(obj.getClass())) {
throw new Exception("Object not of type: " + clazz.getName()); throw new Exception("Object not of type: " + clazz.getName());
} }
@ -204,7 +206,7 @@ public class FileTools {
} }
public static boolean saveObjectToFolder(Object object, File folder, boolean forceOverwrite, Component parentComponent) { public static boolean saveObjectToFolder(Object object, File folder, boolean forceOverwrite, Component parentComponent) {
String predefName = null; String predefName;
try { try {
predefName = (String) BeanInspector.callIfAvailable(object, "getName", null); predefName = (String) BeanInspector.callIfAvailable(object, "getName", null);
predefName = StringTools.simplifySymbols(predefName) + ".yml"; predefName = StringTools.simplifySymbols(predefName) + ".yml";

View File

@ -16,7 +16,7 @@ import java.util.Set;
import java.util.TreeSet; import java.util.TreeSet;
/** /**
* Created by becker on 02.11.2014. *
*/ */
public class BeanSerializer { public class BeanSerializer {
@ -25,6 +25,7 @@ public class BeanSerializer {
options.setAllowReadOnlyProperties(false); options.setAllowReadOnlyProperties(false);
options.setIndent(4); options.setIndent(4);
Yaml yaml = new Yaml(new OptimizationRepresenter(), options); Yaml yaml = new Yaml(new OptimizationRepresenter(), options);
return yaml.dump(obj); return yaml.dump(obj);
} }