/plugins/Console/tags/release-4-2-5/console/HistoryText.java

# · Java · 355 lines · 231 code · 38 blank · 86 comment · 47 complexity · 13a97938b337ac67e5d858ee44dfe1b2 MD5 · raw file

  1. /*
  2. * HistoryText.java - Common code for text components with a history
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2004 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 console;
  23. //{{{ Imports
  24. import javax.swing.*;
  25. import javax.swing.text.*;
  26. import javax.swing.event.MouseInputAdapter;
  27. import java.awt.*;
  28. import java.awt.event.*;
  29. import org.gjt.sp.jedit.*;
  30. import org.gjt.sp.jedit.gui.*;
  31. //}}}
  32. /**
  33. * This file is from 4.3pre1. It is included with the Console plugin to
  34. * make it compatible with 4.2final.
  35. *
  36. * @author Slava Pestov
  37. * @version $Id: HistoryText.java 600 2004-11-16 03:46:55Z spestov $
  38. */
  39. public class HistoryText
  40. {
  41. //{{{ HistoryText constructor
  42. public HistoryText(JTextComponent text, String name)
  43. {
  44. this.text = text;
  45. setModel(name);
  46. index = -1;
  47. } //}}}
  48. //{{{ fireActionPerformed() method
  49. public void fireActionPerformed()
  50. {
  51. } //}}}
  52. //{{{ getIndex() mehtod
  53. public int getIndex()
  54. {
  55. return index;
  56. } //}}}
  57. //{{{ setIndex() mehtod
  58. public void setIndex(int index)
  59. {
  60. this.index = index;
  61. } //}}}
  62. //{{{ getModel() method
  63. /**
  64. * Returns the underlying history controller.
  65. * @since jEdit 4.3pre1
  66. */
  67. public HistoryModel getModel()
  68. {
  69. return historyModel;
  70. } //}}}
  71. //{{{ setModel() method
  72. /**
  73. * Sets the history list controller.
  74. * @param name The model name
  75. * @since jEdit 4.3pre1
  76. */
  77. public void setModel(String name)
  78. {
  79. if(name == null)
  80. historyModel = null;
  81. else
  82. historyModel = HistoryModel.getModel(name);
  83. index = -1;
  84. } //}}}
  85. //{{{ setInstantPopups() method
  86. /**
  87. * Sets if selecting a value from the popup should immediately fire
  88. * an ActionEvent.
  89. */
  90. public void setInstantPopups(boolean instantPopups)
  91. {
  92. this.instantPopups = instantPopups;
  93. } //}}}
  94. //{{{ getInstantPopups() method
  95. /**
  96. * Returns if selecting a value from the popup should immediately fire
  97. * an ActionEvent.
  98. */
  99. public boolean getInstantPopups()
  100. {
  101. return instantPopups;
  102. } //}}}
  103. //{{{ addCurrentToHistory() method
  104. /**
  105. * Adds the currently entered item to the history.
  106. */
  107. public void addCurrentToHistory()
  108. {
  109. if(historyModel != null)
  110. historyModel.addItem(getText());
  111. index = 0;
  112. } //}}}
  113. //{{{ doBackwardSearch() method
  114. public void doBackwardSearch()
  115. {
  116. if(historyModel == null)
  117. return;
  118. if(text.getSelectionEnd() != getDocument().getLength())
  119. {
  120. text.setCaretPosition(getDocument().getLength());
  121. }
  122. int start = getInputStart();
  123. String t = getText().substring(0,
  124. text.getSelectionStart() - start);
  125. if(t == null)
  126. {
  127. historyPrevious();
  128. return;
  129. }
  130. for(int i = index + 1; i < historyModel.getSize(); i++)
  131. {
  132. String item = historyModel.getItem(i);
  133. if(item.startsWith(t))
  134. {
  135. text.replaceSelection(item.substring(t.length()));
  136. text.select(getInputStart() + t.length(),
  137. getDocument().getLength());
  138. index = i;
  139. return;
  140. }
  141. }
  142. text.getToolkit().beep();
  143. } //}}}
  144. //{{{ doForwardSearch() method
  145. public void doForwardSearch()
  146. {
  147. if(historyModel == null)
  148. return;
  149. if(text.getSelectionEnd() != getDocument().getLength())
  150. {
  151. text.setCaretPosition(getDocument().getLength());
  152. }
  153. int start = getInputStart();
  154. String t = getText().substring(0,
  155. text.getSelectionStart() - start);
  156. if(t == null)
  157. {
  158. historyNext();
  159. return;
  160. }
  161. for(int i = index - 1; i >= 0; i--)
  162. {
  163. String item = historyModel.getItem(i);
  164. if(item.startsWith(t))
  165. {
  166. text.replaceSelection(item.substring(t.length()));
  167. text.select(getInputStart() + t.length(),
  168. getDocument().getLength());
  169. index = i;
  170. return;
  171. }
  172. }
  173. text.getToolkit().beep();
  174. } //}}}
  175. //{{{ historyPrevious() method
  176. public void historyPrevious()
  177. {
  178. if(historyModel == null)
  179. return;
  180. if(index == historyModel.getSize() - 1)
  181. text.getToolkit().beep();
  182. else if(index == -1)
  183. {
  184. current = getText();
  185. setText(historyModel.getItem(0));
  186. index = 0;
  187. }
  188. else
  189. {
  190. // have to do this because setText() sets index to -1
  191. int newIndex = index + 1;
  192. setText(historyModel.getItem(newIndex));
  193. index = newIndex;
  194. }
  195. } //}}}
  196. //{{{ historyNext() method
  197. public void historyNext()
  198. {
  199. if(historyModel == null)
  200. return;
  201. if(index == -1)
  202. text.getToolkit().beep();
  203. else if(index == 0)
  204. setText(current);
  205. else
  206. {
  207. // have to do this because setText() sets index to -1
  208. int newIndex = index - 1;
  209. setText(historyModel.getItem(newIndex));
  210. index = newIndex;
  211. }
  212. } //}}}
  213. //{{{ getDocument() method
  214. public Document getDocument()
  215. {
  216. return text.getDocument();
  217. } //}}}
  218. //{{{ getText() method
  219. /**
  220. * Subclasses can override this to provide funky history behavior,
  221. * for JTextPanes and such.
  222. */
  223. public String getText()
  224. {
  225. return text.getText();
  226. } //}}}
  227. //{{{ setText() method
  228. /**
  229. * Subclasses can override this to provide funky history behavior,
  230. * for JTextPanes and such.
  231. */
  232. public void setText(String text)
  233. {
  234. this.index = -1;
  235. this.text.setText(text);
  236. } //}}}
  237. //{{{ getInputStart() method
  238. /**
  239. * Subclasses can override this to provide funky history behavior,
  240. * for JTextPanes and such.
  241. */
  242. public int getInputStart()
  243. {
  244. return 0;
  245. } //}}}
  246. //{{{ showPopupMenu() method
  247. public void showPopupMenu(String t, int x, int y)
  248. {
  249. if(historyModel == null)
  250. return;
  251. text.requestFocus();
  252. if(popup != null && popup.isVisible())
  253. {
  254. popup.setVisible(false);
  255. return;
  256. }
  257. popup = new JPopupMenu();
  258. JMenuItem caption = new JMenuItem(jEdit.getProperty(
  259. "history.caption"));
  260. caption.getModel().setEnabled(false);
  261. popup.add(caption);
  262. popup.addSeparator();
  263. for(int i = 0; i < historyModel.getSize(); i++)
  264. {
  265. String item = historyModel.getItem(i);
  266. if(item.startsWith(t))
  267. {
  268. JMenuItem menuItem = new JMenuItem(item);
  269. menuItem.setActionCommand(String.valueOf(i));
  270. menuItem.addActionListener(
  271. new ActionHandler());
  272. popup.add(menuItem);
  273. }
  274. }
  275. GUIUtilities.showPopupMenu(popup,text,x,y,false);
  276. } //}}}
  277. //{{{ showPopupMenu() method
  278. public void showPopupMenu(boolean search)
  279. {
  280. if(search)
  281. showPopupMenu(getText().substring(getInputStart(),
  282. text.getSelectionStart()),0,text.getHeight());
  283. else
  284. showPopupMenu("",0,text.getHeight());
  285. } //}}}
  286. //{{{ Private members
  287. private JTextComponent text;
  288. private HistoryModel historyModel;
  289. private int index;
  290. private String current;
  291. private JPopupMenu popup;
  292. private boolean instantPopups;
  293. //}}}
  294. //{{{ ActionHandler class
  295. class ActionHandler implements ActionListener
  296. {
  297. public void actionPerformed(ActionEvent evt)
  298. {
  299. int ind = Integer.parseInt(evt.getActionCommand());
  300. if(ind == -1)
  301. {
  302. if(index != -1)
  303. setText(current);
  304. }
  305. else
  306. {
  307. setText(historyModel.getItem(ind));
  308. index = ind;
  309. }
  310. if(instantPopups)
  311. {
  312. addCurrentToHistory();
  313. fireActionPerformed();
  314. }
  315. }
  316. } //}}}
  317. }