Search in sources :

Example 1 with MuAction

use of com.mucommander.ui.action.MuAction in project mucommander by mucommander.

the class DrivePopupButton method getPopupMenu.

// //////////////////////////////
// PopupButton implementation //
// //////////////////////////////
@Override
public JPopupMenu getPopupMenu() {
    JPopupMenu popupMenu = new JPopupMenu();
    // Update the list of volumes in case new ones were mounted
    volumes = getDisplayableVolumes();
    // Add volumes
    int nbVolumes = volumes.length;
    final MainFrame mainFrame = folderPanel.getMainFrame();
    // Provides mnemonics and ensures uniqueness
    MnemonicHelper mnemonicHelper = new MnemonicHelper();
    JMenuItem item;
    MuAction action;
    String volumeName;
    boolean useExtendedDriveNames = fileSystemView != null;
    ArrayList<JMenuItem> itemsV = new ArrayList<JMenuItem>();
    for (int i = 0; i < nbVolumes; i++) {
        action = new CustomOpenLocationAction(mainFrame, new Hashtable<String, Object>(), volumes[i]);
        volumeName = volumes[i].getName();
        // volume's path, to disambiguate
        for (int j = 0; j < nbVolumes; j++) {
            if (j != i && volumes[j].getName().equalsIgnoreCase(volumeName)) {
                action.setLabel(volumes[i].getAbsolutePath());
                break;
            }
        }
        item = popupMenu.add(action);
        setMnemonic(item, mnemonicHelper);
        // Set icon from cache
        Icon icon = iconCache.get(volumes[i]);
        if (icon != null) {
            item.setIcon(icon);
        }
        if (useExtendedDriveNames) {
            // Use the last known value (if any) while we update it in a separate thread
            String previousExtendedName = extendedNameCache.get(volumes[i]);
            if (previousExtendedName != null)
                item.setText(previousExtendedName);
        }
        // JMenu offers no way to retrieve a particular JMenuItem, so we have to keep them
        itemsV.add(item);
    }
    new RefreshDriveNamesAndIcons(popupMenu, itemsV).start();
    popupMenu.add(new JSeparator());
    // Add boookmarks
    java.util.List<Bookmark> bookmarks = BookmarkManager.getBookmarks();
    if (!bookmarks.isEmpty()) {
        for (Bookmark bookmark : bookmarks) {
            item = popupMenu.add(new CustomOpenLocationAction(mainFrame, new Hashtable<String, Object>(), bookmark));
            setMnemonic(item, mnemonicHelper);
        }
    } else {
        // No bookmark : add a disabled menu item saying there is no bookmark
        popupMenu.add(Translator.get("bookmarks_menu.no_bookmark")).setEnabled(false);
    }
    popupMenu.add(new JSeparator());
    // Add 'Network shares' shortcut
    if (FileFactory.isRegisteredProtocol(FileProtocols.SMB)) {
        action = new CustomOpenLocationAction(mainFrame, new Hashtable<String, Object>(), new Bookmark(Translator.get("drive_popup.network_shares"), "smb:///"));
        action.setIcon(IconManager.getIcon(IconManager.FILE_ICON_SET, CustomFileIconProvider.NETWORK_ICON_NAME));
        setMnemonic(popupMenu.add(action), mnemonicHelper);
    }
    // Add Bonjour services menu
    setMnemonic(popupMenu.add(new BonjourMenu() {

        @Override
        public MuAction getMenuItemAction(BonjourService bs) {
            return new CustomOpenLocationAction(mainFrame, new Hashtable<String, Object>(), bs);
        }
    }), mnemonicHelper);
    popupMenu.add(new JSeparator());
    // Add 'connect to server' shortcuts
    schemaToPanelProvider.values().stream().sorted(Comparator.comparing(ProtocolPanelProvider::priority)).map(this::toServerConnectAction).map(popupMenu::add).forEach(menuItem -> setMnemonic(menuItem, mnemonicHelper));
    return popupMenu;
}
Also used : Hashtable(java.util.Hashtable) ProtocolPanelProvider(com.mucommander.protocol.ui.ProtocolPanelProvider) ArrayList(java.util.ArrayList) MnemonicHelper(com.mucommander.commons.util.ui.helper.MnemonicHelper) JPopupMenu(javax.swing.JPopupMenu) JSeparator(javax.swing.JSeparator) Bookmark(com.mucommander.bookmark.Bookmark) BonjourService(com.mucommander.bonjour.BonjourService) MuAction(com.mucommander.ui.action.MuAction) BonjourMenu(com.mucommander.bonjour.BonjourMenu) Icon(javax.swing.Icon) JMenuItem(javax.swing.JMenuItem)

Example 2 with MuAction

use of com.mucommander.ui.action.MuAction in project mucommander by mucommander.

the class MainMenuBar method menuSelected.

// ////////////////////////
// MenuListener methods //
// ////////////////////////
public void menuSelected(MenuEvent e) {
    Object source = e.getSource();
    if (source == viewMenu) {
        FileTable activeTable = mainFrame.getActiveTable();
        // Select the 'sort by' criterion currently in use in the active table
        sortByItems[activeTable.getSortInfo().getCriterion().ordinal()].setSelected(true);
        toggleShowFoldersFirstItem.setSelected(activeTable.getSortInfo().getFoldersFirst());
        toggleShowHiddenFilesItem.setSelected(MuConfigurations.getPreferences().getVariable(MuPreference.SHOW_HIDDEN_FILES, MuPreferences.DEFAULT_SHOW_HIDDEN_FILES));
        toggleTreeItem.setSelected(activeTable.getFolderPanel().isTreeVisible());
        toggleToggleAutoSizeItem.setSelected(mainFrame.isAutoSizeColumnsEnabled());
        toggleUseSinglePanel.setSelected(mainFrame.isSinglePanel());
    /* TODO branch toggleBranchView.setSelected(activeTable.getFolderPanel().isBranchView()); */
    } else if (source == columnsMenu) {
        // Update the selected and enabled state of each column menu item.
        FileTable activeTable = mainFrame.getActiveTable();
        for (Column c : Column.values()) {
            if (// Name column doesn't have a menu item as it cannot be disabled
            c == Column.NAME)
                continue;
            JCheckBoxMenuItem item = toggleColumnItems[c.ordinal()];
            item.setSelected(activeTable.isColumnEnabled(c));
            item.setEnabled(activeTable.isColumnDisplayable(c));
            // Override the action's label to a shorter one
            item.setText(c.getLabel());
        }
    } else if (source == goMenu) {
        // as they might have changed since menu was last selected
        for (int i = goMenu.getItemCount(); i > volumeOffset; i--) goMenu.remove(volumeOffset);
        AbstractFile[] volumes = LocalFile.getVolumes();
        int nbFolders = volumes.length;
        for (int i = 0; i < nbFolders; i++) goMenu.add(new OpenLocationAction(mainFrame, new Hashtable<String, Object>(), volumes[i]));
    } else if (source == bookmarksMenu) {
        // as bookmarks might have changed since menu was last selected
        for (int i = bookmarksMenu.getItemCount(); i > bookmarksOffset; i--) bookmarksMenu.remove(bookmarksOffset);
        // Add bookmarks menu items
        java.util.List<Bookmark> bookmarks = BookmarkManager.getBookmarks();
        int nbBookmarks = bookmarks.size();
        if (nbBookmarks > 0) {
            for (int i = 0; i < nbBookmarks; i++) MenuToolkit.addMenuItem(bookmarksMenu, new OpenLocationAction(mainFrame, new Hashtable<String, Object>(), bookmarks.get(i)), null);
        } else {
            // Show 'No bookmark' as a disabled menu item instead showing nothing
            JMenuItem noBookmarkItem = MenuToolkit.addMenuItem(bookmarksMenu, Translator.get("bookmarks_menu.no_bookmark"), null, null, null);
            noBookmarkItem.setEnabled(false);
        }
    } else if (source == windowMenu) {
        // Select the split orientation currently in use
        if (mainFrame.getSplitPaneOrientation())
            splitVerticallyItem.setSelected(true);
        else
            splitHorizontallyItem.setSelected(true);
        // menu has been deselected.
        for (int i = windowMenu.getItemCount(); i > windowOffset; i--) windowMenu.remove(windowOffset);
        // This WeakHashMap maps menu items to frame instances. It has to be a weakly referenced hash map
        // and not a regular hash map, since it will not (and cannot) be emptied when the menu has been deselected
        // and we really do not want this hash map to prevent the frames to be GCed
        windowMenuFrames = new WeakHashMap<JMenuItem, Frame>();
        // Create a menu item for each of the MainFrame instances, that displays the MainFrame's path
        // and a keyboard accelerator to recall the frame (for the first 10 frames only).
        java.util.List<MainFrame> mainFrames = WindowManager.getMainFrames();
        MainFrame mainFrame;
        JCheckBoxMenuItem checkBoxMenuItem;
        int nbFrames = mainFrames.size();
        for (int i = 0; i < nbFrames; i++) {
            mainFrame = mainFrames.get(i);
            checkBoxMenuItem = new JCheckBoxMenuItem();
            // If frame number is less than 10, use the corresponding action class (accelerator will be displayed in the menu item)
            MuAction recallWindowAction;
            if (i < 10) {
                recallWindowAction = ActionManager.getActionInstance(RECALL_WINDOW_ACTION_IDS[i], this.mainFrame);
            } else // Else use the generic RecallWindowAction
            {
                Hashtable<String, Object> actionProps = new Hashtable<String, Object>();
                // Specify the window number using the dedicated property
                actionProps.put(RecallWindowAction.WINDOW_NUMBER_PROPERTY_KEY, "" + (i + 1));
                recallWindowAction = ActionManager.getActionInstance(new ActionParameters(RecallWindowAction.Descriptor.ACTION_ID, actionProps), this.mainFrame);
            }
            checkBoxMenuItem.setAction(recallWindowAction);
            // Replace the action's label and use the MainFrame's current folder path instead
            checkBoxMenuItem.setText((i + 1) + " " + mainFrame.getActiveTable().getFolderPanel().getCurrentFolder().getAbsolutePath());
            // Use the action's label as a tooltip
            checkBoxMenuItem.setToolTipText(recallWindowAction.getLabel());
            // Check current MainFrame (the one this menu bar belongs to)
            checkBoxMenuItem.setSelected(mainFrame == this.mainFrame);
            windowMenu.add(checkBoxMenuItem);
        }
        // Add 'other' (non-MainFrame) windows : viewer and editor frames, no associated accelerator
        Frame[] frames = Frame.getFrames();
        nbFrames = frames.length;
        Frame frame;
        JMenuItem menuItem;
        boolean firstFrame = true;
        for (int i = 0; i < nbFrames; i++) {
            frame = frames[i];
            // Test if Frame is not hidden (disposed), Frame.getFrames() returns both active and disposed frames
            if (frame.isShowing() && (frame instanceof FileFrame)) {
                // and other frames
                if (firstFrame) {
                    windowMenu.add(new JSeparator());
                    firstFrame = false;
                }
                // Use frame's window title
                menuItem = new JMenuItem(frame.getTitle());
                menuItem.addActionListener(this);
                windowMenu.add(menuItem);
                windowMenuFrames.put(menuItem, frame);
            }
        }
    } else if (source == themesMenu) {
        // Remove all previous theme items, create new ones for each available theme and select the current theme
        themesMenu.removeAll();
        ButtonGroup buttonGroup = new ButtonGroup();
        Iterator<Theme> themes = ThemeManager.availableThemes();
        Theme theme;
        JCheckBoxMenuItem item;
        themesMenu.add(new JMenuItem(new EditCurrentThemeAction()));
        themesMenu.add(new JSeparator());
        while (themes.hasNext()) {
            theme = themes.next();
            item = new JCheckBoxMenuItem(new ChangeCurrentThemeAction(theme));
            buttonGroup.add(item);
            if (ThemeManager.isCurrentTheme(theme))
                item.setSelected(true);
            themesMenu.add(item);
        }
    }
}
Also used : FileFrame(com.mucommander.ui.viewer.FileFrame) MainFrame(com.mucommander.ui.main.MainFrame) Frame(java.awt.Frame) AbstractFile(com.mucommander.commons.file.AbstractFile) FileTable(com.mucommander.ui.main.table.FileTable) MainFrame(com.mucommander.ui.main.MainFrame) JSeparator(javax.swing.JSeparator) Column(com.mucommander.ui.main.table.Column) Iterator(java.util.Iterator) MuAction(com.mucommander.ui.action.MuAction) JMenuItem(javax.swing.JMenuItem) Hashtable(java.util.Hashtable) FileFrame(com.mucommander.ui.viewer.FileFrame) JCheckBoxMenuItem(javax.swing.JCheckBoxMenuItem) ActionParameters(com.mucommander.ui.action.ActionParameters) ButtonGroup(javax.swing.ButtonGroup) Theme(com.mucommander.ui.theme.Theme) OpenLocationAction(com.mucommander.ui.action.impl.OpenLocationAction)

Example 3 with MuAction

use of com.mucommander.ui.action.MuAction in project mucommander by mucommander.

the class CommandBarButton method setButtonAction.

/**
 * Sets the given button's action, custom label showing the accelerator and icon taking into account the scale factor.
 */
public void setButtonAction(String actionId, MainFrame mainFrame) {
    MuAction action = ActionManager.getActionInstance(actionId, mainFrame);
    setAction(action);
    // Append the action's shortcut to the button's label
    String label;
    label = action.getLabel();
    if (action.getAcceleratorText() != null)
        label += " [" + action.getAcceleratorText() + ']';
    setText(label);
    // Scale icon if scale factor is different from 1.0
    if (scaleFactor != 1.0f)
        setIcon(IconManager.getScaledIcon(action.getIcon(), scaleFactor));
}
Also used : MuAction(com.mucommander.ui.action.MuAction)

Example 4 with MuAction

use of com.mucommander.ui.action.MuAction in project mucommander by mucommander.

the class ToolBar method addButtons.

private void addButtons(String[] actionIds) {
    for (String actionId : actionIds) {
        if (actionId == null)
            addSeparator(SEPARATOR_DIMENSION);
        else {
            // Get a MuAction instance
            MuAction action = ActionManager.getActionInstance(actionId, mainFrame);
            // Do not add buttons for actions that do not have an icon
            if (action.getIcon() != null)
                addButton(action);
        }
    }
    if (OsFamily.MAC_OS.isCurrent()) {
        int nbComponents = getComponentCount();
        // Set the 'segment position' required for the 'segmented capsule' style
        for (int i = 0; i < nbComponents; i++) {
            Component comp = getComponent(i);
            if (!(comp instanceof JButton))
                continue;
            boolean hasPrevious = i != 0 && (getComponent(i - 1) instanceof JButton);
            boolean hasNext = i != nbComponents - 1 && (getComponent(i + 1) instanceof JButton);
            String segmentPosition;
            if (hasPrevious && hasNext)
                segmentPosition = "middle";
            else if (hasPrevious)
                segmentPosition = "last";
            else if (hasNext)
                segmentPosition = "first";
            else
                segmentPosition = "only";
            ((JButton) comp).putClientProperty("JButton.segmentPosition", segmentPosition);
        }
    }
}
Also used : JButton(javax.swing.JButton) MuAction(com.mucommander.ui.action.MuAction) Component(java.awt.Component)

Aggregations

MuAction (com.mucommander.ui.action.MuAction)4 Hashtable (java.util.Hashtable)2 JMenuItem (javax.swing.JMenuItem)2 JSeparator (javax.swing.JSeparator)2 BonjourMenu (com.mucommander.bonjour.BonjourMenu)1 BonjourService (com.mucommander.bonjour.BonjourService)1 Bookmark (com.mucommander.bookmark.Bookmark)1 AbstractFile (com.mucommander.commons.file.AbstractFile)1 MnemonicHelper (com.mucommander.commons.util.ui.helper.MnemonicHelper)1 ProtocolPanelProvider (com.mucommander.protocol.ui.ProtocolPanelProvider)1 ActionParameters (com.mucommander.ui.action.ActionParameters)1 OpenLocationAction (com.mucommander.ui.action.impl.OpenLocationAction)1 MainFrame (com.mucommander.ui.main.MainFrame)1 Column (com.mucommander.ui.main.table.Column)1 FileTable (com.mucommander.ui.main.table.FileTable)1 Theme (com.mucommander.ui.theme.Theme)1 FileFrame (com.mucommander.ui.viewer.FileFrame)1 Component (java.awt.Component)1 Frame (java.awt.Frame)1 ArrayList (java.util.ArrayList)1