KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > propertiesfileeditor > PropertyValueScanner


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.jdt.internal.ui.propertiesfileeditor;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.jface.preference.IPreferenceStore;
18
19 import org.eclipse.jface.text.BadLocationException;
20 import org.eclipse.jface.text.IRegion;
21 import org.eclipse.jface.text.rules.IToken;
22 import org.eclipse.jface.text.rules.IWordDetector;
23 import org.eclipse.jface.text.rules.WhitespaceRule;
24 import org.eclipse.jface.text.rules.WordRule;
25
26 import org.eclipse.jdt.ui.PreferenceConstants;
27 import org.eclipse.jdt.ui.text.IColorManager;
28
29 import org.eclipse.jdt.internal.ui.text.AbstractJavaScanner;
30 import org.eclipse.jdt.internal.ui.text.JavaWhitespaceDetector;
31
32
33 /**
34  * A rule based property value scanner.
35  *
36  * @since 3.1
37  */

38 public final class PropertyValueScanner extends AbstractJavaScanner {
39
40     public class AssignmentDetector implements IWordDetector {
41
42         /*
43          * @see IWordDetector#isWordStart
44          */

45         public boolean isWordStart(char c) {
46             if ('=' != c && ':' != c || fDocument == null)
47                 return false;
48
49             try {
50                 // check whether it is the first '='
51
IRegion lineInfo= fDocument.getLineInformationOfOffset(fOffset);
52                 int offset= lineInfo.getOffset();
53                 String JavaDoc line= fDocument.get(offset, lineInfo.getLength());
54                 int i= line.indexOf(c);
55                 return i != -1 && i + lineInfo.getOffset() + 1 == fOffset;
56             } catch (BadLocationException ex) {
57                 return false;
58             }
59         }
60
61         /*
62          * @see IWordDetector#isWordPart
63          */

64         public boolean isWordPart(char c) {
65             return false;
66         }
67     }
68
69
70     private static String JavaDoc[] fgTokenProperties= {
71         PreferenceConstants.PROPERTIES_FILE_COLORING_VALUE,
72         PreferenceConstants.PROPERTIES_FILE_COLORING_ARGUMENT,
73         PreferenceConstants.PROPERTIES_FILE_COLORING_ASSIGNMENT
74     };
75
76
77     /**
78      * Creates a property value code scanner
79      *
80      * @param manager the color manager
81      * @param store the preference store
82      */

83     public PropertyValueScanner(IColorManager manager, IPreferenceStore store) {
84         super(manager, store);
85         initialize();
86     }
87
88     /*
89      * @see org.eclipse.jdt.internal.ui.text.AbstractJavaScanner#getTokenProperties()
90      */

91     protected String JavaDoc[] getTokenProperties() {
92         return fgTokenProperties;
93     }
94
95     /*
96      * @see org.eclipse.jdt.internal.ui.text.AbstractJavaScanner#createRules()
97      */

98     protected List JavaDoc createRules() {
99         setDefaultReturnToken(getToken(PreferenceConstants.PROPERTIES_FILE_COLORING_VALUE));
100         List JavaDoc rules= new ArrayList JavaDoc();
101
102         // Add rule for arguments.
103
IToken token= getToken(PreferenceConstants.PROPERTIES_FILE_COLORING_ARGUMENT);
104         rules.add(new ArgumentRule(token));
105
106         // Add word rule for assignment operator.
107
token= getToken(PreferenceConstants.PROPERTIES_FILE_COLORING_ASSIGNMENT);
108         WordRule wordRule= new WordRule(new AssignmentDetector(), token);
109         rules.add(wordRule);
110
111         // Add generic whitespace rule.
112
rules.add(new WhitespaceRule(new JavaWhitespaceDetector()));
113
114         return rules;
115     }
116 }
117
Popular Tags