1 | /* |
---|
2 | * Jeti, a Java Jabber client, Copyright (C) 2004 E.S. de Boer |
---|
3 | * |
---|
4 | * This program is free software; you can redistribute it and/or modify |
---|
5 | * it under the terms of the GNU General Public License as published by |
---|
6 | * the Free Software Foundation; either version 2 of the License, or |
---|
7 | * (at your option) any later version. |
---|
8 | * |
---|
9 | * This program is distributed in the hope that it will be useful, |
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | * GNU General Public License for more details. |
---|
13 | * |
---|
14 | * You should have received a copy of the GNU General Public License |
---|
15 | * along with this program; if not, write to the Free Software |
---|
16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
17 | * |
---|
18 | * For questions, comments etc, |
---|
19 | * use the website at http://jeti.jabberstudio.org |
---|
20 | * or mail me at eric@jeti.tk or Jabber at jeti@jabber.org |
---|
21 | * |
---|
22 | * Created on 27-feb-2004 |
---|
23 | */ |
---|
24 | |
---|
25 | package nu.fw.jeti.util; |
---|
26 | |
---|
27 | import java.awt.event.ActionEvent; |
---|
28 | import java.awt.event.KeyEvent; |
---|
29 | import java.text.MessageFormat; |
---|
30 | |
---|
31 | import javax.swing.AbstractAction; |
---|
32 | import javax.swing.JOptionPane; |
---|
33 | import javax.swing.KeyStroke; |
---|
34 | import javax.swing.text.JTextComponent; |
---|
35 | |
---|
36 | /** |
---|
37 | * @author E.S. de Boer |
---|
38 | * |
---|
39 | */ |
---|
40 | public class FindAction extends AbstractAction |
---|
41 | { |
---|
42 | private String searchString = null; |
---|
43 | private int startPoint = 0; |
---|
44 | private int endPoint = 0; |
---|
45 | |
---|
46 | public FindAction(){ |
---|
47 | super(I18N.gettext("main.popup.Find")); |
---|
48 | putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_F)); |
---|
49 | putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke( KeyEvent.VK_F, ActionEvent.CTRL_MASK)); |
---|
50 | } |
---|
51 | |
---|
52 | public void actionPerformed(ActionEvent e) |
---|
53 | { |
---|
54 | if (e != null) |
---|
55 | { |
---|
56 | Object o = e.getSource(); |
---|
57 | if (o instanceof JTextComponent) |
---|
58 | { |
---|
59 | JTextComponent text = (JTextComponent) o; |
---|
60 | if(e.getModifiers() == 0 ) |
---|
61 | { |
---|
62 | //find Next |
---|
63 | if (searchString==null) askSearchString(text); |
---|
64 | if (searchString!=null)search(text,text.getCaretPosition()); |
---|
65 | } |
---|
66 | else |
---|
67 | { |
---|
68 | askSearchString(text); |
---|
69 | if (searchString!=null)search(text,0); |
---|
70 | } |
---|
71 | } |
---|
72 | } |
---|
73 | } |
---|
74 | |
---|
75 | private void askSearchString(JTextComponent text) |
---|
76 | { |
---|
77 | searchString = JOptionPane.showInputDialog(text.getTopLevelAncestor(), I18N.gettext("main.popup.Enter_the_text_to_search_for"),text.getSelectedText()); |
---|
78 | } |
---|
79 | |
---|
80 | |
---|
81 | private void search(JTextComponent text, int startPositionIn) |
---|
82 | { |
---|
83 | String fulltext = text.getText(); |
---|
84 | |
---|
85 | //get the substring based on the requested start position |
---|
86 | fulltext = ( fulltext ).substring(startPositionIn); |
---|
87 | |
---|
88 | int searchStringLength = searchString.length(); |
---|
89 | // so long as we can find the text in the file, go ahead and highlight it |
---|
90 | // if we can't show the dialog |
---|
91 | if(fulltext.indexOf(searchString) > -1) |
---|
92 | { |
---|
93 | startPoint = (fulltext.indexOf(searchString) ) + startPositionIn; |
---|
94 | endPoint = startPoint + searchStringLength; |
---|
95 | text.requestFocus(); |
---|
96 | text.setCaretPosition(startPoint); |
---|
97 | text.moveCaretPosition(endPoint); |
---|
98 | } |
---|
99 | else if (startPositionIn!=0) |
---|
100 | { |
---|
101 | if(JOptionPane.showConfirmDialog(text.getTopLevelAncestor(),MessageFormat.format(I18N.gettext("main.popup.{0}_not_found_until_the_end_of_this_file,_start_again_from_the_beginning?"),new Object[]{searchString})) == JOptionPane.OK_OPTION) |
---|
102 | { |
---|
103 | search(text, 0); |
---|
104 | } |
---|
105 | } |
---|
106 | else |
---|
107 | { |
---|
108 | JOptionPane.showMessageDialog(text.getTopLevelAncestor(),MessageFormat.format(I18N.gettext("main.popup.{0}_not_found"),new Object[]{searchString})); |
---|
109 | } |
---|
110 | } |
---|
111 | } |
---|
112 | |
---|
113 | /* |
---|
114 | * Overrides for emacs |
---|
115 | * Local variables: |
---|
116 | * tab-width: 4 |
---|
117 | * End: |
---|
118 | */ |
---|