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.issuevalue.Domain;
|
---|
15 | import tudelft.utilities.logging.Reporter;
|
---|
16 |
|
---|
17 | public class SaveDomainAction extends GuiAction {
|
---|
18 |
|
---|
19 | public SaveDomainAction(Reporter log) {
|
---|
20 | super("Save Domain", log);
|
---|
21 | putValue(SHORT_DESCRIPTION, "Save the domain only");
|
---|
22 | putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_S,
|
---|
23 | ActionEvent.CTRL_MASK + ActionEvent.SHIFT_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 |
|
---|
38 | try {
|
---|
39 | Domain domain = model.getCurrentValue().getDomain();
|
---|
40 | JFileChooser chooser = new JFileChooser();
|
---|
41 | chooser.setSelectedFile(new File(domain.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, domain);
|
---|
50 | } catch (Exception e) {
|
---|
51 | log.log(Level.WARNING, "Failed to save:" + e.getMessage());
|
---|
52 | e.printStackTrace();
|
---|
53 | }
|
---|
54 | }
|
---|
55 | }
|
---|