/plugins/SideKick/tags/release-0-7-5/sidekick/SideKickCompletionPopup.java

# · Java · 196 lines · 138 code · 22 blank · 36 comment · 16 complexity · 32c189224d3b1b031dc36f4429d43770 MD5 · raw file

  1. /*
  2. * SideKickCompletionPopup.java - Completer popup
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright 2000, 2005 Slava Pestov
  7. * 2005 Robert McKinnon
  8. *
  9. * The XML plugin is licensed under the GNU General Public License, with
  10. * the following exception:
  11. *
  12. * "Permission is granted to link this code with software released under
  13. * the Apache license version 1.1, for example used by the Xerces XML
  14. * parser package."
  15. */
  16. package sidekick;
  17. //{{{ Imports
  18. import javax.swing.*;
  19. import java.awt.event.*;
  20. import java.awt.*;
  21. import org.gjt.sp.jedit.gui.CompletionPopup;
  22. import org.gjt.sp.jedit.textarea.JEditTextArea;
  23. import org.gjt.sp.jedit.*;
  24. //}}}
  25. public class SideKickCompletionPopup extends CompletionPopup
  26. {
  27. //{{{ Instance variables
  28. private View view;
  29. private SideKickParser parser;
  30. private SideKickCompletion complete;
  31. //}}}
  32. //{{{ SideKickCompletionPopup constructor
  33. public SideKickCompletionPopup(View view, SideKickParser parser,
  34. int caret, SideKickCompletion complete)
  35. {
  36. super(view, getLocation(view.getTextArea(), caret, complete));
  37. this.view = view;
  38. this.parser = parser;
  39. this.complete = complete;
  40. reset(new Candidates(), false);
  41. } //}}}
  42. //{{{ keyPressed() method
  43. protected void keyPressed(KeyEvent evt)
  44. {
  45. // These code should be reduced to make this popup behave
  46. // like a builtin popup. But these are here to keep
  47. // compatibility with the old implementation before
  48. // ractoring out of CompletionPopup.
  49. switch(evt.getKeyCode())
  50. {
  51. case KeyEvent.VK_ENTER:
  52. keyTyped('\n');
  53. evt.consume();
  54. break;
  55. case KeyEvent.VK_TAB:
  56. keyTyped('\t');
  57. evt.consume();
  58. break;
  59. case KeyEvent.VK_SPACE:
  60. evt.consume();
  61. break;
  62. case KeyEvent.VK_BACK_SPACE:
  63. if(!parser.canHandleBackspace())
  64. {
  65. dispose();
  66. }
  67. break;
  68. case KeyEvent.VK_DELETE:
  69. dispose();
  70. break;
  71. default:
  72. break;
  73. }
  74. } //}}}
  75. //{{{ keyTyped() method
  76. public void keyTyped(KeyEvent evt)
  77. {
  78. char ch = evt.getKeyChar();
  79. if(ch == '\b' && !parser.canHandleBackspace())
  80. {
  81. evt.consume();
  82. return;
  83. }
  84. keyTyped(ch);
  85. evt.consume();
  86. } //}}}
  87. //{{{ Private members
  88. //{{{ getLocation() method
  89. private static Point getLocation(JEditTextArea textArea, int caret,
  90. SideKickCompletion complete)
  91. {
  92. Point location = textArea.offsetToXY(caret - complete.getTokenLength());
  93. location.y += textArea.getPainter().getFontMetrics().getHeight();
  94. SwingUtilities.convertPointToScreen(location,
  95. textArea.getPainter());
  96. return location;
  97. } //}}}
  98. //{{{ Candidates class
  99. private class Candidates implements CompletionPopup.Candidates
  100. {
  101. private final ListCellRenderer renderer;
  102. public Candidates()
  103. {
  104. renderer = complete.getRenderer();
  105. }
  106. public int getSize()
  107. {
  108. return complete.size();
  109. }
  110. public boolean isValid()
  111. {
  112. return complete != null && complete.size() > 0;
  113. }
  114. public void complete(int index)
  115. {
  116. complete.insert(index);
  117. }
  118. public Component getCellRenderer(JList list, int index,
  119. boolean isSelected, boolean cellHasFocus)
  120. {
  121. return renderer.getListCellRendererComponent(list,
  122. complete.get(index), index,
  123. isSelected, cellHasFocus);
  124. }
  125. public String getDescription(int index)
  126. {
  127. return complete.getCompletionDescription(index);
  128. }
  129. } //}}}
  130. //{{{ keyTyped() method
  131. private void keyTyped(char ch)
  132. {
  133. // If no completion is selected, do not pass the key to
  134. // handleKeystroke() method. This avoids interfering
  135. // between a bit intermittent user typing and automatic
  136. // completion (which is not selected initially).
  137. int selected = getSelectedIndex();
  138. if(selected == -1)
  139. {
  140. view.getTextArea().userInput(ch);
  141. updateCompletion();
  142. }
  143. else if(complete.handleKeystroke(selected, ch))
  144. {
  145. updateCompletion();
  146. }
  147. else {
  148. dispose();
  149. }
  150. } //}}}
  151. //{{{ updateCompletion() method
  152. private void updateCompletion()
  153. {
  154. SideKickCompletion newComplete = complete;
  155. EditPane editPane = view.getEditPane();
  156. JEditTextArea textArea = editPane.getTextArea();
  157. int caret = textArea.getCaretPosition();
  158. if(!newComplete.updateInPlace(editPane, caret))
  159. {
  160. newComplete = parser.complete(editPane, caret);
  161. }
  162. if(newComplete == null || newComplete.size() == 0)
  163. {
  164. dispose();
  165. }
  166. else
  167. {
  168. complete = newComplete;
  169. setLocation(getLocation(textArea, caret, complete));
  170. reset(new Candidates(), false);
  171. }
  172. } //}}}
  173. //}}}
  174. }