1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| public synchronized void setColorOnEdit()
{
String str = edit.getText();
boolean comment = false;
boolean isSelected = false;
int start = 0;
int stop = 0;
for (int i=0; i < str.length(); i++)
{
if (!isSelected)
{
if(str.regionMatches(i, "<!--", 0, 4))
{
comment = true;
isSelected = true;
start = i;
if (stop < start)
{
DefaultStyledDocument dsd = (DefaultStyledDocument)edit.getDocument();
dsd.setCharacterAttributes(stop, start, BLACK, false);
}
}
else if (str.regionMatches(i, "<", 0, 1) && !comment)
{
isSelected = true;
start = i;
if (stop < start)
{
DefaultStyledDocument dsd = (DefaultStyledDocument)edit.getDocument();
dsd.setCharacterAttributes(stop, start, BLACK, false);
}
}
}
else
{
if (str.regionMatches(i, "-->", 0, 3))
{
isSelected = false;
DefaultStyledDocument dsd = (DefaultStyledDocument)edit.getDocument();
dsd.setCharacterAttributes(start, i+1 - start, ITALIC_GRAY, false);
stop = i+1;
comment = false;
}
else if (str.regionMatches(i, ">", 0, 1) && !comment)
{
isSelected = false;
DefaultStyledDocument dsd = (DefaultStyledDocument)edit.getDocument();
dsd.setCharacterAttributes(start, i+1 - start, BLUE, false);
stop = i+1;
}
}
}
} |
Partager