KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > text > ArgumentRule


1 /*******************************************************************************
2  * Copyright (c) 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 package org.eclipse.pde.internal.ui.editor.text;
12
13 import org.eclipse.jface.text.rules.ICharacterScanner;
14 import org.eclipse.jface.text.rules.IToken;
15 import org.eclipse.jface.text.rules.IWordDetector;
16 import org.eclipse.jface.text.rules.WordPatternRule;
17
18 public class ArgumentRule extends WordPatternRule {
19
20
21     private static class ArgumentDetector implements IWordDetector {
22
23         /*
24          * @see IWordDetector#isWordStart
25          */

26         public boolean isWordStart(char c) {
27             return '{' == c;
28         }
29
30         /*
31          * @see IWordDetector#isWordPart
32          */

33         public boolean isWordPart(char c) {
34             return c == '}' || Character.isDigit(c);
35         }
36     }
37
38
39     private int fCount= 0;
40
41     /**
42      * Creates an argument rule for the given <code>token</code>.
43      *
44      * @param token the token to be returned on success
45      */

46     public ArgumentRule(IToken token) {
47         super(new ArgumentDetector(), "{", "}", token); //$NON-NLS-1$//$NON-NLS-2$
48
}
49
50     /*
51      * @see org.eclipse.jface.text.rules.WordPatternRule#endSequenceDetected(org.eclipse.jface.text.rules.ICharacterScanner)
52      */

53     protected boolean endSequenceDetected(ICharacterScanner scanner) {
54         fCount++;
55
56         if (scanner.read() == '}')
57             return fCount > 2;
58
59         scanner.unread();
60         return super.endSequenceDetected(scanner);
61     }
62
63     /*
64      * @see org.eclipse.jface.text.rules.PatternRule#sequenceDetected(org.eclipse.jface.text.rules.ICharacterScanner, char[], boolean)
65      */

66     protected boolean sequenceDetected(ICharacterScanner scanner, char[] sequence, boolean eofAllowed) {
67         fCount= 0;
68         return super.sequenceDetected(scanner, sequence, eofAllowed);
69     }
70 }
71
Popular Tags