| 1 | // Jomic - a viewer for comic book archives. |
| 2 | // Copyright (C) 2004-2011 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 | |
| 25 | import org.apache.commons.logging.Log; |
| 26 | import org.apache.commons.logging.LogFactory; |
| 27 | |
| 28 | /** |
| 29 | * JomicMenuBar to be used under Mac OS X when no window is open. Most items will be disabled |
| 30 | * because they only make sense in context of a comic. |
| 31 | * |
| 32 | * @see net.roydesign.mac.MRJAdapter#setFramelessJMenuBar(javax.swing.JMenuBar) |
| 33 | * @author Thomas Aglassinger |
| 34 | */ |
| 35 | public class FramelessJomicMenuBar extends JomicMenuBar |
| 36 | { |
| 37 | private String[] commandsToEnable = new String[]{ |
| 38 | Commands.CONVERT, |
| 39 | Commands.DUMMY_GO_TO_URI, |
| 40 | Commands.HELP, |
| 41 | Commands.NEW_COMIC, |
| 42 | Commands.OPEN, |
| 43 | Commands.OPEN_IN_NEW_WINDOW, |
| 44 | Commands.OPEN_RECENT, |
| 45 | Commands.SYSTEM_INFO |
| 46 | }; |
| 47 | private Log logger; |
| 48 | private StringTools stringTools; |
| 49 | |
| 50 | public FramelessJomicMenuBar() { |
| 51 | super(); |
| 52 | logger = LogFactory.getLog(FramelessJomicMenuBar.class); |
| 53 | stringTools = StringTools.instance(); |
| 54 | Arrays.sort(commandsToEnable); |
| 55 | |
| 56 | // Disable all menu items except those that make sense without a comic |
| 57 | // opened. |
| 58 | for (int menuIndex = 0; menuIndex < getMenuCount(); menuIndex += 1) { |
| 59 | boolean anyItemEnabled = false; |
| 60 | JMenu menu = getMenu(menuIndex); |
| 61 | |
| 62 | for (int itemIndex = 0; itemIndex < menu.getItemCount(); itemIndex += 1) { |
| 63 | JMenuItem item = menu.getItem(itemIndex); |
| 64 | boolean isSeparator = (item == null); |
| 65 | boolean isSubMenu = (item instanceof JMenu); |
| 66 | |
| 67 | if (!isSeparator && !isSubMenu) { |
| 68 | String command = item.getActionCommand(); |
| 69 | |
| 70 | if ((command == null) || (stringTools.equalsAnyOf(commandsToEnable, command))) { |
| 71 | anyItemEnabled = true; |
| 72 | // There is no window open, so we actually have to |
| 73 | // open the comic in a new window. |
| 74 | if ((command != null) && command.equals(Commands.OPEN)) { |
| 75 | item.setActionCommand(Commands.OPEN_IN_NEW_WINDOW); |
| 76 | } |
| 77 | } else { |
| 78 | if (logger.isDebugEnabled()) { |
| 79 | logger.debug("disable menu item \"" + item.getText() + "\" (command=" + command + ")"); |
| 80 | } |
| 81 | item.setEnabled(false); |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | if (!anyItemEnabled) { |
| 86 | menu.setEnabled(false); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | } |
| 91 | } |