PageRenderTime 69ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/gui/HistoryText.java

#
Java | 370 lines | 248 code | 38 blank | 84 comment | 48 complexity | 1c2a56498cb5fe2a367000085d98c471 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. * Controller (manager of models) for HistoryTextArea.
  33. * @author Slava Pestov
  34. * @version $Id: HistoryText.java 16341 2009-10-14 10:05:51Z kpouer $
  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. popup = null;
  253. return;
  254. }
  255. popup = new JPopupMenu()
  256. {
  257. @Override
  258. public void setVisible(boolean b)
  259. {
  260. if (!b)
  261. {
  262. popup = null;
  263. }
  264. super.setVisible(b);
  265. }
  266. };
  267. JMenuItem caption = new JMenuItem(jEdit.getProperty(
  268. "history.caption"));
  269. caption.addActionListener(new ActionListener()
  270. {
  271. public void actionPerformed(ActionEvent e)
  272. {
  273. new ListModelEditor().open(historyModel);
  274. }
  275. });
  276. popup.add(caption);
  277. popup.addSeparator();
  278. for(int i = 0; i < historyModel.getSize(); i++)
  279. {
  280. String item = historyModel.getItem(i);
  281. if(item.startsWith(t))
  282. {
  283. JMenuItem menuItem = new JMenuItem(item);
  284. menuItem.setActionCommand(String.valueOf(i));
  285. menuItem.addActionListener(
  286. new ActionHandler());
  287. popup.add(menuItem);
  288. }
  289. }
  290. GUIUtilities.showPopupMenu(popup,text,x,y,false);
  291. } //}}}
  292. //{{{ showPopupMenu() method
  293. public void showPopupMenu(boolean search)
  294. {
  295. if(search)
  296. showPopupMenu(getText().substring(getInputStart(),
  297. text.getSelectionStart()),0,text.getHeight());
  298. else
  299. showPopupMenu("",0,text.getHeight());
  300. } //}}}
  301. //{{{ Private members
  302. private JTextComponent text;
  303. private HistoryModel historyModel;
  304. private int index;
  305. private String current;
  306. private JPopupMenu popup;
  307. private boolean instantPopups;
  308. //}}}
  309. //{{{ ActionHandler class
  310. class ActionHandler implements ActionListener
  311. {
  312. public void actionPerformed(ActionEvent evt)
  313. {
  314. int ind = Integer.parseInt(evt.getActionCommand());
  315. if(ind == -1)
  316. {
  317. if(index != -1)
  318. setText(current);
  319. }
  320. else
  321. {
  322. setText(historyModel.getItem(ind));
  323. index = ind;
  324. }
  325. if(instantPopups)
  326. {
  327. addCurrentToHistory();
  328. fireActionPerformed();
  329. }
  330. }
  331. } //}}}
  332. }