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