Search in sources :

Example 1 with MenuKeyListener

use of javax.swing.event.MenuKeyListener in project omegat by omegat-org.

the class ProjectFilesListController method addContextMenuItem.

private void addContextMenuItem(JPopupMenu menu, boolean isSource, List<File> files) {
    long presentFiles = files.stream().filter(File::isFile).count();
    String defaultTitle, modTitle;
    if (presentFiles > 1) {
        defaultTitle = StringUtil.format(OStrings.getString(isSource ? "PF_OPEN_SOURCE_FILES" : "PF_OPEN_TARGET_FILES"), presentFiles);
        modTitle = StringUtil.format(OStrings.getString(isSource ? "PF_OPEN_SOURCE_FILES" : "PF_OPEN_TARGET_FILES"), presentFiles);
    } else {
        defaultTitle = OStrings.getString(isSource ? "PF_OPEN_SOURCE_FILE" : "PF_OPEN_TARGET_FILE");
        modTitle = OStrings.getString(isSource ? "PF_REVEAL_SOURCE_FILE" : "PF_REVEAL_TARGET_FILE");
    }
    JMenuItem item = menu.add(defaultTitle);
    item.addActionListener(e -> {
        boolean openParent = (e.getModifiers() & Java8Compat.getMenuShortcutKeyMaskEx()) != 0;
        Stream<File> stream;
        if (openParent) {
            stream = files.stream().map(File::getParentFile).distinct().filter(File::isDirectory);
        } else {
            stream = files.stream().filter(File::isFile);
        }
        stream.forEach(f -> {
            try {
                DesktopWrapper.open(f);
            } catch (IOException ex) {
                Log.log(ex);
            }
        });
    });
    item.setEnabled(presentFiles > 0);
    item.addMenuKeyListener(new MenuKeyListener() {

        @Override
        public void menuKeyTyped(MenuKeyEvent e) {
        }

        @Override
        public void menuKeyReleased(MenuKeyEvent e) {
            if ((e.getModifiersEx() & Java8Compat.getMenuShortcutKeyMaskEx()) != 0 || e.getKeyCode() == KeyEvent.VK_META || e.getKeyCode() == KeyEvent.VK_CONTROL) {
                setText(defaultTitle);
            }
        }

        @Override
        public void menuKeyPressed(MenuKeyEvent e) {
            if ((e.getModifiersEx() & Java8Compat.getMenuShortcutKeyMaskEx()) != 0) {
                setText(modTitle);
            }
        }

        private void setText(String text) {
            item.setText(text);
            menu.pack();
        }
    });
}
Also used : MenuKeyEvent(javax.swing.event.MenuKeyEvent) MenuKeyListener(javax.swing.event.MenuKeyListener) IOException(java.io.IOException) JMenuItem(javax.swing.JMenuItem) File(java.io.File)

Example 2 with MenuKeyListener

use of javax.swing.event.MenuKeyListener in project i2p.i2p by i2p.

the class TrayManager method getSwingTrayIcon.

private TrayIcon getSwingTrayIcon(String tooltip) throws AWTException {
    // A JPopupMenu by itself is hard to get rid of,
    // so we hang it off a zero-size, undecorated JFrame.
    // http://stackoverflow.com/questions/1498789/jpopupmenu-behavior
    // http://stackoverflow.com/questions/2581314/how-do-you-hide-a-swing-popup-when-you-click-somewhere-else
    final JFrame frame = new JFrame();
    // http://stackoverflow.com/questions/2011601/jframe-without-frame-border-maximum-button-minimum-button-and-frame-icon
    frame.setUndecorated(true);
    frame.setMinimumSize(new Dimension(0, 0));
    frame.setSize(0, 0);
    final JPopupMenu menu = getSwingMainMenu();
    menu.setFocusable(true);
    frame.add(menu);
    TrayIcon ti = new TrayIcon(getTrayImage(), tooltip, null);
    ti.addMouseListener(new MouseListener() {

        public void mouseClicked(MouseEvent e) {
        }

        public void mouseEntered(MouseEvent e) {
        }

        public void mouseExited(MouseEvent e) {
        }

        public void mousePressed(MouseEvent e) {
            handle(e);
        }

        public void mouseReleased(MouseEvent e) {
            handle(e);
        }

        private void handle(MouseEvent e) {
            // menu visible check is never true
            if (!frame.isVisible()) /* || !menu.isVisible() */
            {
                frame.setLocation(e.getX(), e.getY());
                frame.setVisible(true);
                menu.show(frame, 0, 0);
            }
            updateMenu();
        }
    });
    menu.addPopupMenuListener(new PopupMenuListener() {

        public void popupMenuCanceled(PopupMenuEvent e) {
        /* frame.setVisible(false); */
        }

        public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
            frame.setVisible(false);
        }

        public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
        }
    });
    // this is to make it go away when we click elsewhere
    // doesn't do anything
    menu.addFocusListener(new FocusListener() {

        public void focusGained(FocusEvent e) {
        }

        public void focusLost(FocusEvent e) {
            frame.setVisible(false);
        }
    });
    // this is to make it go away when we hit escape
    // doesn't do anything
    menu.addMenuKeyListener(new MenuKeyListener() {

        public void menuKeyPressed(MenuKeyEvent e) {
        }

        public void menuKeyReleased(MenuKeyEvent e) {
        }

        public void menuKeyTyped(MenuKeyEvent e) {
            if (e.getKeyChar() == (char) 0x1b)
                frame.setVisible(false);
        }
    });
    return ti;
}
Also used : MenuKeyEvent(javax.swing.event.MenuKeyEvent) MenuKeyListener(javax.swing.event.MenuKeyListener) MouseListener(java.awt.event.MouseListener) MouseEvent(java.awt.event.MouseEvent) JFrame(javax.swing.JFrame) TrayIcon(java.awt.TrayIcon) PopupMenuListener(javax.swing.event.PopupMenuListener) Dimension(java.awt.Dimension) PopupMenuEvent(javax.swing.event.PopupMenuEvent) FocusListener(java.awt.event.FocusListener) FocusEvent(java.awt.event.FocusEvent) JPopupMenu(javax.swing.JPopupMenu)

Aggregations

MenuKeyEvent (javax.swing.event.MenuKeyEvent)2 MenuKeyListener (javax.swing.event.MenuKeyListener)2 Dimension (java.awt.Dimension)1 TrayIcon (java.awt.TrayIcon)1 FocusEvent (java.awt.event.FocusEvent)1 FocusListener (java.awt.event.FocusListener)1 MouseEvent (java.awt.event.MouseEvent)1 MouseListener (java.awt.event.MouseListener)1 File (java.io.File)1 IOException (java.io.IOException)1 JFrame (javax.swing.JFrame)1 JMenuItem (javax.swing.JMenuItem)1 JPopupMenu (javax.swing.JPopupMenu)1 PopupMenuEvent (javax.swing.event.PopupMenuEvent)1 PopupMenuListener (javax.swing.event.PopupMenuListener)1