use of javax.swing.event.MenuKeyEvent 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() & Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()) != 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 {
Desktop.getDesktop().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.getModifiers() & Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()) != 0 || e.getKeyCode() == KeyEvent.VK_META || e.getKeyCode() == KeyEvent.VK_CONTROL) {
setText(defaultTitle);
}
}
@Override
public void menuKeyPressed(MenuKeyEvent e) {
if ((e.getModifiers() & Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()) != 0) {
setText(modTitle);
}
}
private void setText(String text) {
item.setText(text);
menu.pack();
}
});
}
use of javax.swing.event.MenuKeyEvent 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;
}
Aggregations