From 1f3befac59fcce3963c8fe03d7702e384a76dcbf Mon Sep 17 00:00:00 2001 From: benfry Date: Sat, 20 Oct 2012 19:51:16 +0000 Subject: [PATCH] oh yeah, that --- app/src/processing/app/Toolkit.java | 178 ++++++++++++++++++++++++++++ 1 file changed, 178 insertions(+) create mode 100644 app/src/processing/app/Toolkit.java diff --git a/app/src/processing/app/Toolkit.java b/app/src/processing/app/Toolkit.java new file mode 100644 index 000000000..fae30dec8 --- /dev/null +++ b/app/src/processing/app/Toolkit.java @@ -0,0 +1,178 @@ +/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */ + +/* + Part of the Processing project - http://processing.org + + Copyright (c) 2012 The Processing Foundation + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License version 2 + as published by the Free Software Foundation. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +package processing.app; + +import java.awt.Dimension; +import java.awt.Frame; +import java.awt.Image; +import java.awt.datatransfer.Clipboard; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.KeyEvent; + +import javax.swing.JCheckBoxMenuItem; +import javax.swing.JComponent; +import javax.swing.JMenu; +import javax.swing.JMenuItem; +import javax.swing.JRootPane; +import javax.swing.KeyStroke; + +import processing.core.PApplet; + +/** + * Utility functions for base that require a java.awt.Toolkit object. These + * are broken out from Base as we start moving toward the possibility of the + * code running in headless mode. + * @author fry + */ +public class Toolkit { + static final java.awt.Toolkit awtToolkit = + java.awt.Toolkit.getDefaultToolkit(); + + /** Command on Mac OS X, Ctrl on Windows and Linux */ + static final int SHORTCUT_KEY_MASK = + awtToolkit.getMenuShortcutKeyMask(); + /** Command-W on Mac OS X, Ctrl-W on Windows and Linux */ + public static final KeyStroke WINDOW_CLOSE_KEYSTROKE = + KeyStroke.getKeyStroke('W', SHORTCUT_KEY_MASK); + /** Command-Option on Mac OS X, Ctrl-Alt on Windows and Linux */ + static final int SHORTCUT_ALT_KEY_MASK = ActionEvent.ALT_MASK | + awtToolkit.getMenuShortcutKeyMask(); + + + /** + * A software engineer, somewhere, needs to have his abstraction + * taken away. In some countries they jail or beat people for crafting + * the sort of API that would require a five line helper function + * just to set the shortcut key for a menu item. + */ + static public JMenuItem newJMenuItem(String title, int what) { + JMenuItem menuItem = new JMenuItem(title); + int modifiers = awtToolkit.getMenuShortcutKeyMask(); + menuItem.setAccelerator(KeyStroke.getKeyStroke(what, modifiers)); + return menuItem; + } + + + /** + * Like newJMenuItem() but adds shift as a modifier for the shortcut. + */ + static public JMenuItem newJMenuItemShift(String title, int what) { + JMenuItem menuItem = new JMenuItem(title); + int modifiers = awtToolkit.getMenuShortcutKeyMask(); + modifiers |= ActionEvent.SHIFT_MASK; + menuItem.setAccelerator(KeyStroke.getKeyStroke(what, modifiers)); + return menuItem; + } + + + /** + * Same as newJMenuItem(), but adds the ALT (on Linux and Windows) + * or OPTION (on Mac OS X) key as a modifier. + */ + static public JMenuItem newJMenuItemAlt(String title, int what) { + JMenuItem menuItem = new JMenuItem(title); + menuItem.setAccelerator(KeyStroke.getKeyStroke(what, SHORTCUT_ALT_KEY_MASK)); + return menuItem; + } + + + static public JCheckBoxMenuItem newJCheckBoxMenuItem(String title, int what) { + JCheckBoxMenuItem menuItem = new JCheckBoxMenuItem(title); + int modifiers = awtToolkit.getMenuShortcutKeyMask(); + menuItem.setAccelerator(KeyStroke.getKeyStroke(what, modifiers)); + return menuItem; + } + + + static public void addDisabledItem(JMenu menu, String title) { + JMenuItem item = new JMenuItem(title); + item.setEnabled(false); + menu.add(item); + } + + + // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . + + + static public Dimension getScreenSize() { + return awtToolkit.getScreenSize(); + } + + + /** + * Give this Frame a Processing icon. + */ + static public void setIcon(Frame frame) { + Image image = awtToolkit.createImage(PApplet.ICON_IMAGE); + frame.setIconImage(image); + } + + + // someone needs to be slapped + //static KeyStroke closeWindowKeyStroke; + + /** + * Return true if the key event was a Ctrl-W or an ESC, + * both indicators to close the window. + * Use as part of a keyPressed() event handler for frames. + */ + /* + static public boolean isCloseWindowEvent(KeyEvent e) { + if (closeWindowKeyStroke == null) { + int modifiers = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask(); + closeWindowKeyStroke = KeyStroke.getKeyStroke('W', modifiers); + } + return ((e.getKeyCode() == KeyEvent.VK_ESCAPE) || + KeyStroke.getKeyStrokeForEvent(e).equals(closeWindowKeyStroke)); + } + */ + + /** + * Registers key events for a Ctrl-W and ESC with an ActionListener + * that will take care of disposing the window. + */ + static public void registerWindowCloseKeys(JRootPane root, + ActionListener disposer) { + KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0); + root.registerKeyboardAction(disposer, stroke, + JComponent.WHEN_IN_FOCUSED_WINDOW); + + int modifiers = awtToolkit.getMenuShortcutKeyMask(); + stroke = KeyStroke.getKeyStroke('W', modifiers); + root.registerKeyboardAction(disposer, stroke, + JComponent.WHEN_IN_FOCUSED_WINDOW); + } + + + // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . + + + static public void beep() { + awtToolkit.beep(); + } + + + static public Clipboard getSystemClipboard() { + return awtToolkit.getSystemClipboard(); + } +} \ No newline at end of file