Java Code Examples for javax.swing.text.Utilities#getTabbedTextWidth()

The following examples show how to use javax.swing.text.Utilities#getTabbedTextWidth() . 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: SyntaxStyle.java    From visualvm with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Draw text.  This can directly call the Utilities.drawTabbedText.
 * Sub-classes can override this method to provide any other decorations.
 * @param  segment - the source of the text
 * @param  x - the X origin >= 0
 * @param  y - the Y origin >= 0
 * @param  graphics - the graphics context
 * @param e - how to expand the tabs. If this value is null, tabs will be 
 * expanded as a space character.
 * @param startOffset - starting offset of the text in the document >= 0 
 * @return
 */
public int drawText(Segment segment, int x, int y,
        Graphics graphics, TabExpander e, int startOffset) {
    graphics.setFont(graphics.getFont().deriveFont(getFontStyle()));
    FontMetrics fontMetrics = graphics.getFontMetrics();
    int a = fontMetrics.getAscent();
    int h = a + fontMetrics.getDescent();
    int w = Utilities.getTabbedTextWidth(segment, fontMetrics, 0, e, startOffset);
    int rX = x - 1;
    int rY = y - a;
    int rW = w + 2;
    int rH = h;
    if ((getFontStyle() & 0x10) != 0) {
        graphics.setColor(Color.decode("#EEEEEE"));
        graphics.fillRect(rX, rY, rW, rH);
    }
    graphics.setColor(getColor());
    x = Utilities.drawTabbedText(segment, x, y, graphics, e, startOffset);
    if ((getFontStyle() & 0x8) != 0) {
        graphics.setColor(Color.RED);
        graphics.drawRect(rX, rY, rW, rH);
    }
    return x;
}
 
Example 2
Source File: SyntaxStyle.java    From nextreports-designer with Apache License 2.0 6 votes vote down vote up
/**
 * Draw text. This can directly call the Utilities.drawTabbedText.
 * Sub-classes can override this method to provide any other decorations.
 * 
 * @param  segment - the source of the text
 * @param  x - the X origin >= 0
 * @param  y - the Y origin >= 0
 * @param  graphics - the graphics context
 * @param e - how to expand the tabs. If this value is null, tabs will be 
 * expanded as a space character.
 * @param startOffset - starting offset of the text in the document >= 0 
 * @return
 */
public int drawText(Segment segment, int x, int y,
        Graphics graphics, TabExpander e, int startOffset) {
    graphics.setFont(graphics.getFont().deriveFont(getFontStyle()));
    FontMetrics fontMetrics = graphics.getFontMetrics();
    int a = fontMetrics.getAscent();
    int h = a + fontMetrics.getDescent();
    int w = Utilities.getTabbedTextWidth(segment, fontMetrics, 0, e, startOffset);
    int rX = x - 1;
    int rY = y - a;
    int rW = w + 2;
    int rH = h;
    if ((getFontStyle() & 0x10) != 0) {
        graphics.setColor(Color.decode("#EEEEEE"));
        graphics.fillRect(rX, rY, rW, rH);
    }
    graphics.setColor(getColor());
    x = Utilities.drawTabbedText(segment, x, y, graphics, e, startOffset);
    if ((getFontStyle() & 0x8) != 0) {
        graphics.setColor(Color.RED);
        graphics.drawRect(rX, rY, rW, rH);
    }
    
    return x;
}
 
Example 3
Source File: UniTools.java    From jpexs-decompiler with GNU General Public License v3.0 5 votes vote down vote up
public static int getTabbedTextWidth(Graphics g,Segment segment,int x,TabExpander e, int startOffset){
    List<Segment> segments=new ArrayList<Segment>();
    List<Boolean> unis=new ArrayList<Boolean>();
    getSegments(g.getFont(), segment, segments, unis);
    Font origFont=g.getFont();
    Font uniFont = defaultUniFont.deriveFont(origFont.getStyle(),origFont.getSize2D());
    int ret=0;
    int pos=0;
    for(int i=0;i<segments.size();i++){
        Segment seg=segments.get(i);
        ret += Utilities.getTabbedTextWidth(seg, g.getFontMetrics(unis.get(i)?uniFont:origFont), 0, e, startOffset+pos);     
        pos += seg.length();
    }
    return ret;       
}
 
Example 4
Source File: MultiLineToolTip.java    From pdfxtk with Apache License 2.0 5 votes vote down vote up
public Dimension getPreferredSize(JComponent c) {
     FontMetrics metrics = c.getFontMetrics(c.getFont());
     String tipText = ((JToolTip)c).getTipText();
     if(tipText == null) tipText = "";
     
     ArrayList linesa = new ArrayList();
     String[]  strs   = Strings.split(tipText, '\n');
     for(int i = 0; i < strs.length; i++) {     
Segment s = new Segment(strs[i].toCharArray(), 0, strs[i].length());

for(int brk = 0; ;) {
  if(Utilities.getTabbedTextWidth(s, metrics, 0, null, 0) < maxAllowedWidth) {
    linesa.add(new String(s.array, s.offset, s.array.length - s.offset));
    break;
  }
  brk = Utilities.getBreakLocation(s, metrics, 0, maxAllowedWidth, null, 0);
  linesa.add(new String(s.array, s.offset, brk));
  s.offset += brk;
  s.count  -= brk;
}
     }
     
     lines = (String[])linesa.toArray(new String[linesa.size()]);
     maxWidth = 0;
     for(int i = 0; i < lines.length; i++)
maxWidth = Math.max(maxWidth, SwingUtilities.computeStringWidth(metrics, lines[i]));
     return new Dimension(maxWidth + 6, metrics.getHeight() * lines.length + 2);
   }
 
Example 5
Source File: JEditTextArea.java    From rapidminer-studio with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Converts an offset in a line into an x co-ordinate. This is a fast version that should only
 * be used if no changes were made to the text since the last repaint.
 * 
 * @param line
 *            The line
 * @param offset
 *            The offset, from the start of the line
 */
public int _offsetToX(int line, int offset) {
	TokenMarker tokenMarker = getTokenMarker();

	/* Use painter's cached info for speed */
	FontMetrics fm = painter.getFontMetrics();

	getLineText(line, lineSegment);

	int segmentOffset = lineSegment.offset;
	int x = horizontalOffset;

	/* If syntax coloring is disabled, do simple translation */
	if (tokenMarker == null) {
		lineSegment.count = offset;
		return x + Utilities.getTabbedTextWidth(lineSegment, fm, x, painter, 0);
	}
	/*
	 * If syntax coloring is enabled, we have to do this because tokens can vary in width
	 */
	else {
		Token tokens;
		if (painter.currentLineIndex == line && painter.currentLineTokens != null) {
			tokens = painter.currentLineTokens;
		} else {
			painter.currentLineIndex = line;
			tokens = painter.currentLineTokens = tokenMarker.markTokens(lineSegment, line);
		}

		Font defaultFont = painter.getFont();
		SyntaxStyle[] styles = painter.getStyles();

		for (;;) {
			byte id = tokens.id;
			if (id == Token.END) {
				return x;
			}

			if (id == Token.NULL) {
				fm = painter.getFontMetrics();
			} else {
				fm = styles[id].getFontMetrics(defaultFont);
			}

			int length = tokens.length;

			if (offset + segmentOffset < lineSegment.offset + length) {
				lineSegment.count = offset - (lineSegment.offset - segmentOffset);
				return x + Utilities.getTabbedTextWidth(lineSegment, fm, x, painter, 0);
			} else {
				lineSegment.count = length;
				x += Utilities.getTabbedTextWidth(lineSegment, fm, x, painter, 0);
				lineSegment.offset += length;
			}
			tokens = tokens.next;
		}
	}
}