View Javadoc

1   /*
2    * @(#)$Id: SaveProjectAsAction.java 689 2009-07-22 00:10:27Z bsigner $
3    *
4    * Author       :   Michele Croci, mcroci@gmail.com
5    *
6    * Purpose      :   Saves the mappings with a new name.
7    *
8    * -----------------------------------------------------------------------
9    *
10   * Revision Information:
11   *
12   * Date             Who         Reason
13   *
14   * Jan 16, 2008     crocimi     Initial Release
15   * Jan 20, 2008     bsigner     Cleanup
16   *
17   * -----------------------------------------------------------------------
18   *
19   * Copyright 1999-2009 ETH Zurich. All Rights Reserved.
20   *
21   * This software is the proprietary information of ETH Zurich.
22   * Use is subject to license terms.
23   * 
24   */
25  
26  
27  package org.ximtec.igesture.geco.gui.action;
28  
29  import java.awt.event.ActionEvent;
30  import java.io.File;
31  
32  import javax.swing.JFileChooser;
33  
34  import org.sigtec.graphix.widget.BasicAction;
35  import org.sigtec.util.FileHandler;
36  import org.sigtec.util.MIME;
37  import org.ximtec.igesture.geco.gui.MainView;
38  import org.ximtec.igesture.geco.util.Constant;
39  import org.ximtec.igesture.geco.util.ExtensionFileFilter;
40  import org.ximtec.igesture.geco.util.GuiBundleTool;
41  
42  
43  /**
44   * Saves the mappings with a new name.
45   * 
46   * @version 0.9, Jan 2008
47   * @author Michele Croci, mcroci@gmail.com
48   * @author Beat Signer, signer@inf.ethz.ch
49   */
50  public class SaveProjectAsAction extends BasicAction {
51  
52     /**
53      * The key used to retrieve action details from the resource bundle.
54      */
55     public static final String KEY = "SaveProjectAsAction";
56  
57     private MainView mainView;
58  
59  
60     public SaveProjectAsAction(MainView mainView) {
61        super(KEY, GuiBundleTool.getBundle());
62        this.mainView = mainView;
63     }
64  
65  
66     /**
67      * Saves the mappings.
68      * 
69      * @param event the action event.
70      */
71     public void actionPerformed(ActionEvent event) {
72        showSaveDialog();
73     } // actionPerformed
74  
75  
76     private void showSaveDialog() {
77        String xmlExtension = MIME.getExtension(MIME.XML);
78        JFileChooser fileChooser = new JFileChooser();
79        fileChooser.setFileFilter(new ExtensionFileFilter(xmlExtension,
80              new String[] { xmlExtension }));
81        fileChooser.setCurrentDirectory(mainView.getModel().getProjectFile());
82        int status = fileChooser.showSaveDialog(null);
83  
84        if (status == JFileChooser.APPROVE_OPTION) {
85           File selectedFile = fileChooser.getSelectedFile();
86  
87           if (selectedFile != null) {
88              String ext = Constant.EMPTY_STRING;
89  
90              if (selectedFile.getName().length() >= 4) {
91                 ext = selectedFile.getName().substring(
92                       selectedFile.getName().length() - 4,
93                       selectedFile.getName().length());
94              }
95  
96              File pFile = null;
97  
98              if (!ext.equals(Constant.DOT + xmlExtension)) {
99                 pFile = FileHandler.createFile(selectedFile.getPath(),
100                      xmlExtension);
101             }
102             else {
103                pFile = selectedFile;
104             }
105 
106             String projectName = pFile.getName();
107             projectName = projectName.substring(0, projectName.length() - 4);
108             mainView.getModel().setProjectFile(pFile);
109             mainView.getModel().setProjectName(projectName);
110             mainView.initProjectView(projectName);
111             mainView.updateGestureList();
112             mainView.enableMenuItem();
113             mainView.getModel().setNeedSave(true);
114             (new SaveProjectAction(mainView)).save();
115             (new OpenProjectAction(mainView)).exportConfiguration();
116          }
117 
118       }
119 
120    } // showSaveDialog
121 
122 }