Ajuda

Pessoal criei um codigo que verifica o inicio e o fim de um comentario, mas quando eu executo o codigo ele verifica apenas uma vez, como posso fazer para que o codigo fizesse uma verificação constante?

Codigo:

package shc;

import javax.swing.*;
import javax.swing.text.*;
import javax.swing.event.*;
import java.awt.*;

public class Main extends JFrame implements DocumentListener {

	JTextPane jp;
	Segment seg = new Segment();
	Document pd;
	
	boolean DELIMITER_START;
	boolean STAR_START;
	boolean DELIMITER_END;
	boolean STAR_END;
	boolean isSL_COMMENT;
	boolean isML_COMMENT;
		
	public Main(){
		initJTextPane();
		init();
	}
	
	private void initJTextPane(){
		jp = new JTextPane();
		jp.setPreferredSize(new Dimension(400,500));
		jp.setFont(new Font("Courier New",Font.PLAIN,13));
		pd = jp.getDocument();
		pd.addDocumentListener(this);
	}
	
	private void init(){
		add(jp);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		pack();
		setVisible(true);
	}
	
	private void is(Element context) throws BadLocationException {
		int offset = context.getStartOffset();
		int end = context.getEndOffset();
		pd.getText(offset, end, seg);
		for(int i=0; i < seg.length(); i++){
			char indexed = charAt(i);
			if((!DELIMITER_START && !STAR_START && !isSL_COMMENT && !isML_COMMENT && indexed == '/') || 
				!DELIMITER_START && !STAR_START && DELIMITER_END && STAR_END && !isSL_COMMENT && !isML_COMMENT && indexed == '/'){
				DELIMITER_START = true;
			}
			else if(DELIMITER_START && !STAR_START && !isSL_COMMENT && !isML_COMMENT && indexed == '/'){
				isSL_COMMENT = true;
				match("SingleLine Comment");
			}
			else if(DELIMITER_START && !STAR_START && !isSL_COMMENT && !isML_COMMENT && indexed == '*'){
				STAR_START = true;
			}
			else if(DELIMITER_START && STAR_START && !isSL_COMMENT && !isML_COMMENT){
				isML_COMMENT = true;
				match("MultiLine Comment");
			}
			else if(DELIMITER_START && !STAR_START && isSL_COMMENT && !isML_COMMENT && indexed == '\n'){
				match("SingleLine Comment End");
			}
			else if(DELIMITER_START && STAR_START && !DELIMITER_END && !STAR_END && !isSL_COMMENT && isML_COMMENT && indexed == '*'){
				STAR_END = true;
			}
			else if(DELIMITER_START && STAR_START && !DELIMITER_END && STAR_END && !isSL_COMMENT && isML_COMMENT && indexed == '/'){
				DELIMITER_END = true;
				match("MultiLine Comment End");
			}
			else {
				DELIMITER_START = false;
				STAR_START = false;
				DELIMITER_END = false;
				STAR_END = false;
			}
		}
	}
	
	private void match(String b){
		System.out.println(b);
	}
	
	private char charAt(int index){
		return seg.array[seg.offset+index];
	}
	
	private void start(){
		Runnable run = new Runnable(){
			public void run(){
				try {
					is(pd.getDefaultRootElement());
				} catch (BadLocationException e) {
					e.printStackTrace();
				}
			}
		};
		SwingUtilities.invokeLater(run);
	}
	
	public static void main(String[] string){
		new Main();
	}

	public void changedUpdate(DocumentEvent arg0) {}

	public void insertUpdate(DocumentEvent arg0) {
		start();		
	}

	public void removeUpdate(DocumentEvent arg0) {
		start();
	}
	
}

Agradeço desde ja.

Use a classe java.io.StreamTokenizer (que consegue lidar com comentários e outras coisas parecidas) para efetuar o parse de seu texto.
Esse monte de flags está destruindo sua vida; use uma máquina de estados (se quiser efetuar uma análise léxica você mesmo) em vez desse monte de flags.

Uma ÓTIMA alternativa é usar o antlr.
http://www.antlr.org/

Um exemplo de uso do antlr está nesse editor de textos opensource (os fontes dele estão bem fáceis de entender):
http://www.guj.com.br/posts/list/48736.java

thingol, como seria esta maquina de estados?

ViniGodoy, ja tentei usar o antlr, mas o grande problema e criar o codigo do parser, se tiver alguma referencia ou tutorial que mostra como usar a API, agradeço.

Pessoal criei este codigo, ele verifica a existencia da letra p e colori a letra, mas quando dou um espaço no texto ele para de reconhecer, alguem sabe porque isto acontece?

package shc;

import javax.swing.*;
import javax.swing.text.*;
import javax.swing.event.*;
import java.awt.*;
import java.io.*;

public class Teste extends JFrame implements DocumentListener {

	JTextPane pane;
	Document doc;
		
	public Teste(){
		
		pane = new JTextPane();
		pane.setPreferredSize(new Dimension(400,500));
		pane.setFont(new Font("Courier New",0,13));
		
		doc = pane.getDocument();
		doc.addDocumentListener(this);
		
		add(pane);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		pack();
		setVisible(true);
		
	}
	
	private void setStyle(StyledDocument document, Color color, int offset, int length){
		Style style = document.addStyle("style",null);
		StyleConstants.setForeground(style, color);
		StyleConstants.setBold(style, true);
		document.setCharacterAttributes(offset, length, style, true);
	}
	
	private void LexicalAnalyzer() throws BadLocationException, IOException {
		
		Reader reader = new BufferedReader(new StringReader(doc.getText(doc.getStartPosition().getOffset(), doc.getEndPosition().getOffset())));
		StreamTokenizer tokens = new StreamTokenizer(reader);
		while(tokens.nextToken() != StreamTokenizer.TT_EOF){
			for(int i=0; i < tokens.sval.length(); i++){
				char token = tokens.sval.charAt(i);
				if(token == 'p'){
					setStyle(pane.getStyledDocument(), new Color(127, 0, 65), i, tokens.lineno());
				}
				else {
					setStyle(pane.getStyledDocument(), new Color(0, 0, 155), i, tokens.lineno());
				}
			}
		}
			
	}
	
	private void start(){
		
		SwingUtilities.invokeLater(new Runnable(){
			public void run(){
				try {
					LexicalAnalyzer();
				} catch (BadLocationException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		});
		
	}

	public void changedUpdate(DocumentEvent arg0) {}

	public void insertUpdate(DocumentEvent arg0) {
		start();		
	}

	public void removeUpdate(DocumentEvent arg0) {
		start();		
	}
	
	public static void main(String[] string){
		new Teste();
	}

}

Valeu pela ajuda!

Dê uma olhada na própria página do Antlr:
http://www.antlr.org/doc/getting-started.html

Baixe também o editor de textos do André (que está no outro post) e o plugin do antlr no eclipse. O código do editor é relativamente fácil de entender.

Valeu pela ajuda ViniGodoy!

Xi, acho que é melhor você ler algo sobre análise léxica (teoria de compiladores).
Depois de pegar o jeito, a máquina de estados é algo fácil, só um pouco trabalhoso; mas é melhor você ler um livro, que eu tentar explicar isso para você.

Vou pesquisar sobre o assunto.

Obrigado pela ajuda.