本文整理汇总了Java中javax.swing.text.DefaultStyledDocument.insertString方法的典型用法代码示例。如果您正苦于以下问题:Java DefaultStyledDocument.insertString方法的具体用法?Java DefaultStyledDocument.insertString怎么用?Java DefaultStyledDocument.insertString使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.swing.text.DefaultStyledDocument
的用法示例。
在下文中一共展示了DefaultStyledDocument.insertString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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));
}
}
示例2: 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;
}
示例3: setContent
import javax.swing.text.DefaultStyledDocument; //导入方法依赖的package包/类
/**
* Set the content of the syntax highlighter. It is better to set other
* settings first and set this the last.
*
* @param content the content to set
*/
public void setContent(String content) {
String newContent = content == null ? "" : content;
DefaultStyledDocument document = (DefaultStyledDocument) getDocument();
try {
document.remove(0, document.getLength());
if (theme != null) {
document.insertString(0, newContent, theme.getPlain().getAttributeSet());
} else {
document.insertString(0, newContent, new SimpleAttributeSet());
}
} catch (BadLocationException ex) {
LOG.log(Level.SEVERE, null, ex);
}
//setCaretPosition(0);
// clear the style list
styleList = null;
}
示例4: testWrite
import javax.swing.text.DefaultStyledDocument; //导入方法依赖的package包/类
public void testWrite() throws Exception {
StringWriter writer = new StringWriter();
final String content = "Hello, World!";
final int start = 1;
final int end = 4;
DefaultStyledDocument doc = new DefaultStyledDocument();
doc.insertString(0, content, null);
editorKit.write(writer, doc, start, end);
String output = writer.toString();
assertTrue(output.indexOf("<html>") != -1);
if (isHarmony()) {
assertFalse(output.indexOf(content) != -1);
assertTrue(output.indexOf(content.substring(start, end)) != -1);
}
writer = new StringWriter();
doc = (HTMLDocument)editorKit.createDefaultDocument();
doc.insertString(0, content, null);
editorKit.write(writer, doc, start, end);
output = writer.toString();
assertTrue(output.indexOf("<html>") != -1);
assertFalse(output.indexOf(content) != -1);
assertTrue(output.indexOf(content.substring(start, end)) != -1);
}
示例5: append
import javax.swing.text.DefaultStyledDocument; //导入方法依赖的package包/类
@Override
public void append(String str, Color c) {
StyleContext sc = StyleContext.getDefaultStyleContext();
AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c);
aset = sc.addAttribute(aset, StyleConstants.FontFamily, "Sans");
aset = sc.addAttribute(aset, StyleConstants.Alignment, StyleConstants.ALIGN_JUSTIFIED);
/*
* int len = area.getDocument().getLength(); area.setCaretPosition(len);
* area.setCharacterAttributes(aset, false); area.replaceSelection(str);
*/
DefaultStyledDocument document = (DefaultStyledDocument) area.getDocument();
try {
if (area.getText().length() < 3)
area.setText("");
// document.insertString(0, str, aset);
// else
document.insertString(document.getEndPosition().getOffset() - 1, str, aset);
} catch (BadLocationException e) {}
}
示例6: createDocument
import javax.swing.text.DefaultStyledDocument; //导入方法依赖的package包/类
private Document createDocument(Language lang, String text) {
try {
DefaultStyledDocument doc = new DefaultStyledDocument();
doc.putProperty(Language.class, lang);
doc.insertString(0, text, SimpleAttributeSet.EMPTY);
return doc;
} catch (BadLocationException e) {
fail(e.getMessage());
return null;
}
}
示例7: appendText
import javax.swing.text.DefaultStyledDocument; //导入方法依赖的package包/类
/**
* Appends the given text in the given color to the content written on this
* textpane.
*
* @param mText
* The text to append.
*
* @param mColor
* The color to write the text in.
*/
public void appendText(final String mText, final Color mColor) {
DefaultStyledDocument s = (DefaultStyledDocument) this.getStyledDocument();
StyleContext context = new StyleContext();
Style style = context.addStyle("test", null);
StyleConstants.setForeground(style, mColor);
try {
s.insertString(s.getLength(), mText, style);
} catch (BadLocationException e) {
e.printStackTrace();
}
}
示例8: writeTextNode
import javax.swing.text.DefaultStyledDocument; //导入方法依赖的package包/类
private static void writeTextNode(DefaultStyledDocument doc,
StyleContext styles, Node node, int indent,
Collection<IdentifierLocation> idLocations, boolean flagged)
throws BadLocationException {
String text = CoqDoc.getTextFromTextNode(node);
Style style = styles.getStyle(STYLE_TEXT);
if(flagged) style.addAttributes(styles.getStyle(STYLE_FLAGGED));
doc.insertString(doc.getLength(), text, style);
}
示例9: writeKeywordNode
import javax.swing.text.DefaultStyledDocument; //导入方法依赖的package包/类
private static void writeKeywordNode(DefaultStyledDocument doc,
StyleContext styles, Node node, int indent,
Collection<IdentifierLocation> idLocations, boolean flagged)
throws BadLocationException {
Style style = styles.getStyle(STYLE_KEYWORD);
if(flagged) style.addAttributes(styles.getStyle(STYLE_FLAGGED));
doc.insertString(doc.getLength(), getKeywordFromKeywordNode(node), style);
}
示例10: writeIdentifierNode
import javax.swing.text.DefaultStyledDocument; //导入方法依赖的package包/类
private static void writeIdentifierNode(DefaultStyledDocument doc,
StyleContext styles, Node node, int indent,
Collection<IdentifierLocation> idLocations, boolean flagged)
throws BadLocationException {
String id = CoqDoc.getIDFromIdentifierNode(node);
int offset = doc.getLength();
Style style = styles.getStyle(STYLE_IDENTIFIER);
if(flagged) style.addAttributes(styles.getStyle(STYLE_FLAGGED));
doc.insertString(offset, id, style);
if(idLocations != null) {
idLocations.add(new IdentifierLocation(offset, id.length(), id));
}
}
示例11: buildDocument
import javax.swing.text.DefaultStyledDocument; //导入方法依赖的package包/类
/**
* buildDocument.
*/
private void buildDocument() {
final StyleContext context = new StyleContext();
document = new DefaultStyledDocument(context);
final Style style = context.getStyle(StyleContext.DEFAULT_STYLE);
StyleConstants.setAlignment(style, StyleConstants.ALIGN_LEFT);
StyleConstants.setFontSize(style, 14);
StyleConstants.setSpaceAbove(style, 4);
StyleConstants.setSpaceBelow(style, 4);
// Insert content
try {
document.insertString(document.getLength(), message, style);
} catch (final BadLocationException badLocationException) {
log.error(badLocationException.getMessage());
}
final SimpleAttributeSet attributes = new SimpleAttributeSet();
StyleConstants.setBold(attributes, true);
StyleConstants.setItalic(attributes, true);
// Third style for icon/component
final Style labelStyle = context.getStyle(StyleContext.DEFAULT_STYLE);
final Icon icon = new ImageIcon("Computer.gif");
final JLabel label = new JLabel(icon);
StyleConstants.setComponent(labelStyle, label);
}
示例12: string2document
import javax.swing.text.DefaultStyledDocument; //导入方法依赖的package包/类
private final DefaultStyledDocument string2document(String text) {
DefaultStyledDocument doc = new DefaultStyledDocument();
SimpleAttributeSet set = new SimpleAttributeSet();
try {
doc.insertString(0, text, set);
} catch (BadLocationException e) {
System.err.println("EditorType: " + e);
e.printStackTrace();
}
return doc;
}
示例13: getFormatedDocument
import javax.swing.text.DefaultStyledDocument; //导入方法依赖的package包/类
public StyledDocument getFormatedDocument(String content){
StyleContext context = new StyleContext();
DefaultStyledDocument document = new DefaultStyledDocument(context);
try{
String tagedText= translateLine(content, false);
int pointer=0;
int tagIndex=0;
while(tagedText.indexOf(BalloonTemplateHandler.START_TAG,pointer)!=-1){
tagIndex=tagedText.indexOf(BalloonTemplateHandler.START_TAG,pointer);
// Is there any character before the strat tag which is not parsed yet?
if (tagIndex!=pointer){
document.insertString(document.getLength(), tagedText.substring(pointer, tagIndex), NewCSVColumnDialog.getDefaultStyle());
pointer = tagIndex;
}
pointer+=BalloonTemplateHandler.START_TAG.length();
tagIndex=tagedText.indexOf(BalloonTemplateHandler.END_TAG,pointer);
document.insertString(document.getLength(), tagedText.substring(pointer, tagIndex), NewCSVColumnDialog.getLabelStyle());
pointer=tagIndex+BalloonTemplateHandler.END_TAG.length();
}
if (pointer!=tagedText.length()-1){
document.insertString(document.getLength(), tagedText.substring(pointer,tagedText.length()), NewCSVColumnDialog.getDefaultStyle());
}
// check for [EOL]
pointer=0;
tagIndex=0;
tagedText= document.getText(0, document.getLength());
String lineSeparator=System.getProperty("line.separator");
while(tagedText.indexOf(lineSeparator,pointer)!=-1){
tagIndex=tagedText.indexOf(lineSeparator,pointer);
document.remove(tagIndex,lineSeparator.length());
document.insertString(tagIndex,NewCSVColumnDialog.EOL,
NewCSVColumnDialog.getEOLStyle());
pointer=tagIndex+NewCSVColumnDialog.EOL.length();
tagedText= document.getText(0, document.getLength());
}
}catch(Exception e){}
return document;
}
示例14: CSVColumns
import javax.swing.text.DefaultStyledDocument; //导入方法依赖的package包/类
public CSVColumns( String title, String textcontent, String comment){
this.title=title;
this.textcontent=textcontent;
this.comment=comment;
StyleContext context = new StyleContext();
document = new DefaultStyledDocument(context);
try {
document.insertString(0, textcontent, NewCSVColumnDialog.getDefaultStyle());
} catch (BadLocationException e) {}
}
示例15: testDocumentEventSimilarity
import javax.swing.text.DefaultStyledDocument; //导入方法依赖的package包/类
public void testDocumentEventSimilarity() throws Exception {
System.out.println("testDocumentEventSimilarity");
DefaultStyledDocument styled = new DefaultStyledDocument();
OutWriter ow = new OutWriter ();
OutputDocument doc = new OutputDocument (ow);
ODListener docListener = new ODListener(doc);
ODListener styListener = new ODListener(styled);
String s = "This is a string I will append";
styled.insertString(styled.getLength(), s + OutWriter.LINE_SEPARATOR, SimpleAttributeSet.EMPTY);
ow.println (s);
ow.flush();
docListener.assertChanged();
styListener.assertChanged();
styled.insertString(styled.getLength(), s + OutWriter.LINE_SEPARATOR, SimpleAttributeSet.EMPTY);
ow.println (s);
ow.flush();
// //Wait for async event firing from output document
// SwingUtilities.invokeAndWait (new Runnable() {
// public void run() {
// System.currentTimeMillis();
// }
// });
// Thread.currentThread().sleep (1000);
int styLen = styled.getLength();
int docLen = doc.getLength();
assertTrue ("Length should be same, but OutputDocument length is " +
docLen + " and similar StyledDocument length is " + styLen,
doc.getLength() == styled.getLength());
DocumentEvent docEvent = docListener.getEvent();
DocumentEvent styEvent = styListener.getEvent();
assertNotNull("StyledDocument should have fired an event", styEvent);
assertNotNull("OutputDocument should have fired an event", docEvent);
assertEventsIdentical (styled, doc, styEvent, docEvent);
//Stress test it to ensure no off-by-ones that show up only when the file is large
for (int i = 0; i < 10; i++) {
for (int j=0; j < STRINGS.length; j++) {
styled.insertString(styled.getLength(), s + OutWriter.LINE_SEPARATOR, SimpleAttributeSet.EMPTY);
ow.println (s);
ow.flush();
// //Wait for async event firing from output document
// SwingUtilities.invokeAndWait (new Runnable() {
// public void run() {
// System.currentTimeMillis();
// }
// });
// SwingUtilities.invokeAndWait (new Runnable() {
// public void run() {
// System.currentTimeMillis();
// }
// });
// Thread.currentThread().sleep (500);
styLen = styled.getLength();
docLen = doc.getLength();
assertTrue ("Length should be same, but OutputDocument length is " +
docLen + " and similar StyledDocument length is " + styLen,
doc.getLength() == styled.getLength());
docEvent = docListener.getEvent();
styEvent = styListener.getEvent();
assertEventsIdentical (styled, doc, styEvent, docEvent);
}
}
}