Login | Register
My pages Projects Community openCollabNet

Discussions > cvs > CVS update: /antelope/src/ise/library/

antelope
Discussion topic

Back to topic list

CVS update: /antelope/src/ise/library/

Author danson
Full name Dale Anson
Date 2004-05-23 21:11:27 PDT
Message User: danson
Date: 04/05/23 21:11:27

Added:
 /antelope/src/ise/library/
  Finder.java

Log:
 added Finder
 Issue number:
 Obtained from:
 Submitted by:
 Reviewed by:

File Changes:

Directory: /antelope/src/ise/library/
====================​=================

File [added]: Finder.java
Url: http://antelope.tigr​is.org/source/browse​/antelope/src/ise/li​brary/Finder.java?re​v=1.1&content-ty​pe=text/vnd.viewcvs-​markup
Added lines: 61
---------------
package ise.library;

import javax.swing.text.JTe​xtComponent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.regex.*;

/**
 * Action to do a find in a text component.
Does regular expression searching.
 * @author Dale Anson
 */
public class Finder implements ActionListener {

    private JTextComponent _to_find;
    private JTextComponent _textarea;
    private boolean _from_caret;
    private boolean _wrap;

    /**
     * Will search for <code>look_for​.getText()</code​> in <code>look_in​</code>.
     * If found, will select the found text.
     * @param look_for a text component that contains some text to look for. The
     * text may be any string acceptable to java.util.Matcher.
     * @param look_in the text component in which to find the look_for text.
     * @param from_caret if true, find from current caret position, otherwise,
     * from start of document.
     * @param wrap if true, wrap searching to top of document.
     */
    public Finder(JTextComponent look_for, JTextComponent look_in, boolean from_caret, boolean wrap) {
        _to_find = look_for;
        _textarea = look_in;
        _from_caret = from_caret;
        _wrap = wrap;
    }

    public void actionPerformed( ActionEvent ae ) {
        String text_to_find = _to_find.getText();
        if ( text_to_find == null || text_to_find.length() == 0 ) {
            return ;
        }
        try {
            String doc = _textarea.getDocumen​t().getText(0, _textarea.getDocumen​t().getLength());
            Pattern pattern = Pattern.compile( text_to_find, Pattern.DOTALL );
            Matcher matcher = pattern.matcher( doc );
            int find_from = _from_caret ? _textarea.getCaretPosition() : 0;
            if (!matcher.find(find_from) && _wrap)
                find_from = 0;
            if ( matcher.find(find_from) ) {
                int start = matcher.start();
                int end = matcher.end();
                String found = doc.substring( start, end );
                _textarea.setCaretPosition( start );
                _textarea.select( start, end );
            }
        }
        catch ( Exception e ) {
            e.printStackTrace();
        }
    }
}





--------------------​--------------------​--------------------​---------
To unsubscribe, e-mail: cvs-unsubscribe@ante​lope.tigris.org
For additional commands, e-mail: cvs-help at antelope dot tigris dot org

« Previous message in topic | 1 of 1 | Next message in topic »

Messages

Show all messages in topic

CVS update: /antelope/src/ise/library/ danson Dale Anson 2004-05-23 21:11:27 PDT
Messages per page: