001    /*
002     * This file is part of muCommander, http://www.mucommander.com
003     * Copyright (C) 2002-2008 Maxence Bernard
004     *
005     * muCommander is free software; you can redistribute it and/or modify
006     * it under the terms of the GNU General Public License as published by
007     * the Free Software Foundation; either version 3 of the License, or
008     * (at your option) any later version.
009     *
010     * muCommander is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013     * GNU General Public License for more details.
014     *
015     * You should have received a copy of the GNU General Public License
016     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017     */
018    
019    package com.mucommander.bonjour;
020    
021    import com.mucommander.text.Translator;
022    import com.mucommander.ui.action.MuAction;
023    import com.mucommander.ui.helper.MnemonicHelper;
024    import com.mucommander.ui.icon.IconManager;
025    
026    import javax.swing.*;
027    import javax.swing.event.MenuEvent;
028    import javax.swing.event.MenuListener;
029    
030    /**
031     * An abstract JMenu that contains an item for each Bonjour service available
032     * (as returned {@link BonjourDirectory#getServices()} displaying the Bonjour service's name. When an item is clicked,
033     * the action returned by {@link #getMenuItemAction(BonjourService)} is returned.
034     *
035     * <p>Note: the items list is refreshed each time the menu is selected. In other words, a new instance of BonjourMenu
036     * does not have to be created in order to see new Bonjour services.</p>
037     *
038     * @author Maxence Bernard
039     */
040    public abstract class BonjourMenu extends JMenu implements MenuListener {
041    
042        /**
043         * Creates a new instance of <code>BonjourMenu</code>.
044         */
045        public BonjourMenu() {
046            super(Translator.get("bonjour.bonjour_services"));
047    
048            setIcon(IconManager.getIcon(IconManager.FILE_ICON_SET, "bonjour.png"));
049    
050            // Menu items will be added when menu gets selected
051            addMenuListener(this);
052        }
053    
054    
055        /**
056         * Returns the action to perform for the given {@link BonjourService}. This method is called for every
057         * BonjourService available when this menu is selected.
058         *
059         * @param bs the BonjourService
060         * @return the action to perform for the given BonjourService
061         */
062        public abstract MuAction getMenuItemAction(BonjourService bs);
063    
064    
065        /////////////////////////////////
066        // MenuListener implementation //
067        /////////////////////////////////
068    
069        public void menuSelected(MenuEvent menuEvent) {
070            // Remove previous menu items (if any)
071            removeAll();
072    
073            if(BonjourDirectory.isActive()) {
074                BonjourService services[] = BonjourDirectory.getServices();
075                int nbServices = services.length;
076    
077                if(nbServices>0) {
078                    // Add a menu item for each Bonjour service.
079                    // When clicked, the corresponding URL will opened in the active table.
080                    JMenuItem menuItem;
081                    MnemonicHelper mnemonicHelper = new MnemonicHelper();
082    
083                    for(int i=0; i<nbServices; i++) {
084                        menuItem = new JMenuItem(getMenuItemAction(services[i]));
085                        menuItem.setMnemonic(mnemonicHelper.getMnemonic(menuItem.getText()));
086    
087                        add(menuItem);
088                    }
089                }
090                else {
091                    // Inform that no service have been discovered
092                    add(new JMenuItem(Translator.get("bonjour.no_service_discovered"))).setEnabled(false);
093                }
094            }
095            else {
096                // Inform that Bonjour support has been disabled
097                add(new JMenuItem(Translator.get("bonjour.bonjour_disabled"))).setEnabled(false);
098            }
099        }
100    
101        public void menuDeselected(MenuEvent menuEvent) {
102        }
103    
104        public void menuCanceled(MenuEvent menuEvent) {
105        }
106    }