[1014] | 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("find", EditorUtilities.loadIcon("icons/Find16.gif")); |
---|
| 48 | super(I18N.gettext("main.popup.Find")); |
---|
| 49 | putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_F)); |
---|
| 50 | putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke( KeyEvent.VK_F, ActionEvent.CTRL_MASK)); |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | public void actionPerformed(ActionEvent e) |
---|
| 54 | { |
---|
| 55 | if (e != null) |
---|
| 56 | { |
---|
| 57 | Object o = e.getSource(); |
---|
| 58 | if (o instanceof JTextComponent) |
---|
| 59 | { |
---|
| 60 | JTextComponent text = (JTextComponent) o; |
---|
| 61 | if(e.getModifiers() == 0 ) |
---|
| 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 | // // strip out the surplus \r characters |
---|
| 86 | // StringTokenizer tokenizer = new StringTokenizer(fulltext, "\r"); |
---|
| 87 | // StringBuffer buffer = new StringBuffer(); |
---|
| 88 | // while( tokenizer.hasMoreTokens() ){ |
---|
| 89 | // buffer.append( tokenizer.nextToken() ); |
---|
| 90 | // } |
---|
| 91 | |
---|
| 92 | //get the substring based on the requested start position |
---|
| 93 | fulltext = ( fulltext ).substring(startPositionIn); |
---|
| 94 | |
---|
| 95 | int searchStringLength = searchString.length(); |
---|
| 96 | // so long as we can find the text in the file, go ahead and highlight it |
---|
| 97 | // if we can't show the dialog |
---|
| 98 | if(fulltext.indexOf(searchString) > -1){ |
---|
| 99 | startPoint = (fulltext.indexOf(searchString) ) + startPositionIn; |
---|
| 100 | |
---|
| 101 | endPoint = startPoint + searchStringLength; |
---|
| 102 | text.requestFocus(); |
---|
| 103 | text.setCaretPosition(startPoint); |
---|
| 104 | //move caret to endPoint - thus highlighting the selected text |
---|
| 105 | text.moveCaretPosition(endPoint); |
---|
| 106 | //make the selection visible |
---|
| 107 | //text.getCaret().setSelectionVisible(true); |
---|
| 108 | } |
---|
| 109 | else if (startPositionIn!=0){ |
---|
| 110 | 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) |
---|
| 111 | { |
---|
| 112 | search(text, 0); |
---|
| 113 | } |
---|
| 114 | } |
---|
| 115 | else |
---|
| 116 | { |
---|
| 117 | JOptionPane.showMessageDialog(text.getTopLevelAncestor(),MessageFormat.format(I18N.gettext("main.popup.{0}_not_found"),new Object[]{searchString})); |
---|
| 118 | } |
---|
| 119 | } |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | /* |
---|
| 123 | * Overrides for emacs |
---|
| 124 | * Local variables: |
---|
| 125 | * tab-width: 4 |
---|
| 126 | * End: |
---|
| 127 | */ |
---|