001    // GraphLab Project: http://graphlab.sharif.edu
002    // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology
003    // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/
004    
005    package graphlab.samples.ui.texteditor.myplugin.actions;
006    
007    import graphlab.platform.core.BlackBoard;
008    import graphlab.ui.extension.UIActionExtension;
009    
010    import javax.swing.*;
011    import java.io.File;
012    import java.io.IOException;
013    
014    public class SaveAction implements UIActionExtension {
015    
016        public void actionPerformed(BlackBoard blackBoard) {
017            String path = (String) blackBoard.getData("last file");
018    
019            if (path == null || path.equals("")) {
020                JFileChooser jfc = new JFileChooser();
021                jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
022                jfc.setMultiSelectionEnabled(false);
023                jfc.setDialogType(JFileChooser.SAVE_DIALOG);
024                int ret = jfc.showSaveDialog(null);
025                if (ret != JFileChooser.APPROVE_OPTION) {
026                    return;
027                }
028                path = jfc.getSelectedFile().getAbsolutePath();
029            }
030    
031            //fetch the text area (body of application) from blackboard
032            JTextArea editor = Utils.getMainEditor(blackBoard);
033    
034            try {
035                System.out.println(path);
036    
037                File f = new File(path);
038                if (!f.exists()) {
039                    f.createNewFile();
040                }
041                ReadWriteTextFile.setContents(f, editor.getText());
042            } catch (IOException e) {
043                JOptionPane.showMessageDialog(null, "Error while saving file! <br> " + e.getMessage(), "Save Error", JOptionPane.ERROR_MESSAGE);
044                e.printStackTrace();
045            }
046        }
047    
048    }