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.event.ActionEvent;
024import java.awt.event.KeyEvent;
025import java.text.DateFormat;
026import java.text.Format;
027import java.text.NumberFormat;
028
029import javax.swing.ImageIcon;
030
031import csheets.core.Cell;
032import csheets.core.IllegalValueTypeException;
033import csheets.core.Value;
034import csheets.ext.style.StylableCell;
035import csheets.ext.style.StyleExtension;
036import csheets.ui.ctrl.FocusOwnerAction;
037import csheets.ui.ctrl.SelectionEvent;
038import csheets.ui.ctrl.SelectionListener;
039import csheets.ui.ctrl.UIController;
040
041/**
042 * A format changing operation.
043 * @author Einar Pehrson
044 */
045@SuppressWarnings("serial")
046public class FormatAction extends FocusOwnerAction implements SelectionListener {
047
048        /** The user interface controller */
049        private UIController uiController;
050
051        /** The cell being styled */
052        private StylableCell cell;
053
054        /**
055         * Creates a new format action.
056         * @param uiController the user interface controller
057         */
058        public FormatAction(UIController uiController) {
059                this.uiController = uiController;
060                uiController.addSelectionListener(this);
061        }
062
063        protected String getName() {
064                return "Format...";
065        }
066
067        protected void defineProperties() {
068                putValue(MNEMONIC_KEY, KeyEvent.VK_D);
069                putValue(SMALL_ICON, new ImageIcon(StyleExtension.class.getResource("res/img/format.gif")));
070        }
071
072        /**
073         * Updates the state of the action when a new cell is selected.
074         * @param event the selection event that was fired
075         */
076        public void selectionChanged(SelectionEvent event) {
077                Cell c = event.getCell();
078                cell = c == null ? null : (StylableCell)c.getExtension(StyleExtension.NAME);
079                setEnabled(c == null ? false : cell.isFormattable());
080        }
081
082        /**
083         * Lets the user select a format from a chooser.
084         * Then applies the format to the selected cells in the focus owner table.
085         * @param event the event that was fired
086         */
087        public void actionPerformed(ActionEvent event) {
088                if (focusOwner == null)
089                        return;
090                // Lets user select a format
091                Format format = null;
092                try {
093                        if (cell.getValue().getType() == Value.Type.NUMERIC)
094                                format = new FormatChooser(
095                                        (NumberFormat)cell.getFormat().clone(), cell.getValue().toNumber()
096                                ).showDialog(null, "Choose Format");
097                        else if (cell.getValue().getType() == Value.Type.DATE)
098                                format = new FormatChooser(
099                                        (DateFormat)cell.getFormat().clone(), cell.getValue().toDate()
100                                ).showDialog(null, "Choose Format");
101                } catch (IllegalValueTypeException e) {}
102
103                if (format != null) {
104                        // Changes the format of each selected cell
105                        for (Cell[] row : focusOwner.getSelectedCells())
106                                for (Cell cell : row) {
107                                        StylableCell stylableCell = (StylableCell)cell.getExtension(
108                                                StyleExtension.NAME);
109                                        stylableCell.setFormat(
110                                                stylableCell.isFormattable() ? format : null);
111                                }
112        
113                        uiController.setWorkbookModified(focusOwner.getSpreadsheet().getWorkbook());
114                        focusOwner.repaint();
115                }
116        }
117}