org.eclipse.swt.graphics.TextLayout Java Examples

The following examples show how to use org.eclipse.swt.graphics.TextLayout. 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: XbaseInformationControl.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Creates and initializes the text layout used to compute the size hint.
 * 
 * @since 3.2
 */
private void createTextLayout() {
	fTextLayout = new TextLayout(fSashForm.getDisplay());

	// Initialize fonts
	String symbolicFontName = fSymbolicFontName == null ? JFaceResources.DIALOG_FONT : fSymbolicFontName;
	Font font = JFaceResources.getFont(symbolicFontName);
	fTextLayout.setFont(font);
	fTextLayout.setWidth(-1);
	font = JFaceResources.getFontRegistry().getBold(symbolicFontName);
	fBoldStyle = new TextStyle(font, null, null);

	// Compute and set tab width
	fTextLayout.setText("    "); //$NON-NLS-1$
	int tabWidth = fTextLayout.getBounds().width;
	fTextLayout.setTabs(new int[] { tabWidth });
	fTextLayout.setText(""); //$NON-NLS-1$
}
 
Example #2
Source File: CustomBrowserInformationControl.java    From APICloud-Studio with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Creates and initializes the text layout used to compute the size hint.
 * 
 * @since 3.2
 */
private void createTextLayout()
{
	fTextLayout = new TextLayout(fBrowser.getDisplay());

	// Initialize fonts
	String symbolicFontName = fSymbolicFontName == null ? JFaceResources.DIALOG_FONT : fSymbolicFontName;
	Font font = JFaceResources.getFont(symbolicFontName);
	fTextLayout.setFont(font);
	fTextLayout.setWidth(-1);
	font = JFaceResources.getFontRegistry().getBold(symbolicFontName);
	fBoldStyle = new TextStyle(font, null, null);

	// Compute and set tab width
	fTextLayout.setText("    "); //$NON-NLS-1$
	int tabWidth = fTextLayout.getBounds().width;
	fTextLayout.setTabs(new int[] { tabWidth });
	fTextLayout.setText(""); //$NON-NLS-1$
}
 
Example #3
Source File: CellFigure.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
protected void drawBlankString( Graphics g, String s )
{
	TextLayout tl = new TextLayout( Display.getCurrent( ) );

	// bidi_hcg: Apply text direction
	tl.setOrientation( this.rtl ? SWT.RIGHT_TO_LEFT : SWT.LEFT_TO_RIGHT );
	
	tl.setText( s );
	Rectangle rc = tl.getBounds( );

	int left = ( getClientArea( ).width - rc.width ) / 2;
	int top = ( getClientArea( ).height - rc.height ) / 2;

	g.drawText( s, getClientArea( ).x + left, getClientArea( ).y + top );
	tl.dispose();
}
 
Example #4
Source File: TextPainterWithPadding.java    From translationstudio8 with GNU General Public License v2.0 6 votes vote down vote up
public int getPreferredHeight(LayerCell cell, GC gc, IConfigRegistry configRegistry) {
	if (innerTagFactory == null) {
		innerTagFactory = new XliffInnerTagFactory(placeHolderBuilder);
	}
	innerTagFactory.reset();
	TextLayout layout = getCellTextLayout(cell);

	int counts = layout.getLineCount();
	int contentHeight = 0;
	for (int i = 0; i < counts; i++) {
		contentHeight += layout.getLineBounds(i).height;
	}
	layout.dispose();
	contentHeight += Math.max(counts - 1, 0) * SEGMENT_LINE_SPACING;
	contentHeight += 4;// 加上编辑模式下,StyledTextCellEditor的边框
	contentHeight += topPadding;
	contentHeight += bottomPadding;
	return contentHeight;
}
 
Example #5
Source File: TagStyleConfigurator.java    From tmxeditor8 with GNU General Public License v2.0 6 votes vote down vote up
public static void configure(TextLayout textLayout) {
	String text = textLayout.getText();
	Document doc = new Document(text);
	ITokenScanner scanner = getRecipeScanner(doc);
	scanner.setRange(doc, 0, doc.getLength());
	IToken token;
	while ((token = scanner.nextToken()) != Token.EOF) {
		int offset = scanner.getTokenOffset();
		int length = scanner.getTokenLength();
		Object data = token.getData();
		if (data != null && data instanceof TextStyle) {
			TextStyle textStyle = (TextStyle) data;
			textLayout.setStyle(textStyle, offset, offset + length - 1);
		}
	}
}
 
Example #6
Source File: TagStyleConfigurator.java    From tmxeditor8 with GNU General Public License v2.0 6 votes vote down vote up
public static void configure(TextLayout textLayout) {
	String text = textLayout.getText();
	Document doc = new Document(text);
	ITokenScanner scanner = getRecipeScanner(doc);
	scanner.setRange(doc, 0, doc.getLength());
	IToken token;
	while ((token = scanner.nextToken()) != Token.EOF) {
		int offset = scanner.getTokenOffset();
		int length = scanner.getTokenLength();
		Object data = token.getData();
		if (data != null && data instanceof TextStyle) {
			TextStyle textStyle = (TextStyle) data;
			textLayout.setStyle(textStyle, offset, offset + length - 1);
		}
	}
}
 
Example #7
Source File: TagStyleConfigurator.java    From tmxeditor8 with GNU General Public License v2.0 6 votes vote down vote up
public static void configure(TextLayout textLayout) {
	String text = textLayout.getText();
	Document doc = new Document(text);
	ITokenScanner scanner = getRecipeScanner(doc);
	scanner.setRange(doc, 0, doc.getLength());
	IToken token;
	while ((token = scanner.nextToken()) != Token.EOF) {
		int offset = scanner.getTokenOffset();
		int length = scanner.getTokenLength();
		Object data = token.getData();
		if (data != null && data instanceof TextStyle) {
			TextStyle textStyle = (TextStyle) data;
			textLayout.setStyle(textStyle, offset, offset + length - 1);
		}
	}
	scanner = null;
	doc = null;
}
 
Example #8
Source File: XGridCellRenderer.java    From tmxeditor8 with GNU General Public License v2.0 6 votes vote down vote up
public TextLayout getTextLayout(GC gc, GridItem item, int columnIndex, boolean innerTagStyled, boolean drawInnerTag) {
	TextLayout layout = new TextLayout(gc.getDevice());
	layout.setFont(JFaceResources.getFont(net.heartsome.cat.ts.ui.Constants.MATCH_VIEWER_TEXT_FONT));
	innerTagFactory.reset();
	
	String displayStr = "";
	try{
		displayStr = InnerTagUtil.resolveTag(innerTagFactory.parseInnerTag(item.getText(columnIndex)));
	}catch (NullPointerException e) {
		return null;
	}
	layout.setText(displayStr);
	int width = getBounds().width - leftMargin - rightMargin;
	layout.setWidth(width < 1 ? 1 : width);
	layout.setSpacing(Constants.SEGMENT_LINE_SPACING);
	layout.setAlignment(SWT.LEFT);

	if (displayStr.length() != 0 && innerTagStyled) {
		attachInnertTagStyle(gc, layout, drawInnerTag);
	}
	return layout;
}
 
Example #9
Source File: TagStyleConfigurator.java    From translationstudio8 with GNU General Public License v2.0 6 votes vote down vote up
public static void configure(TextLayout textLayout) {
	String text = textLayout.getText();
	Document doc = new Document(text);
	ITokenScanner scanner = getRecipeScanner(doc);
	scanner.setRange(doc, 0, doc.getLength());
	IToken token;
	while ((token = scanner.nextToken()) != Token.EOF) {
		int offset = scanner.getTokenOffset();
		int length = scanner.getTokenLength();
		Object data = token.getData();
		if (data != null && data instanceof TextStyle) {
			TextStyle textStyle = (TextStyle) data;
			textLayout.setStyle(textStyle, offset, offset + length - 1);
		}
	}
	scanner = null;
	doc = null;
}
 
Example #10
Source File: TextPainterWithPadding.java    From tmxeditor8 with GNU General Public License v2.0 6 votes vote down vote up
public int getPreferredHeight(LayerCell cell, GC gc, IConfigRegistry configRegistry) {
	if (innerTagFactory == null) {
		innerTagFactory = new XliffInnerTagFactory(placeHolderBuilder);
	}
	innerTagFactory.reset();
	TextLayout layout = getCellTextLayout(cell);

	int counts = layout.getLineCount();
	int contentHeight = 0;
	for (int i = 0; i < counts; i++) {
		contentHeight += layout.getLineBounds(i).height;
	}
	layout.dispose();
	contentHeight += Math.max(counts - 1, 0) * SEGMENT_LINE_SPACING;
	contentHeight += 4;// 加上编辑模式下,StyledTextCellEditor的边框
	contentHeight += topPadding;
	contentHeight += bottomPadding;
	return contentHeight;
}
 
Example #11
Source File: XGridCellRenderer.java    From translationstudio8 with GNU General Public License v2.0 6 votes vote down vote up
public TextLayout getTextLayout(GC gc, GridItem item, int columnIndex, boolean innerTagStyled, boolean drawInnerTag) {
	TextLayout layout = new TextLayout(gc.getDevice());
	layout.setFont(font);
	layout.setTabs(new int[]{tabWidth});
	innerTagFactory.reset();
	
	String displayStr = "";
	try{
		displayStr = InnerTagUtil.resolveTag(innerTagFactory.parseInnerTag(item.getText(columnIndex)));
	}catch (NullPointerException e) {
		return null;
	}
	layout.setText(displayStr);
	int width = getBounds().width - leftMargin - rightMargin;
	layout.setWidth(width < 1 ? 1 : width);
	layout.setSpacing(Constants.SEGMENT_LINE_SPACING);
	layout.setAlignment(SWT.LEFT);
	layout.setOrientation(item.getParent().getOrientation());
	if (displayStr.length() != 0 && innerTagStyled) {
		attachInnertTagStyle(gc, layout, drawInnerTag);
	}
	return layout;
}
 
Example #12
Source File: XGridCellRenderer.java    From translationstudio8 with GNU General Public License v2.0 6 votes vote down vote up
protected void drawInnerTag(GC gc, TextLayout layout) {
	String displayStr = layout.getText();
	List<InnerTagBean> innerTagBeans = innerTagFactory.getInnerTagBeans();
	Rectangle bounds = getBounds();
	for (InnerTagBean innerTagBean : innerTagBeans) {
		String placeHolder = placeHolderBuilder.getPlaceHolder(innerTagBeans, innerTagBeans.indexOf(innerTagBean));
		int start = displayStr.indexOf(placeHolder);
		if (start == -1) {
			continue;
		}			
		if (gc != null) {
			Point p = layout.getLocation(start, false);
			int x = bounds.x + p.x + leftMargin;
			x += SEGMENT_LINE_SPACING;

			Point tagSize = tagRender.calculateTagSize(innerTagBean);
			int lineIdx = layout.getLineIndex(start);
			Rectangle r = layout.getLineBounds(lineIdx);
			int y = bounds.y + p.y + topMargin + r.height / 2 - tagSize.y /2;				
			tagRender.draw(gc, innerTagBean, x, y - layout.getAscent());
		}
	}
}
 
Example #13
Source File: TagStyleConfigurator.java    From translationstudio8 with GNU General Public License v2.0 6 votes vote down vote up
public static void configure(TextLayout textLayout) {
	String text = textLayout.getText();
	Document doc = new Document(text);
	ITokenScanner scanner = getRecipeScanner(doc);
	scanner.setRange(doc, 0, doc.getLength());
	IToken token;
	while ((token = scanner.nextToken()) != Token.EOF) {
		int offset = scanner.getTokenOffset();
		int length = scanner.getTokenLength();
		Object data = token.getData();
		if (data != null && data instanceof TextStyle) {
			TextStyle textStyle = (TextStyle) data;
			textLayout.setStyle(textStyle, offset, offset + length - 1);
		}
	}
}
 
Example #14
Source File: TmxEditorTextPainter.java    From tmxeditor8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int getPreferredHeight(LayerCell cell, GC gc, IConfigRegistry configRegistry) {
	TextLayout layout = getCellTextLayout(cell);
	int contentHeight = layout.getBounds().height;
	layout.dispose();
	tuv = null;
	contentHeight += topPadding;
	contentHeight += bottomPadding;
	contentHeight += 4;// 加上编辑模式下,StyledTextCellEditor的边框
	return contentHeight;
}
 
Example #15
Source File: AttributePainter.java    From tmxeditor8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void paintCell(LayerCell cell, GC gc, Rectangle bounds, IConfigRegistry configRegistry) {
	super.paintCell(cell, gc, bounds, configRegistry);
	IStyle cellStyle = CellStyleUtil.getCellStyle(cell, configRegistry);
	setupGCFromConfig(gc, cellStyle);

	TextLayout layout = getCellTextLayout(cell);
	Rectangle rectangle = cell.getBounds();
	layout.draw(gc, rectangle.x + leftPadding, rectangle.y + topPadding);
	layout.dispose();
}
 
Example #16
Source File: AttributePainter.java    From tmxeditor8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int getPreferredHeight(LayerCell cell, GC gc, IConfigRegistry configRegistry) {
	TextLayout layout = getCellTextLayout(cell);
	int contentHeight = layout.getBounds().height;
	layout.dispose();
	contentHeight += topPadding;
	contentHeight += bottomPadding;
	contentHeight += 4;// 加上编辑模式下,StyledTextCellEditor的边框
	return contentHeight;
}
 
Example #17
Source File: XGridCellRenderer.java    From translationstudio8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 设置字体
 * @param font
 *            ;
 */
public void setFont(Font font) {
	TextLayout layout = new TextLayout(Display.getDefault());
	layout.setFont(font);
	StringBuffer tabBuffer = new StringBuffer(tabSize);
	for (int i = 0; i < tabSize; i++) {
		tabBuffer.append(' ');
	}
	layout.setText(tabBuffer.toString());
	tabWidth = layout.getBounds().width;
	layout.dispose();
	this.font = font;
}
 
Example #18
Source File: TextPainterWithPadding.java    From tmxeditor8 with GNU General Public License v2.0 5 votes vote down vote up
private TextLayout getCellTextLayout(LayerCell cell) {
	int orientation = editor.getTable().getStyle() & (SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT);
	TextLayout layout = new TextLayout(editor.getTable().getDisplay());
	layout.setOrientation(orientation);
	layout.setSpacing(Constants.SEGMENT_LINE_SPACING);
	layout.setFont(font);
	layout.setAscent(ascent);
	layout.setDescent(descent); // 和 StyledTextEditor 同步
	layout.setTabs(new int[] { tabWidth });

	Rectangle rectangle = cell.getBounds();
	int width = rectangle.width - leftPadding - rightPadding;
	width -= 1;
	if (wrapText && width > 0) {
		layout.setWidth(width);
	}

	String displayText = InnerTagUtil.resolveTag(innerTagFactory.parseInnerTag((String) cell.getDataValue()));
	if (XliffEditorParameter.getInstance().isShowNonpirnttingCharacter()) {
		displayText = displayText.replaceAll("\\n", Constants.LINE_SEPARATOR_CHARACTER + "\n");
		displayText = displayText.replaceAll("\\t", Constants.TAB_CHARACTER + "");
		displayText = displayText.replaceAll(" ", Constants.SPACE_CHARACTER + "");
	}
	layout.setText(displayText);
	List<InnerTagBean> innerTagBeans = innerTagFactory.getInnerTagBeans();
	for (InnerTagBean innerTagBean : innerTagBeans) {
		String placeHolder = placeHolderBuilder.getPlaceHolder(innerTagBeans, innerTagBeans.indexOf(innerTagBean));
		int start = displayText.indexOf(placeHolder);
		if (start == -1) {
			continue;
		}
		TextStyle style = new TextStyle();
		Point rect = tagRender.calculateTagSize(innerTagBean);
		style.metrics = new GlyphMetrics(rect.y, 0, rect.x + SEGMENT_LINE_SPACING * 2);
		layout.setStyle(style, start, start + placeHolder.length() - 1);
	}

	return layout;
}
 
Example #19
Source File: AccordionLabel.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
int validateOffset( TextLayout layout, int offset )
{
	int nextOffset = layout.getNextOffset( offset, SWT.MOVEMENT_CLUSTER );
	if ( nextOffset != offset )
		return layout.getPreviousOffset( nextOffset, SWT.MOVEMENT_CLUSTER );
	return offset;
}
 
Example #20
Source File: TextPainterWithPadding.java    From translationstudio8 with GNU General Public License v2.0 5 votes vote down vote up
private void appendNonprintingStyle(TextLayout layout) {
	TextStyle style = new TextStyle(font, GUIHelper.getColor(new RGB(100, 100, 100)), null);
	String s = layout.getText();
	Matcher matcher = Constants.NONPRINTING_PATTERN.matcher(s);
	while (matcher.find()) {
		int start = matcher.start();
		int end = matcher.end();
		// style.metrics = new GlyphMetrics(10, 0, 1);
		layout.setStyle(style, start, end - 1);
	}
}
 
Example #21
Source File: TextPainterWithPadding.java    From translationstudio8 with GNU General Public License v2.0 5 votes vote down vote up
private TextLayout getCellTextLayout(LayerCell cell) {
	int orientation = editor.getTable().getStyle() & (SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT);
	TextLayout layout = new TextLayout(editor.getTable().getDisplay());
	layout.setOrientation(orientation);
	layout.setSpacing(Constants.SEGMENT_LINE_SPACING);
	layout.setFont(font);
	layout.setAscent(ascent);
	layout.setDescent(descent); // 和 StyledTextEditor 同步
	layout.setTabs(new int[] { tabWidth });

	Rectangle rectangle = cell.getBounds();
	int width = rectangle.width - leftPadding - rightPadding;
	width -= 1;
	if (wrapText && width > 0) {
		layout.setWidth(width);
	}

	String displayText = InnerTagUtil.resolveTag(innerTagFactory.parseInnerTag((String) cell.getDataValue()));
	if (XliffEditorParameter.getInstance().isShowNonpirnttingCharacter()) {
		displayText = displayText.replaceAll("\\n", Constants.LINE_SEPARATOR_CHARACTER + "\n");
		displayText = displayText.replaceAll("\\t", Constants.TAB_CHARACTER + "\u200B");
		displayText = displayText.replaceAll(" ", Constants.SPACE_CHARACTER + "\u200B");
	}
	layout.setText(displayText);
	List<InnerTagBean> innerTagBeans = innerTagFactory.getInnerTagBeans();
	for (InnerTagBean innerTagBean : innerTagBeans) {
		String placeHolder = placeHolderBuilder.getPlaceHolder(innerTagBeans, innerTagBeans.indexOf(innerTagBean));
		int start = displayText.indexOf(placeHolder);
		if (start == -1) {
			continue;
		}
		TextStyle style = new TextStyle();
		Point rect = tagRender.calculateTagSize(innerTagBean);
		style.metrics = new GlyphMetrics(rect.y, 0, rect.x + SEGMENT_LINE_SPACING * 2);
		layout.setStyle(style, start, start + placeHolder.length() - 1);
	}

	return layout;
}
 
Example #22
Source File: ComponentStatusLabel.java    From arx with Apache License 2.0 5 votes vote down vote up
/**
 * Shorten text
 *
 * @param gc
 * @param t
 * @param width
 * @return
 */
protected String shortenText(GC gc, String t, int width) {
    if (t == null) return null;
    int w = gc.textExtent(ELLIPSIS, DRAW_FLAGS).x;
    if (width <= w) return t;
    int l = t.length();
    int max = l / 2;
    int min = 0;
    int mid = (max + min) / 2 - 1;
    if (mid <= 0) return t;
    TextLayout layout = new TextLayout(getDisplay());
    layout.setText(t);
    mid = validateOffset(layout, mid);
    while (min < mid && mid < max) {
        String s1 = t.substring(0, mid);
        String s2 = t.substring(validateOffset(layout, l - mid), l);
        int l1 = gc.textExtent(s1, DRAW_FLAGS).x;
        int l2 = gc.textExtent(s2, DRAW_FLAGS).x;
        if (l1 + w + l2 > width) {
            max = mid;
            mid = validateOffset(layout, (max + min) / 2);
        } else if (l1 + w + l2 < width) {
            min = mid;
            mid = validateOffset(layout, (max + min) / 2);
        } else {
            min = max;
        }
    }
    String result = mid == 0 ? t : t.substring(0, mid) + ELLIPSIS + t.substring(validateOffset(layout, l - mid), l);
    layout.dispose();
    return result;
}
 
Example #23
Source File: GridCopyEnable.java    From tmxeditor8 with GNU General Public License v2.0 4 votes vote down vote up
void handleVerticalScroll(Event event) {
	GridVisibleRange visibleR = gridTable.getVisibleRange();
	GridItem[] items = visibleR.getItems();
	boolean itemFlg = false;
	for (GridItem item : items) {
		if (focusItem == item) {
			itemFlg = true;
		}
	}
	boolean columnFlg = false;
	GridColumn[] columns = visibleR.getColumns();
	if (columns.length - 1 >= focusColIndex) {
		columnFlg = true;
	}
	if (!itemFlg || !columnFlg) {
		defaultCaret.setVisible(false);
		return;
	}
	defaultCaret.setVisible(true);

	GridColumn col = gridTable.getColumn(focusColIndex);
	GridCellRenderer gcr = col.getCellRenderer();
	int colIndex = gcr.getColumn();
	if (gcr == null || !(gcr instanceof XGridCellRenderer) || !copyAbleColumnIndexs.contains(colIndex)) {
		return;
	}
	XGridCellRenderer cellRender = (XGridCellRenderer) gcr;

	Rectangle cellBounds = focusItem.getBounds(colIndex);
	GC gc = new GC(Display.getDefault());
	TextLayout layout = null;
	try {
		layout = cellRender.getTextLayout(gc, focusItem, colIndex, true, false);
		if (layout == null) {
			gc.dispose();
			return;
		}
		Point point = layout.getLocation(caretOffset, false);
		coordinateOffsetX = cellBounds.x + cellRender.leftMargin;
		coordinateOffsetY = cellBounds.y + cellRender.topMargin + cellRender.textTopMargin;
		defaultCaret.setLocation(point.x + coordinateOffsetX, point.y + coordinateOffsetY);
	} finally {
		if (layout != null) {
			layout.dispose();
		}
		if (gc != null) {
			gc.dispose();
		}
	}
}
 
Example #24
Source File: Sleak.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
void refreshLabel() {
  int colors = 0, cursors = 0, fonts = 0, gcs = 0, images = 0;
  int paths = 0, patterns = 0, regions = 0, textLayouts = 0, transforms = 0;
  for ( int i = 0; i < objects.length; i++ ) {
    Object object = objects[i];
    if ( object instanceof Color ) {
      colors++;
    }
    if ( object instanceof Cursor ) {
      cursors++;
    }
    if ( object instanceof Font ) {
      fonts++;
    }
    if ( object instanceof GC ) {
      gcs++;
    }
    if ( object instanceof Image ) {
      images++;
    }
    if ( object instanceof Path ) {
      paths++;
    }
    if ( object instanceof Pattern ) {
      patterns++;
    }
    if ( object instanceof Region ) {
      regions++;
    }
    if ( object instanceof TextLayout ) {
      textLayouts++;
    }
    if ( object instanceof Transform ) {
      transforms++;
    }
  }
  String string = "";
  if ( colors != 0 ) {
    string += colors + " Color(s)\n";
  }
  if ( cursors != 0 ) {
    string += cursors + " Cursor(s)\n";
  }
  if ( fonts != 0 ) {
    string += fonts + " Font(s)\n";
  }
  if ( gcs != 0 ) {
    string += gcs + " GC(s)\n";
  }
  if ( images != 0 ) {
    string += images + " Image(s)\n";
  }
  if ( paths != 0 ) {
    string += paths + " Paths(s)\n";
  }
  if ( patterns != 0 ) {
    string += patterns + " Pattern(s)\n";
  }
  if ( regions != 0 ) {
    string += regions + " Region(s)\n";
  }
  if ( textLayouts != 0 ) {
    string += textLayouts + " TextLayout(s)\n";
  }
  if ( transforms != 0 ) {
    string += transforms + " Transform(s)\n";
  }
  if ( string.length() != 0 ) {
    string = string.substring( 0, string.length() - 1 );
  }
  label.setText( string );
}
 
Example #25
Source File: TypeColunmCellRenderer.java    From tmxeditor8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Point computeSize(GC gc, int wHint, int hHint, Object value) {
	GridItem item = (GridItem) value;

	gc.setFont(item.getFont(getColumn()));

	int x = 0;

	x += leftMargin;

	int y = 0;

	Image image = item.getImage(getColumn());
	if (image != null) {
		y = topMargin + image.getBounds().height + bottomMargin;

		x += image.getBounds().width + 3;
	}

	// MOPR-DND
	// MOPR: replaced this code (to get correct preferred height for cells in word-wrap columns)
	//
	// x += gc.stringExtent(item.getText(column)).x + rightMargin;
	//
	// y = Math.max(y,topMargin + gc.getFontMetrics().getHeight() + bottomMargin);
	//
	// with this code:

	int textHeight = 0;
	if (!isWordWrap()) {
		x += gc.textExtent(item.getText(getColumn())).x + rightMargin;

		textHeight = topMargin + textTopMargin + gc.getFontMetrics().getHeight() + textBottomMargin + bottomMargin;
	} else {
		int plainTextWidth;
		if (wHint == SWT.DEFAULT)
			plainTextWidth = getBounds().width - x - rightMargin;
		else
			plainTextWidth = wHint - x - rightMargin;

		TextLayout currTextLayout = new TextLayout(gc.getDevice());
		currTextLayout.setFont(gc.getFont());
		currTextLayout.setText(item.getText(getColumn()));
		currTextLayout.setAlignment(getAlignment());
		currTextLayout.setWidth(plainTextWidth < 1 ? 1 : plainTextWidth);

		x += plainTextWidth + rightMargin;

		textHeight += topMargin + textTopMargin;
		for (int cnt = 0; cnt < currTextLayout.getLineCount(); cnt++)
			textHeight += currTextLayout.getLineBounds(cnt).height;
		textHeight += textBottomMargin + bottomMargin;

		currTextLayout.dispose();
	}

	y = Math.max(y, textHeight);

	return new Point(x, y);
}
 
Example #26
Source File: TBSearchCellRenderer.java    From tmxeditor8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Point computeSize(GC gc, int wHint, int hHint, Object value) {
	GridItem item = (GridItem) value;

	gc.setFont(item.getFont(getColumn()));

	int x = 0;

	x += leftMargin;

	if (isTree()) {
		x += getToggleIndent(item);

		x += toggleRenderer.getBounds().width + insideMargin;

	}

	if (isCheck()) {
		x += checkRenderer.getBounds().width + insideMargin;
	}

	int y = 0;

	Image image = item.getImage(getColumn());
	if (image != null) {
		y = topMargin + image.getBounds().height + bottomMargin;

		x += image.getBounds().width + insideMargin;
	}

	// MOPR-DND
	// MOPR: replaced this code (to get correct preferred height for cells in word-wrap columns)
	//
	// x += gc.stringExtent(item.getText(column)).x + rightMargin;
	//
	// y = Math.max(y,topMargin + gc.getFontMetrics().getHeight() + bottomMargin);
	//
	// with this code:

	int textHeight = 0;
	if (!isWordWrap()) {
		x += gc.textExtent(item.getText(getColumn())).x + rightMargin;

		textHeight = topMargin + textTopMargin + gc.getFontMetrics().getHeight() + textBottomMargin + bottomMargin;
	} else {
		int plainTextWidth;
		if (wHint == SWT.DEFAULT)
			plainTextWidth = getBounds().width - x - rightMargin;
		else
			plainTextWidth = wHint - x - rightMargin;

		TextLayout currTextLayout = new TextLayout(gc.getDevice());
		currTextLayout.setFont(gc.getFont());
		currTextLayout.setText(item.getText(getColumn()));
		currTextLayout.setAlignment(getAlignment());
		currTextLayout.setWidth(plainTextWidth < 1 ? 1 : plainTextWidth);

		x += plainTextWidth + rightMargin;

		textHeight += topMargin + textTopMargin;
		for (int cnt = 0; cnt < currTextLayout.getLineCount(); cnt++)
			textHeight += currTextLayout.getLineBounds(cnt).height;
		textHeight += textBottomMargin + bottomMargin;

		currTextLayout.dispose();
	}

	y = Math.max(y, textHeight);

	return new Point(x, y);
}
 
Example #27
Source File: DefaultCellRenderer.java    From tmxeditor8 with GNU General Public License v2.0 4 votes vote down vote up
/**
     * {@inheritDoc}
     */
    public Point computeSize(GC gc, int wHint, int hHint, Object value)
    {
        GridItem item = (GridItem)value;

        gc.setFont(item.getFont(getColumn()));

        int x = 0;

        x += leftMargin;

        if (isTree())
        {
            x += getToggleIndent(item);

            x += toggleRenderer.getBounds().width + insideMargin;

        }

        if (isCheck())
        {
            x += checkRenderer.getBounds().width + insideMargin;
        }

        int y = 0;

        Image image = item.getImage(getColumn());
        if (image != null)
        {
            y = topMargin + image.getBounds().height + bottomMargin;

            x += image.getBounds().width + insideMargin;
        }

// MOPR-DND
// MOPR: replaced this code (to get correct preferred height for cells in word-wrap columns)
//
//       x += gc.stringExtent(item.getText(column)).x + rightMargin;
//
//        y = Math.max(y,topMargin + gc.getFontMetrics().getHeight() + bottomMargin);
//
// with this code:

        int textHeight = 0;
        if(!isWordWrap())
        {
            x += gc.textExtent(item.getText(getColumn())).x + rightMargin;

            textHeight = topMargin + textTopMargin + gc.getFontMetrics().getHeight() + textBottomMargin + bottomMargin;
        }
        else
        {
        	int plainTextWidth;
        	if (wHint == SWT.DEFAULT)
        	  plainTextWidth = getBounds().width - x - rightMargin;
        	else
        		plainTextWidth = wHint - x - rightMargin;

            TextLayout currTextLayout = new TextLayout(gc.getDevice());
            currTextLayout.setFont(gc.getFont());
            currTextLayout.setText(item.getText(getColumn()));
            currTextLayout.setAlignment(getAlignment());
            currTextLayout.setWidth(plainTextWidth < 1 ? 1 : plainTextWidth);

            x += plainTextWidth + rightMargin;

            textHeight += topMargin + textTopMargin;
            for(int cnt=0;cnt<currTextLayout.getLineCount();cnt++)
                textHeight += currTextLayout.getLineBounds(cnt).height;
            textHeight += textBottomMargin + bottomMargin;

            currTextLayout.dispose();
        }

        y = Math.max(y, textHeight);

        return new Point(x, y);
    }
 
Example #28
Source File: TextCellRenderer.java    From tmxeditor8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void draw(GC gc, JaretTable jaretTable, ICellStyle cellStyle, Rectangle drawingArea, IRow row,
        IColumn column, boolean drawFocus, boolean selected, boolean printing) {
    
    drawBackground(gc, drawingArea, cellStyle, selected, printing);
    Rectangle drect = drawBorder(gc, cellStyle, drawingArea, printing);
    Rectangle rect = applyInsets(drect);

    // convert the value to a string
    String s = convertValue(row, column);

    Color fg = gc.getForeground();
    Color bg = gc.getBackground();
    Font font = gc.getFont();

   

    // draw comment marker if comment is present and not printing
    if (!printing && getComment(row, column) != null) {
        drawCommentMarker(gc, drawingArea, _commentColor, COMMENTMARKER_SIZE);
    }

    if (drawFocus) {
        drawFocus(gc, drect);
    }
    drawSelection(gc, drawingArea, cellStyle, selected, printing);

    gc.setForeground(fg);
    gc.setBackground(bg);
    gc.setFont(font);
    if (s != null) {
        if (selected && !printing) {
            gc.setBackground(SELECTIONCOLOR);
        } else {
            gc.setBackground(getBackgroundColor(cellStyle, printing));
        }
        gc.setForeground(getForegroundColor(cellStyle, printing));
        gc.setFont(getFont(cellStyle, printing, gc.getFont()));

        drawCellString(gc, rect, s, cellStyle);
        if (s.indexOf("we") != -1) {
            TextLayout textLayout = new TextLayout(gc.getDevice());
            textLayout.setText(s);
            textLayout.setFont(gc.getFont());
            textLayout.setWidth(rect.width);
            Color color = new Color(gc.getDevice(), 150, 100, 100);
    		Font font2 = new Font(gc.getDevice(), gc.getFont().getFontData()[0].getName(), gc.getFont()
    				.getFontData()[0].getHeight(), SWT.ITALIC);
    		TextStyle style = new TextStyle(font2, color, null);
    		for (int i = 1; i < s.length(); i++) {
    			int j = indexOf(s, "we", i, false);
    			if (j != -1) {
    				textLayout.setStyle(style, j, j + 3);
    			} else {
    				break;
    			}

    		}
    		gc.fillRectangle(rect);
    		textLayout.draw(gc, rect.x, rect.y);
    		gc.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_LIST_FOREGROUND));
            }
    }
}
 
Example #29
Source File: AccordionLabel.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Shorten the given text <code>t</code> so that its length doesn't exceed
 * the given width. The default implementation replaces characters in the
 * center of the original string with an ellipsis ("..."). Override if you
 * need a different strategy.
 * 
 * @param gc
 *            the gc to use for text measurement
 * @param t
 *            the text to shorten
 * @param width
 *            the width to shorten the text to, in pixels
 * @return the shortened text
 */
protected String shortenText( GC gc, String t, int width )
{
	if ( t == null )
		return null;
	int w = gc.textExtent( ELLIPSIS, DRAW_FLAGS ).x;
	if ( width <= w )
		return t;
	int l = t.length( );
	int max = l / 2;
	int min = 0;
	int mid = ( max + min ) / 2 - 1;
	if ( mid <= 0 )
		return t;
	TextLayout layout = new TextLayout( getDisplay( ) );
	layout.setText( t );
	mid = validateOffset( layout, mid );
	while ( min < mid && mid < max )
	{
		String s1 = t.substring( 0, mid );
		String s2 = t.substring( validateOffset( layout, l - mid ), l );
		int l1 = gc.textExtent( s1, DRAW_FLAGS ).x;
		int l2 = gc.textExtent( s2, DRAW_FLAGS ).x;
		if ( l1 + w + l2 > width )
		{
			max = mid;
			mid = validateOffset( layout, ( max + min ) / 2 );
		}
		else if ( l1 + w + l2 < width )
		{
			min = mid;
			mid = validateOffset( layout, ( max + min ) / 2 );
		}
		else
		{
			min = max;
		}
	}
	String result = mid == 0 ? t : t.substring( 0, mid )
			+ ELLIPSIS
			+ t.substring( validateOffset( layout, l - mid ), l );
	layout.dispose( );
	return result;
}
 
Example #30
Source File: Sleak.java    From hop with Apache License 2.0 4 votes vote down vote up
void refreshLabel() {
  int colors = 0, cursors = 0, fonts = 0, gcs = 0, images = 0;
  int paths = 0, patterns = 0, regions = 0, textLayouts = 0, transforms = 0;
  for ( int i = 0; i < objects.length; i++ ) {
    Object object = objects[ i ];
    if ( object instanceof Color ) {
      colors++;
    }
    if ( object instanceof Cursor ) {
      cursors++;
    }
    if ( object instanceof Font ) {
      fonts++;
    }
    if ( object instanceof GC ) {
      gcs++;
    }
    if ( object instanceof Image ) {
      images++;
    }
    if ( object instanceof Path ) {
      paths++;
    }
    if ( object instanceof Pattern ) {
      patterns++;
    }
    if ( object instanceof Region ) {
      regions++;
    }
    if ( object instanceof TextLayout ) {
      textLayouts++;
    }
    if ( object instanceof Transform ) {
      transforms++;
    }
  }
  String string = "";
  if ( colors != 0 ) {
    string += colors + " Color(s)\n";
  }
  if ( cursors != 0 ) {
    string += cursors + " Cursor(s)\n";
  }
  if ( fonts != 0 ) {
    string += fonts + " Font(s)\n";
  }
  if ( gcs != 0 ) {
    string += gcs + " GC(s)\n";
  }
  if ( images != 0 ) {
    string += images + " Image(s)\n";
  }
  if ( paths != 0 ) {
    string += paths + " Paths(s)\n";
  }
  if ( patterns != 0 ) {
    string += patterns + " Pattern(s)\n";
  }
  if ( regions != 0 ) {
    string += regions + " Region(s)\n";
  }
  if ( textLayouts != 0 ) {
    string += textLayouts + " TextLayout(s)\n";
  }
  if ( transforms != 0 ) {
    string += transforms + " Transform(s)\n";
  }
  if ( string.length() != 0 ) {
    string = string.substring( 0, string.length() - 1 );
  }
  label.setText( string );
}