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.ext.style.ui;
022
023import java.awt.Insets;
024import java.util.HashMap;
025import java.util.Map;
026
027import javax.swing.Action;
028import javax.swing.ButtonGroup;
029import javax.swing.JButton;
030import javax.swing.JToggleButton;
031import javax.swing.JToolBar;
032import javax.swing.SwingConstants;
033
034import csheets.ext.style.StylableCell;
035import csheets.ext.style.StyleExtension;
036import csheets.ui.ctrl.SelectionEvent;
037import csheets.ui.ctrl.SelectionListener;
038import csheets.ui.ctrl.UIController;
039
040/**
041 * A tool bar that displays style-related actions.
042 * @author Einar Pehrson
043 */
044@SuppressWarnings("serial")
045public class StyleToolBar extends JToolBar implements SelectionListener {
046
047        /** The common button insets */
048        private static final Insets INSETS = new Insets(2, 2, 2, 2);
049
050        /** The button for making the current font bold */
051        private JToggleButton boldButton;
052
053        /** The button for making the current font italic */
054        private JToggleButton italicButton;
055
056        /** The button for applying left alignment */
057        private Map<Integer, JToggleButton> hAlignButtons
058                = new HashMap<Integer, JToggleButton>();
059
060        /**
061         * Creates a new style tool bar.
062         * @param uiController the user interface controller
063         */
064        public StyleToolBar(UIController uiController) {
065                super("Style");
066                uiController.addSelectionListener(this);
067
068                // Adds font actions
069                add(new FontAction(uiController));
070                boldButton = addToggleButton(new BoldAction(uiController));
071                italicButton = addToggleButton(new ItalicAction(uiController));
072                addSeparator();
073
074                // Adds color actions
075                add(new FormatAction(uiController));
076                add(new BorderAction(uiController));
077                add(new ForegroundAction(uiController));
078                add(new BackgroundAction(uiController));
079                addSeparator();
080
081                // Adds alignment actions
082                ButtonGroup hAlignGroup = new ButtonGroup();
083                hAlignButtons.put(SwingConstants.LEFT, addToggleButton(
084                        new AlignLeftAction(uiController), hAlignGroup));
085                hAlignButtons.put(SwingConstants.CENTER, addToggleButton(
086                        new AlignCenterAction(uiController), hAlignGroup));
087                hAlignButtons.put(SwingConstants.RIGHT, addToggleButton(
088                        new AlignRightAction(uiController), hAlignGroup));
089        }
090
091        /**
092         * Adds a button with the given action to the tool bar, and reduces the
093         * default insets.
094         * @param action the action to add
095         * @return the button that was added
096         */
097        public JButton add(Action action) {
098                JButton button = super.add(action);
099                button.setMargin(INSETS);
100                return button;
101        }
102
103        /**
104         * Adds a button with the given action to the tool bar, and reduces the
105         * default insets.
106         * @param action the action to add
107         * @param groups the button groups to which the button belongs
108         * @return the button that was added
109         */
110        public JToggleButton addToggleButton(Action action, ButtonGroup... groups) {
111                JToggleButton button = new JToggleButton(action);
112                button.setText(null);
113                button.setMargin(INSETS);
114                add(button);
115                for (ButtonGroup group : groups)
116                        group.add(button);
117                return button;
118        }
119
120        /**
121         * Selects buttons depending on the style of the active cell.
122         * @param event the selection event that was fired
123         */
124        public void selectionChanged(SelectionEvent event) {
125                if (event.getCell() != null && event.isCellChanged()) {
126                        StylableCell cell = (StylableCell)event.getCell().getExtension(
127                                StyleExtension.NAME);
128                        boldButton.setSelected(cell.getFont().isBold());
129                        italicButton.setSelected(cell.getFont().isItalic());
130                        hAlignButtons.get(cell.getHorizontalAlignment()).setSelected(true);
131                }
132        }
133}