Let EditorToolbar use the global KeyboardFocusManager

For some toolbar buttons, when it is clicked while shift is pressed, its
function changes. When handling the click event, this information is
directly taken from KeyEvent.isShiftDown(). However, to also show the
proper tooltip *before* clicking, EditorToolbar listened to key events
on the main text area, to know when shift is (not) pressed.

This approach means that pressing shift while the text area is not
focused will not change the tooltip, and creates some unwanted coupling
between the toolbar and the text area.

This commit changes this approach to instead use the global
KeyboardFocusManager. Any key presses pass through there before being
dispatched to the currently focused component, so this makes sure that
any shift presses are caught, as well as making EditorToolbar a bit more
self-contained.
This commit is contained in:
Matthijs Kooijman 2015-12-03 18:08:12 +01:00 committed by Martino Facchin
parent 2e708410d4
commit c945b6c30a
2 changed files with 7 additions and 22 deletions

View File

@ -337,9 +337,6 @@ public class Editor extends JFrame implements RunnerListener {
// listener = new EditorListener(this, textarea);
pane.add(box);
// get shift down/up events so we can show the alt version of toolbar buttons
textarea.addKeyListener(toolbar);
pane.setTransferHandler(new FileDropHandler());
// Set the minimum size for the editor window

View File

@ -27,7 +27,6 @@ import javax.swing.*;
import javax.swing.event.MouseInputListener;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import static processing.app.I18n.tr;
@ -36,7 +35,7 @@ import static processing.app.Theme.scale;
/**
* run/stop/etc buttons for the ide
*/
public class EditorToolbar extends JComponent implements MouseInputListener, KeyListener {
public class EditorToolbar extends JComponent implements MouseInputListener, KeyEventDispatcher {
/**
* Rollover titles for each button.
@ -136,6 +135,7 @@ public class EditorToolbar extends JComponent implements MouseInputListener, Key
addMouseListener(this);
addMouseMotionListener(this);
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(this);
}
private void loadButtons() {
@ -451,24 +451,12 @@ public class EditorToolbar extends JComponent implements MouseInputListener, Key
return new Dimension(scale(3000), BUTTON_HEIGHT);
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_SHIFT) {
shiftPressed = true;
public boolean dispatchKeyEvent(final KeyEvent e) {
if (shiftPressed != e.isShiftDown()) {
shiftPressed = !shiftPressed;
repaint();
}
// Return false to continue processing this keyEvent
return false;
}
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_SHIFT) {
shiftPressed = false;
repaint();
}
}
public void keyTyped(KeyEvent e) {
}
}