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.