ERLEDIGT
NEIN
NEIN
ANTWORTEN
0
0
ZUGRIFFE
1516
1516
EMPFEHLEN
-
Ich habe mal das Syntax Highlighting Beispiel von http://java.sys-con.com/read/36509.htm ein wenig erweitert.
Farben lassen sich einstellen, keywords theoretisch auch, ist aber sehr auf Java-fokussiert momentan.
Code java: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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
package de.tutorials; import java.awt.Color; import java.util.HashMap; import java.util.Map; import java.util.Set; 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 CodeDocument extends DefaultStyledDocument { private String word = ""; private SimpleAttributeSet bold = new SimpleAttributeSet(); private SimpleAttributeSet string = new SimpleAttributeSet(); private SimpleAttributeSet normal = new SimpleAttributeSet(); private SimpleAttributeSet number = new SimpleAttributeSet(); private SimpleAttributeSet comments = new SimpleAttributeSet(); private SimpleAttributeSet javadoc = new SimpleAttributeSet(); private SimpleAttributeSet annotation = new SimpleAttributeSet(); private int currentPos = 0; private Map<String, SimpleAttributeSet> keywordSets = new HashMap<String, SimpleAttributeSet>(); public static int STRING_MODE = 10; public static int TEXT_MODE = 11; public static int NUMBER_MODE = 12; public static int COMMENT_MODE = 13; public static int LINE_COMMENT_MODE = 14; public static int JAVADOC_MODE = 15; public static int ANNOTATION_MODE = 16; private int mode = TEXT_MODE; public CodeDocument() { // set the bold attribute StyleConstants.setBold(bold, true); StyleConstants.setForeground(string, Color.blue); StyleConstants.setForeground(number, Color.red); StyleConstants.setForeground(comments, Color.green); StyleConstants.setItalic(comments, true); StyleConstants.setForeground(javadoc, Color.cyan); StyleConstants.setItalic(javadoc, true); StyleConstants.setForeground(annotation, Color.GRAY); StyleConstants.setBold(annotation, true); } public void setCommentColor(Color color) { StyleConstants.setForeground(comments, color); } public void setJavadocColor(Color color) { StyleConstants.setForeground(javadoc, color); } public void setAnnotationColor(Color color) { StyleConstants.setForeground(annotation, color); } private void insertKeyword(String str, int pos, SimpleAttributeSet as) { try { // remove the old word and formatting this.remove(pos - str.length(), str.length()); /* * replace it with the same word, but new formatting we MUST call * the super class insertString method here, otherwise we would end * up in an infinite loop !! */ super.insertString(pos - str.length(), str, as); } catch (Exception ex) { ex.printStackTrace(); } } private void insertTextString(String str, int pos) { try { // remove the old word and formatting this.remove(pos, str.length()); super.insertString(pos, str, string); } catch (Exception ex) { ex.printStackTrace(); } } private void insertNumberString(String str, int pos) { try { // remove the old word and formatting this.remove(pos, str.length()); super.insertString(pos, str, number); } catch (Exception ex) { ex.printStackTrace(); } } private void insertCommentString(String str, int pos) { try { // remove the old word and formatting this.remove(pos, str.length()); super.insertString(pos, str, comments); } catch (Exception ex) { ex.printStackTrace(); } } private void insertJavadocString(String str, int pos) { try { // remove the old word and formatting this.remove(pos, str.length()); super.insertString(pos, str, javadoc); } catch (Exception ex) { ex.printStackTrace(); } } private void insertAnnotationString(String str, int pos) { try { // remove the old word and formatting this.remove(pos, str.length()); super.insertString(pos, str, annotation); } catch (Exception ex) { ex.printStackTrace(); } } private void checkForString() { int offs = this.currentPos; Element element = this.getParagraphElement(offs); String elementText = ""; try { // this gets our chuck of current text for the element we're on elementText = this.getText(element.getStartOffset(), element .getEndOffset() - element.getStartOffset()); } catch (Exception ex) { // whoops! System.out.println("no text"); } int strLen = elementText.length(); if (strLen == 0) { return; } int i = 0; if (element.getStartOffset() > 0) { // translates backward if neccessary offs = offs - element.getStartOffset(); } int quoteCount = 0; if ((offs >= 0) && (offs <= strLen - 1)) { i = offs; while (i > 0) { // the while loop walks back until we hit a delimiter char charAt = elementText.charAt(i); if ((charAt == '"')) { quoteCount++; } i--; } int rem = quoteCount % 2; // System.out.println(rem); mode = (rem == 0) ? TEXT_MODE : STRING_MODE; } } private void checkForKeyword() { if (mode != TEXT_MODE) { return; } int offs = this.currentPos; Element element = this.getParagraphElement(offs); String elementText = ""; try { // this gets our chuck of current text for the element we're on elementText = this.getText(element.getStartOffset(), element .getEndOffset() - element.getStartOffset()); } catch (Exception ex) { // whoops! System.out.println("no text"); } int strLen = elementText.length(); if (strLen == 0) { return; } int i = 0; if (element.getStartOffset() > 0) { // translates backward if neccessary offs = offs - element.getStartOffset(); } if ((offs >= 0) && (offs <= strLen - 1)) { i = offs; while (i > 0) { // the while loop walks back until we hit a delimiter i--; char charAt = elementText.charAt(i); if ((charAt == ' ') | (i == 0) | (charAt == '(') | (charAt == ')') | (charAt == '{') | (charAt == '}')) { // if i // == 0 // then // we're // at // the // begininng if (i != 0) { i++; } word = elementText.substring(i, offs);// skip the period String s = word.trim().toLowerCase(); // this is what actually checks for a matching keyword if (keywordSets.containsKey(s)) { insertKeyword(word, currentPos, keywordSets.get(s)); } break; } } } } private void checkForNumber() { int offs = this.currentPos; Element element = this.getParagraphElement(offs); String elementText = ""; try { // this gets our chuck of current text for the element we're on elementText = this.getText(element.getStartOffset(), element .getEndOffset() - element.getStartOffset()); } catch (Exception ex) { // whoops! System.out.println("no text"); } int strLen = elementText.length(); if (strLen == 0) { return; } int i = 0; if (element.getStartOffset() > 0) { // translates backward if neccessary offs = offs - element.getStartOffset(); } mode = TEXT_MODE; if ((offs >= 0) && (offs <= strLen - 1)) { i = offs; while (i > 0) { // the while loop walks back until we hit a delimiter char charAt = elementText.charAt(i); if ((charAt == ' ') | (i == 0) | (charAt == '(') | (charAt == ')') | (charAt == '{') | (charAt == '}') /* | */) { // if i // == 0 // then // we're // at // the // begininng if (i != 0) { i++; } mode = NUMBER_MODE; break; } else if (!(charAt >= '0' & charAt <= '9' | charAt == '.' | charAt == '+' | charAt == '-' | charAt == '/' | charAt == '*' | charAt == '%' | charAt == '=')) { mode = TEXT_MODE; break; } i--; } } } private void checkForComment() { int offs = this.currentPos; Element element = this.getParagraphElement(offs); String elementText = ""; try { // this gets our chuck of current text for the element we're on elementText = this.getText(element.getStartOffset(), element .getEndOffset() - element.getStartOffset()); } catch (Exception ex) { // whoops! System.out.println("no text"); } int strLen = elementText.length(); if (strLen == 0) { return; } int i = 0; if (element.getStartOffset() > 0) { // translates backward if neccessary offs = offs - element.getStartOffset(); } if ((offs >= 1) && (offs <= strLen - 1)) { i = offs; char commentStartChar1 = elementText.charAt(i - 1); char commentStartChar2 = elementText.charAt(i); if (mode == COMMENT_MODE && commentStartChar1 == '*' && commentStartChar2 == '*') { mode = JAVADOC_MODE; this.insertJavadocString("/**", currentPos - 2); } else if (commentStartChar1 == '/' && commentStartChar2 == '*') { mode = COMMENT_MODE; this.insertCommentString("/*", currentPos - 1); } else if (commentStartChar1 == '/' && commentStartChar2 == '/') { mode = LINE_COMMENT_MODE; this.insertCommentString("//", currentPos - 1); } else if (commentStartChar1 == '*' && commentStartChar2 == '/') { boolean javadoc = false; if (mode == JAVADOC_MODE) { javadoc = true; } mode = TEXT_MODE; if (javadoc) { this.insertJavadocString("*/", currentPos - 1); } else { this.insertCommentString("*/", currentPos - 1); } } } } private void processChar(String str) { char strChar = str.charAt(0); if (mode != COMMENT_MODE && mode != LINE_COMMENT_MODE && mode != JAVADOC_MODE && mode != ANNOTATION_MODE) { mode = TEXT_MODE; } switch (strChar) { case ('@'): if (mode == TEXT_MODE) { mode = ANNOTATION_MODE; } break; case ('{'): case ('}'): case (' '): case ('\n'): case ('('): case (')'): case (';'): case ('.'): { checkForKeyword(); if (mode == ANNOTATION_MODE && strChar == '(') { mode = TEXT_MODE; } if ((mode == STRING_MODE || mode == LINE_COMMENT_MODE || mode == ANNOTATION_MODE) && strChar == '\n') { mode = TEXT_MODE; } } break; case ('"'): { insertTextString(str, currentPos); this.checkForString(); } break; case ('0'): case ('1'): case ('2'): case ('3'): case ('4'): case ('5'): case ('6'): case ('7'): case ('8'): case ('9'): { checkForNumber(); } break; case ('*'): case ('/'): { checkForComment(); } break; } if (mode == TEXT_MODE) { this.checkForString(); } if (mode == STRING_MODE) { insertTextString(str, this.currentPos); } else if (mode == NUMBER_MODE) { insertNumberString(str, this.currentPos); } else if (mode == COMMENT_MODE) { insertCommentString(str, this.currentPos); } else if (mode == LINE_COMMENT_MODE) { insertCommentString(str, this.currentPos); } else if (mode == JAVADOC_MODE) { insertJavadocString(str, this.currentPos); } else if (mode == ANNOTATION_MODE) { insertAnnotationString(str, this.currentPos); } } private void processChar(char strChar) { char[] chrstr = new char[1]; chrstr[0] = strChar; String str = new String(chrstr); processChar(str); } public void insertString(int offs, String str, AttributeSet a) throws BadLocationException { super.insertString(offs, str, normal); int strLen = str.length(); int endpos = offs + strLen; int strpos; for (int i = offs; i < endpos; i++) { currentPos = i; strpos = i - offs; processChar(str.charAt(strpos)); } currentPos = offs; } public Set<String> getKeywords() { return this.keywordSets.keySet(); } public void setKeywords(Map<String, Color> aKeywordList) { if (aKeywordList != null) { for (Map.Entry<String, Color> entry : aKeywordList.entrySet()) { SimpleAttributeSet temp = new SimpleAttributeSet(); StyleConstants.setForeground(temp, entry.getValue()); StyleConstants.setBold(temp, true); this.keywordSets.put(entry.getKey(), temp); } // this.keywords = aKeywordList; } } }
Code java: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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
package de.tutorials; import java.awt.Color; import java.util.HashMap; import java.util.Map; import javax.swing.JFrame; import javax.swing.JTextPane; import javax.swing.WindowConstants; public class Test{ public static void main(String[] args) { JFrame frame = new JFrame(); JTextPane editor = new JTextPane(); CodeDocument doc = new CodeDocument(); Map<String,Color> keywords = new HashMap<String,Color>(); Color comment = new Color(63,197,95); Color javadoc = new Color(63,95,191); Color annotation = new Color(100,100,100); doc.setCommentColor(comment); doc.setJavadocColor(javadoc); doc.setAnnotationColor(annotation); Color defColor = new Color(127,0,85); keywords.put("abstract",defColor); keywords.put("boolean",defColor); keywords.put("break",defColor); keywords.put("byte",defColor); keywords.put("case",defColor); keywords.put("catch",defColor); keywords.put("char",defColor); keywords.put("class",defColor); keywords.put("continue",defColor); keywords.put("default",defColor); keywords.put("do",defColor); keywords.put("double",defColor); keywords.put("enum",defColor); keywords.put("extends",defColor); keywords.put("else",defColor); keywords.put("false",defColor); keywords.put("final",defColor); keywords.put("finally",defColor); keywords.put("float",defColor); keywords.put("for",defColor); keywords.put("if",defColor); keywords.put("implements",defColor); keywords.put("import",defColor); keywords.put("instanceof",defColor); keywords.put("int",defColor); keywords.put("interface",defColor); keywords.put("long",defColor); keywords.put("native",defColor); keywords.put("new",defColor); keywords.put("null",defColor); keywords.put("package",defColor); keywords.put("private",defColor); keywords.put("protected",defColor); keywords.put("public",defColor); keywords.put("return",defColor); keywords.put("short",defColor); keywords.put("static",defColor); keywords.put("super",defColor); keywords.put("switch",defColor); keywords.put("synchronized",defColor); keywords.put("this",defColor); keywords.put("throw",defColor); keywords.put("throws",defColor); keywords.put("transient",defColor); keywords.put("true",defColor); keywords.put("try",defColor); keywords.put("void",defColor); keywords.put("volatile",defColor); keywords.put("while",defColor); doc.setKeywords(keywords); editor.setDocument(doc); frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); frame.setSize(400,400); frame.getContentPane().add(editor); frame.setVisible(true); } }
Ähnliche Themen
-
Java Syntax Highlighting nach Beispiel
Von Leroux im Forum Swing, Java2D/3D, SWT, JFaceAntworten: 0Letzter Beitrag: 01.10.08, 09:07 -
Syntax Highlighting
Von RedWing im Forum vB-TestforumAntworten: 0Letzter Beitrag: 15.03.08, 01:05 -
Syntax-Highlighting
Von DataFox im Forum PHPAntworten: 4Letzter Beitrag: 18.02.08, 22:45 -
Java Syntax highlighting (PHP)
Von Thomas Darimont im Forum JavaAntworten: 0Letzter Beitrag: 01.07.04, 10:34






Zitieren
Login





