Java Code Examples for org.eclipse.jface.text.TextAttribute#getForeground()

The following examples show how to use org.eclipse.jface.text.TextAttribute#getForeground() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TextAttributeProvider.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
private TextAttribute merge(TextAttribute first, TextAttribute second) {
	if (first == null)
		return second;
	if (second == null)
		return first;
	int style = first.getStyle() | second.getStyle();
	Color fgColor = second.getForeground();
	if (fgColor == null)
		fgColor = first.getForeground();
	Color bgColor = second.getBackground();
	if (bgColor == null)
		bgColor = first.getBackground();
	Font font = second.getFont();
	if (font == null)
		font = first.getFont();
	return new TextAttribute(fgColor, bgColor, style, font);
}
 
Example 2
Source File: PyUnitView.java    From Pydev with Eclipse Public License 1.0 6 votes vote down vote up
@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);
}
 
Example 3
Source File: StyledTextForShowingCodeFactory.java    From Pydev with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * 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();
    }
}
 
Example 4
Source File: NonRuleBasedDamagerRepairer.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * 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 )
	{
		int style= attr.getStyle();
		int fontStyle= style & (SWT.ITALIC | SWT.BOLD | SWT.NORMAL);
		StyleRange range = new StyleRange( offset,
				length,
				attr.getForeground( ),
				attr.getBackground( ),
				fontStyle );
		range.strikeout = ( attr.getStyle( ) & TextAttribute.STRIKETHROUGH ) != 0;
		range.underline = ( attr.getStyle( ) & TextAttribute.UNDERLINE ) != 0;
		range.font= attr.getFont();
		presentation.addStyleRange( range );
	}
}
 
Example 5
Source File: DamageRepairer.java    From LogViewer with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * 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
 * @param wholeLine the boolean switch to declare that the whole line should be colored
 */
private void addRange(TextPresentation presentation, int offset, int length, TextAttribute attr, boolean wholeLine) {
    if (attr != null) {
        int style= attr.getStyle();
        int fontStyle= style & (SWT.ITALIC | SWT.BOLD | SWT.NORMAL);
        if(wholeLine) {
            try {
                int line = document.getLineOfOffset(offset);
                int start = document.getLineOffset(line);
                length = document.getLineLength(line);
                offset = start;
            } catch (BadLocationException e) {
            }
        }
        StyleRange styleRange = new StyleRange(offset,length,attr.getForeground(),attr.getBackground(),fontStyle);
        styleRange.strikeout = (style & TextAttribute.STRIKETHROUGH) != 0;
        styleRange.underline = (style & TextAttribute.UNDERLINE) != 0;
        presentation.addStyleRange(styleRange);
    }
}
 
Example 6
Source File: StyleRanges.java    From statecharts with Eclipse Public License 1.0 6 votes vote down vote up
public List<StyleRange> getRanges(String expression) {
	final List<StyleRange> ranges = Lists.newArrayList();
	DocumentEvent event = new DocumentEvent();
	event.fDocument = new DummyDocument(expression);
	DocumentTokenSource tokenSource = tokenSourceProvider.get();
	tokenSource.updateStructure(event);
	Iterator<ILexerTokenRegion> iterator = tokenSource.getTokenInfos().iterator();
	while (iterator.hasNext()) {
		ILexerTokenRegion next = iterator.next();
		TextAttribute attribute = attributeProvider.getAttribute(tokenTypeMapper.getId(next.getLexerTokenType()));
		StyleRange range = new StyleRange(next.getOffset(), next.getLength(), attribute.getForeground(),
				attribute.getBackground());
		range.font = attribute.getFont();
		range.fontStyle = attribute.getStyle();
		ranges.add(range);
	}
	return merge(ranges);
}
 
Example 7
Source File: Theme.java    From APICloud-Studio with GNU General Public License v3.0 5 votes vote down vote up
public Color getForeground(String scope)
{
	TextAttribute attr = getTextAttribute(scope);
	if (attr == null)
	{
		return null;
	}
	return attr.getForeground();
}
 
Example 8
Source File: ThemeingDamagerRepairer.java    From APICloud-Studio with GNU General Public License v3.0 5 votes vote down vote up
private boolean matchesDefaults(TextAttribute attr)
{
	if (attr == null)
	{
		return false;
	}

	// Make sure font is just normal
	int style = attr.getStyle();
	int fontStyle = style & (SWT.ITALIC | SWT.BOLD | SWT.NORMAL);
	if (fontStyle != SWT.NORMAL)
	{
		return false;
	}
	if ((style & TextAttribute.STRIKETHROUGH) != 0)
	{
		return false;
	}
	if ((style & TextAttribute.UNDERLINE) != 0)
	{
		return false;
	}

	// Is FG different?
	Color fg = attr.getForeground();
	if (fg != null && !fg.getRGB().equals(getCurrentTheme().getForeground()))
	{
		return false;
	}

	// Is BG different?
	Color bg = attr.getBackground();
	if (bg != null && !bg.getRGB().equals(getCurrentTheme().getBackground()))
	{
		return false;
	}
	return true;
}
 
Example 9
Source File: SemanticHighlightingManager.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @return Returns a corresponding style range.
 */
public StyleRange createStyleRange() {
	int len= 0;
	if (fStyle.isEnabled())
		len= getLength();

	TextAttribute textAttribute= fStyle.getTextAttribute();
	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;

	return styleRange;
}
 
Example 10
Source File: PyDefaultDamagerRepairer.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * 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) {
        int style = attr.getStyle();
        int fontStyle = style & (SWT.ITALIC | SWT.BOLD | SWT.NORMAL);
        StyleRange styleRange = new StyleRange(offset, length, attr.getForeground(), attr.getBackground(),
                fontStyle);
        styleRange.strikeout = (style & TextAttribute.STRIKETHROUGH) != 0;
        styleRange.underline = (style & TextAttribute.UNDERLINE) != 0;
        styleRange.font = attr.getFont();
        presentation.addStyleRange(styleRange);
    }
}
 
Example 11
Source File: AttributedPosition.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @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;
}
 
Example 12
Source File: PyUnitView.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @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;
}
 
Example 13
Source File: PyUnitView.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @return the color that should be used for the foreground.
 */
public Color getForegroundColor() {
    TextAttribute attribute = ColorManager.getDefault().getForegroundTextAttribute();
    if (attribute == null) {
        return null;
    }
    Color errorColor = attribute.getForeground();
    return errorColor;
}
 
Example 14
Source File: ExpressionSyntaxColoringPage.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
private void activate( String namedStyle )
{
	Color foreground = fDefaultForeground;
	Color background = fDefaultBackground;
	if ( namedStyle == null )
	{
		fClearStyle.setEnabled( false );
		fBold.setEnabled( false );
		fItalic.setEnabled( false );
		fStrike.setEnabled( false );
		fUnderline.setEnabled( false );
		fForegroundLabel.setEnabled( false );
		fBackgroundLabel.setEnabled( false );
		fForegroundColorEditor.setEnabled( false );
		fBackgroundColorEditor.setEnabled( false );
		fBold.setSelection( false );
		fItalic.setSelection( false );
		fStrike.setSelection( false );
		fUnderline.setSelection( false );
	}
	else
	{
		TextAttribute attribute = getAttributeFor( namedStyle );
		fClearStyle.setEnabled( true );
		fBold.setEnabled( true );
		fItalic.setEnabled( true );
		fStrike.setEnabled( true );
		fUnderline.setEnabled( true );
		fForegroundLabel.setEnabled( true );
		fBackgroundLabel.setEnabled( true );
		fForegroundColorEditor.setEnabled( true );
		fBackgroundColorEditor.setEnabled( true );
		fBold.setSelection( ( attribute.getStyle( ) & SWT.BOLD ) != 0 );
		fItalic.setSelection( ( attribute.getStyle( ) & SWT.ITALIC ) != 0 );
		fStrike.setSelection( ( attribute.getStyle( ) & TextAttribute.STRIKETHROUGH ) != 0 );
		fUnderline.setSelection( ( attribute.getStyle( ) & TextAttribute.UNDERLINE ) != 0 );
		if ( attribute.getForeground( ) != null )
		{
			foreground = attribute.getForeground( );
		}
		if ( attribute.getBackground( ) != null )
		{
			background = attribute.getBackground( );
		}
	}

	fForegroundColorEditor.setColorValue( foreground.getRGB( ) );
	fBackgroundColorEditor.setColorValue( background.getRGB( ) );
}
 
Example 15
Source File: ExpressionSyntaxColoringPage.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Color the text in the sample area according to the current preferences
 */
void applyStyles( )
{
	if ( fText == null || fText.isDisposed( ) )
		return;

	try
	{
		ITypedRegion[] regions = TextUtilities.computePartitioning( fDocument,
				IDocumentExtension3.DEFAULT_PARTITIONING,
				0,
				fDocument.getLength( ),
				true );
		if ( regions.length > 0 )
		{
			for ( int i = 0; i < regions.length; i++ )
			{
				ITypedRegion region = regions[i];
				String namedStyle = (String) fContextToStyleMap.get( region.getType( ) );
				if ( namedStyle == null )
					continue;
				TextAttribute attribute = getAttributeFor( namedStyle );
				if ( attribute == null )
					continue;
				int fontStyle = attribute.getStyle( )
						& ( SWT.ITALIC | SWT.BOLD | SWT.NORMAL );
				StyleRange style = new StyleRange( region.getOffset( ),
						region.getLength( ),
						attribute.getForeground( ),
						attribute.getBackground( ),
						fontStyle );
				style.strikeout = ( attribute.getStyle( ) & TextAttribute.STRIKETHROUGH ) != 0;
				style.underline = ( attribute.getStyle( ) & TextAttribute.UNDERLINE ) != 0;
				style.font = attribute.getFont( );
				fText.setStyleRange( style );
			}
		}
	}
	catch ( BadLocationException e )
	{
		ExceptionHandler.handle( e );
	}
}
 
Example 16
Source File: ConsoleStyleProvider.java    From Pydev with Eclipse Public License 1.0 4 votes vote down vote up
private ScriptStyleRange getIt(String content, int offset, TextAttribute attr, int scriptStyle) {
    //background is the default (already set)
    Color background = attr.getBackground();
    return new ScriptStyleRange(offset, content.length(), attr.getForeground(), background, scriptStyle,
            attr.getStyle());
}
 
Example 17
Source File: PyUnitView.java    From Pydev with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Called when a test is selected in the tree (shows its results in the text output text component).
 * Makes the line tracker aware of the changes so that links are properly created.
 */
/*default*/void onSelectResult(PyUnitTestResult result) {
    tempOnSelectResult.clear();

    boolean addedErrors = false;
    if (result != null) {
        if (result.errorContents != null && result.errorContents.length() > 0) {
            addedErrors = true;
            tempOnSelectResult.append(ERRORS_HEADER);
            tempOnSelectResult.append(result.errorContents);
        }

        if (result.capturedOutput != null && result.capturedOutput.length() > 0) {
            if (tempOnSelectResult.length() > 0) {
                tempOnSelectResult.append("\n");
            }
            tempOnSelectResult.append(CAPTURED_OUTPUT_HEADER);
            tempOnSelectResult.append(result.capturedOutput);
        }
    }
    String string = tempOnSelectResult.toString();
    testOutputText.setFont(JFaceResources.getFont(IDebugUIConstants.PREF_CONSOLE_FONT));

    testOutputText.setText(string + "\n\n\n"); // Add a few lines to the test output so that we can scroll a bit more.
    testOutputText.setStyleRange(new StyleRange());

    if (addedErrors) {
        StyleRange range = new StyleRange();
        //Set the proper color if it's available.
        TextAttribute errorTextAttribute = ColorManager.getDefault().getConsoleErrorTextAttribute();
        if (errorTextAttribute != null) {
            range.foreground = errorTextAttribute.getForeground();
        }
        range.start = ERRORS_HEADER.length();
        range.length = result.errorContents.length();
        testOutputText.setStyleRange(range);
    }

    PythonConsoleLineTracker lineTracker = new PythonConsoleLineTracker();

    ILaunchConfiguration launchConfiguration = null;
    PyUnitTestRun testRun = result.getTestRun();
    if (testRun != null) {
        IPyUnitLaunch pyUnitLaunch = testRun.getPyUnitLaunch();
        if (pyUnitLaunch != null) {
            launchConfiguration = pyUnitLaunch.getLaunchConfiguration();
        }
    }
    lineTracker.init(launchConfiguration, iLinkContainer);
    lineTracker.setOnlyCreateLinksForExistingFiles(this.onlyCreateLinksForExistingFiles);
    lineTracker.splitInLinesAndAppendToLineTracker(string);
}
 
Example 18
Source File: AbstractSyntaxColoringTest.java    From dsl-devkit with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * Creates a {@link StyleRange} from the given parameters.
 *
 * @param offset
 *          the offset
 * @param length
 *          the length of the range
 * @param textAttribute
 *          the {@link TextAttribute}
 * @return a {@link StyleRange} from the given parameters
 */
public static StyleRange createStyleRange(final int offset, final int length, final TextAttribute textAttribute) {
  int style = textAttribute.getStyle();
  int fontStyle = style & (SWT.ITALIC | SWT.BOLD | SWT.NORMAL);
  StyleRange styleRange = new StyleRange(offset, length, textAttribute.getForeground(), textAttribute.getBackground(), fontStyle);
  styleRange.strikeout = (style & TextAttribute.STRIKETHROUGH) != 0;
  styleRange.underline = (style & TextAttribute.UNDERLINE) != 0;
  styleRange.font = textAttribute.getFont();
  return styleRange;
}
 
Example 19
Source File: TMPresentationReconciler.java    From tm4e with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * 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
 * @param lastLineStyleRanges
 */
protected void addRange(TextPresentation presentation, int offset, int length, TextAttribute attr) {
	if (attr != null) {
		int style = attr.getStyle();
		int fontStyle = style & (SWT.ITALIC | SWT.BOLD | SWT.NORMAL);
		StyleRange styleRange = new StyleRange(offset, length, attr.getForeground(), attr.getBackground(),
				fontStyle);
		styleRange.strikeout = (style & TextAttribute.STRIKETHROUGH) != 0;
		styleRange.underline = (style & TextAttribute.UNDERLINE) != 0;
		styleRange.font = attr.getFont();
		presentation.addStyleRange(styleRange);
	}
}