PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/bundles/plugins-trunk/RubyPlugin/src/org/jedit/ruby/structure/TypeAheadPopupKeyHandler.java

#
Java | 145 lines | 98 code | 25 blank | 22 comment | 16 complexity | 448e489c07e8285c4fff17198940859f MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * TypeAheadPopupKeyHandler.java -
  3. *
  4. * Copyright 2005 Robert McKinnon
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. package org.jedit.ruby.structure;
  21. import org.jedit.ruby.structure.*;
  22. import org.jedit.ruby.structure.TypeAheadPopup;
  23. import org.jedit.ruby.RubyPlugin;
  24. import java.awt.event.KeyAdapter;
  25. import java.awt.event.KeyEvent;
  26. /**
  27. * @author robmckinnon at users.sourceforge.net
  28. */
  29. final class TypeAheadPopupKeyHandler extends KeyAdapter {
  30. private final TypeAheadPopup typeAheadPopup;
  31. private final String validCharacters;
  32. public TypeAheadPopupKeyHandler(TypeAheadPopup typeAheadPopup) {
  33. this.typeAheadPopup = typeAheadPopup;
  34. validCharacters = "_(),.[]";
  35. }
  36. public final void keyPressed(KeyEvent event) {
  37. switch (event.getKeyCode()) {
  38. case KeyEvent.VK_ESCAPE:
  39. handleEscapePressed(event);
  40. break;
  41. case KeyEvent.VK_UP:
  42. incrementSelection(event, -1);
  43. break;
  44. case KeyEvent.VK_DOWN:
  45. incrementSelection(event, 1);
  46. break;
  47. case KeyEvent.VK_PAGE_UP:
  48. case KeyEvent.VK_LEFT:
  49. incrementSelection(event, -1 * (TypeAheadPopup.VISIBLE_LIST_SIZE - 1));
  50. break;
  51. case KeyEvent.VK_PAGE_DOWN:
  52. case KeyEvent.VK_RIGHT:
  53. incrementSelection(event, (org.jedit.ruby.structure.TypeAheadPopup.VISIBLE_LIST_SIZE - 1));
  54. break;
  55. case KeyEvent.VK_HOME:
  56. incrementSelection(event, -1 * getListSize());
  57. break;
  58. case KeyEvent.VK_END:
  59. incrementSelection(event, getListSize());
  60. break;
  61. case KeyEvent.VK_BACK_SPACE:
  62. handleBackSpacePressed(event);
  63. break;
  64. case KeyEvent.VK_F4:
  65. handleSelection(event, false);
  66. default:
  67. handleOtherKeys(event);
  68. break;
  69. }
  70. }
  71. private void handleOtherKeys(KeyEvent event) {
  72. if (event.isAltDown() || event.isMetaDown()) {
  73. char keyChar = event.getKeyChar();
  74. boolean handled = typeAheadPopup.handleAltPressedWith(keyChar);
  75. if (handled) {
  76. event.consume();
  77. }
  78. }
  79. }
  80. public final void keyTyped(KeyEvent event) {
  81. char character = event.getKeyChar();
  82. int keyCode = event.getKeyCode();
  83. int keyChar = event.getKeyChar();
  84. if (keyCode == KeyEvent.VK_TAB || keyCode == KeyEvent.VK_ENTER ||
  85. keyChar == KeyEvent.VK_TAB || keyChar == KeyEvent.VK_ENTER) {
  86. handleSelection(event, true);
  87. } else {
  88. if (!RubyPlugin.ignoreKeyTyped(keyCode, character, event)) {
  89. handleCharacterTyped(character, event);
  90. }
  91. }
  92. }
  93. private void handleBackSpacePressed(KeyEvent event) {
  94. typeAheadPopup.handleBackSpacePressed();
  95. event.consume();
  96. }
  97. private int getListSize() {
  98. return typeAheadPopup.getListSize();
  99. }
  100. private void incrementSelection(KeyEvent event, int increment) {
  101. typeAheadPopup.incrementSelection(increment);
  102. event.consume();
  103. }
  104. private void handleEscapePressed(KeyEvent event) {
  105. typeAheadPopup.handleEscapePressed();
  106. event.consume();
  107. }
  108. private void handleSelection(KeyEvent event, boolean showMenu) {
  109. event.consume();
  110. typeAheadPopup.handleSelection(showMenu);
  111. }
  112. private void handleCharacterTyped(char character, KeyEvent event) {
  113. boolean valid = Character.isLetterOrDigit(character)
  114. || validCharacters.indexOf(character) != -1;
  115. if (valid) {
  116. typeAheadPopup.updateMatchedMembers(character);
  117. event.consume();
  118. }
  119. }
  120. }