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 SaveAction extends GuiAction {
|
---|
18 |
|
---|
19 | public SaveAction(Reporter log) {
|
---|
20 | super("Save", log);
|
---|
21 | putValue(SHORT_DESCRIPTION, "Save the profile");
|
---|
22 | putValue(ACCELERATOR_KEY,
|
---|
23 | KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
|
---|
24 | }
|
---|
25 |
|
---|
26 | @Override
|
---|
27 | public void actionPerformed(ActionEvent e) {
|
---|
28 | save(ProfileEditor.getInstance().getModel());
|
---|
29 | }
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Save a model.
|
---|
33 | *
|
---|
34 | * @param model the {@link LinearAdditiveUtilitySpaceModel} to be saved
|
---|
35 | */
|
---|
36 | protected void save(LinearAdditiveUtilitySpaceModel model) {
|
---|
37 | try {
|
---|
38 | LinearAdditiveUtilitySpace profile = model.getCurrentValue();
|
---|
39 |
|
---|
40 | JFileChooser chooser = new JFileChooser();
|
---|
41 | chooser.setSelectedFile(new File(profile.getName() + ".json"));
|
---|
42 | FileNameExtensionFilter filter = new FileNameExtensionFilter(
|
---|
43 | "JSON file", "json");
|
---|
44 | chooser.setFileFilter(filter);
|
---|
45 | int selection = chooser.showSaveDialog(null);
|
---|
46 | if (selection != JFileChooser.APPROVE_OPTION)
|
---|
47 | return;
|
---|
48 | File file = chooser.getSelectedFile();
|
---|
49 | jackson.writeValue(file, profile);
|
---|
50 | } catch (Exception e) {
|
---|
51 | log.log(Level.SEVERE, "Failed to save:" + e.getMessage());
|
---|
52 | e.printStackTrace();
|
---|
53 | }
|
---|
54 | }
|
---|
55 | }
|
---|