use of java.text.AttributedString in project hid-serial by rayshobby.
the class StyledString method readObject.
private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
ois.defaultReadObject();
// Recreate transient elements
spacer = getParagraghSpacer(wrapWidth);
styledText = new AttributedString(plainText);
styledText = insertParagraphMarkers(plainText, styledText);
linesInfo = new LinkedList<TextLayoutInfo>();
applyAttributes();
}
use of java.text.AttributedString in project hid-serial by rayshobby.
the class StyledString method convertToSingleLineText.
/**
* Converts this StyledString from multi-line to single-line by replacing all EOL
* characters with the space character
* for paragraphs
* @param ptext
* @param as
* @return the converted string
*/
StyledString convertToSingleLineText() {
// Make sure we have something to work with.
if (styledText == null || plainText == null) {
plainText = "";
styledText = new AttributedString(plainText);
} else {
// Scan through plain text and for each EOL replace the paragraph spacer from
// the attributed string (styledText).
int fromIndex = plainText.indexOf('\n', 0);
if (fromIndex >= 0) {
while (fromIndex >= 0) {
try {
// if text == "\n" then an exception is thrown
styledText.addAttribute(TextAttribute.CHAR_REPLACEMENT, ' ', fromIndex, fromIndex + 1);
fromIndex = plainText.indexOf('\n', fromIndex + 1);
} catch (Exception excp) {
break;
}
}
// Finally replace all EOL in the plainText
plainText = plainText.replace('\n', ' ');
}
}
wrapWidth = Integer.MAX_VALUE;
return this;
}
use of java.text.AttributedString in project jdk8u_jdk by JetBrains.
the class SwingUtilities2 method clipString.
/**
* Clips the passed in String to the space provided. NOTE: this assumes
* the string does not fit in the available space.
*
* @param c JComponent that will display the string, may be null
* @param fm FontMetrics used to measure the String width
* @param string String to display
* @param availTextWidth Amount of space that the string can be drawn in
* @return Clipped string that can fit in the provided space.
*/
public static String clipString(JComponent c, FontMetrics fm, String string, int availTextWidth) {
// c may be null here.
String clipString = "...";
availTextWidth -= SwingUtilities2.stringWidth(c, fm, clipString);
if (availTextWidth <= 0) {
//can not fit any characters
return clipString;
}
boolean needsTextLayout;
synchronized (charsBufferLock) {
int stringLength = syncCharsBuffer(string);
needsTextLayout = isComplexLayout(charsBuffer, 0, stringLength);
if (!needsTextLayout) {
int width = 0;
for (int nChars = 0; nChars < stringLength; nChars++) {
width += fm.charWidth(charsBuffer[nChars]);
if (width > availTextWidth) {
string = string.substring(0, nChars);
break;
}
}
}
}
if (needsTextLayout) {
AttributedString aString = new AttributedString(string);
if (c != null) {
aString.addAttribute(TextAttribute.NUMERIC_SHAPING, c.getClientProperty(TextAttribute.NUMERIC_SHAPING));
}
LineBreakMeasurer measurer = new LineBreakMeasurer(aString.getIterator(), BreakIterator.getCharacterInstance(), getFontRenderContext(c, fm));
string = string.substring(0, measurer.nextOffset(availTextWidth));
}
return string + clipString;
}
use of java.text.AttributedString in project jdk8u_jdk by JetBrains.
the class SwingUtilities2 method getTrimmedTrailingSpacesIterator.
private static AttributedCharacterIterator getTrimmedTrailingSpacesIterator(AttributedCharacterIterator iterator) {
int curIdx = iterator.getIndex();
char c = iterator.last();
while (c != CharacterIterator.DONE && Character.isWhitespace(c)) {
c = iterator.previous();
}
if (c != CharacterIterator.DONE) {
int endIdx = iterator.getIndex();
if (endIdx == iterator.getEndIndex() - 1) {
iterator.setIndex(curIdx);
return iterator;
} else {
AttributedString trimmedText = new AttributedString(iterator, iterator.getBeginIndex(), endIdx + 1);
return trimmedText.getIterator();
}
} else {
return null;
}
}
use of java.text.AttributedString in project jdk8u_jdk by JetBrains.
the class CodePointInputMethod method sendComposedText.
/**
* Send the composed text to the client.
*/
private void sendComposedText() {
AttributedString as = new AttributedString(buffer.toString());
as.addAttribute(TextAttribute.INPUT_METHOD_HIGHLIGHT, InputMethodHighlight.SELECTED_RAW_TEXT_HIGHLIGHT);
context.dispatchInputMethodEvent(InputMethodEvent.INPUT_METHOD_TEXT_CHANGED, as.getIterator(), 0, TextHitInfo.leading(insertionPoint), null);
}
Aggregations