Line 0
Link Here
|
|
|
1 |
/** |
2 |
* Copyright (c) 2008 Motorola. |
3 |
* |
4 |
* All rights reserved. This program and the accompanying materials |
5 |
* are made available under the terms of the Eclipse Public License v1.0 |
6 |
* which accompanies this distribution, and is available at |
7 |
* http://www.eclipse.org/legal/epl-v10.html |
8 |
* |
9 |
* Contributors: |
10 |
* Diego Sandin (Motorola) - Initial implementation |
11 |
*/ |
12 |
package org.eclipse.mtj.internal.ui.editors.jad.source.rules; |
13 |
|
14 |
import org.eclipse.jface.resource.ColorRegistry; |
15 |
import org.eclipse.jface.text.TextAttribute; |
16 |
import org.eclipse.jface.text.rules.IRule; |
17 |
import org.eclipse.jface.text.rules.IWhitespaceDetector; |
18 |
import org.eclipse.jface.text.rules.IWordDetector; |
19 |
import org.eclipse.jface.text.rules.RuleBasedScanner; |
20 |
import org.eclipse.jface.text.rules.SingleLineRule; |
21 |
import org.eclipse.jface.text.rules.Token; |
22 |
import org.eclipse.jface.text.rules.WhitespaceRule; |
23 |
import org.eclipse.jface.text.rules.WordRule; |
24 |
import org.eclipse.mtj.core.project.midp.DescriptorPropertyDescription; |
25 |
import org.eclipse.mtj.internal.ui.editors.jad.form.JADAttributesRegistry; |
26 |
import org.eclipse.mtj.ui.editors.jad.IJADDescriptorsProvider; |
27 |
import org.eclipse.swt.SWT; |
28 |
import org.eclipse.swt.graphics.RGB; |
29 |
|
30 |
/** |
31 |
* A scanner programmed with a sequence of rules specific to the JAD file. |
32 |
* |
33 |
* @author Diego Madruga Sandin |
34 |
*/ |
35 |
public class JadScanner extends RuleBasedScanner { |
36 |
|
37 |
/** |
38 |
* Symbolic name for the keyword colors |
39 |
*/ |
40 |
private static final String KEYWORD_COLOR= "mtj.jad.keyword.color"; //$NON-NLS-1$ |
41 |
/** |
42 |
* Symbolic name for value colors |
43 |
*/ |
44 |
private static final String VALUE_COLOR= "mtj.jad.value.color"; //$NON-NLS-1$ |
45 |
|
46 |
|
47 |
// This is probably not the best way to handle the colors for this scanner |
48 |
// it will do until the colors are integrated with preferences. |
49 |
private static final ColorRegistry colorRegistry = new ColorRegistry(); |
50 |
static{ |
51 |
colorRegistry.put(KEYWORD_COLOR, new RGB(127,0, 85)); |
52 |
colorRegistry.put(VALUE_COLOR, new RGB(0,0, 0)); |
53 |
} |
54 |
|
55 |
/** |
56 |
* Creates a new Scanner |
57 |
*/ |
58 |
public JadScanner() { |
59 |
|
60 |
IWordDetector detector = new IWordDetector() { |
61 |
public boolean isWordPart(char c) { |
62 |
if (c == ':') { |
63 |
return false; |
64 |
} |
65 |
if (c == '-' ){ |
66 |
return true; |
67 |
} |
68 |
return Character.isJavaIdentifierPart(c); |
69 |
} |
70 |
public boolean isWordStart(char c) { |
71 |
return Character.isJavaIdentifierStart(c); |
72 |
} |
73 |
}; |
74 |
|
75 |
final Token keyword = new Token(new TextAttribute(colorRegistry.get(KEYWORD_COLOR), null, SWT.BOLD )); |
76 |
final Token key = new Token(new TextAttribute(colorRegistry.get(KEYWORD_COLOR), null, SWT.NONE)); |
77 |
|
78 |
WordRule rule = new WordRule(detector,key); |
79 |
|
80 |
// add tokens for each reserved word |
81 |
IJADDescriptorsProvider[] providers = JADAttributesRegistry.getAllJADDescriptorProviders(); |
82 |
for (int i = 0; i < providers.length; i++) { |
83 |
DescriptorPropertyDescription[] descriptions = providers[i].getDescriptorPropertyDescriptions(); |
84 |
for (int j = 0; j < descriptions.length; j++) { |
85 |
rule.addWord( descriptions[j].getPropertyName(), keyword); |
86 |
} |
87 |
} |
88 |
|
89 |
Token string = new Token(new TextAttribute(colorRegistry.get(VALUE_COLOR))); |
90 |
|
91 |
// Rule for Values |
92 |
SingleLineRule singleLineRule = new SingleLineRule(":", null, string, '\\'); //$NON-NLS-1$ |
93 |
// Add generic whitespace rule. |
94 |
WhitespaceRule whiteSpaceRule = new WhitespaceRule(new IWhitespaceDetector() { |
95 |
public boolean isWhitespace(char c) { |
96 |
return Character.isWhitespace(c); |
97 |
} |
98 |
}); |
99 |
|
100 |
setRules(new IRule[] { rule, singleLineRule, whiteSpaceRule }); |
101 |
} |
102 |
} |