/** * Set the default text for this control. If provided this text will be displayed in italic * whenever it is empty. * * @param dtext */ public void setDefaultText(String dtext) { if (dtext == null || dtext.length() == 0) defaultText = null; else { defaultText = new StyledString(dtext, wrapWidth); defaultText.addAttribute(G4P.POSTURE, G4P.POSTURE_OBLIQUE); } bufferInvalid = true; }
/** * Save the styled text used by this control to file. <br> * It will also save the start and end position of any text selection. * * @param fname the name of the file to use * @return true if saved successfully else false */ public boolean saveText(String fname) { if (stext == null) return false; if (hasSelection()) { stext.startIdx = startTLHI.tli.startCharIndex + startTLHI.thi.getInsertionIndex(); stext.endIdx = endTLHI.tli.startCharIndex + endTLHI.thi.getInsertionIndex(); } else { stext.startIdx = stext.endIdx = -1; } StyledString.save(winApp, stext, fname); return true; }
/** * Applies decoration styles to the decorated string and adds the styles of the previously * undecorated string. * * <p>If the <code>decoratedString</code> contains the <code>styledString</code>, then the result * keeps the styles of the <code>styledString</code> and styles the decorations with the <code> * decorationStyler</code>. Otherwise, the decorated string is returned without any styles. * * @param decoratedString the decorated string * @param decorationStyler the styler to use for the decoration or <code>null</code> for no styles * @param styledString the original styled string * @return the styled decorated string (can be the given <code>styledString</code>) * @since 3.5 */ public static StyledString styleDecoratedString( String decoratedString, StyledString.Styler decorationStyler, StyledString styledString) { String label = styledString.getString(); int originalStart = decoratedString.indexOf(label); if (originalStart == -1) { return new StyledString(decoratedString); // the decorator did something wild } if (decoratedString.length() == label.length()) return styledString; if (originalStart > 0) { StyledString newString = new StyledString(decoratedString.substring(0, originalStart), decorationStyler); newString.append(styledString); styledString = newString; } if (decoratedString.length() > originalStart + label.length()) { // decorator appended something return styledString.append( decoratedString.substring(originalStart + label.length()), decorationStyler); } return styledString; // no change }
/** * Load the styled string to be used by this control. <br> * It will also restore any text selection saved with the text. * * @param fname the name of the file to use * @return true if loaded successfully else false */ public boolean loadText(String fname) { StyledString ss = StyledString.load(winApp, fname); if (ss == null) return false; setStyledText(ss); // Now restore any text selection if (stext.startIdx >= 0) { // we have a selection // Selection starts at ... startTLHI = new TextLayoutHitInfo(); startTLHI.tli = stext.getTLIforCharNo(stext.startIdx); int pInLayout = stext.startIdx - startTLHI.tli.startCharIndex; if (pInLayout == 0) startTLHI.thi = startTLHI.tli.layout.getNextLeftHit(1); else startTLHI.thi = startTLHI.tli.layout.getNextRightHit(pInLayout - 1); // Selection ends at ... endTLHI = new TextLayoutHitInfo(); endTLHI.tli = stext.getTLIforCharNo(stext.endIdx); pInLayout = stext.endIdx - endTLHI.tli.startCharIndex; if (pInLayout == 0) endTLHI.thi = endTLHI.tli.layout.getNextLeftHit(1); else endTLHI.thi = endTLHI.tli.layout.getNextRightHit(pInLayout - 1); calculateCaretPos(endTLHI); } bufferInvalid = true; return true; }
/** * Get the default text for this control * * @return the default text without styling */ public String getDefaultText() { return defaultText.getPlainText(); }