PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/bundles/plugins-trunk/SideKick/sidekick/SideKickCompletionPopup.java

#
Java | 210 lines | 149 code | 24 blank | 37 comment | 18 complexity | 8ad020e68d94494a00ab3181a7fab97a 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. * 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, boolean active)
  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(), active);
  41. }
  42. /// This constructor makes an inactive popup as in SideKick 0.7.5.
  43. public SideKickCompletionPopup(View view, SideKickParser parser,
  44. int caret, SideKickCompletion complete)
  45. {
  46. this(view, parser, caret, complete, false);
  47. } //}}}
  48. //{{{ keyPressed() method
  49. protected void keyPressed(KeyEvent evt)
  50. {
  51. // These code should be reduced to make this popup behave
  52. // like a builtin popup. But these are here to keep
  53. // compatibility with the old implementation before
  54. // ractoring out of CompletionPopup.
  55. switch(evt.getKeyCode())
  56. {
  57. case KeyEvent.VK_ENTER:
  58. keyTyped('\n');
  59. evt.consume();
  60. break;
  61. case KeyEvent.VK_TAB:
  62. keyTyped('\t');
  63. evt.consume();
  64. break;
  65. case KeyEvent.VK_SPACE:
  66. evt.consume();
  67. break;
  68. case KeyEvent.VK_BACK_SPACE:
  69. if(!parser.canHandleBackspace())
  70. {
  71. dispose();
  72. }
  73. break;
  74. case KeyEvent.VK_DELETE:
  75. dispose();
  76. break;
  77. default:
  78. break;
  79. }
  80. } //}}}
  81. //{{{ keyTyped() method
  82. public void keyTyped(KeyEvent evt)
  83. {
  84. char ch = evt.getKeyChar();
  85. if(ch == '\b' && !parser.canHandleBackspace())
  86. {
  87. evt.consume();
  88. return;
  89. }
  90. keyTyped(ch);
  91. evt.consume();
  92. } //}}}
  93. //{{{ Private members
  94. //{{{ getLocation() method
  95. private static Point getLocation(JEditTextArea textArea, int caret,
  96. SideKickCompletion complete)
  97. {
  98. Point location = textArea.offsetToXY(caret - complete.getTokenLength());
  99. location.y += textArea.getPainter().getFontMetrics().getHeight();
  100. SwingUtilities.convertPointToScreen(location,
  101. textArea.getPainter());
  102. return location;
  103. } //}}}
  104. //{{{ Candidates class
  105. private class Candidates implements CompletionPopup.Candidates
  106. {
  107. private final ListCellRenderer renderer;
  108. public Candidates()
  109. {
  110. renderer = complete.getRenderer();
  111. }
  112. public int getSize()
  113. {
  114. return complete.size();
  115. }
  116. public boolean isValid()
  117. {
  118. return complete != null && complete.size() > 0;
  119. }
  120. public void complete(int index)
  121. {
  122. complete.insert(index);
  123. }
  124. public Component getCellRenderer(JList list, int index,
  125. boolean isSelected, boolean cellHasFocus)
  126. {
  127. return renderer.getListCellRendererComponent(list,
  128. complete.get(index), index,
  129. isSelected, cellHasFocus);
  130. }
  131. public String getDescription(int index)
  132. {
  133. return complete.getCompletionDescription(index);
  134. }
  135. } //}}}
  136. //{{{ keyTyped() method
  137. private void keyTyped(char ch)
  138. {
  139. // If no completion is selected, do not pass the key to
  140. // handleKeystroke() method. This avoids interfering
  141. // between a bit intermittent user typing and automatic
  142. // completion (which is not selected initially).
  143. int selected = getSelectedIndex();
  144. if(selected == -1)
  145. {
  146. Macros.Recorder recorder = view.getMacroRecorder();
  147. if(recorder != null)
  148. {
  149. recorder.recordInput(1,ch,
  150. view.getTextArea().isOverwriteEnabled());
  151. }
  152. view.getTextArea().userInput(ch);
  153. updateCompletion(false);
  154. }
  155. else if(complete.handleKeystroke(selected, ch))
  156. {
  157. updateCompletion(true);
  158. }
  159. else {
  160. dispose();
  161. }
  162. } //}}}
  163. //{{{ updateCompletion() method
  164. private void updateCompletion(boolean active)
  165. {
  166. SideKickCompletion newComplete = complete;
  167. EditPane editPane = view.getEditPane();
  168. JEditTextArea textArea = editPane.getTextArea();
  169. int caret = textArea.getCaretPosition();
  170. if(!newComplete.updateInPlace(editPane, caret))
  171. {
  172. newComplete = parser.complete(editPane, caret);
  173. }
  174. if(newComplete == null || newComplete.size() == 0)
  175. {
  176. dispose();
  177. }
  178. else
  179. {
  180. complete = newComplete;
  181. setLocation(getLocation(textArea, caret, complete));
  182. reset(new Candidates(), active);
  183. }
  184. } //}}}
  185. //}}}
  186. }