| 1 | // Jomic - a viewer for comic book archives. |
| 2 | // Copyright (C) 2004-2008 Thomas Aglassinger |
| 3 | // |
| 4 | // This program is free software: you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation, either version 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // |
| 9 | // This program is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU General Public License |
| 15 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | package net.sf.jomic.ui; |
| 17 | |
| 18 | import java.util.Arrays; |
| 19 | |
| 20 | import javax.swing.JMenu; |
| 21 | import javax.swing.JMenuItem; |
| 22 | |
| 23 | import net.sf.jomic.tools.StringTools; |
| 24 | import net.sf.wraplog.Logger; |
| 25 | |
| 26 | /** |
| 27 | * JomicMenuBar to be used under Mac OS X when no window is open. Most items will be disabled |
| 28 | * because they only make sense in context of a comic. |
| 29 | * |
| 30 | * @see net.roydesign.mac.MRJAdapter#setFramelessJMenuBar(javax.swing.JMenuBar) |
| 31 | * @author Thomas Aglassinger |
| 32 | */ |
| 33 | public class FramelessJomicMenuBar extends JomicMenuBar |
| 34 | { |
| 35 | private String[] commandsToEnable = new String[]{ |
| 36 | Commands.CONVERT, |
| 37 | Commands.DUMMY_GO_TO_URI, |
| 38 | Commands.HELP, |
| 39 | Commands.NEW_COMIC, |
| 40 | Commands.OPEN, |
| 41 | Commands.OPEN_IN_NEW_WINDOW, |
| 42 | Commands.OPEN_RECENT, |
| 43 | Commands.SYSTEM_INFO |
| 44 | }; |
| 45 | private Logger logger; |
| 46 | private StringTools stringTools; |
| 47 | |
| 48 | public FramelessJomicMenuBar() { |
| 49 | super(); |
| 50 | logger = Logger.getLogger(FramelessJomicMenuBar.class); |
| 51 | stringTools = StringTools.instance(); |
| 52 | Arrays.sort(commandsToEnable); |
| 53 | |
| 54 | // Disable all menu items except those that make sense without a comic |
| 55 | // opened. |
| 56 | for (int menuIndex = 0; menuIndex < getMenuCount(); menuIndex += 1) { |
| 57 | boolean anyItemEnabled = false; |
| 58 | JMenu menu = getMenu(menuIndex); |
| 59 | |
| 60 | for (int itemIndex = 0; itemIndex < menu.getItemCount(); itemIndex += 1) { |
| 61 | JMenuItem item = menu.getItem(itemIndex); |
| 62 | boolean isSeparator = (item == null); |
| 63 | boolean isSubMenu = (item instanceof JMenu); |
| 64 | |
| 65 | if (!isSeparator && !isSubMenu) { |
| 66 | String command = item.getActionCommand(); |
| 67 | |
| 68 | if ((command == null) || (stringTools.equalsAnyOf(commandsToEnable, command))) { |
| 69 | anyItemEnabled = true; |
| 70 | // There is no window open, so we actually have to |
| 71 | // open the comic in a new window. |
| 72 | if (command.equals(Commands.OPEN)) { |
| 73 | item.setActionCommand(Commands.OPEN_IN_NEW_WINDOW); |
| 74 | } |
| 75 | } else { |
| 76 | logger.debug("disable menu item \"{}\" (command={})", item.getText(), command); |
| 77 | item.setEnabled(false); |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | if (!anyItemEnabled) { |
| 82 | menu.setEnabled(false); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | } |
| 87 | } |