private static int abbreviationLength( String text, int start, int end, FontMetrics metrics, int maxWidth, boolean replaceSeparators) { if (metrics.charWidth('m') * (end - start) <= maxWidth) return end - start; int abbrWidth = metrics.charWidth(ABBREVIATION_SUFFIX); int abbrLength = 0; CharSequenceSubSequence subSeq = new CharSequenceSubSequence(text, start, end); for (LineTokenizer lt = new LineTokenizer(subSeq); !lt.atEnd(); lt.advance()) { for (int i = 0; i < lt.getLength(); i++, abbrLength++) { abbrWidth += metrics.charWidth(subSeq.charAt(lt.getOffset() + i)); if (abbrWidth >= maxWidth) return abbrLength; } if (replaceSeparators && lt.getLineSeparatorLength() != 0) { abbrWidth += metrics.charWidth(RETURN_SYMBOL); if (abbrWidth >= maxWidth) return abbrLength; abbrLength += lt.getLineSeparatorLength(); } } return abbrLength; }
@Override public Dimension getPreferredSize() { if (myPreferredSize == null) { int maxLineLength = 0; int linesCount = 0; for (LineTokenizer lt = new LineTokenizer(myRawText); !lt.atEnd(); lt.advance()) { maxLineLength = Math.max(maxLineLength, lt.getLength()); linesCount++; } FontMetrics fontMetrics = ((EditorImpl) getEditor()) .getFontMetrics( myTextAttributes != null ? myTextAttributes.getFontType() : Font.PLAIN); int preferredHeight = getEditor().getLineHeight() * Math.max(1, linesCount); int preferredWidth = fontMetrics.charWidth('m') * maxLineLength; Insets insets = getInsets(); if (insets != null) { preferredHeight += insets.top + insets.bottom; preferredWidth += insets.left + insets.right; } myPreferredSize = new Dimension(preferredWidth, preferredHeight); } return myPreferredSize; }