Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2010 itemis AG (http://www.itemis.eu) and others. All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html /
  
  package org.eclipse.xtext.ui.label;
  
 import static org.eclipse.xtext.ui.editor.utils.EditorUtils.*;
 
 import  org.eclipse.jface.resource.ColorRegistry;
 import  org.eclipse.jface.resource.FontDescriptor;
 import  org.eclipse.jface.resource.JFaceResources;
 import  org.eclipse.jface.text.TextAttribute;
 import  org.eclipse.jface.viewers.StyledString;
 import  org.eclipse.jface.viewers.StyledString.Styler;
 import  org.eclipse.swt.graphics.FontData;
 import  org.eclipse.swt.graphics.TextStyle;
 import  org.eclipse.swt.widgets.Display;

Helper class for styled strings.

Author(s):
Jan Koehnlein - Initial contribution and API
 
 public class StylerFactory {

Returns:
DefaultFontStyler
 
 	public Styler createStyler(String foregroundColorNameString backgroundColorName) {
 		return new DefaultFontStyler(nullforegroundColorNamebackgroundColorName);
 	}

Creates a styler that takes the given descriptor of a font, foreground and background colors from the JFace color registry.

Parameters:
fontDescriptor the descriptor of a font to apply
foregroundColorName the color name for the foreground color
backgroundColorName the color name for the background color
Returns:
the created style
 
 	public Styler createStyler(FontDescriptor fontDescriptorString foregroundColorName,
 			String backgroundColorName) {
 		return new DefaultFontStyler(fontDescriptorforegroundColorNamebackgroundColorName);
 	}

Creates an adapter styler for XtextStyles.

Parameters:
xtextStyle the xtextStyle to apply
Returns:
the created style
 
 		return new XtextStyleAdapterStyler(xtextStyle);
 	}

Convenience method to create a new StyledString from a given label text and XtextTextStyle

Parameters:
text the label to display
xtextStyle the style to apply to the given text
Returns:
the created style
 
 	public StyledString createFromXtextStyle(String text,
 		return new StyledString(textnew XtextStyleAdapterStyler(xtextStyle));
 	}
 
 	protected static class XtextStyleAdapterStyler extends Styler {
 
 			this. = textStyle;
 		}
 
 		public void applyStyles(TextStyle textStyle) {
 			textStyle.strikeout = (.getStyle() & TextAttribute.STRIKETHROUGH) != 0;
 			textStyle.underline = (.getStyle() & TextAttribute.UNDERLINE) != 0;
 			if (.getFontData() == null
 				FontData fontData = new FontData();
 				fontData.setStyle(.getStyle());
 			}
 			if (.getBackgroundColor() != null
 				textStyle.background = colorFromRGB(.getBackgroundColor());
 			textStyle.foreground = colorFromRGB(.getColor());
		}
	}
	protected static class DefaultFontStyler extends Styler {
		private final FontDescriptor fontDescriptor;
		private final String foregroundColorName;
		private final String backgroundColorName;
		public DefaultFontStyler(FontDescriptor fontDescriptorString foregroundColorNameString backgroundColorName) {
			this. = fontDescriptor;
			this. = foregroundColorName;
			this. = backgroundColorName;
		}
		public void applyStyles(TextStyle textStyle) {
			ColorRegistry colorRegistry = JFaceResources.getColorRegistry();
			if ( != null) {
				textStyle.font = .createFont(Display.getCurrent());
			}
			if ( != null) {
				textStyle.foreground = colorRegistry.get();
			}
			if ( != null) {
				textStyle.background = colorRegistry.get();
			}
		}
	}
New to GrepCode? Check out our FAQ X