package batch;
import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Element;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
public class HighlitedBatchDocument extends DefaultStyledDocument {
private static final String[] keywords = { "pause", "color ", "cls", "if ", "shutdown ", "mode ", "for ", "do ", "set ", "start ", "copy ", "xcopy ", "mkdir ",
"md ", "taskkill ", "ren ", "ping ", "title ", "goto ", ":" };
public void highlightText() {
// Schutz vor overflow
Runnable thread = new Runnable() {
public void run() {
String text = null;
try{
text = getText(0, getLength());
}
catch(BadLocationException ex){
}
// erstmal wieder alles in schwartz
setForeground(0, text.length(), Color.black);
int index = text.indexOf("@");
while(index != -1){
// "@" in blau
setForeground(index, index+1, Color.magenta);
index = text.indexOf("@", index+1);
}
// andere Schlüsselwörter in Blau
for(String keyword : keywords){
index = text.indexOf(keyword);
while(index != -1){
setForeground(index, index+keyword.length(), Color.blue);
// Rest der Zeile in Schwartz
setForeground(index+1+keyword.length(), text.indexOf("\n", index+1+keyword.length()), Color.black);
index = text.indexOf(keyword, index+1);
}
}
index = text.indexOf("echo");
while(index != -1){
// "echo" in blau
setForeground(index, index+4, Color.blue);
// Text von "echo" in orange
setForeground(index+5, text.indexOf("\n", index+5), Color.orange);
index = text.indexOf("echo", index+1);
}
index = text.indexOf(" on");
while(index != -1){
// "on" in blau
setForeground(index+1, index+3, Color.black);
index = text.indexOf(" on", index+1);
}
index = text.indexOf(" off");
while(index != -1){
// "off" in blau
setForeground(index+1, index+4, Color.black);
index = text.indexOf(" off", index+1);
}
index = 0;
// Variablen (z.B. %testvar%) in schwartz
int firstModulo = 0;
while(index != -1 && firstModulo != -1){
if(firstModulo == 0){
firstModulo = text.indexOf("%", index+1);
}
else{
index = text.indexOf("%", firstModulo+1);
setForeground(firstModulo, index+1, Color.black);
firstModulo = 0;
}
}
// Variablen (z.B. %%i) in schwartz
index = text.indexOf("::");
while(index != -1){
// Kommentare in Hellgrau
setForeground(index, text.indexOf("\n", index), Color.gray);
index = text.indexOf("::", index+1);
}
index = text.indexOf(">>");
while(index != -1){
// ">>" in blau
setForeground(index, index+2, Color.blue);
// Text nach ">>" in schwartz
setForeground(index+2, text.indexOf("\n", index+2), Color.black);
index = text.indexOf(">>", index+1);
}
index = text.indexOf("::");
while(index != -1){
// Kommentare in Hellgrau
setForeground(index, text.indexOf("\n", index), Color.gray);
index = text.indexOf("::", index+1);
}
};
};
EventQueue.invokeLater(thread);
}
public void setForeground(int startPosition, int endPosition, Color col) {
for(int i = startPosition; i < endPosition; i++){
setForeground(getSimpleAttributeSet(i), i, col);
}
}
private void setForeground(SimpleAttributeSet sas, int pos, Color col) {
StyleConstants.setForeground(sas, col);
setCharacterAttributes(pos, 1, sas, true);
}
private SimpleAttributeSet getSimpleAttributeSet(int pos) {
Element ele = getCharacterElement(pos);
AttributeSet as = ele.getAttributes();
SimpleAttributeSet sas = new SimpleAttributeSet(as);
return sas;
}
}