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 */ 021package csheets.ui.ctrl; 022 023import java.awt.event.ActionEvent; 024import java.awt.event.KeyEvent; 025import java.io.File; 026import java.io.FileNotFoundException; 027import java.io.InvalidClassException; 028import java.io.OptionalDataException; 029import java.io.StreamCorruptedException; 030 031import javax.swing.ImageIcon; 032import javax.swing.KeyStroke; 033 034import csheets.CleanSheets; 035import csheets.core.Workbook; 036import csheets.ui.FileChooser; 037 038/** 039 * An action for opening a spreadsheet. 040 * @author Einar Pehrson 041 */ 042@SuppressWarnings("serial") 043public class OpenAction extends BaseAction { 044 045 /** The CleanSheets application */ 046 protected CleanSheets app; 047 048 /** The user interface controller */ 049 protected UIController uiController; 050 051 /** The file chooser to use when prompting the user for the file to open */ 052 private FileChooser chooser; 053 054 /** 055 * Creates a new open action. 056 * @param app the CleanSheets application 057 * @param uiController the user interface controller 058 * @param chooser the file chooser to use when prompting the user for the file to open 059 */ 060 public OpenAction(CleanSheets app, UIController uiController, FileChooser chooser) { 061 // Stores members 062 this.app = app; 063 this.uiController = uiController; 064 this.chooser = chooser; 065 setEnabled(chooser != null); 066 } 067 068 protected String getName() { 069 return "Open..."; 070 } 071 072 protected void defineProperties() { 073 putValue(MNEMONIC_KEY, KeyEvent.VK_O); 074 putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK)); 075 putValue(SMALL_ICON, new ImageIcon(CleanSheets.class.getResource("res/img/open.gif"))); 076 } 077 078 public void actionPerformed(ActionEvent event) { 079 File file = getFile(); 080 if (file != null) { 081 Workbook workbook = app.getWorkbook(file); 082 if (workbook == null) 083 try { 084 // Opens the file 085 app.load(file); 086 } catch (FileNotFoundException fe) { 087 showErrorDialog("The file you requested was not found."); 088 } catch (ClassNotFoundException e) { 089 showErrorDialog("The class of a serialized object cannot be found:\n" + e.getMessage() + "."); 090 } catch (InvalidClassException e) { 091 showErrorDialog("Something is wrong with a class used by serialization."); 092 } catch (StreamCorruptedException e) { 093 showErrorDialog("Control information in the input stream is inconsistent."); 094 } catch (OptionalDataException e) { 095 showErrorDialog("Primitive data was found in the input stream instead of objects."); 096 } catch (Exception e) { 097 showErrorDialog("An I/O error occurred when loading the file."); 098 } 099 else 100 uiController.setActiveWorkbook(workbook); 101 } 102 } 103 104 /** 105 * Returns the file to open. 106 * @return file the file to open 107 */ 108 public File getFile() { 109 return chooser.getFileToOpen(); 110 } 111}