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 javax.swing.AbstractAction;
024    import javax.swing.JOptionPane;
025    
026    import csheets.ui.BlankIcon;
027    
028    /**
029     * An base-class for actions.
030     * @author Einar Pehrson
031     */
032    public abstract class BaseAction extends AbstractAction {
033    
034            /**
035             * Creates a new base action.
036             */
037            public BaseAction() {
038                    // Configures action
039                    String name = getName();
040                    putValue(NAME, name);
041                    putValue(SHORT_DESCRIPTION, name);
042                    putValue(ACTION_COMMAND_KEY, name);
043                    putValue(SMALL_ICON, new BlankIcon(16));
044                    defineProperties();
045            }
046    
047            /**
048             * Returns the action's name.
049             * @return the action's name, which is used as short description and action command
050             */
051            protected abstract String getName();
052    
053            /**
054             * Defines the action's properties.
055             */
056            protected void defineProperties() {}
057    
058            /**
059             * Returns whether the action requires the active workbook to be
060             * modified in order to be enabled. By default, the method returns false.
061             * @return whether the action should be enabled
062             */
063            protected boolean requiresModification() {
064                    return false;
065            }
066    
067            /**
068             * Returns whether the action requires the active workbook to be
069             * stored in a file in order to be enabled. By default, the method
070             * returns false.
071             * @return whether the action should be enabled
072             */
073            protected boolean requiresFile() {
074                    return false;
075            }
076    
077            /**
078             * Shows the user an error message.
079             */
080            protected void showErrorDialog(Object message) {
081                    JOptionPane.showMessageDialog(
082                            null,
083                            message,
084                            "Error",
085                            JOptionPane.ERROR_MESSAGE
086                    );
087            }
088    }