use of javax.swing.text.MutableAttributeSet in project omegat by omegat-org.
the class Document3 method setAlignment.
/**
* Set alignment for specified part of text.
*
* @param beginOffset
* begin offset
* @param endOffset
* end offset
* @param isRightAlignment
* false - left alignment, true - right alignment
*/
protected void setAlignment(int beginOffset, int endOffset, boolean isRightAlignment) {
try {
writeLock();
DefaultDocumentEvent changes = new DefaultDocumentEvent(beginOffset, endOffset - beginOffset, DocumentEvent.EventType.CHANGE);
Element root = getDefaultRootElement();
int parBeg = root.getElementIndex(beginOffset);
int parEnd = root.getElementIndex(endOffset - 1);
for (int par = parBeg; par <= parEnd; par++) {
Element el = root.getElement(par);
MutableAttributeSet attr = (MutableAttributeSet) el.getAttributes();
attr.addAttribute(StyleConstants.Alignment, isRightAlignment ? StyleConstants.ALIGN_RIGHT : StyleConstants.ALIGN_LEFT);
}
changes.end();
fireChangedUpdate(changes);
} finally {
writeUnlock();
}
}
use of javax.swing.text.MutableAttributeSet in project omegat by omegat-org.
the class FontFallbackListener method getAttributes.
private AttributeSet getAttributes(Font font) {
MutableAttributeSet attrs = new SimpleAttributeSet();
StyleConstants.setFontFamily(attrs, font.getFamily());
StyleConstants.setFontSize(attrs, defaultFont.getSize());
return attrs;
}
use of javax.swing.text.MutableAttributeSet in project opennars by opennars.
the class SwingText method print.
public void print(final Color color, final Color bgColor, CharSequence text, Action action) {
MutableAttributeSet aset = getInputAttributes();
StyleConstants.setForeground(aset, color);
StyleConstants.setBackground(aset, bgColor != null ? bgColor : Color.BLACK);
try {
if (action == null) {
doc.insertString(doc.getLength(), text.toString(), aset);
} else {
// http://stackoverflow.com/questions/16131811/clickable-text-in-a-jtextpane
Style link = doc.addStyle(null, StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE));
StyleConstants.setForeground(link, color);
// StyleConstants.setUnderline(link, true);
// StyleConstants.setBold(link, true);
link.addAttribute("linkact", action);
doc.insertString(doc.getLength(), text.toString(), link);
}
} catch (BadLocationException ex) {
Logger.getLogger(SwingText.class.getName()).log(Level.SEVERE, null, ex);
}
}
use of javax.swing.text.MutableAttributeSet in project alfresco-repository by Alfresco.
the class HtmlMetadataExtracter method extractRaw.
@Override
protected Map<String, Serializable> extractRaw(ContentReader reader) throws Throwable {
final Map<String, Serializable> rawProperties = newRawMap();
HTMLEditorKit.ParserCallback callback = new HTMLEditorKit.ParserCallback() {
StringBuffer title = null;
boolean inHead = false;
public void handleText(char[] data, int pos) {
if (title != null) {
title.append(data);
}
}
public void handleComment(char[] data, int pos) {
// Perhaps sniff for Office 9+ metadata in here?
}
public void handleStartTag(HTML.Tag t, MutableAttributeSet a, int pos) {
if (HTML.Tag.HEAD.equals(t)) {
inHead = true;
} else if (HTML.Tag.TITLE.equals(t) && inHead) {
title = new StringBuffer();
} else
handleSimpleTag(t, a, pos);
}
public void handleEndTag(HTML.Tag t, int pos) {
if (HTML.Tag.HEAD.equals(t)) {
inHead = false;
} else if (HTML.Tag.TITLE.equals(t) && title != null) {
putRawValue(KEY_TITLE, title.toString(), rawProperties);
title = null;
}
}
public void handleSimpleTag(HTML.Tag t, MutableAttributeSet a, int pos) {
if (HTML.Tag.META.equals(t)) {
Object nameO = a.getAttribute(HTML.Attribute.NAME);
Object valueO = a.getAttribute(HTML.Attribute.CONTENT);
if (nameO == null || valueO == null)
return;
String name = nameO.toString();
if (name.equalsIgnoreCase("creator") || name.equalsIgnoreCase("author") || name.equalsIgnoreCase("dc.creator")) {
putRawValue(KEY_AUTHOR, valueO.toString(), rawProperties);
} else if (name.equalsIgnoreCase("description") || name.equalsIgnoreCase("dc.description")) {
putRawValue(KEY_DESCRIPTION, valueO.toString(), rawProperties);
}
}
}
public void handleError(String errorMsg, int pos) {
}
};
String charsetGuess = "UTF-8";
int tries = 0;
while (tries < 3) {
rawProperties.clear();
Reader r = null;
InputStream cis = null;
try {
cis = reader.getContentInputStream();
// TODO: for now, use default charset; we should attempt to map from html meta-data
r = new InputStreamReader(cis, charsetGuess);
HTMLEditorKit.Parser parser = new ParserDelegator();
parser.parse(r, callback, tries > 0);
break;
} catch (ChangedCharSetException ccse) {
tries++;
charsetGuess = ccse.getCharSetSpec();
int begin = charsetGuess.indexOf("charset=");
if (begin > 0)
charsetGuess = charsetGuess.substring(begin + 8, charsetGuess.length());
reader = reader.getReader();
} finally {
if (r != null)
r.close();
if (cis != null)
cis.close();
}
}
// Done
return rawProperties;
}
Aggregations