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.ext;
022
023import java.util.Properties;
024
025import javax.swing.Icon;
026import javax.swing.JComponent;
027import javax.swing.JMenu;
028import javax.swing.JToolBar;
029
030import csheets.ext.Extension;
031import csheets.ui.ctrl.UIController;
032
033/**
034 * <p>A base class for user interface extensions to the CleanSheets application.
035 * Subclasses should override the accessor methods for the components that they
036 * provide, and add components as selection listeners whenever required.
037 * <p>A user interface extension should only create one instance of each type of
038 * component, and therefore always return the same component when a given
039 * method is called.
040 * @see csheets.ui.ctrl.SelectionListener
041 * @author Einar Pehrson
042 */
043public abstract class UIExtension {
044
045        /** The name of the extension */
046        protected final Extension extension;
047
048        /** The user interface controller */
049        protected final UIController uiController;
050
051        /** The application's user properties */
052        protected final Properties props;
053
054        /**
055         * Creates a new user interface extension..
056         * @param extension the extension for which components are provided
057         * @param uiController the user interface controller
058         */
059        public UIExtension(Extension extension, UIController uiController) {
060                // Stores member
061                this.extension = extension;
062                this.uiController = uiController;
063                this.props = uiController.getUserProperties();
064
065                // Enables components according to properties
066                if (getCellDecorator() != null)
067                        getCellDecorator().setEnabled(Boolean.TRUE.equals(
068                                getEnabledProperty("celldecorator")));
069                if (getTableDecorator() != null)
070                        getTableDecorator().setEnabled(Boolean.TRUE.equals(
071                                getEnabledProperty("tabledecorator")));
072                if (getToolBar() != null)
073                        getToolBar().setVisible(Boolean.TRUE.equals(
074                                getEnabledProperty("toolbar")));
075                if (getSideBar() != null)
076                        getSideBar().setEnabled(Boolean.TRUE.equals(
077                                getEnabledProperty("sidebar")));
078        }
079
080        /**
081         * Returns the extension for which this UI extension provides components.
082         * @return the extension for which this UI extension provides components
083         */
084        public final Extension getExtension() {
085                return extension;
086        }
087
088        /**
089         * Returns the enabled property corresponding to the given component key.
090         * @param propKey the property key of the relevant component
091         * @return a Boolean value if a property is found, null otherwise
092         */
093        public final Boolean getEnabledProperty(String propKey) {
094                String prop = props.getProperty(extension.getPropertyKey() + propKey);
095                if (prop == null)
096                        return null;
097                else if (prop.equalsIgnoreCase("true"))
098                        return true;
099                else if (prop.equalsIgnoreCase("false"))
100                        return false;
101                else
102                        return null;
103        }
104
105        /**
106         * Sets the enabled property corresponding to the given component key.
107         * @param propKey the property key of the relevant component
108         * @param enabled whether the component should be enabled or not
109         */
110        public final void setEnabledProperty(String propKey, boolean enabled) {
111                props.setProperty(extension.getPropertyKey() + propKey,
112                        Boolean.toString(enabled));
113        }
114
115        /**
116         * Returns an icon to display with the extension's name.
117         * @return an icon, or null if the extension does not provide one
118         */
119        public Icon getIcon() {
120                return null;
121        }
122
123        /**
124         * Returns a cell decorator that visualizes the data added by the extension.
125         * @return a cell decorator, or null if the extension does not provide one
126         */
127        public CellDecorator getCellDecorator() {
128                return null;
129        }
130
131        /**
132         * Returns a table decorator that visualizes the data added by the extension.
133         * @return a table decorator, or null if the extension does not provide one
134         */
135        public TableDecorator getTableDecorator() {
136                return null;
137        }
138
139        /**
140         * Returns a menu component that gives access to extension-specific
141         * functionality.
142         * @return a JMenu component, or null if the extension does not provide one
143         */
144        public JMenu getMenu() {
145                return null;
146        }
147
148        /**
149         * Returns a toolbar that gives access to extension-specific
150         * functionality.
151         * @return a JToolBar component, or null if the extension does not provide one
152         */
153        public JToolBar getToolBar() {
154                return null;
155        }
156
157        /**
158         * Returns a side bar that gives access to extension-specific
159         * functionality.
160         * @return a component, or null if the extension does not provide one
161         */
162        public JComponent getSideBar() {
163                return null;
164        }
165}