source: domaineditor/src/main/java/geniusweb/actions/SaveAction.java

Last change on this file was 52, checked in by ruud, 20 months ago

Fixed small issues in domaineditor.

File size: 1.6 KB
Line 
1package geniusweb.actions;
2
3import java.awt.event.ActionEvent;
4import java.awt.event.KeyEvent;
5import java.io.File;
6import java.util.logging.Level;
7
8import javax.swing.JFileChooser;
9import javax.swing.KeyStroke;
10import javax.swing.filechooser.FileNameExtensionFilter;
11
12import geniusweb.domaineditor.ProfileEditor;
13import geniusweb.domaineditor.model.profile.LinearAdditiveUtilitySpaceModel;
14import geniusweb.profile.utilityspace.LinearAdditiveUtilitySpace;
15import tudelft.utilities.logging.Reporter;
16
17public 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}
Note: See TracBrowser for help on using the repository browser.