PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

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

#
Java | 188 lines | 93 code | 16 blank | 79 comment | 4 complexity | 6c008a623e5be0f56abaab67cb622f86 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. * SideKickCompletion.java
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2003, 2005 Slava Pestov
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. package sidekick;
  23. //{{{ Imports
  24. import javax.swing.*;
  25. import java.util.*;
  26. import org.gjt.sp.jedit.*;
  27. import org.gjt.sp.jedit.buffer.JEditBuffer;
  28. import org.gjt.sp.jedit.textarea.*;
  29. //}}}
  30. /**
  31. * A code completion instance.<p>
  32. *
  33. * This is a wrapper around a collection of possible completions, with callbacks
  34. * for inserting and displaying the completions in a popup menu.
  35. *
  36. * @author Slava Pestov
  37. * @version $Id: SideKickCompletion.java 10427 2007-08-22 22:43:15Z ezust $
  38. * @since SideKick 0.3
  39. */
  40. public abstract class SideKickCompletion
  41. {
  42. //{{{ SideKickCompletion constructor
  43. /**
  44. * @deprecated Use the other constructor instead.
  45. */
  46. public SideKickCompletion() {}
  47. //}}}
  48. //{{{ SideKickCompletion constructor
  49. /**
  50. * @since SideKick 0.3.2
  51. */
  52. public SideKickCompletion(View view, String text)
  53. {
  54. this.view = view;
  55. textArea = view.getTextArea();
  56. this.text = text;
  57. } //}}}
  58. //{{{ SideKickCompletion constructor
  59. /**
  60. * @since SideKick 0.3.2
  61. * @param items - a list of strings containing the possible completions.
  62. */
  63. public SideKickCompletion(View view, String text, List items)
  64. {
  65. this(view,text);
  66. this.items = items;
  67. } //}}}
  68. //{{{ SideKickCompletion constructor
  69. /**
  70. * @since SideKick 0.3.2
  71. */
  72. public SideKickCompletion(View view, String text, Object[] items)
  73. {
  74. this(view,text);
  75. this.items = Arrays.asList(items);
  76. } //}}}
  77. //{{{ size() method
  78. public int size()
  79. {
  80. return items.size();
  81. } //}}}
  82. //{{{ get() method
  83. public Object get(int index)
  84. {
  85. return items.get(index);
  86. } //}}}
  87. //{{{ getCompletionDescription() method
  88. public String getCompletionDescription(int index)
  89. {
  90. return null;
  91. } //}}}
  92. //{{{ isCompletionSelectable() method
  93. /**
  94. * @deprecated
  95. * Do not return false from this method. Unselectable completion
  96. * is not useful.
  97. */
  98. @Deprecated
  99. public boolean isCompletionSelectable(int index)
  100. {
  101. return true;
  102. } //}}}
  103. //{{{ updateInPlace() method
  104. /**
  105. * @return If this returns false, then we create a new completion
  106. * object after user input.
  107. */
  108. public boolean updateInPlace(EditPane editPane, int caret)
  109. {
  110. return false;
  111. } //}}}
  112. //{{{ getRenderer() method
  113. public ListCellRenderer getRenderer()
  114. {
  115. return new DefaultListCellRenderer();
  116. } //}}}
  117. //{{{ insert() method
  118. public void insert(int index)
  119. {
  120. String selected = String.valueOf(get(index));
  121. int caret = textArea.getCaretPosition();
  122. Selection s = textArea.getSelectionAtOffset(caret);
  123. int start = (s == null ? caret : s.getStart());
  124. int end = (s == null ? caret : s.getEnd());
  125. JEditBuffer buffer = textArea.getBuffer();
  126. try
  127. {
  128. buffer.beginCompoundEdit();
  129. buffer.remove(start - text.length(),text.length());
  130. buffer.insert(start - text.length(),selected);
  131. }
  132. finally
  133. {
  134. buffer.endCompoundEdit();
  135. }
  136. } //}}}
  137. //{{{ getTokenLength() method
  138. /**
  139. * The length of the text being completed (popup will be positioned there).
  140. */
  141. public int getTokenLength()
  142. {
  143. return text.length();
  144. } //}}}
  145. //{{{ handleKeystroke() method
  146. /**
  147. * @param selectedIndex The index of the selected completion.
  148. * @param keyChar the character typed by the user.
  149. * @return True if completion should continue, false otherwise.
  150. * @since SideKick 0.3.2
  151. */
  152. public boolean handleKeystroke(int selectedIndex, char keyChar)
  153. {
  154. // if(keyChar == '\t' || keyChar == '\n')
  155. if(SideKickActions.acceptChars.indexOf(keyChar) > -1)
  156. {
  157. insert(selectedIndex);
  158. if(SideKickActions.insertChars.indexOf(keyChar) > -1)
  159. textArea.userInput(keyChar);
  160. return false;
  161. }
  162. else
  163. {
  164. textArea.userInput(keyChar);
  165. return true;
  166. }
  167. } //}}}
  168. protected View view;
  169. protected JEditTextArea textArea;
  170. protected String text;
  171. protected List items = new ArrayList();
  172. }