PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-3-pre5/org/gjt/sp/jedit/gui/HistoryText.java

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