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