1 | package geniusweb.actions;
|
---|
2 |
|
---|
3 | import java.awt.event.ActionEvent;
|
---|
4 | import java.awt.event.KeyEvent;
|
---|
5 | import java.io.File;
|
---|
6 | import java.util.logging.Level;
|
---|
7 |
|
---|
8 | import javax.swing.JFileChooser;
|
---|
9 | import javax.swing.KeyStroke;
|
---|
10 | import javax.swing.filechooser.FileNameExtensionFilter;
|
---|
11 |
|
---|
12 | import geniusweb.domaineditor.ProfileEditor;
|
---|
13 | import geniusweb.domaineditor.model.profile.LinearAdditiveUtilitySpaceModel;
|
---|
14 | import geniusweb.profile.utilityspace.LinearAdditiveUtilitySpace;
|
---|
15 | import tudelft.utilities.logging.Reporter;
|
---|
16 |
|
---|
17 | public class LoadAction extends GuiAction {
|
---|
18 |
|
---|
19 | public LoadAction(Reporter log) {
|
---|
20 | super("Open", log);
|
---|
21 | putValue(SHORT_DESCRIPTION, "Load the profile");
|
---|
22 | putValue(ACCELERATOR_KEY,
|
---|
23 | KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
|
---|
24 | }
|
---|
25 |
|
---|
26 | @Override
|
---|
27 | public void actionPerformed(ActionEvent e) {
|
---|
28 | load(ProfileEditor.getInstance().getModel());
|
---|
29 | }
|
---|
30 |
|
---|
31 | public void load(LinearAdditiveUtilitySpaceModel model) {
|
---|
32 | try {
|
---|
33 | JFileChooser chooser = new JFileChooser();
|
---|
34 | FileNameExtensionFilter filter = new FileNameExtensionFilter(
|
---|
35 | "JSON file", "json");
|
---|
36 | chooser.setFileFilter(filter);
|
---|
37 | int selection = chooser.showOpenDialog(null);
|
---|
38 | if (selection != JFileChooser.APPROVE_OPTION)
|
---|
39 | return;
|
---|
40 | File file = chooser.getSelectedFile();
|
---|
41 |
|
---|
42 | LinearAdditiveUtilitySpace profi = jackson.readValue(file,
|
---|
43 | LinearAdditiveUtilitySpace.class);
|
---|
44 | model.setCurrentValue(profi);
|
---|
45 | } catch (Exception e1) {
|
---|
46 | log.log(Level.WARNING,
|
---|
47 | "Failed to read file " + ": " + e1.getMessage());
|
---|
48 | e1.printStackTrace();
|
---|
49 | }
|
---|
50 | }
|
---|
51 | }
|
---|