|
22 | 22 | */
|
23 | 23 |
|
24 | 24 | package processing.app;
|
| 25 | + |
| 26 | +import processing.app.helpers.Keys; |
25 | 27 | import processing.app.helpers.OSUtils;
|
| 28 | +import processing.app.helpers.SimpleAction; |
26 | 29 | import processing.app.tools.MenuScroller;
|
27 | 30 | import static processing.app.I18n.tr;
|
28 | 31 |
|
@@ -72,12 +75,69 @@ public class EditorHeader extends JComponent {
|
72 | 75 |
|
73 | 76 | static Image[][] pieces;
|
74 | 77 |
|
75 |
| - // |
76 |
| - |
77 | 78 | Image offscreen;
|
78 | 79 | int sizeW, sizeH;
|
79 | 80 | int imageW, imageH;
|
80 | 81 |
|
| 82 | + public class Actions { |
| 83 | + public final Action newTab = new SimpleAction(tr("New Tab"), |
| 84 | + Keys.ctrlShift(KeyEvent.VK_N), |
| 85 | + () -> editor.getSketch().handleNewCode()); |
| 86 | + |
| 87 | + public final Action renameTab = new SimpleAction(tr("Rename"), |
| 88 | + () -> editor.getSketch().handleRenameCode()); |
| 89 | + |
| 90 | + public final Action deleteTab = new SimpleAction(tr("Delete"), () -> { |
| 91 | + try { |
| 92 | + editor.getSketch().handleDeleteCode(); |
| 93 | + } catch (IOException e) { |
| 94 | + e.printStackTrace(); |
| 95 | + } |
| 96 | + }); |
| 97 | + |
| 98 | + public final Action prevTab = new SimpleAction(tr("Previous Tab"), |
| 99 | + Keys.ctrlAlt(KeyEvent.VK_LEFT), |
| 100 | + () -> editor.sketch.handlePrevCode()); |
| 101 | + |
| 102 | + public final Action nextTab = new SimpleAction(tr("Next Tab"), |
| 103 | + Keys.ctrlAlt(KeyEvent.VK_RIGHT), |
| 104 | + () -> editor.sketch.handleNextCode()); |
| 105 | + |
| 106 | + Actions() { |
| 107 | + // Explicitly bind keybindings for the actions with accelerators above |
| 108 | + // Normally, this happens automatically for any actions bound to menu |
| 109 | + // items, but only for menus attached to a window, not for popup menus. |
| 110 | + Keys.bind(EditorHeader.this, newTab); |
| 111 | + Keys.bind(EditorHeader.this, prevTab); |
| 112 | + Keys.bind(EditorHeader.this, nextTab); |
| 113 | + |
| 114 | + // Add alternative keybindings to switch tabs |
| 115 | + Keys.bind(EditorHeader.this, prevTab, Keys.ctrlShift(KeyEvent.VK_TAB)); |
| 116 | + Keys.bind(EditorHeader.this, nextTab, Keys.ctrl(KeyEvent.VK_TAB)); |
| 117 | + } |
| 118 | + } |
| 119 | + public Actions actions = new Actions(); |
| 120 | + |
| 121 | + /** |
| 122 | + * Called whenever we, or any of our ancestors, is added to a container. |
| 123 | + */ |
| 124 | + public void addNotify() { |
| 125 | + super.addNotify(); |
| 126 | + /* |
| 127 | + * Once we get added to a window, remove Ctrl-Tab and Ctrl-Shift-Tab from |
| 128 | + * the keys used for focus traversal (so our bindings for these keys will |
| 129 | + * work). All components inherit from the window eventually, so this should |
| 130 | + * work whenever the focus is inside our window. Some components (notably |
| 131 | + * JTextPane / JEditorPane) keep their own focus traversal keys, though, and |
| 132 | + * have to be treated individually (either the same as below, or by |
| 133 | + * disabling focus traversal entirely). |
| 134 | + */ |
| 135 | + Window window = SwingUtilities.getWindowAncestor(this); |
| 136 | + if (window != null) { |
| 137 | + Keys.killFocusTraversalBinding(window, Keys.ctrl(KeyEvent.VK_TAB)); |
| 138 | + Keys.killFocusTraversalBinding(window, Keys.ctrlShift(KeyEvent.VK_TAB)); |
| 139 | + } |
| 140 | + } |
81 | 141 |
|
82 | 142 | public EditorHeader(Editor eddie) {
|
83 | 143 | this.editor = eddie; // weird name for listener
|
@@ -236,151 +296,41 @@ public void rebuild() {
|
236 | 296 |
|
237 | 297 |
|
238 | 298 | public void rebuildMenu() {
|
239 |
| - //System.out.println("rebuilding"); |
240 | 299 | if (menu != null) {
|
241 | 300 | menu.removeAll();
|
242 | 301 |
|
243 | 302 | } else {
|
244 | 303 | menu = new JMenu();
|
245 | 304 | MenuScroller.setScrollerFor(menu);
|
246 | 305 | popup = menu.getPopupMenu();
|
247 |
| - add(popup); |
248 | 306 | popup.setLightWeightPopupEnabled(true);
|
249 |
| - |
250 |
| - /* |
251 |
| - popup.addPopupMenuListener(new PopupMenuListener() { |
252 |
| - public void popupMenuCanceled(PopupMenuEvent e) { |
253 |
| - // on redraw, the isVisible() will get checked. |
254 |
| - // actually, a repaint may be fired anyway, so this |
255 |
| - // may be redundant. |
256 |
| - repaint(); |
257 |
| - } |
258 |
| -
|
259 |
| - public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { } |
260 |
| - public void popupMenuWillBecomeVisible(PopupMenuEvent e) { } |
261 |
| - }); |
262 |
| - */ |
263 | 307 | }
|
264 | 308 | JMenuItem item;
|
265 | 309 |
|
266 |
| - // maybe this shouldn't have a command key anyways.. |
267 |
| - // since we're not trying to make this a full ide.. |
268 |
| - //item = Editor.newJMenuItem("New", 'T'); |
269 |
| - |
270 |
| - /* |
271 |
| - item = Editor.newJMenuItem("Previous", KeyEvent.VK_PAGE_UP); |
272 |
| - item.addActionListener(new ActionListener() { |
273 |
| - public void actionPerformed(ActionEvent e) { |
274 |
| - System.out.println("prev"); |
275 |
| - } |
276 |
| - }); |
277 |
| - if (editor.sketch != null) { |
278 |
| - item.setEnabled(editor.sketch.codeCount > 1); |
279 |
| - } |
280 |
| - menu.add(item); |
281 |
| -
|
282 |
| - item = Editor.newJMenuItem("Next", KeyEvent.VK_PAGE_DOWN); |
283 |
| - item.addActionListener(new ActionListener() { |
284 |
| - public void actionPerformed(ActionEvent e) { |
285 |
| - System.out.println("ext"); |
286 |
| - } |
287 |
| - }); |
288 |
| - if (editor.sketch != null) { |
289 |
| - item.setEnabled(editor.sketch.codeCount > 1); |
290 |
| - } |
291 |
| - menu.add(item); |
292 |
| -
|
293 |
| - menu.addSeparator(); |
294 |
| - */ |
295 |
| - |
296 |
| - //item = new JMenuItem("New Tab"); |
297 |
| - item = Editor.newJMenuItemShift(tr("New Tab"), 'N'); |
298 |
| - item.addActionListener(new ActionListener() { |
299 |
| - public void actionPerformed(ActionEvent e) { |
300 |
| - editor.getSketch().handleNewCode(); |
301 |
| - } |
302 |
| - }); |
303 |
| - menu.add(item); |
304 |
| - |
305 |
| - item = new JMenuItem(tr("Rename")); |
306 |
| - item.addActionListener(new ActionListener() { |
307 |
| - public void actionPerformed(ActionEvent e) { |
308 |
| - editor.getSketch().handleRenameCode(); |
309 |
| - /* |
310 |
| - // this is already being called by nameCode(), the second stage of rename |
311 |
| - if (editor.sketch.current == editor.sketch.code[0]) { |
312 |
| - editor.sketchbook.rebuildMenus(); |
313 |
| - } |
314 |
| - */ |
315 |
| - } |
316 |
| - }); |
317 |
| - menu.add(item); |
318 |
| - |
319 |
| - item = new JMenuItem(tr("Delete")); |
320 |
| - item.addActionListener(new ActionListener() { |
321 |
| - public void actionPerformed(ActionEvent event) { |
322 |
| - try { |
323 |
| - editor.getSketch().handleDeleteCode(); |
324 |
| - } catch (IOException e) { |
325 |
| - e.printStackTrace(); |
326 |
| - } |
327 |
| - } |
328 |
| - }); |
329 |
| - menu.add(item); |
330 |
| - |
| 310 | + menu.add(new JMenuItem(actions.newTab)); |
| 311 | + menu.add(new JMenuItem(actions.renameTab)); |
| 312 | + menu.add(new JMenuItem(actions.deleteTab)); |
331 | 313 | menu.addSeparator();
|
332 |
| - |
333 |
| - // KeyEvent.VK_LEFT and VK_RIGHT will make Windows beep |
334 |
| - |
335 |
| - item = new JMenuItem(tr("Previous Tab")); |
336 |
| - KeyStroke ctrlAltLeft = KeyStroke |
337 |
| - .getKeyStroke(KeyEvent.VK_LEFT, Editor.SHORTCUT_ALT_KEY_MASK); |
338 |
| - item.setAccelerator(ctrlAltLeft); |
339 |
| - item.addActionListener(new ActionListener() { |
340 |
| - @Override |
341 |
| - public void actionPerformed(ActionEvent e) { |
342 |
| - editor.sketch.handlePrevCode(); |
343 |
| - } |
344 |
| - }); |
345 |
| - menu.add(item); |
346 |
| - |
347 |
| - item = new JMenuItem(tr("Next Tab")); |
348 |
| - KeyStroke ctrlAltRight = KeyStroke |
349 |
| - .getKeyStroke(KeyEvent.VK_RIGHT, Editor.SHORTCUT_ALT_KEY_MASK); |
350 |
| - item.setAccelerator(ctrlAltRight); |
351 |
| - item.addActionListener(new ActionListener() { |
352 |
| - @Override |
353 |
| - public void actionPerformed(ActionEvent e) { |
354 |
| - editor.sketch.handleNextCode(); |
355 |
| - } |
356 |
| - }); |
357 |
| - menu.add(item); |
| 314 | + menu.add(new JMenuItem(actions.prevTab)); |
| 315 | + menu.add(new JMenuItem(actions.nextTab)); |
358 | 316 |
|
359 | 317 | Sketch sketch = editor.getSketch();
|
360 | 318 | if (sketch != null) {
|
361 | 319 | menu.addSeparator();
|
362 |
| - |
363 |
| - ActionListener jumpListener = new ActionListener() { |
364 |
| - public void actionPerformed(ActionEvent e) { |
365 |
| - editor.getSketch().setCurrentCode(e.getActionCommand()); |
366 |
| - } |
367 |
| - }; |
| 320 | + int i = 0; |
368 | 321 | for (SketchCode code : sketch.getCodes()) {
|
| 322 | + final int index = i++; |
369 | 323 | item = new JMenuItem(code.isExtension(sketch.getDefaultExtension()) ?
|
370 | 324 | code.getPrettyName() : code.getFileName());
|
371 |
| - item.setActionCommand(code.getFileName()); |
372 |
| - item.addActionListener(jumpListener); |
| 325 | + item.addActionListener((ActionEvent e) -> { |
| 326 | + editor.getSketch().setCurrentCode(index); |
| 327 | + }); |
373 | 328 | menu.add(item);
|
374 | 329 | }
|
375 | 330 | }
|
376 | 331 | }
|
377 | 332 |
|
378 | 333 |
|
379 |
| - public void deselectMenu() { |
380 |
| - repaint(); |
381 |
| - } |
382 |
| - |
383 |
| - |
384 | 334 | public Dimension getPreferredSize() {
|
385 | 335 | return getMinimumSize();
|
386 | 336 | }
|
|
0 commit comments