001 /*
002 * Copyright (c) 2005 Einar Pehrson <einar@pehrson.nu>.
003 *
004 * This file is part of
005 * CleanSheets - a spreadsheet application for the Java platform.
006 *
007 * CleanSheets is free software; you can redistribute it and/or modify
008 * it under the terms of the GNU General Public License as published by
009 * the Free Software Foundation; either version 2 of the License, or
010 * (at your option) any later version.
011 *
012 * CleanSheets is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015 * GNU General Public License for more details.
016 *
017 * You should have received a copy of the GNU General Public License
018 * along with CleanSheets; if not, write to the Free Software
019 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020 */
021 package csheets.ui.ctrl;
022
023 import java.awt.event.ActionEvent;
024 import java.awt.event.KeyEvent;
025 import java.io.File;
026 import java.io.IOException;
027
028 import javax.swing.ImageIcon;
029 import javax.swing.JOptionPane;
030
031 import csheets.CleanSheets;
032 import csheets.core.Workbook;
033 import csheets.ui.FileChooser;
034
035 /**
036 * An action for saving a spreadsheet to a user-supplied file.
037 * @author Einar Pehrson
038 */
039 @SuppressWarnings("serial")
040 public class SaveAsAction extends BaseAction {
041
042 /** The CleanSheets application */
043 protected CleanSheets app;
044
045 /** The user interface controller */
046 protected UIController uiController;
047
048 /** The file chooser to use when prompting the user for the file to save */
049 protected FileChooser chooser;
050
051 /**
052 * Creates a new save as action.
053 * @param app the CleanSheets application
054 * @param uiController the user interface controller
055 * @param chooser the file chooser to use when prompting the user for the file to save
056 */
057 public SaveAsAction(CleanSheets app, UIController uiController, FileChooser chooser) {
058 // Stores members
059 this.app = app;
060 this.uiController = uiController;
061 this.chooser = chooser;
062 }
063
064 protected String getName() {
065 return "Save As...";
066 }
067
068 protected void defineProperties() {
069 putValue(MNEMONIC_KEY, KeyEvent.VK_A);
070 putValue(SMALL_ICON, new ImageIcon(CleanSheets.class.getResource("res/img/save_as.gif")));
071 }
072
073 public void actionPerformed(ActionEvent e) {
074 // Fetches data
075 Workbook workbook = uiController.getActiveWorkbook();
076 File oldFile = app.getFile(workbook);
077 File file = null;
078
079 // Prompts the user for a file
080 boolean promptForFile = true;
081 while (promptForFile) {
082 file = chooser.getFileToSave();
083 if (file != null) {
084 if (file.exists() && (oldFile == null || !file.equals(oldFile))) {
085 // Prompt to overwrite the file
086 int option = JOptionPane.showConfirmDialog(
087 null,
088 "The chosen file " + file + " already exists\n" +
089 "Do you want to overwrite it?",
090 "Replace existing file?",
091 JOptionPane.YES_NO_CANCEL_OPTION,
092 JOptionPane.WARNING_MESSAGE
093 );
094
095 if (option == JOptionPane.YES_OPTION)
096 promptForFile = false;
097 else if (option == JOptionPane.CANCEL_OPTION
098 || option == JOptionPane.CLOSED_OPTION)
099 return;
100 } else
101 promptForFile = false;
102 } else
103 return;
104 }
105
106 // Saves the file
107 try {
108 app.saveAs(workbook, file);
109 } catch (IOException ex) {
110 showErrorDialog("An I/O error occurred when saving the file.");
111 return;
112 }
113 }
114
115 public void setEnabled(boolean enabled) {
116 super.setEnabled(chooser == null ? false : enabled);
117 }
118
119 protected boolean requiresFile() {
120 return true;
121 }
122 }