3, editor grammatical color function
(1) Basic concept
l First is to define various partitions (text not overlapping in the document), which is used to distinguish between the title.
l Each character in the document must belong to a certain partition or default partition (iDocument.default_content_type)
l The comment should belong to a separate partition
l Since log4j.properties In addition to comments in addition to comments, there are many Name = Value form, so divided into three partitions.
Ø Note (starting with #
Ø Value value (including =)
Ø Default Partition (including Name)
l Need to provide a partition scanner for TextEditor calls to determine which partition belongs to which partition
L also needs to provide the Token Scanner for TextEditor call
l Token is the minimum text unit used to set colors
l Every partition requires a unique token scanner
(2) Partition scanner
l Partition scanner parses the parsing rule (definition partition) to divide the text area into different partition
l This example uses the rule-based scanner provided by JFACE, so you need to add org.eclipse.jface.text plugins in the DepENDENCIES page of the Plugin.xml list editor
Package org.xqtu.log4j.editor;
Import org.eclipse.jface.text.idocument;
Import org.eclipse.jface.text.rules.iPredicaterule;
Import org.eclipse.jface.text.rules.rulebasedPartitionscanner;
Import org.eclipse.jface.text.rules.singelinerule;
Import org.eclipse.jface.text.rules.token;
Public Class PropertiesPartitionscanner Extends RulebasedPartitionscanner {
Public final static string log4j_comment = "__log4j_comment";
Public final static string log4j_value = "__log4j_value";
Public propertyTIESPARTITIONSCANNER () {
Super ();
Token CommentPartition = new token (log4j_comment);
Token valuePartition = new token (log4j_value);
SingleLineRule Commentrulect = New SingleLineRule ("#", NULL,
CommentPartition, (char) 0, true);
Commentrulect.SetColumnConstraint (0);
SingleLineRule Valyerule = New SingleLineRule ("=", NULL,
ValuePartition, (char) 0, true); setpredicaterules (new ipredicaterules (new ipredicaterule ";
}
Public static string [] getlegalcontenttypes () {
Return new string [] {iDocument.default_content_type,
PropertiesPartitionscanner.log4j_comment,
PropertiesPartitionscanner.log4j_value};
}
}
l This is extended here, and the partition rules provided by reading text, app
l Defining the partition type constant at the beginning
l Defining each partition type in the construction method (herein includes the default partition type)
l First, create each partition's own token object.
l Define the Partition rules using SingLelineRule, SingLELINERULE includes four parameters:
Ø Start sequence matching mode
Ø End sequence matching mode
Ø Escape character
Ø Set to True means that it is terminated when the file is reached
l SetColumnConstRaint () Method Sets the column limit for the Partition rule: Return to match the rule of the rule starting from the specified column number.
l setpredicaterules () method tells the Partition rules defined by the scanner
l The last GetlegalContentTypes () method returns the Partition type supported by the partition scanner
(3) TOKEN Manager
l Color (specific instance of the SWT Color class) is a limited system resource, you need to track them, manage them: not allocate, release them after running
l Token Manager is used to track and manage token and colors, the main purpose is to support the ability to allow users to set colors in Preferences.
Package org.xqtu.log4j.editor;
Import java.util.hashmap;
Import java.util.iterator;
Import java.util.map;
Import org.eclipse.jface.preference.ipReferenceStore;
Import org.eclipse.jface.resource.stringconverter;
Import org.eclipse.jface.text.texttattribute;
Import org.eclipse.jface.text.rules.iToken;
Import org.eclipse.jface.text.rules.token;
Import org.eclipse.jface.util.propertyChangeEvent;
Import org.eclipse.swt.graphics.color;
Import org.eclipse.swt.graphics.rgb;
Import org.eclipse.swt.widgets.display;
Public class tokenManager {
Private map colorable = new hashmap (10);
Private map tokenable = new hashmap (10); private final ipreferenceore preferencestore;
Public tokenManager (iPreferenceore Preferenceore) {
This.Preferenceore = Preferenceore;
}
Public Itoken gettoken (String prefkey) {
Token token = (token) tokenTable.get (prefKey);
IF (token == NULL) {
String colorName = preferencestore.getstring (prefKey);
RGB RGB = StringConverter.ASRGB (ColorName);
Token = new token (New TextAttribute (GetColor (RGB)));
TokenTable.Put (PrefKey, Token);
}
Return token;
}
Public void dispose () {
Iterator E = ColORTABLE.VALUES (). Iterator ();
While (E.hasNext ()) {
(Color) E.NEXT ()). Dispose ();
}
}
Private Color getColor (RGB RGB) {
Color Color = (color) ColorTable.get (RGB);
IF (color == NULL) {
Color = New color (), RGB);
ColORTABLE.PUT (RGB, Color);
}
Return Color;
}
Public Boolean AffectStextPresentation (PropertyChangeEvent Event) {
Token token = (token) tokenTable.get (Event.getProperty ());
Return (Token! = NULL);
}
Public Void HandlepreferenceRenderChanged (PropertyChangeEvent Event) {
String prefKey = event.getproperty ();
Token token = (token) tokenTable.get (prefKey);
IF (Token! = NULL) {
String colorName = preferencestore.getstring (prefKey);
RGB RGB = StringConverter.ASRGB (ColorName);
Token.SetData (New TextAttribute (GetColor (RGB));
}
}
}
l Started part defines the HashMap list for managing token and colors.
l gettoken () method Get the specified token: First look up in the token list, if not, create a new Token based on the color obtained in Preferences, and save it to the token list
l In Creating token, the getColor () method is called, get the specified color, the method is similar: first in the color list, if you do not assign a new color, save to the colors list
l TextAttribute object is used to describe the properties of the text. This example only specifies the foreground color, or specifies the background color or model, such as a bold, bertlast, etc. L Dispose () method used to release all color resources.
l Typically, the editor monitors the changes of preferences by registering the way to call when changing.
Ø AffectStextPresentation () method determines whether the changes in the properties in Preferencees need to be applied to text in the editor, and the usual method is only the attribute is the name of a token in the token list.
Ø HandlePReferenceStoreChanged () method Apply attribute changes to Toke;
(4) Note Token Scanner
l Every partition requires a corresponding token scanner
Package org.xqtu.log4j.editor.scanners;
Import org.eclipse.jface.text.rules.iToken;
Import org.eclipse.jface.text.rules.rulebasedscanner;
Import org.xqtu.log4j.log4jplugin;
Import org.xqtu.log4j.editor.tokenManager;
Public class commentscanner extends rulebasedscanner {
Public CommentScanner (TokenManager tokenManager) {
IToken CommentToken = TokenManager
.gettoken (log4jplugin.pref_comment_color);
SetDefaultreturntoken (CommentToken);
}
}
l All token scanners are extended RulebaseDscanner
l Get the specified TOKEN using the gettoken () method of TokenManager.
l Token is assigned to each part of the text in the partition
l Since anything in partition is comment, no rules are required, as long as the returned default token
(5) Default Token Scanner
Package org.xqtu.log4j.editor.scanners;
Import org.eclipse.jface.text.rules.irule;
Import org.eclipse.jface.text.rules.iToken;
Import org.eclipse.jface.text.rules.rulebasedscanner;
Import org.eclipse.jface.text.rules.whitespaceer;
Import org.xqtu.log4j.log4jplugin;
Import org.xqtu.log4j.editor.tokenManager;
Public class defaultscanner extends rulebasedscanner {
Public defaultscanner (TokenManager tokenManager) {
IToken propertyTytoken = tokenManager
.gettoken (log4jplugin.pref_property_color);
SetDefaultReturntoken (PropertyToken); SetRules (New Irule [] {New WhitespaceDeutor ())};
}
}
l The default partition is considered to be a property name except for the blank character, so you need to call the setRules () method to set the rules of blank characters.
l Detector WhitespaceDetector implements the IWHITESPACEDETECTOR interface, determines whether the characters in the current content are blank characters.
Package org.xqtu.log4j.editor.scanners;
Import org.eclipse.jface.text.rules.iWhitespaceDetector;
Public class whitespaceDetector IMPLEMENTS IWHITESPACEDETECTOR {
Public Boolean IsWhitespace (CHAR C) {
Return Character.isWhitespace (C);
}
}
(6) Value Value token Scanner
l Value Value The Token Scanner is relatively complex because you need to handle keywords and like% M,% D {HH: mm: ss a}.
Package org.xqtu.log4j.editor.scanners;
Import org.eclipse.jface.text.rules.irule;
Import org.eclipse.jface.text.rules.iToken;
Import org.eclipse.jface.text.rules.rulebasedscanner;
Import org.eclipse.jface.text.rules.singelinerule;
Import org.eclipse.jface.text.rules.whitespaceer;
Import org.eclipse.jface.text.rules.WordRule;
Import org.xqtu.log4j.log4jplugin;
Import org.xqtu.log4j.editor.tokenManager;
Public Class Valuescanner Extends rulebasedscanner {
String [] Keywords = {"all", "debug", "error", "fat", "info", "OFF",
"Warn", "Inherited", "Inherit", "NULL", "True", "false",}
Public valueScanner (TokenManager tokenManager) {
IToken DefaultToken = TokenManager
.gettoken (log4jplugin.pref_default_color);
IToken FormatToken = TokenManager
.gettoken (log4jplugin.pref_format_color);
IToken KeywordToken = tokenManager
.gettoken (log4jplugin.pref_keyword_color);
Irule Bracer = New SingleLineRule ("{", "}", formattleken, (char) 0,
True);
Wordrule KeywordRule = New WordRule (New WordDetector ()); for (int i = 0; i KeywordRule.Addword (Keywords [i], keywordtoken; } Irule formalecrule = new formaTrule (FormatToken); Irule Whitespaceerule = New WhitespaceDetector ()); SetDefaultReturntoken (DefaultToken); SetRules (new irule [] {BraceRule, FormaTrule, KeywordRule, Whitespaceerule,}); } } l Value Value Token Scanner Processing includes keywords, formatting, and default three token l Defining a list of keywords in the start section, use WordRule to detect all keyword token, and apply the WordDetector detector l WordDetector probe implements iWordDetector interface, decided to start with letters, including letters or numbers for Word Package org.xqtu.log4j.editor.scanners; Import org.eclipse.jface.text.rules.iWordDetector; Public Class WordDetector IMPLEments IWORDDDETOR { Public Boolean IswordStart (Char C) { Return Character.isletter (C); } Public Boolean IswordPart (Char C) { Return Character.isletterOrdigit (C); } } The content in l {} is as a format token, using SingLeLineRule to define the Token rule l The third rule is a custom formatting token rule, telling later Like, you need to define blank character rules to match blank characters. l Other text returns as the default token l Setting the application application token rules (7) Customize token rules l Since the Eclipse platform does not provide rules that provide log4J format, you need to customize rules. Package org.xqtu.log4j.editor.scanners; Import org.eclipse.jface.text.rules.icharacterscanner; Import org.eclipse.jface.text.rules.irule; Import org.eclipse.jface.text.rules.iToken; Import org.eclipse.jface.text.rules.token; Public Class FormaTrule Implements Irule { PRIVATE FINAL ITOKEN TOKEN Public formatrule (Itoken token) { THIS.TOKEN = Tokeen; } Public Itoken Evaluate (iCharacterscanner Scanner) { INT C = scanner.read (); IF (c == '%') { Do { C = scanner.read (); WHILE (C! = iCharacterscanner.eof && (Character.isletterOrdigit (CHAR) C) || c == '-' || c == '.'); Scanner.unread (); Return token; } Scanner.unread (); Return token.undefined; } } l Custom rules implement the IRULE interface, use the evata () method to match the rules one by one character. l In the construction method, save the Token to match first. l At the evata () method, read characters one by one using iCharacterscanner, regular matching l If you start with% to format Token, read the contents containing letters, numbers, "-", and ".", return as token l Otherwise, return token.undefined, indicating that the Token does not match, so that the Token Scanner makes the next rule match L, the unread () method of calling the iCharactersCanner rewinds the read operation without matching content.