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) {
textArea.setText(null);
} else if (src == saveItem) {
FileTools.saveObjectWithFileChooser(frame, textArea.getText());
FileTools.saveObjectWithFileChooser(frame, textArea.getText(), null);
} else {
System.err.println("Unknown popup component (JTextoutputFrame)!");
}

View File

@ -6,8 +6,11 @@ import eva2.tools.BasicResourceLoader;
import eva2.tools.EVAHELP;
import eva2.tools.SerializedObject;
import eva2.util.annotation.Description;
import eva2.yaml.BeanSerializer;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.plaf.basic.BasicComboBoxRenderer;
import java.awt.*;
import java.awt.event.ActionEvent;
@ -110,7 +113,8 @@ public class OptimizationEditorPanel extends JPanel implements ItemListener {
openButton.addActionListener(new ActionListener() {
@Override
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) {
// setValue takes care of: Making sure obj is of right type,
// and firing property change.
@ -128,7 +132,8 @@ public class OptimizationEditorPanel extends JPanel implements ItemListener {
saveButton.addActionListener(new ActionListener() {
@Override
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.tools.BasicResourceLoader;
import eva2.tools.StringTools;
import org.yaml.snakeyaml.Yaml;
import javax.swing.filechooser.FileFilter;
import javax.swing.*;
import java.awt.*;
import java.io.*;
@ -112,12 +114,13 @@ public class FileTools {
* @param parentComponent the parent component
* @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;
File sFile;
boolean finished = false;
do {
JFileChooser fc = createFileChooser();
fc.setFileFilter(filter);
returnVal = fc.showSaveDialog(parentComponent);
if (returnVal == JFileChooser.APPROVE_OPTION) {
sFile = fc.getSelectedFile();
@ -169,16 +172,15 @@ public class FileTools {
*
* @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;
JFileChooser fc = createFileChooser();
fc.setFileFilter(filter);
int returnVal = fc.showOpenDialog(parentComponent);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File selected = fc.getSelectedFile();
try {
ObjectInputStream oi = new ObjectInputStream(new BufferedInputStream(new FileInputStream(selected)));
obj = oi.readObject();
oi.close();
obj = new Yaml().load(new FileInputStream(selected.getAbsolutePath()));
if (!clazz.isAssignableFrom(obj.getClass())) {
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) {
String predefName = null;
String predefName;
try {
predefName = (String) BeanInspector.callIfAvailable(object, "getName", null);
predefName = StringTools.simplifySymbols(predefName) + ".yml";

View File

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