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.awt.Container;
024import java.awt.event.ActionEvent;
025
026import javax.swing.JComponent;
027import javax.swing.JTabbedPane;
028
029import csheets.ui.ctrl.FocusOwnerAction;
030
031/**
032 * An action for showing and hiding UI extension side bars.
033 * @author Einar Pehrson
034 */
035@SuppressWarnings("serial")
036public class SideBarAction extends FocusOwnerAction {
037
038        /** The extension to control*/
039        private UIExtension extension;
040
041        /** The component to control */
042        private JComponent component;
043
044        /** The side bar pane */
045        private JTabbedPane sideBar;
046
047        /**
048         * Creates a new side bar action.
049         * @param extension the extension to control
050         * @param component the component to control
051         */
052        public SideBarAction(UIExtension extension, JComponent component) {
053                // Stores members
054                this.extension = extension;
055                this.component = component;
056
057                // Fetches parent, and removes component if appropriate
058                Container parent = component.getParent();
059                if (parent instanceof JTabbedPane)
060                        sideBar = (JTabbedPane)parent;
061                if (!component.isEnabled())
062                        sideBar.remove(component);
063
064                // Configures action
065                String name = extension.getExtension().getName();
066                putValue(NAME, name);
067                putValue(SHORT_DESCRIPTION, name);
068                putValue(ACTION_COMMAND_KEY, name);
069                putValue(SMALL_ICON, extension.getIcon());
070        }
071
072        protected String getName() {
073                return null;
074        }
075
076        /**
077         * Toggles the visiblity of the component.
078         * @param event the event that was fired
079         */
080        public void actionPerformed(ActionEvent event) {
081                if (sideBar != null) {
082                        // Toggles component
083                        if (component.isEnabled())
084                                sideBar.remove(component);
085                        else {
086                                int i = 0;
087                                for (; i < sideBar.getTabCount()
088                                        && component.getName().compareTo(sideBar.getTitleAt(i)) < 0; i++);
089                                sideBar.insertTab(extension.getExtension().getName(),
090                                        extension.getIcon(), component, null, i);
091                        }
092
093                        // Toggles properties
094                        component.setEnabled(!component.isEnabled());
095                        extension.setEnabledProperty("sidebar", component.isEnabled());
096                }
097        }
098}