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.ext.style.ui;
022    
023    import java.awt.Font;
024    import java.awt.event.ActionEvent;
025    import java.awt.event.KeyEvent;
026    
027    import javax.swing.ImageIcon;
028    
029    import csheets.core.Cell;
030    import csheets.ext.style.StylableCell;
031    import csheets.ext.style.StyleExtension;
032    import csheets.ui.ctrl.FocusOwnerAction;
033    import csheets.ui.ctrl.UIController;
034    
035    /**
036     * A font changing operation.
037     * @author Einar Pehrson
038     */
039    @SuppressWarnings("serial")
040    public class FontAction extends FocusOwnerAction {
041    
042            /** The user interface controller */
043            protected UIController uiController;
044    
045            /**
046             * Creates a new font action.
047             * @param uiController the user interface controller
048             */
049            public FontAction(UIController uiController) {
050                    this.uiController = uiController;
051            }
052    
053            protected String getName() {
054                    return "Font...";
055            }
056    
057            protected void defineProperties() {
058                    putValue(MNEMONIC_KEY, KeyEvent.VK_F);
059                    putValue(SMALL_ICON, new ImageIcon(StyleExtension.class.getResource("res/img/font.gif")));
060            }
061    
062            /**
063             * Lets the user select a font from a chooser.
064             * Then applies the font to the selected cells in the focus owner table.
065             * @param event the event that was fired
066             */
067            public void actionPerformed(ActionEvent event) {
068                    if (focusOwner == null)
069                            return;
070    
071                    // Lets user select a font
072                    Font font = FontChooser.showDialog(
073                            null,
074                            "Choose Font",
075                            ((StylableCell)focusOwner.getSelectedCell().
076                                    getExtension(StyleExtension.NAME)).getFont());
077    
078                    if (font != null) {
079                            // Changes the font of each selected cell
080                            for (Cell[] row : focusOwner.getSelectedCells())
081                                    for (Cell cell : row) {
082                                            StylableCell stylableCell = (StylableCell)cell.getExtension(
083                                                    StyleExtension.NAME);
084                                            stylableCell.setFont(font);
085                                    }
086            
087                            uiController.setWorkbookModified(focusOwner.getSpreadsheet().getWorkbook());
088                            focusOwner.repaint();
089                    }
090            }
091    }