下面列出了怎么用org.eclipse.jface.text.TextAttribute的API类实例代码及写法,或者点击链接到github查看源代码。
private void adaptToStyleChange(Token token, PropertyChangeEvent event, int styleAttribute) {
boolean eventValue = false;
Object value = event.getNewValue();
if (value instanceof Boolean)
eventValue = ((Boolean) value).booleanValue();
else if (IPreferenceStore.TRUE.equals(value))
eventValue = true;
Object data = token.getData();
if (data instanceof TextAttribute) {
TextAttribute oldAttr = (TextAttribute) data;
boolean activeValue = (oldAttr.getStyle() & styleAttribute) == styleAttribute;
if (activeValue != eventValue)
token.setData(new TextAttribute(oldAttr.getForeground(), oldAttr.getBackground(),
eventValue ? oldAttr.getStyle() | styleAttribute : oldAttr.getStyle() & ~styleAttribute));
}
}
@Override
public void addLink(IHyperlink link, int offset, int length) {
if (testOutputText == null) {
return;
}
StyleRangeWithCustomData range = new StyleRangeWithCustomData();
range.underline = true;
try {
range.underlineStyle = SWT.UNDERLINE_LINK;
} catch (Throwable e) {
//Ignore (not available on earlier versions of eclipse)
}
//Set the proper color if it's available.
TextAttribute textAttribute = ColorManager.getDefault().getHyperlinkTextAttribute();
if (textAttribute != null) {
range.foreground = textAttribute.getForeground();
} else {
range.foreground = JFaceColors.getHyperlinkText(Display.getDefault());
}
range.start = offset;
range.length = length + 1;
range.customData = link;
testOutputText.setStyleRange(range);
}
public XMLTextScanner( ColorManager colorManager) {
ESCAPED_CHAR = new Token(new TextAttribute(colorManager.getColor(IXMLColorConstants.ESCAPED_CHAR)));
CDATA_START = new Token(new TextAttribute(colorManager.getColor(IXMLColorConstants.CDATA)));
CDATA_END = new Token(new TextAttribute(colorManager.getColor(IXMLColorConstants.CDATA)));
CDATA_TEXT = new Token(new TextAttribute(colorManager.getColor(IXMLColorConstants.CDATA_TEXT)));
IRule[] rules = new IRule[2];
// Add rule to pick up escaped chars
// Add rule to pick up start of CDATA section
rules[0] = new CDataRule(CDATA_START, true);
// Add a rule to pick up end of CDATA sections
rules[1] = new CDataRule(CDATA_END, false);
setRules(rules);
}
@Override
public TextAttribute getMergedAttributes(String[] ids) {
if (ids.length < 2)
throw new IllegalStateException();
String mergedIds = getMergedIds(ids);
TextAttribute result = getAttribute(mergedIds);
if (result == null) {
for (String id : ids) {
result = merge(result, getAttribute(id));
}
if (result != null)
attributes.put(mergedIds, result);
else
attributes.remove(mergedIds);
}
return result;
}
private void handleTreeSelection() {
RootTreeItem root = getRootTreeItem();
if (root != null) {
List<HighlightingColorTreeItem> lst = new ArrayList<HighlightingColorTreeItem>();
getHLItemsFromRoot(root, lst);
fPreviewer.activateContent(root.syntaxColoring, lst);
fPreviewer.updateColors();
}
HighlightingColorTreeItem it = getHighlightingColorListItem();
fEnable.setEnabled(it != null && it.mayBeDisabled);
fSyntaxForegroundColorEditor.setEnabled(it != null);
fBoldCheckBox.setEnabled(it != null);
fItalicCheckBox.setEnabled(it != null);
fStrikethroughCheckBox.setEnabled(it != null);
fUnderlineCheckBox.setEnabled(it != null);
fEnable.setSelection(it == null || !it.isDisabled);
fSyntaxForegroundColorEditor.setColorValue(it == null ? new RGB(0x80, 0x80, 0x80) : it.rgb);
fBoldCheckBox.setSelection(it == null ? false : (it.style & SWT.BOLD) != 0);
fItalicCheckBox.setSelection(it == null ? false : (it.style & SWT.ITALIC) != 0);
fStrikethroughCheckBox.setSelection(it == null ? false : (it.style & TextAttribute.STRIKETHROUGH) != 0);
fUnderlineCheckBox.setSelection(it == null ? false : (it.style & TextAttribute.UNDERLINE) != 0);
}
/**
* Creates the ranges from parsing the code with the PyCodeScanner.
*
* @param textPresentation this is the container of the style ranges.
* @param scanner the scanner used to parse the document.
* @param doc document to parse.
* @param partitionOffset the offset of the document we should parse.
* @param partitionLen the length to be parsed.
*/
private void createDefaultRanges(TextPresentation textPresentation, PyCodeScanner scanner, Document doc,
int partitionOffset, int partitionLen) {
scanner.setRange(doc, partitionOffset, partitionLen);
IToken nextToken = scanner.nextToken();
while (!nextToken.isEOF()) {
Object data = nextToken.getData();
if (data instanceof TextAttribute) {
TextAttribute textAttribute = (TextAttribute) data;
int offset = scanner.getTokenOffset();
int len = scanner.getTokenLength();
Color foreground = textAttribute.getForeground();
Color background = textAttribute.getBackground();
int style = textAttribute.getStyle();
textPresentation.addStyleRange(new StyleRange(offset, len, foreground, background, style));
}
nextToken = scanner.nextToken();
}
}
public void createPresentation(TextPresentation presentation, ITypedRegion region) {
int start= region.getOffset();
int length= 0;
boolean firstToken= true;
TextAttribute attribute = getTokenTextAttribute(Token.UNDEFINED);
scanner.setRange(document,start,region.getLength());
while (true) {
IToken resultToken = scanner.nextToken();
if (resultToken.isEOF()) {
break;
}
if(resultToken.equals(Token.UNDEFINED)) {
continue;
}
if (!firstToken) {
addRange(presentation,start,length,attribute,true);
}
firstToken = false;
attribute = getTokenTextAttribute(resultToken);
start = scanner.getTokenOffset();
length = scanner.getTokenLength();
}
addRange(presentation,start,length,attribute,true);
}
public Color getForeground(String scope)
{
TextAttribute attr = getTextAttribute(scope);
if (attr == null)
{
return null;
}
return attr.getForeground();
}
private RuleBasedScanner getValueScanner(){
if (valueScanner == null) {
valueScanner = new HValueScanner();
valueScanner.setDefaultReturnToken(new Token(new TextAttribute(ResourceUtils.getColor(Styles.STRING))));
}
return valueScanner;
}
/**
* @return the color that should be used for errors.
*/
public Color getErrorColor() {
TextAttribute attribute = ColorManager.getDefault().getConsoleErrorTextAttribute();
if (attribute == null) {
return null;
}
Color errorColor = attribute.getForeground();
return errorColor;
}
protected XMLTextScanner getXMLTextScanner(){
if( textScanner == null) {
textScanner = new XMLTextScanner(colorManager);
textScanner.setDefaultReturnToken(new Token(new TextAttribute(
colorManager.getColor(IXMLColorConstants.DEFAULT))));
}
return textScanner;
}
protected CDataScanner getCDataScanner(){
if( cdataScanner == null) {
cdataScanner = new CDataScanner(colorManager);
cdataScanner.setDefaultReturnToken(new Token(new TextAttribute(
colorManager.getColor(IXMLColorConstants.CDATA_TEXT))));
}
return cdataScanner;
}
/**
* Adds style information to the given text presentation.
*
* @param presentation
* the text presentation to be extended
* @param offset
* the offset of the range to be styled
* @param length
* the length of the range to be styled
* @param attr
* the attribute describing the style of the range to be styled
*/
protected void addRange( TextPresentation presentation, int offset,
int length, TextAttribute attr )
{
if ( attr != null )
{
presentation.addStyleRange( new StyleRange( offset,
length,
attr.getForeground( ),
attr.getBackground( ),
attr.getStyle( ) ) );
}
}
/**
* Adds style information to the given text presentation.
*
* @param presentation the text presentation to be extended
* @param offset the offset of the range to be styled
* @param length the length of the range to be styled
* @param attr the attribute describing the style of the range to be styled
*/
protected void addRange(
TextPresentation presentation,
int offset,
int length,
TextAttribute attr) {
if (attr != null)
presentation.addStyleRange(
new StyleRange(
offset,
length,
attr.getForeground(),
attr.getBackground(),
attr.getStyle()));
}
private TextAttribute getTextAttribute(String name)
{
if (getCurrentTheme() != null)
{
return getCurrentTheme().getTextAttribute(name);
}
return new TextAttribute(ThemePlugin.getDefault().getColorManager().getColor(new RGB(255, 255, 255)));
}
ITokenScanner getCommentScanner() {
// lazy init
if (this.commentScanner == null) {
final Token comment = new Token(new TextAttribute(JFaceResources.getColorRegistry().get(COMMENT_COLOR)));
// no rules needed, because this will apply to comment partition
// only
final RuleBasedScanner ruleBasedScanner = new RuleBasedScanner();
// this will apply the syntax
ruleBasedScanner.setDefaultReturnToken(comment);
this.commentScanner = ruleBasedScanner;
}
return commentScanner;
}
@Inject
public TextAttributeProvider(IHighlightingConfiguration highlightingConfig,
IPreferenceStoreAccess preferenceStoreAccess, PreferenceStoreAccessor prefStoreAccessor) {
this.highlightingConfig = highlightingConfig;
this.preferencesAccessor = prefStoreAccessor;
this.attributes = new HashMap<String, TextAttribute>();
preferenceStoreAccess.getPreferenceStore().addPropertyChangeListener(this);
initialize();
}
protected TextAttribute createTextAttribute(String id, TextStyle defaultTextStyle) {
TextStyle textStyle = new TextStyle();
preferencesAccessor.populateTextStyle(id, textStyle, defaultTextStyle);
int style = textStyle.getStyle();
Font fontFromFontData = EditorUtils.fontFromFontData(textStyle.getFontData());
return new TextAttribute(EditorUtils.colorFromRGB(textStyle.getColor()), EditorUtils.colorFromRGB(textStyle
.getBackgroundColor()), style, fontFromFontData);
}
/**
* @return Returns a corresponding style range.
*/
public StyleRange createStyleRange() {
int len = getLength();
TextAttribute textAttribute = attribute;
int style = textAttribute.getStyle();
int fontStyle = style & (SWT.ITALIC | SWT.BOLD | SWT.NORMAL);
StyleRange styleRange = new StyleRange(getOffset(), len, textAttribute.getForeground(),
textAttribute.getBackground(), fontStyle);
styleRange.strikeout = (style & TextAttribute.STRIKETHROUGH) != 0;
styleRange.underline = (style & TextAttribute.UNDERLINE) != 0;
styleRange.font = textAttribute.getFont();
return styleRange;
}
/**
* Invalidate text presentation of positions with the given highlighting.
*
* @param highlighting
* The highlighting
*/
public void highlightingStyleChanged(TextAttribute highlighting) {
for (int i = 0, n = fPositions.size(); i < n; i++) {
AttributedPosition position = fPositions.get(i);
if (position.getHighlighting() == highlighting)
fSourceViewer.invalidateTextPresentation(position.getOffset(), position.getLength());
}
}
public CommentScanner() {
List<IRule> rules = new ArrayList<IRule>();
Token commentToken = new Token(new TextAttribute(new Color(
Display.getCurrent(), new RGB(63, 127, 95))));
rules.add(new EndOfLineRule("--", commentToken));
rules.add(new EndOfLineRule("//", commentToken));
rules.add(new MultiLineRule("/*", "*/", commentToken));
setRules(rules.toArray(new IRule[rules.size()]));
}
private void adaptToTextStyleChange(Highlighting highlighting, PropertyChangeEvent event, int styleAttribute) {
boolean eventValue= false;
Object value= event.getNewValue();
if (value instanceof Boolean)
eventValue= ((Boolean) value).booleanValue();
else if (IPreferenceStore.TRUE.equals(value))
eventValue= true;
TextAttribute oldAttr= highlighting.getTextAttribute();
boolean activeValue= (oldAttr.getStyle() & styleAttribute) == styleAttribute;
if (activeValue != eventValue)
highlighting.setTextAttribute(new TextAttribute(oldAttr.getForeground(), oldAttr.getBackground(), eventValue ? oldAttr.getStyle() | styleAttribute : oldAttr.getStyle() & ~styleAttribute));
}
/**
* Creates a BibTeX document scanner
*
* @param provider The color provider for syntax highlighting
*/
public BibCodeScanner(BibColorProvider provider) {
IToken keyword = new Token(new TextAttribute(provider
.getColor(BibColorProvider.KEYWORD)));
IToken comment = new Token(new TextAttribute(provider
.getColor(BibColorProvider.SINGLE_LINE_COMMENT)));
IToken other = new Token(new TextAttribute(provider
.getColor(BibColorProvider.DEFAULT)));
List rules = new ArrayList();
// Add rule for single line comments.
rules.add(new EndOfLineRule("%", comment));
rules.add(new BibCommandRule(keyword));
// Add generic whitespace rule.
rules.add(new WhitespaceRule(new WhitespaceDetector()));
// Add word rule for keywords, types, and constants.
WordRule wordRule = new WordRule(new BibWordDetector(), other);
rules.add(wordRule);
IRule[] result = new IRule[rules.size()];
rules.toArray(result);
setRules(result);
}
/**
* Defines a math partition scanner and sets the default
* color for it.
* @return a scanner to detect math partitions
*/
protected TexMathScanner getTeXMathScanner() {
if (mathScanner == null) {
mathScanner = new TexMathScanner(colorManager);
mathScanner.setDefaultReturnToken(
new Token(
new TextAttribute(
colorManager.getColor(ColorManager.EQUATION),
null,
colorManager.getStyle(ColorManager.EQUATION_STYLE))));
}
return mathScanner;
}
public void adaptToPreferenceChange(PropertyChangeEvent event) {
String p = event.getProperty();
int index = indexOf(p);
Token token = getToken(fPropertyNamesColor[index]);
if (fPropertyNamesColor[index].equals(p))
adaptToColorChange(token, event);
else if (fPropertyNamesBold[index].equals(p))
adaptToStyleChange(token, event, SWT.BOLD);
else if (fPropertyNamesItalic[index].equals(p))
adaptToStyleChange(token, event, SWT.ITALIC);
else if (fPropertyNamesStrikethrough[index].equals(p))
adaptToStyleChange(token, event, TextAttribute.STRIKETHROUGH);
else if (fPropertyNamesUnderline[index].equals(p))
adaptToStyleChange(token, event, TextAttribute.UNDERLINE);
}
/**
* Defines an optional argument (square bracket) scanner and sets the default color for it
* @return a scanner to detect argument partitions
*/
protected TexOptArgScanner getTexOptArgScanner() {
if (optArgumentScanner == null) {
optArgumentScanner = new TexOptArgScanner(colorManager);
optArgumentScanner.setDefaultReturnToken(
new Token(
new TextAttribute(
colorManager.getColor(ColorManager.SQUARE_BRACKETS),
null,
colorManager.getStyle(ColorManager.SQUARE_BRACKETS_STYLE))));
}
return optArgumentScanner;
}
public StringScanner() {
List<IRule> rules = new ArrayList<IRule>();
Token stringToken = new Token(new TextAttribute(new Color(
Display.getCurrent(), new RGB(42, 0, 255))));
rules.add(new SingleLineRule("'", "'", stringToken));
rules.add(new SingleLineRule("\"", "\"", stringToken));
setRules(rules.toArray(new IRule[rules.size()]));
}
public void adaptToPreferenceChange(PropertyChangeEvent event) {
String p= event.getProperty();
int index= indexOf(p);
Token token= getToken(fPropertyNamesColor[index]);
if (fPropertyNamesColor[index].equals(p))
adaptToColorChange(token, event);
else if (fPropertyNamesBold[index].equals(p))
adaptToStyleChange(token, event, SWT.BOLD);
else if (fPropertyNamesItalic[index].equals(p))
adaptToStyleChange(token, event, SWT.ITALIC);
else if (fPropertyNamesStrikethrough[index].equals(p))
adaptToStyleChange(token, event, TextAttribute.STRIKETHROUGH);
else if (fPropertyNamesUnderline[index].equals(p))
adaptToStyleChange(token, event, TextAttribute.UNDERLINE);
}
public InstructionsRuleScanner(ColorProvider provider) {
IToken stringToken = new Token(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PreferenceConstants.P_STRING_COLOR))));
IToken commentToken = new Token(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PreferenceConstants.P_COMMENT_COLOR))));
IToken instructToken = new Token(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PreferenceConstants.P_INSTRUCT_COLOR)), null, Font.ITALIC));
IToken definitionsToken = new Token(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PreferenceConstants.P_DEF_COLOR)), null, Font.ITALIC));
List<IRule> rules = Lists.newArrayList();
// rule for Strings - may spanning multiple lines
rules.add(new MultiLineRule("\"", "\"", stringToken));
rules.add(new MultiLineRule("\'", "\'", stringToken));
rules.add(new EndOfLineRule("#%", instructToken));
// rule for comments - ended by a line delimiter
rules.add(new EndOfLineRule("//", commentToken));
WordRule wordRule = RuleFactory.buildRule(ImpexRules.KEYWORD, null);
// rule for instructions
for (String word : Formatter.INSTRUCTION_CLASS_PROPOSALS) {
wordRule.addWord(word, instructToken);
}
rules.add(wordRule);
// rule for definitions
wordRule = RuleFactory.buildRule(ImpexRules.VARIABLE, definitionsToken);
rules.add(wordRule);
IRule[] ruleArray = new IRule[rules.size()];
setRules(rules.toArray(ruleArray));
}
public Color getBackground(String scope)
{
TextAttribute attr = getTextAttribute(scope);
if (attr == null)
{
return null;
}
return attr.getBackground();
}