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.event.KeyEvent; 024 025import javax.swing.Icon; 026import javax.swing.ImageIcon; 027import javax.swing.JCheckBoxMenuItem; 028import javax.swing.JComponent; 029import javax.swing.JMenu; 030import javax.swing.JMenuBar; 031 032import csheets.CleanSheets; 033import csheets.ui.ctrl.ActionManager; 034import csheets.ui.ctrl.UIController; 035import csheets.ui.ext.CellDecorator; 036import csheets.ui.ext.CellDecoratorAction; 037import csheets.ui.ext.ComponentAction; 038import csheets.ui.ext.SideBarAction; 039import csheets.ui.ext.TableDecorator; 040import csheets.ui.ext.TableDecoratorAction; 041import csheets.ui.ext.UIExtension; 042 043/** 044 * The menu bar. 045 * @author Einar Pehrson 046 */ 047@SuppressWarnings("serial") 048public class MenuBar extends JMenuBar { 049 050 /** 051 * Creates the menu bar. 052 * @param app the CleanSheets application 053 * @param actionManager a manager for actions 054 * @param uiController the user interface controller 055 */ 056 public MenuBar(CleanSheets app, ActionManager actionManager, UIController uiController) { 057 // Creates the file menu 058 JMenu fileMenu = addMenu("File", KeyEvent.VK_F); 059 fileMenu.add(actionManager.getAction("new")); 060 fileMenu.add(actionManager.getAction("open")); 061 fileMenu.add(new ReopenMenu(app, uiController)); 062 fileMenu.add(actionManager.getAction("save")); 063 fileMenu.add(actionManager.getAction("saveas")); 064 fileMenu.addSeparator(); 065 fileMenu.add(actionManager.getAction("print")); 066 fileMenu.addSeparator(); 067 fileMenu.add(actionManager.getAction("close")); 068 fileMenu.add(actionManager.getAction("closeall")); 069 fileMenu.add(actionManager.getAction("exit")); 070 071 // Creates the edit menu 072 JMenu editMenu = addMenu("Edit", KeyEvent.VK_E); 073 editMenu.add(actionManager.getAction("undo")); 074 editMenu.add(actionManager.getAction("redo")); 075 editMenu.addSeparator(); 076 editMenu.add(actionManager.getAction("cut")); 077 editMenu.add(actionManager.getAction("copy")); 078 editMenu.add(actionManager.getAction("paste")); 079 editMenu.addSeparator(); 080 editMenu.add(actionManager.getAction("selectall")); 081 editMenu.add(actionManager.getAction("sort")); 082 editMenu.add(actionManager.getAction("search")); 083 editMenu.add(actionManager.getAction("clear")); 084 editMenu.addSeparator(); 085 editMenu.add(actionManager.getAction("prefs")); 086 087 // Creates the view menu 088 JMenu viewMenu = addMenu("View", KeyEvent.VK_V); 089 090 JMenu sideBarMenu = (JMenu)viewMenu.add(new JMenu("Side Bars")); 091 sideBarMenu.setMnemonic(KeyEvent.VK_S); 092 sideBarMenu.setIcon(new ImageIcon(CleanSheets.class.getResource("res/img/sidebar.gif"))); 093 094 JMenu toolBarMenu = (JMenu)viewMenu.add(new JMenu("Tool Bars")); 095 toolBarMenu.setMnemonic(KeyEvent.VK_T); 096 toolBarMenu.setIcon(new ImageIcon(CleanSheets.class.getResource("res/img/toolbar.gif"))); 097 098 viewMenu.addSeparator(); 099 JMenu cellDecMenu = (JMenu)viewMenu.add(new JMenu("Cell Decorators")); 100 cellDecMenu.setMnemonic(KeyEvent.VK_C); 101 cellDecMenu.setIcon(new BlankIcon(16)); 102 103 JMenu tableDecMenu = (JMenu)viewMenu.add(new JMenu("Table Decorators")); 104 tableDecMenu.setMnemonic(KeyEvent.VK_C); 105 tableDecMenu.setIcon(new BlankIcon(16)); 106 107 // Populates the view sub-menus 108 for (UIExtension extension : uiController.getExtensions()) { 109 110 CellDecorator cellDec = extension.getCellDecorator(); 111 if (cellDec != null) 112 cellDecMenu.add(new JCheckBoxMenuItem(new CellDecoratorAction( 113 extension))).setSelected(cellDec.isEnabled()); 114 115 TableDecorator tableDec = extension.getTableDecorator(); 116 if (tableDec != null) 117 tableDecMenu.add(new JCheckBoxMenuItem(new TableDecoratorAction( 118 extension))).setSelected(tableDec.isEnabled()); 119 120 JComponent sideBar = extension.getSideBar(); 121 if (sideBar != null) 122 sideBarMenu.add(new JCheckBoxMenuItem( 123 new SideBarAction(extension, sideBar)) 124 ).setSelected(sideBar.isEnabled()); 125 126 JComponent toolBar = extension.getToolBar(); 127 if (toolBar != null) 128 toolBarMenu.add(new JCheckBoxMenuItem( 129 new ComponentAction(extension, toolBar, "toolbar")) 130 ).setSelected(toolBar.isVisible()); 131 } 132 133 // Creates the spreadsheet menu 134 JMenu sheetMenu = addMenu("Spreadsheet", KeyEvent.VK_S); 135 sheetMenu.add(actionManager.getAction("addsheet")); 136 sheetMenu.add(actionManager.getAction("removesheet")); 137 sheetMenu.add(actionManager.getAction("renamesheet")); 138 sheetMenu.addSeparator(); 139 sheetMenu.add(actionManager.getAction("insertcolumn")); 140 sheetMenu.add(actionManager.getAction("removecolumn")); 141 sheetMenu.add(actionManager.getAction("insertrow")); 142 sheetMenu.add(actionManager.getAction("removerow")); 143 144 // Creates the extension menus 145 JMenu extensionsMenu = addMenu("Extensions", KeyEvent.VK_X); 146 for (UIExtension extension : uiController.getExtensions()) { 147 JMenu extensionMenu = extension.getMenu(); 148 if (extensionMenu != null) { 149 // Updates icon 150 if (extensionMenu.getIcon() == null) { 151 Icon icon = extension.getIcon(); 152 extensionMenu.setIcon(icon != null ? icon : new BlankIcon(16)); 153 } 154 155 // Adds menu 156 extensionsMenu.add(extensionMenu); 157 } 158 } 159 160 // Creates the window menu 161 add(new WindowMenu(app, uiController)); 162 163 // Creates the help menu 164 JMenu helpMenu = addMenu("Help", KeyEvent.VK_H); 165 helpMenu.add(actionManager.getAction("help")); 166 helpMenu.addSeparator(); 167 helpMenu.add(actionManager.getAction("license")); 168 helpMenu.add(actionManager.getAction("about")); 169} 170 171 /** 172 * Creates a menu and adds it to the menu bar. 173 * @param name The name of the menu. 174 * @param mnemonic The shortcut-key to access the menu. 175 * @return The menu created. 176 */ 177 private JMenu addMenu(String name, int mnemonic) { 178 JMenu menu = new JMenu(name); 179 menu.setMnemonic(mnemonic); 180 return add(menu); 181 } 182}