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;
022
023import java.awt.Component;
024import java.awt.event.ActionEvent;
025import java.awt.event.KeyEvent;
026import java.io.File;
027import java.util.HashMap;
028import java.util.Map;
029
030import javax.swing.AbstractAction;
031import javax.swing.ButtonGroup;
032import javax.swing.JCheckBoxMenuItem;
033import javax.swing.JMenu;
034import javax.swing.KeyStroke;
035
036import csheets.CleanSheets;
037import csheets.SpreadsheetAppEvent;
038import csheets.SpreadsheetAppListener;
039import csheets.core.Workbook;
040import csheets.ui.ctrl.SelectionEvent;
041import csheets.ui.ctrl.SelectionListener;
042import csheets.ui.ctrl.UIController;
043
044/**
045 * A menu for listing the open workbooks, and allowing the user to navigate
046 * between them.
047 * @author Einar Pehrson
048 */
049@SuppressWarnings("serial")
050public class WindowMenu extends JMenu implements SelectionListener, SpreadsheetAppListener {
051
052        /** The user interface controller */
053        private UIController uiController;
054
055        /** The workbook selection items */
056        private Map<Workbook, JCheckBoxMenuItem> items
057                = new HashMap<Workbook, JCheckBoxMenuItem>();
058
059        /** The button group to which workbook selection items are added */
060        private ButtonGroup group = new ButtonGroup();
061
062        /** The number of menu items on the menu not denoting workbooks */
063        private int nonWorkbookItems;
064
065        /**
066         * Creates a window menu.
067         * @param app the CleanSheets application
068         * @param uiController the user interface controller
069         */
070        public WindowMenu(CleanSheets app, UIController uiController) {
071                super("Window");
072                this.uiController = uiController;
073                app.addSpreadsheetAppListener(this);
074                uiController.addSelectionListener(this);
075                setMnemonic(KeyEvent.VK_W);
076        }
077
078        /**
079         * Adds an item for the workbook that was created.
080         * @param event the spreadsheet application event that occured
081         */
082        public void workbookCreated(SpreadsheetAppEvent event) {
083                // Creates item and names it
084                JCheckBoxMenuItem item = new JCheckBoxMenuItem(
085                        new SelectWorkbookAction(event.getWorkbook(), uiController));
086                int index = getItemCount() - nonWorkbookItems + 1;
087                File file = event.getFile();
088                item.setText(file == null ? " Untitled" : file.getName());
089
090                // Accelerates item
091                KeyStroke stroke = KeyStroke.getKeyStroke("ctrl " + index);
092                item.setMnemonic(stroke.getKeyChar());
093                item.setAccelerator(stroke);
094                item.setSelected(true);
095
096                // Adds itme
097                items.put(event.getWorkbook(), item);
098                group.add(item);
099                add(item);
100        }
101
102        /**
103         * Adds an item for the workbook that was loaded.
104         * @param event the spreadsheet application event that occured
105         */
106        public void workbookLoaded(SpreadsheetAppEvent event) {
107                workbookCreated(event);
108        }
109
110        /**
111         * Removes the item of the workbook that was unloaded, and renames the other.
112         * @param event the spreadsheet application event that occured
113         */
114        public void workbookUnloaded(SpreadsheetAppEvent event) {
115                // Removes the item
116                JCheckBoxMenuItem item = items.remove(event.getWorkbook());
117                if (item != null)
118                        remove(item);
119
120                // Updates remaining items
121                int index = 1;
122                for (Component c : getMenuComponents()) {
123                        item = (JCheckBoxMenuItem)c;
124                        KeyStroke stroke = KeyStroke.getKeyStroke("ctrl " + index++);
125                        item.setMnemonic(stroke.getKeyChar());
126                        item.setAccelerator(stroke);
127                }
128        }
129
130        /**
131         * Renames the item of the workbook that was saved.
132         * @param event the spreadsheet application event that occured
133         */
134        public void workbookSaved(SpreadsheetAppEvent event) {
135                items.get(event.getWorkbook()).setText(event.getFile().getName());;
136        }
137
138        /**
139         * Selects the item of the workbook that was selected.
140         * @param event the selection event that was fired
141         */
142        public void selectionChanged(SelectionEvent event) {
143                JCheckBoxMenuItem item = items.get(event.getWorkbook());
144                if (item != null)
145                        item.setSelected(true);
146        }
147
148        /**
149         * An action for selecting a workbook.
150         * @author Einar Pehrson
151         */
152        @SuppressWarnings("serial")
153        protected static class SelectWorkbookAction extends AbstractAction {
154
155                /** The workbook to select */
156                private Workbook workbook;
157
158                /** The user interface controller */
159                private UIController uiController;
160
161                /**
162                 * Creates a new workbook selection action.
163                 * @param workbook the workbook to select
164                 * @param uiController the user interface controller
165                 */
166                public SelectWorkbookAction(Workbook workbook, UIController uiController) {
167                        this.workbook = workbook;
168                        this.uiController = uiController;
169                }
170
171                public void actionPerformed(ActionEvent e) {
172                        uiController.setActiveWorkbook(workbook);
173                }
174        }
175}