You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SwingSearchBar.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /*
  2. * Copyright (c) 2006-2011 Chris Smith, Shane Mc Cormack, Gregory Holmes
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.addons.ui_swing.components;
  23. import com.dmdirc.addons.ui_swing.UIUtilities;
  24. import com.dmdirc.addons.ui_swing.actions.SearchAction;
  25. import com.dmdirc.addons.ui_swing.components.frames.InputTextFrame;
  26. import com.dmdirc.addons.ui_swing.components.frames.TextFrame;
  27. import com.dmdirc.addons.ui_swing.components.validating.ValidatingJTextField;
  28. import com.dmdirc.addons.ui_swing.textpane.TextPane;
  29. import com.dmdirc.config.IdentityManager;
  30. import com.dmdirc.interfaces.ConfigChangeListener;
  31. import com.dmdirc.ui.IconManager;
  32. import com.dmdirc.ui.interfaces.SearchBar;
  33. import com.dmdirc.ui.interfaces.SearchBar.Direction;
  34. import com.dmdirc.ui.messages.IRCDocument;
  35. import com.dmdirc.ui.messages.IRCDocumentSearcher;
  36. import com.dmdirc.ui.messages.LinePosition;
  37. import com.dmdirc.util.ListenerList;
  38. import java.awt.event.ActionEvent;
  39. import java.awt.event.ActionListener;
  40. import java.awt.event.KeyEvent;
  41. import java.awt.event.KeyListener;
  42. import javax.swing.JButton;
  43. import javax.swing.JCheckBox;
  44. import javax.swing.JComponent;
  45. import javax.swing.JLabel;
  46. import javax.swing.JPanel;
  47. import javax.swing.JTextField;
  48. import javax.swing.KeyStroke;
  49. import javax.swing.SwingUtilities;
  50. import javax.swing.event.DocumentEvent;
  51. import javax.swing.event.DocumentListener;
  52. import net.miginfocom.swing.MigLayout;
  53. /**
  54. * Status bar, shows message and info on the gui.
  55. */
  56. public final class SwingSearchBar extends JPanel implements ActionListener,
  57. KeyListener, SearchBar, DocumentListener, ConfigChangeListener {
  58. /**
  59. * A version number for this class. It should be changed whenever the class
  60. * structure is changed (or anything else that would prevent serialized
  61. * objects being unserialized with the new class).
  62. */
  63. private static final long serialVersionUID = 6;
  64. /** Frame parent. */
  65. private final TextFrame parent;
  66. /** Close button. */
  67. private ImageButton closeButton;
  68. /** Next match button. */
  69. private JButton nextButton;
  70. /** Previous match button. */
  71. private JButton prevButton;
  72. /** Case sensitive checkbox. */
  73. private JCheckBox caseCheck;
  74. /** Search text field. */
  75. private ValidatingJTextField searchBox;
  76. /** Line to search from. */
  77. private int line;
  78. /** Listener list. */
  79. private final ListenerList listeners;
  80. /** Search validate text. */
  81. private SearchValidator validator;
  82. /** Wrap indicator. */
  83. private JLabel wrapIndicator;
  84. /**
  85. * Creates a new instance of StatusBar.
  86. *
  87. * @param newParent parent frame for the dialog
  88. */
  89. public SwingSearchBar(final TextFrame newParent) {
  90. super();
  91. listeners = new ListenerList();
  92. this.parent = newParent;
  93. getInputMap(JComponent.WHEN_FOCUSED).
  94. put(KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0), "searchAction");
  95. getActionMap().put("searchAction", new SearchAction(this));
  96. initComponents();
  97. layoutComponents();
  98. addListeners();
  99. }
  100. /** Initialises components. */
  101. private void initComponents() {
  102. closeButton = new ImageButton("close",
  103. IconManager.getIconManager().getIcon("close-inactive"),
  104. IconManager.getIconManager().getIcon("close-active"));
  105. nextButton = new JButton();
  106. prevButton = new JButton();
  107. caseCheck = new JCheckBox();
  108. validator = new SearchValidator();
  109. searchBox = new ValidatingJTextField(validator);
  110. wrapIndicator = new JLabel("Search wrapped",
  111. IconManager.getIconManager().getIcon("linewrap"),
  112. JLabel.LEFT);
  113. nextButton.setText("Later");
  114. prevButton.setText("Earlier");
  115. nextButton.setEnabled(false);
  116. prevButton.setEnabled(false);
  117. caseCheck.setText("Case sensitive");
  118. wrapIndicator.setVisible(false);
  119. line = -1;
  120. setColours();
  121. }
  122. /** Lays out components. */
  123. private void layoutComponents() {
  124. this.setLayout(new MigLayout("ins 0, fill, hidemode 3"));
  125. add(closeButton);
  126. add(searchBox, "growx, pushx, sgy all");
  127. add(prevButton, "sgx button, sgy all");
  128. add(nextButton, "sgx button, sgy all");
  129. add(caseCheck, "sgy all");
  130. add(wrapIndicator, "");
  131. }
  132. /** Adds listeners to components. */
  133. private void addListeners() {
  134. closeButton.addActionListener(this);
  135. searchBox.addKeyListener(this);
  136. nextButton.addActionListener(this);
  137. prevButton.addActionListener(this);
  138. caseCheck.addActionListener(this);
  139. searchBox.getDocument().addDocumentListener(this);
  140. IdentityManager.getGlobalConfig().addChangeListener(
  141. "ui", "backgroundcolour", this);
  142. IdentityManager.getGlobalConfig().addChangeListener(
  143. "ui", "foregroundcolour", this);
  144. }
  145. /**
  146. * {@inheritDoc}.
  147. *
  148. * @param e Action event
  149. */
  150. @Override
  151. public void actionPerformed(final ActionEvent e) {
  152. if (e.getSource() == closeButton) {
  153. close();
  154. } else if (e.getSource() == nextButton) {
  155. search(Direction.DOWN, searchBox.getText(), caseCheck.isSelected());
  156. } else if (e.getSource() == prevButton) {
  157. search(Direction.UP, searchBox.getText(), caseCheck.isSelected());
  158. } else if (e.getSource() == caseCheck) {
  159. validator.setValidates(true);
  160. searchBox.checkError();
  161. line = parent.getTextPane().getLastVisibleLine();
  162. }
  163. }
  164. /** {@inheritDoc}. */
  165. @Override
  166. public void open() {
  167. SwingUtilities.invokeLater(new Runnable() {
  168. /** {@inheritDoc} */
  169. @Override
  170. public void run() {
  171. validator.setValidates(true);
  172. searchBox.checkError();
  173. setVisible(true);
  174. getFocus();
  175. }
  176. });
  177. }
  178. /** {@inheritDoc}. */
  179. @Override
  180. public void close() {
  181. SwingUtilities.invokeLater(new Runnable() {
  182. /** {@inheritDoc} */
  183. @Override
  184. public void run() {
  185. setVisible(false);
  186. if (parent instanceof InputTextFrame) {
  187. ((InputTextFrame) parent).getInputField().
  188. requestFocusInWindow();
  189. } else {
  190. parent.requestFocusInWindow();
  191. }
  192. }
  193. });
  194. }
  195. /** {@inheritDoc}. */
  196. @Override
  197. public void search(final String text, final boolean caseSensitive) {
  198. if (!searchBox.getText().isEmpty()) {
  199. if (line == -1) {
  200. line = parent.getTextPane().getLastVisibleLine();
  201. }
  202. search(Direction.UP, text, caseSensitive);
  203. }
  204. }
  205. /** {@inheritDoc}. */
  206. @Override
  207. public void search(final Direction direction, final String text,
  208. final boolean caseSensitive) {
  209. boolean foundText = false;
  210. wrapIndicator.setVisible(false);
  211. final boolean up = Direction.UP == direction;
  212. final TextPane textPane = parent.getTextPane();
  213. final IRCDocument document = textPane.getDocument();
  214. final IRCDocumentSearcher searcher = new IRCDocumentSearcher(text,
  215. document,
  216. caseSensitive);
  217. searcher.setPosition(textPane.getSelectedRange());
  218. final LinePosition result = up ? searcher.searchUp() : searcher.
  219. searchDown();
  220. if (result == null) {
  221. foundText = false;
  222. } else if ((textPane.getSelectedRange().getEndLine() != 0 || textPane.
  223. getSelectedRange().getEndPos() != 0)
  224. && ((up && result.getEndLine() > textPane.getSelectedRange().
  225. getEndLine())
  226. || (!up && result.getStartLine() < textPane.getSelectedRange().
  227. getStartLine()))) {
  228. wrapIndicator.setVisible(true);
  229. textPane.setScrollBarPosition(result.getEndLine());
  230. textPane.setSelectedTexT(result);
  231. validator.setValidates(true);
  232. searchBox.checkError();
  233. } else {
  234. //found, select and return found
  235. textPane.setScrollBarPosition(result.getEndLine());
  236. textPane.setSelectedTexT(result);
  237. foundText = true;
  238. validator.setValidates(foundText);
  239. searchBox.checkError();
  240. }
  241. }
  242. /**
  243. * Returns the textfield used in this search bar.
  244. *
  245. * @return Search textfield
  246. */
  247. public JTextField getTextField() {
  248. return searchBox.getTextField();
  249. }
  250. /**
  251. * {@inheritDoc}.
  252. *
  253. * @param event Key event
  254. */
  255. @Override
  256. public void keyPressed(final KeyEvent event) {
  257. if (event.getSource() == searchBox.getTextField()) {
  258. if (event.getKeyCode() == KeyEvent.VK_ESCAPE) {
  259. close();
  260. } else if (event.getKeyCode() == KeyEvent.VK_ENTER) {
  261. search(Direction.UP, searchBox.getText(),
  262. caseCheck.isSelected());
  263. } else if (event.getKeyCode() != KeyEvent.VK_F3 && event.
  264. getKeyCode() != KeyEvent.VK_F) {
  265. line = parent.getTextPane().getLastVisibleLine();
  266. }
  267. }
  268. for (KeyListener listener : listeners.get(KeyListener.class)) {
  269. listener.keyPressed(event);
  270. }
  271. }
  272. /**
  273. * {@inheritDoc}.
  274. *
  275. * @param event Key event
  276. */
  277. @Override
  278. public void keyTyped(final KeyEvent event) {
  279. //Ignore
  280. }
  281. /**
  282. * {@inheritDoc}.
  283. *
  284. * @param event Key event
  285. */
  286. @Override
  287. public void keyReleased(final KeyEvent event) {
  288. //Ignore
  289. }
  290. /** Focuses the search box in the search bar. */
  291. public void getFocus() {
  292. SwingUtilities.invokeLater(new Runnable() {
  293. /** {@inheritDoc} */
  294. @Override
  295. public void run() {
  296. searchBox.requestFocusInWindow();
  297. searchBox.setSelectionStart(0);
  298. searchBox.setSelectionEnd(searchBox.getText().length());
  299. }
  300. });
  301. }
  302. /** {@inheritDoc}. */
  303. @Override
  304. public String getSearchPhrase() {
  305. return searchBox.getText();
  306. }
  307. /** {@inheritDoc}. */
  308. @Override
  309. public boolean isCaseSensitive() {
  310. return caseCheck.isSelected();
  311. }
  312. /** {@inheritDoc}. */
  313. @Override
  314. public void insertUpdate(final DocumentEvent e) {
  315. validator.setValidates(true);
  316. searchBox.checkError();
  317. nextButton.setEnabled(!searchBox.getText().isEmpty());
  318. prevButton.setEnabled(!searchBox.getText().isEmpty());
  319. }
  320. /** {@inheritDoc}. */
  321. @Override
  322. public void removeUpdate(final DocumentEvent e) {
  323. validator.setValidates(true);
  324. searchBox.checkError();
  325. nextButton.setEnabled(!searchBox.getText().isEmpty());
  326. prevButton.setEnabled(!searchBox.getText().isEmpty());
  327. }
  328. /** {@inheritDoc}. */
  329. @Override
  330. public void changedUpdate(final DocumentEvent e) {
  331. //Ignore
  332. }
  333. /** {@inheritDoc} */
  334. @Override
  335. public void addKeyListener(final KeyListener l) {
  336. UIUtilities.invokeLater(new Runnable() {
  337. /** {@inheritDoc} */
  338. @Override
  339. public void run() {
  340. listeners.add(KeyListener.class, l);
  341. }
  342. });
  343. }
  344. /** {@inheritDoc} */
  345. @Override
  346. public void removeKeyListener(final KeyListener l) {
  347. UIUtilities.invokeLater(new Runnable() {
  348. /** {@inheritDoc} */
  349. @Override
  350. public void run() {
  351. listeners.remove(KeyListener.class, l);
  352. }
  353. });
  354. }
  355. /** {@inheritDoc} */
  356. @Override
  357. public void configChanged(final String domain, final String key) {
  358. setColours();
  359. }
  360. /** Sets the colours used in this document. */
  361. private void setColours() {
  362. searchBox.setForeground(IdentityManager.getGlobalConfig().
  363. getOptionColour("ui", "foregroundcolour"));
  364. searchBox.setBackground(IdentityManager.getGlobalConfig().
  365. getOptionColour("ui", "backgroundcolour"));
  366. searchBox.setCaretColor(IdentityManager.getGlobalConfig().
  367. getOptionColour("ui", "foregroundcolour"));
  368. }
  369. }