PageRenderTime 37ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 422 lines | 235 code | 48 blank | 139 comment | 26 complexity | 72172c3f659336440ab9cd16ec7e0b9d 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. * HistoryTextField.java - Text field with a history
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 1999, 2000, 2001 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.border.Border;
  26. import javax.swing.border.AbstractBorder;
  27. import javax.swing.border.CompoundBorder;
  28. import javax.swing.event.MouseInputAdapter;
  29. import java.awt.*;
  30. import java.awt.event.*;
  31. import org.gjt.sp.jedit.*;
  32. //}}}
  33. /**
  34. * Text field with an arrow-key accessable history.
  35. * @author Slava Pestov
  36. * @version $Id: HistoryTextField.java 5176 2005-01-21 23:04:14Z spestov $
  37. */
  38. public class HistoryTextField extends JTextField
  39. {
  40. //{{{ HistoryTextField constructor
  41. /**
  42. * Creates a new history text field.
  43. * @since jEdit 3.2pre5
  44. */
  45. public HistoryTextField()
  46. {
  47. this(null);
  48. } //}}}
  49. //{{{ HistoryTextField constructor
  50. /**
  51. * Creates a new history text field.
  52. * @param name The history model name
  53. */
  54. public HistoryTextField(String name)
  55. {
  56. this(name,false,true);
  57. } //}}}
  58. //{{{ HistoryTextField constructor
  59. /**
  60. * Creates a new history text field.
  61. * @param name The history model name
  62. * @param instantPopups If true, selecting a value from the history
  63. * popup will immediately fire an ActionEvent. If false, the user
  64. * will have to press 'Enter' first
  65. *
  66. * @since jEdit 2.2pre5
  67. */
  68. public HistoryTextField(String name, boolean instantPopups)
  69. {
  70. this(name,instantPopups,true);
  71. } //}}}
  72. //{{{ HistoryTextField constructor
  73. /**
  74. * Creates a new history text field.
  75. * @param name The history model name
  76. * @param instantPopups If true, selecting a value from the history
  77. * popup will immediately fire an ActionEvent. If false, the user
  78. * will have to press 'Enter' first
  79. * @param enterAddsToHistory If true, pressing the Enter key will
  80. * automatically add the currently entered text to the history.
  81. *
  82. * @since jEdit 2.6pre5
  83. */
  84. public HistoryTextField(String name, boolean instantPopups,
  85. boolean enterAddsToHistory)
  86. {
  87. controller = new HistoryText(this,null)
  88. {
  89. public void fireActionPerformed()
  90. {
  91. HistoryTextField.this.fireActionPerformed();
  92. }
  93. };
  94. setModel(name);
  95. MouseHandler mouseHandler = new MouseHandler();
  96. addMouseListener(mouseHandler);
  97. addMouseMotionListener(mouseHandler);
  98. setInstantPopups(instantPopups);
  99. setEnterAddsToHistory(enterAddsToHistory);
  100. } //}}}
  101. //{{{ setInstantPopups() method
  102. /**
  103. * Sets if selecting a value from the popup should immediately fire
  104. * an ActionEvent.
  105. * @since jEdit 4.0pre3
  106. */
  107. public void setInstantPopups(boolean instantPopups)
  108. {
  109. controller.setInstantPopups(instantPopups);
  110. } //}}}
  111. //{{{ getInstantPopups() method
  112. /**
  113. * Returns if selecting a value from the popup should immediately fire
  114. * an ActionEvent.
  115. * @since jEdit 4.0pre3
  116. */
  117. public boolean getInstantPopups()
  118. {
  119. return controller.getInstantPopups();
  120. } //}}}
  121. //{{{ setEnterAddsToHistory() method
  122. /**
  123. * Sets if pressing Enter should automatically add the currently
  124. * entered text to the history.
  125. * @since jEdit 4.0pre3
  126. */
  127. public void setEnterAddsToHistory(boolean enterAddsToHistory)
  128. {
  129. this.enterAddsToHistory = enterAddsToHistory;
  130. } //}}}
  131. //{{{ getEnterAddsToHistory() method
  132. /**
  133. * Returns if pressing Enter should automatically add the currently
  134. * entered text to the history.
  135. * @since jEdit 4.0pre3
  136. */
  137. public boolean setEnterAddsToHistory()
  138. {
  139. return enterAddsToHistory;
  140. } //}}}
  141. //{{{ setSelectAllOnFocus() method
  142. /**
  143. * Sets if all text should be selected when the field gets focus.
  144. * @since jEdit 4.0pre3
  145. */
  146. public void setSelectAllOnFocus(boolean selectAllOnFocus)
  147. {
  148. this.selectAllOnFocus = selectAllOnFocus;
  149. } //}}}
  150. //{{{ getSelectAllOnFocus() method
  151. /**
  152. * Returns if all text should be selected when the field gets focus.
  153. * @since jEdit 4.0pre3
  154. */
  155. public boolean setSelectAllOnFocus()
  156. {
  157. return selectAllOnFocus;
  158. } //}}}
  159. //{{{ getModel() method
  160. /**
  161. * Returns the underlying history model.
  162. */
  163. public HistoryModel getModel()
  164. {
  165. return controller.getModel();
  166. } //}}}
  167. //{{{ setModel() method
  168. /**
  169. * Sets the history list model.
  170. * @param name The model name
  171. * @since jEdit 2.3pre3
  172. */
  173. public void setModel(String name)
  174. {
  175. controller.setModel(name);
  176. Border textFieldBorder = UIManager.getBorder("TextField.border");
  177. if(name == null)
  178. {
  179. if(textFieldBorder != null)
  180. setBorder(textFieldBorder);
  181. }
  182. else
  183. {
  184. if(textFieldBorder != null)
  185. {
  186. setBorder(new CompoundBorder(textFieldBorder,
  187. new HistoryBorder()));
  188. }
  189. }
  190. repaint();
  191. } //}}}
  192. //{{{ addCurrentToHistory() method
  193. /**
  194. * Adds the currently entered item to the history.
  195. */
  196. public void addCurrentToHistory()
  197. {
  198. controller.addCurrentToHistory();
  199. } //}}}
  200. //{{{ setText() method
  201. /**
  202. * Sets the displayed text.
  203. */
  204. public void setText(String text)
  205. {
  206. super.setText(text);
  207. controller.setIndex(-1);
  208. } //}}}
  209. //{{{ fireActionPerformed() method
  210. /**
  211. * Make it public.
  212. */
  213. public void fireActionPerformed()
  214. {
  215. super.fireActionPerformed();
  216. } //}}}
  217. //{{{ Protected members
  218. //{{{ processKeyEvent() method
  219. protected void processKeyEvent(KeyEvent evt)
  220. {
  221. if(!isEnabled())
  222. return;
  223. if(evt.getID() == KeyEvent.KEY_PRESSED)
  224. {
  225. switch(evt.getKeyCode())
  226. {
  227. case KeyEvent.VK_ENTER:
  228. if(enterAddsToHistory)
  229. addCurrentToHistory();
  230. if(evt.getModifiers() == 0)
  231. {
  232. fireActionPerformed();
  233. evt.consume();
  234. }
  235. break;
  236. case KeyEvent.VK_UP:
  237. if(evt.isShiftDown())
  238. controller.doBackwardSearch();
  239. else
  240. controller.historyPrevious();
  241. evt.consume();
  242. break;
  243. case KeyEvent.VK_DOWN:
  244. if(evt.isShiftDown())
  245. controller.doForwardSearch();
  246. else if(evt.isAltDown())
  247. {
  248. controller.showPopupMenu(
  249. evt.isShiftDown());
  250. }
  251. else
  252. controller.historyNext();
  253. evt.consume();
  254. break;
  255. case KeyEvent.VK_TAB:
  256. if(evt.isControlDown())
  257. {
  258. controller.doBackwardSearch();
  259. evt.consume();
  260. }
  261. break;
  262. }
  263. }
  264. if(!evt.isConsumed())
  265. super.processKeyEvent(evt);
  266. } //}}}
  267. //{{{ processMouseEvent() method
  268. protected void processMouseEvent(MouseEvent evt)
  269. {
  270. if(!isEnabled())
  271. return;
  272. switch(evt.getID())
  273. {
  274. case MouseEvent.MOUSE_PRESSED:
  275. Border border = getBorder();
  276. Insets insets = border.getBorderInsets(HistoryTextField.this);
  277. if(evt.getX() >= getWidth() - insets.right
  278. || GUIUtilities.isPopupTrigger(evt))
  279. {
  280. controller.showPopupMenu(evt.isShiftDown());
  281. }
  282. else
  283. super.processMouseEvent(evt);
  284. break;
  285. case MouseEvent.MOUSE_EXITED:
  286. setCursor(Cursor.getDefaultCursor());
  287. super.processMouseEvent(evt);
  288. break;
  289. default:
  290. super.processMouseEvent(evt);
  291. break;
  292. }
  293. } //}}}
  294. //}}}
  295. //{{{ Private members
  296. //{{{ Instance variables
  297. private HistoryText controller;
  298. private boolean enterAddsToHistory;
  299. private boolean selectAllOnFocus;
  300. //}}}
  301. //}}}
  302. //{{{ Inner classes
  303. //{{{ MouseHandler class
  304. class MouseHandler extends MouseInputAdapter
  305. {
  306. boolean selectAll;
  307. //{{{ mousePressed() method
  308. public void mousePressed(MouseEvent evt)
  309. {
  310. selectAll = (!hasFocus() && selectAllOnFocus);
  311. } //}}}
  312. //{{{ mouseReleased() method
  313. public void mouseReleased(MouseEvent evt)
  314. {
  315. SwingUtilities.invokeLater(new Runnable()
  316. {
  317. public void run()
  318. {
  319. if(selectAll)
  320. selectAll();
  321. }
  322. });
  323. } //}}}
  324. //{{{ mouseMoved() method
  325. public void mouseMoved(MouseEvent evt)
  326. {
  327. Border border = getBorder();
  328. Insets insets = border.getBorderInsets(HistoryTextField.this);
  329. if(evt.getX() >= getWidth() - insets.right)
  330. setCursor(Cursor.getDefaultCursor());
  331. else
  332. setCursor(Cursor.getPredefinedCursor(
  333. Cursor.TEXT_CURSOR));
  334. } //}}}
  335. //{{{ mouseDragged() method
  336. public void mouseDragged(MouseEvent evt)
  337. {
  338. selectAll = false;
  339. } //}}}
  340. } //}}}
  341. //{{{ HistoryBorder class
  342. static class HistoryBorder extends AbstractBorder
  343. {
  344. static final int WIDTH = 16;
  345. public void paintBorder(Component c, Graphics g,
  346. int x, int y, int w, int h)
  347. {
  348. g.translate(x+w-WIDTH,y-1);
  349. //if(c.isEnabled())
  350. //{
  351. // // vertical separation line
  352. // g.setColor(UIManager.getColor("controlDkShadow"));
  353. // g.drawLine(0,0,0,h);
  354. //}
  355. // down arrow
  356. int w2 = WIDTH/2;
  357. int h2 = h/2;
  358. g.setColor(UIManager.getColor(c.isEnabled()
  359. && ((HistoryTextField)c).getModel() != null
  360. ? "TextField.foreground" : "TextField.disabledForeground"));
  361. g.drawLine(w2-5,h2-2,w2+4,h2-2);
  362. g.drawLine(w2-4,h2-1,w2+3,h2-1);
  363. g.drawLine(w2-3,h2 ,w2+2,h2 );
  364. g.drawLine(w2-2,h2+1,w2+1,h2+1);
  365. g.drawLine(w2-1,h2+2,w2 ,h2+2);
  366. g.translate(-(x+w-WIDTH),-(y-1));
  367. }
  368. public Insets getBorderInsets(Component c)
  369. {
  370. return new Insets(0,0,0,WIDTH);
  371. }
  372. } //}}}
  373. //}}}
  374. }