本文整理匯總了Java中javax.swing.text.DefaultStyledDocument類的典型用法代碼示例。如果您正苦於以下問題:Java DefaultStyledDocument類的具體用法?Java DefaultStyledDocument怎麽用?Java DefaultStyledDocument使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
DefaultStyledDocument類屬於javax.swing.text包,在下文中一共展示了DefaultStyledDocument類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getCodeArea
import javax.swing.text.DefaultStyledDocument; //導入依賴的package包/類
private JTextPane getCodeArea(){
final StyleContext sc = new StyleContext();
final DefaultStyledDocument doc = new DefaultStyledDocument(sc);
final JTextPane codeArea = new JTextPane(doc);
codeArea.setBackground(new Color(0x25401C));
codeArea.setCaretColor(new Color(0xD1E8CE));
final Style bodyStyle = sc.addStyle("body", null);
bodyStyle.addAttribute(StyleConstants.Foreground, new Color(0x789C6C));
bodyStyle.addAttribute(StyleConstants.FontSize, 13);
bodyStyle.addAttribute(StyleConstants.FontFamily, "monospaced");
bodyStyle.addAttribute(StyleConstants.Bold, true);
doc.setLogicalStyle(0, bodyStyle);
return codeArea;
}
示例2: applyStyle
import javax.swing.text.DefaultStyledDocument; //導入依賴的package包/類
/**
* Apply the list of style to the script text pane. See
* {@link syntaxhighlighter.parser.Parser#parse(syntaxhighlighter.brush.Brush, boolean, char[], int, int)}.
*/
protected void applyStyle() {
if (theme == null || styleList == null) {
return;
}
DefaultStyledDocument document = (DefaultStyledDocument) getDocument();
// clear all the existing style
document.setCharacterAttributes(0, document.getLength(), theme.getPlain().getAttributeSet(), true);
// apply style according to the style list
for (String key : styleList.keySet()) {
List<ParseResult> posList = styleList.get(key);
SimpleAttributeSet attributeSet = theme.getStyle(key).getAttributeSet();
for (ParseResult pos : posList) {
document.setCharacterAttributes(pos.getOffset(), pos.getLength(), attributeSet, true);
}
}
repaint();
}
示例3: highlightText
import javax.swing.text.DefaultStyledDocument; //導入依賴的package包/類
/**
* This highlights the text within the specified range
*
* @param startingIndex where to start the highlight
* @param endingIndex where to end the highlight
* @param ensureVisible true to scroll to the text
* @param isEmphasized true to use an emphasized highlight (versus a regular highlight). Useful for showing the 'current' highlighted result.
*/
public void highlightText(int startingIndex, int endingIndex, boolean ensureVisible, boolean isEmphasized) {
int length = endingIndex - startingIndex;
AttributeSet style;
if (isEmphasized) {
style = emphasizedHighlightStyle;
} else {
style = highlightStyle;
}
((DefaultStyledDocument) textComponentToSearch.getDocument()).setCharacterAttributes(startingIndex, length, style, true);
if (ensureVisible) {
Utility.scrollToText(textComponentToSearch, startingIndex, endingIndex);
textComponentToSearch.setCaretPosition(endingIndex);
}
textComponentToSearch.repaint();
}
示例4: handleBadLocationException
import javax.swing.text.DefaultStyledDocument; //導入依賴的package包/類
/**
* Try and recover from a BadLocationException thrown when inserting a string
* into the log area. This method must only be called on the AWT event
* handling thread.
*/
private void handleBadLocationException(BadLocationException e,
String textToInsert, Style style) {
originalErr.println("BadLocationException encountered when writing to "
+ "the log area: " + e);
originalErr.println("trying to recover...");
Document newDocument = new DefaultStyledDocument();
try {
StringBuilder sb = new StringBuilder();
sb.append("An error occurred when trying to write a message to the log area. The log\n");
sb.append("has been cleared to try and recover from this problem.\n\n");
sb.append(textToInsert);
newDocument.insertString(0, sb.toString(), style);
} catch(BadLocationException e2) {
// oh dear, all bets are off now...
e2.printStackTrace(originalErr);
return;
}
// replace the log area's document with the new one
setDocument(newDocument);
}
示例5: writeWhitespaceNode
import javax.swing.text.DefaultStyledDocument; //導入依賴的package包/類
private static void writeWhitespaceNode(DefaultStyledDocument doc,
StyleContext styles, Node node, int indent,
Collection<IdentifierLocation> idLocations, boolean flagged) throws BadLocationException {
if(isWhitespaceNewlineNode(node)) {
doc.insertString(doc.getLength(), "\n", styles.getStyle(STYLE_WS));
if(indent > 0) {
doc.insertString(doc.getLength(), indent(indent), styles.getStyle(STYLE_WS));
}
} else if(isWhitespaceIndentNode(node)) {
doc.insertString(doc.getLength(), indent(1), styles.getStyle(STYLE_WS));
NodeList children = node.getChildNodes();
for(int i = 0; i < children.getLength(); i++) {
write(doc, styles, children.item(i), indent + 1, idLocations, flagged);
}
} else {
doc.insertString(doc.getLength(), " ", styles.getStyle(STYLE_WS));
}
}
示例6: makeDocument
import javax.swing.text.DefaultStyledDocument; //導入依賴的package包/類
public static DefaultStyledDocument makeDocument(CoqDocable term,
StyleContext styles, Collection<IdentifierLocation> idLocations,
String optionalSuffix) {
DefaultStyledDocument document = new DefaultStyledDocument();
try {
write(document, styles,
term.makeCoqDocTerm(CoqDoc.makeDocument()), 0,
idLocations, false);
if(optionalSuffix != null) {
document.insertString(document.getLength(), " ",
styles.getStyle(STYLE_WS));
document.insertString(document.getLength(), optionalSuffix,
styles.getStyle(STYLE_TEXT));
}
} catch(BadLocationException e) {
throw new RuntimeException(e);
}
return document;
}
示例7: write
import javax.swing.text.DefaultStyledDocument; //導入依賴的package包/類
private static void write(DefaultStyledDocument doc, StyleContext styles,
Node node, int indent, Collection<IdentifierLocation> idLocations,
boolean flagged)
throws BadLocationException {
if(isWhitespaceNode(node)) {
writeWhitespaceNode(doc, styles, node, indent, idLocations, flagged);
} else if(isTextNode(node)) {
writeTextNode(doc, styles, node, indent, idLocations, flagged);
} else if(isKeywordNode(node)) {
writeKeywordNode(doc, styles, node, indent, idLocations, flagged);
} else if(isIdentifierNode(node)) {
writeIdentifierNode(doc, styles, node, indent, idLocations, flagged);
} else if(isTermNode(node)) {
writeTermNode(doc, styles, node, indent, idLocations, flagged);
} else if(isFlaggedNode(node)) {
writeFlaggedNode(doc, styles, node, indent, idLocations, true);
} else {
throw new IllegalArgumentException("Unknown node type: " + XMLUtils.nodeToString(node));
}
}
示例8: extractFont
import javax.swing.text.DefaultStyledDocument; //導入依賴的package包/類
private Font extractFont(final StyleContext style, final int pos, final Element rootElement,
final AttributeSet attributes) {
Font font = null;
if (attributes.isDefined(StyleConstants.FontSize) || attributes.isDefined(StyleConstants.FontFamily)) {
font = style.getFont(attributes);
}
if (font == null) {
if (document instanceof DefaultStyledDocument) {
font = extractFontFromDefaultStyledDocument((DefaultStyledDocument) document, style, pos, rootElement);
}
else {
font = style.getFont(rootElement.getAttributes());
}
}
return font;
}
示例9: extractText
import javax.swing.text.DefaultStyledDocument; //導入依賴的package包/類
/**
* {@inheritDoc}
*/
public String extractText(InputStream stream, String type, String encoding) throws IOException {
try {
RTFEditorKit rek = new RTFEditorKit();
DefaultStyledDocument doc = new DefaultStyledDocument();
rek.read(stream, doc, 0);
String text = doc.getText(0, doc.getLength());
return text;
} catch (Throwable e) {
logger.warn("Failed to extract RTF text content", e);
throw new IOException(e.getMessage(), e);
} finally {
stream.close();
}
}
示例10: writeStyles
import javax.swing.text.DefaultStyledDocument; //導入依賴的package包/類
/**
* Write the styles used.
*/
protected void writeStyles() throws IOException
{
if(doc instanceof DefaultStyledDocument)
{
Enumeration<?> styles = ((DefaultStyledDocument)doc).getStyleNames();
while(styles.hasMoreElements())
writeStyle(doc.getStyle((String)styles.nextElement()));
}
else
{ // What else to do here?
Style s = doc.getStyle("default");
if(s != null)
writeStyle( s );
}
}
示例11: blockOpen
import javax.swing.text.DefaultStyledDocument; //導入依賴的package包/類
/**
* Instructs the parse buffer to create a block element with the given
* attributes.
*
* @param t the tag that requires opening a new block
* @param attr the attribute set for the new block
*/
protected void blockOpen(HTML.Tag t, MutableAttributeSet attr)
{
if (inImpliedParagraph())
blockClose(HTML.Tag.IMPLIED);
// Push the new tag on top of the stack.
parseStack.push(t);
DefaultStyledDocument.ElementSpec element;
AbstractDocument.AttributeContext ctx = getAttributeContext();
AttributeSet copy = attr.copyAttributes();
copy = ctx.addAttribute(copy, StyleConstants.NameAttribute, t);
element = new DefaultStyledDocument.ElementSpec(copy,
DefaultStyledDocument.ElementSpec.StartTagType);
parseBuffer.addElement(element);
}
示例12: addSpecialElement
import javax.swing.text.DefaultStyledDocument; //導入依賴的package包/類
/**
* Adds content that is specified in the attribute set.
*
* @param t the HTML.Tag
* @param a the attribute set specifying the special content
*/
protected void addSpecialElement(HTML.Tag t, MutableAttributeSet a)
{
if (t != HTML.Tag.FRAME && ! inParagraph())
{
blockOpen(HTML.Tag.IMPLIED, new SimpleAttributeSet());
}
a.addAttribute(StyleConstants.NameAttribute, t);
// The two spaces are required because some special elements like HR
// must be broken. At least two characters are needed to break into the
// two parts.
DefaultStyledDocument.ElementSpec spec =
new DefaultStyledDocument.ElementSpec(a.copyAttributes(),
DefaultStyledDocument.ElementSpec.ContentType,
new char[] {' '}, 0, 1 );
parseBuffer.add(spec);
}
示例13: testTrim
import javax.swing.text.DefaultStyledDocument; //導入依賴的package包/類
@Test
public void testTrim() throws Exception {
final StyledDocument doc = new DefaultStyledDocument();
final StringBuilder buffer = new StringBuilder();
for (int i = 0; i < 10; i++) {
buffer.append("\n");
}
doc.insertString(0, buffer.toString(), null);
ChatMessagePanel.trimLines(doc, 20);
assertEquals(10, doc.getLength());
ChatMessagePanel.trimLines(doc, 10);
assertEquals(10, doc.getLength());
ChatMessagePanel.trimLines(doc, 5);
assertEquals(5, doc.getLength());
ChatMessagePanel.trimLines(doc, 1);
assertEquals(1, doc.getLength());
}
示例14: writeStyles
import javax.swing.text.DefaultStyledDocument; //導入依賴的package包/類
/**
* Write the styles used.
*/
protected void writeStyles() throws IOException
{
if(doc instanceof DefaultStyledDocument)
{
Enumeration styles = ((DefaultStyledDocument)doc).getStyleNames();
while(styles.hasMoreElements())
writeStyle(doc.getStyle((String)styles.nextElement()));
}
else
{ // What else to do here?
Style s = doc.getStyle("default");
if(s != null)
writeStyle( s );
}
}
示例15: blockOpen
import javax.swing.text.DefaultStyledDocument; //導入依賴的package包/類
/**
* Instructs the parse buffer to create a block element with the given
* attributes.
*
* @param t the tag that requires opening a new block
* @param attr the attribute set for the new block
*/
protected void blockOpen(HTML.Tag t, MutableAttributeSet attr)
{
if (inImpliedParagraph())
blockClose(HTML.Tag.IMPLIED);
// Push the new tag on top of the stack.
parseStack.push(t);
DefaultStyledDocument.ElementSpec element;
AbstractDocument.AttributeContext ctx = getAttributeContext();
AttributeSet copy = attr.copyAttributes();
copy = ctx.addAttribute(copy, StyleConstants.NameAttribute, t);
element = new DefaultStyledDocument.ElementSpec(copy,
DefaultStyledDocument.ElementSpec.StartTagType);
parseBuffer.addElement(element);
}