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

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