本文整理汇总了Java中javax.swing.text.StyledDocument.insertString方法的典型用法代码示例。如果您正苦于以下问题:Java StyledDocument.insertString方法的具体用法?Java StyledDocument.insertString怎么用?Java StyledDocument.insertString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.swing.text.StyledDocument
的用法示例。
在下文中一共展示了StyledDocument.insertString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: reallyLog
import javax.swing.text.StyledDocument; //导入方法依赖的package包/类
private void reallyLog(String text, Style style) {
if (log==null || text.length()==0) return;
int i=text.lastIndexOf('\n'), j=text.lastIndexOf('\r');
if (i>=0 && i<j) { i=j; }
StyledDocument doc=log.getStyledDocument();
try {
if (i<0) {
doc.insertString(doc.getLength(), text, style);
} else {
// Performs intelligent caret positioning
doc.insertString(doc.getLength(), text.substring(0,i+1), style);
log.setCaretPosition(doc.getLength());
if (i<text.length()-1) {
doc.insertString(doc.getLength(), text.substring(i+1), style);
}
}
} catch (BadLocationException e) {
// Harmless
}
if (style!=styleRed) { lastSize=doc.getLength(); }
}
示例2: testCallingFromAWTIsOk
import javax.swing.text.StyledDocument; //导入方法依赖的package包/类
public void testCallingFromAWTIsOk() throws Exception {
StyledDocument doc = support.openDocument();
doc.insertString(0, "Ble", null);
assertTrue("Modified", support.isModified());
class AWT implements Runnable {
boolean success;
public synchronized void run() {
success = support.canClose();
}
}
AWT b = new AWT();
javax.swing.SwingUtilities.invokeAndWait(b);
assertTrue("Ok, we managed to ask the question", b.success);
if (ErrManager.messages.length() > 0) {
fail("No messages should be reported: " + ErrManager.messages);
}
}
示例3: setColor
import javax.swing.text.StyledDocument; //导入方法依赖的package包/类
public void setColor(Color c,String s){
try {
StyledDocument doc = jTextPane1.getStyledDocument();
javax.swing.text.Style style = jTextPane1.addStyle("", null);
StyleConstants.setForeground(style, c);
doc.insertString(doc.getLength(), s, style);
jTextPane1.select(0, 10);
jTextPane1.setSelectedTextColor(Color.YELLOW);
} catch (BadLocationException ex) {
System.out.println(ex);
}
}
示例4: insertString
import javax.swing.text.StyledDocument; //导入方法依赖的package包/类
private static void insertString(final StyledDocument doc, final String comment, final int last, final int start, final Style defStyle) {
try {
doc.insertString(doc.getLength(), comment.substring(last, start), defStyle);
} catch (BadLocationException ex) {
Support.LOG.log(Level.SEVERE, null, ex);
}
}
示例5: testModifiedMove
import javax.swing.text.StyledDocument; //导入方法依赖的package包/类
public void testModifiedMove() throws Exception {
FileObject root = FileUtil.toFileObject(getWorkDir());
FileObject data = FileUtil.createData(root, "someFolder/someFile.obj");
DataObject obj = DataObject.find(data);
assertEquals( MyDataObject.class, obj.getClass());
assertEquals(MyLoader.class, obj.getLoader().getClass());
EditorCookie ec = obj.getLookup().lookup(EditorCookie.class);
assertNotNull("Editor cookie found", ec);
ec.open();
JEditorPane[] arr = openedPanes(ec);
assertEquals("One pane is opened", 1, arr.length);
StyledDocument doc = ec.openDocument();
doc.insertString(0, "Ahoj", null);
assertTrue("Modified", obj.isModified());
Thread.sleep(100);
FileObject newFolder = FileUtil.createFolder(root, "otherFolder");
DataFolder df = DataFolder.findFolder(newFolder);
obj.move(df);
assertEquals(newFolder, obj.getPrimaryFile().getParent());
assertEquals("Still one editor", 1, openedPanes(ec).length);
DD.assertNoCalls();
}
示例6: insertColonyButtons
import javax.swing.text.StyledDocument; //导入方法依赖的package包/类
private void insertColonyButtons(StyledDocument doc, List<Colony> colonies) throws Exception {
for (Colony colony : colonies) {
StyleConstants.setComponent(doc.getStyle("button"), createColonyButton(colony, false));
doc.insertString(doc.getLength(), " ", doc.getStyle("button"));
doc.insertString(doc.getLength(), ", ", doc.getStyle("regular"));
}
doc.remove(doc.getLength() - 2, 2);
}
示例7: testModifyAndBlockAWTAndTryToClose
import javax.swing.text.StyledDocument; //导入方法依赖的package包/类
public void testModifyAndBlockAWTAndTryToClose () throws Exception {
StyledDocument doc = support.openDocument();
doc.insertString(0, "Ble", null);
assertTrue("Modified", support.isModified());
class Block implements Runnable {
public synchronized void run() {
try {
wait();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
Block b = new Block();
javax.swing.SwingUtilities.invokeLater(b);
boolean success = support.canClose();
synchronized (b) {
b.notifyAll();
}
assertFalse("Support cannot close as we cannot ask the user", success);
if (ErrManager.messages.indexOf("InterruptedException") == -1) {
fail("InterruptedException exception should be reported: " + ErrManager.messages);
}
}
示例8: insertMessage
import javax.swing.text.StyledDocument; //导入方法依赖的package包/类
private void insertMessage(StyledDocument document, ModelMessage message,
Player player) throws BadLocationException {
for (Object o : message.splitLinks(player)) {
if (o instanceof String) {
document.insertString(document.getLength(), (String)o,
document.getStyle("regular"));
} else if (o instanceof JButton) {
JButton b = (JButton)o;
b.addActionListener(this);
StyleConstants.setComponent(document.getStyle("button"), b);
document.insertString(document.getLength(), " ",
document.getStyle("button"));
}
}
}
示例9: addToConsole
import javax.swing.text.StyledDocument; //导入方法依赖的package包/类
private void addToConsole(LogEntry logentry, boolean update) {
StyledDocument doc = console.textpane.getStyledDocument();
Style style = doc.addStyle("Style", null);
if(update) {
updateLogEntry(logentry);
}
boolean doesLog = true;
switch(logentry.getLevel()) {
case LogEntry.LEVELERROR:
doesLog = showError;
StyleConstants.setBackground(style, Color.WHITE);
StyleConstants.setForeground(style, Color.red);
break;
case LogEntry.LEVELINPUT:
doesLog = showInput;
StyleConstants.setBackground(style, Color.WHITE);
StyleConstants.setForeground(style, Color.BLUE);
break;
case LogEntry.LEVELLOW:
doesLog = showLow;
StyleConstants.setBackground(style, Color.WHITE);
StyleConstants.setForeground(style, Color.GRAY);
break;
case LogEntry.LEVELNORMAL:
doesLog = showNormal;
StyleConstants.setBackground(style, Color.WHITE);
StyleConstants.setForeground(style, Color.BLACK);
break;
case LogEntry.LEVELCOMMAND:
doesLog = showCommand;
StyleConstants.setBackground(style, Color.BLACK);
StyleConstants.setForeground(style, Color.YELLOW);
break;
}
if(doesLog) {
try {
doc.insertString(doc.getLength(), logentry.toString() + "\n", style);
} catch (Exception ex) {
logErr(String.format(lang.getProperty("error_while_adding_to_the_console", "Error while adding to the console: %s"), ex));
}
console.textpane.setCaretPosition(doc.getLength());
}
}
示例10: createFromTemplate
import javax.swing.text.StyledDocument; //导入方法依赖的package包/类
/** Creates a new Java source from the template. Unlike the standard FileEntry.Format,
this indents the resulting text using an indentation engine.
*/
@Override
public FileObject createFromTemplate (FileObject f, String name) throws IOException {
String ext = getFile ().getExt ();
if (name == null) {
name = FileUtil.findFreeFileName(f, getFile ().getName (), ext);
}
FileObject fo = f.createData (name, ext);
java.text.Format frm = createFormat (f, name, ext);
InputStream is=getFile ().getInputStream ();
Charset encoding = FileEncodingQuery.getEncoding(getFile());
Reader reader = new InputStreamReader(is,encoding);
BufferedReader r = new BufferedReader (reader);
StyledDocument doc = createDocument(createEditorKit(fo.getMIMEType()));
IndentEngine eng = (IndentEngine)indentEngine.get();
if (eng == null) eng = IndentEngine.find(doc);
Object attr = getFile().getAttribute(EA_PREFORMATTED);
boolean preformatted = false;
if (attr != null && attr instanceof Boolean) {
preformatted = ((Boolean)attr).booleanValue();
}
try {
FileLock lock = fo.lock ();
try {
encoding = FileEncodingQuery.getEncoding(fo);
OutputStream os=fo.getOutputStream(lock);
OutputStreamWriter w = new OutputStreamWriter(os, encoding);
try {
String line = null;
String current;
int offset = 0;
while ((current = r.readLine ()) != null) {
if (line != null) {
// newline between lines
doc.insertString(offset, NEWLINE, null);
offset++;
}
line = frm.format (current);
// partial indentation used only for pre-formatted sources
// see #19178 etc.
if (!preformatted || !line.equals(current)) {
line = fixupGuardedBlocks(safeIndent(eng, line, doc, offset));
}
doc.insertString(offset, line, null);
offset += line.length();
}
doc.insertString(doc.getLength(), NEWLINE, null);
w.write(doc.getText(0, doc.getLength()));
} catch (javax.swing.text.BadLocationException e) {
} finally {
w.close ();
}
} finally {
lock.releaseLock ();
}
} finally {
r.close ();
}
// copy attributes
FileUtil.copyAttributes (getFile (), fo);
// hack to overcome package-private modifier in setTemplate(fo, boolean)
fo.setAttribute(DataObject.PROP_TEMPLATE, null);
return fo;
}
示例11: testModifyCloseDiscard
import javax.swing.text.StyledDocument; //导入方法依赖的package包/类
public void testModifyCloseDiscard() throws Exception {
InstanceContent ic = new InstanceContent();
Lookup context = new AbstractLookup(ic);
final CES ces = createSupport(context, ic);
ic.add(ces);
ic.add(10);
EventQueue.invokeAndWait(new Runnable() {
@Override
public void run() {
ces.open();
}
});
final CloneableTopComponent tc = (CloneableTopComponent) ces.findPane();
assertNotNull("Component found", tc);
ces.openDocument();
EventQueue.invokeAndWait(new Runnable() {
@Override
public void run() {
assertEquals("One pane is open", 1, ces.getOpenedPanes().length);
}
});
StyledDocument doc = ces.getDocument();
assertEquals("Is empty", 0, doc.getLength());
assertNotNull("Document is opened", doc);
doc.insertString(0, "Ahoj", null);
assertTrue("Is modified", ces.isModified());
final Savable sava = tc.getLookup().lookup(Savable.class);
assertNotNull("Savable present", sava);
assertEquals("Also part of the global registry", sava, Savable.REGISTRY.lookup(Savable.class));
final CountDownLatch docChanged = new CountDownLatch(1);
ces.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (EditorCookie.Observable.PROP_DOCUMENT.equals(evt.getPropertyName())) {
docChanged.countDown();
}
}
});
EventQueue.invokeAndWait(new Runnable() {
@Override
public void run() {
assertTrue("Can be closed without problems", tc.close());
}
});
docChanged.await(5000, TimeUnit.MILLISECONDS);
assertTrue("CES did not fire change of the document after close", docChanged.getCount() == 0);
assertFalse("Not modified", ces.isModified());
StyledDocument newDoc = ces.openDocument();
assertEquals("The document is reverted to empty state", 0, newDoc.getLength());
}
示例12: print
import javax.swing.text.StyledDocument; //导入方法依赖的package包/类
public static final void print(final String s, final ConsoleStyle style) {
final StyledDocument doc = textPane.getStyledDocument();
if (style == null || style.equals(ConsoleStyle.INFO)) StyleConstants.setForeground(Console.style, Color.BLACK);
else if (style.equals(ConsoleStyle.WARNING)) StyleConstants.setForeground(Console.style, Color.ORANGE);
else StyleConstants.setForeground(Console.style, Color.RED);
try {
doc.insertString(doc.getLength(), s + "\n", Console.style);
} catch (final BadLocationException e) {
}
}
示例13: doCreateInteriorSection
import javax.swing.text.StyledDocument; //导入方法依赖的package包/类
/** Create new interior guarded section at a specified place.
* @param pos section to create the new one after
* @param name the name of the new section
* @exception IllegalArgumentException if the name is already in use
* @exception BadLocationException if it is not possible to create a
* new guarded section here
*/
private InteriorSection doCreateInteriorSection(final Position pos,
final String name)
throws IllegalArgumentException, BadLocationException {
StyledDocument loadedDoc = null;
loadedDoc = this.editor.getDocument();
final StyledDocument doc = loadedDoc;
final InteriorSectionImpl[] sect = new InteriorSectionImpl[1];
final BadLocationException[] blex = new BadLocationException[1];
Runnable r = new Runnable() {
public void run() {
try {
int where = pos.getOffset();
doc.insertString(where, "\n \n \n \n", null); // NOI18N
sect[0] = createInteriorSectionImpl(
name,
PositionBounds.create(where + 1, where + 2, GuardedSectionsImpl.this),
PositionBounds.createBodyBounds(where + 3, where + 4, GuardedSectionsImpl.this),
PositionBounds.create(where + 5, where + 6, GuardedSectionsImpl.this)
);
sections.put(sect[0].getName(), sect[0]);
sect[0].markGuarded(doc);
} catch (BadLocationException ex) {
blex[0] = ex;
}
}
};
doRunAtomic(doc, r);
if (blex[0] == null) {
synchronized (this.sections) {
sections.put(name, sect[0]);
return (InteriorSection) sect[0].guard;
}
} else {
throw (BadLocationException) new BadLocationException(
"wrong offset", blex[0].offsetRequested() // NOI18N
).initCause(blex[0]);
}
}
示例14: doCreateSimpleSection
import javax.swing.text.StyledDocument; //导入方法依赖的package包/类
private SimpleSection doCreateSimpleSection(final Position pos, final Position end, final String name)
throws /*IllegalArgumentException,*/ BadLocationException {
StyledDocument loadedDoc = null;
loadedDoc = this.editor.getDocument();
final StyledDocument doc = loadedDoc;
final SimpleSectionImpl[] sect = new SimpleSectionImpl[1];
final BadLocationException[] blex = new BadLocationException[1];
Runnable r = new Runnable() {
public void run() {
try {
int where = pos.getOffset();
int offEnd = where +2;
int offStart = where;
if (end != null) {
offEnd = end.getOffset();
offStart = pos.getOffset();
} else {
offStart = where + 1;
offEnd = where + 2;
doc.insertString(where, "\n \n", null); // NOI18N
}
sect[0] = createSimpleSectionImpl(name, PositionBounds.create(offStart, offEnd, GuardedSectionsImpl.this));
sect[0].markGuarded(doc);
} catch (BadLocationException ex) {
blex[0] = ex;
}
}
};
doRunAtomic(doc, r);
if (blex[0] == null) {
synchronized (this.sections) {
sections.put(name, sect[0]);
return (SimpleSection) sect[0].guard;
}
} else {
throw (BadLocationException) new BadLocationException(
"wrong offset", blex[0].offsetRequested() // NOI18N
).initCause(blex[0]);
}
}
示例15: addAuthor
import javax.swing.text.StyledDocument; //导入方法依赖的package包/类
private void addAuthor (JTextPane pane, RevisionItem item, boolean selected, Collection<SearchHighlight> highlights) throws BadLocationException {
LogEntry entry = item.getUserData();
StyledDocument sd = pane.getStyledDocument();
clearSD(pane, sd);
Style selectedStyle = createSelectedStyle(pane);
Style normalStyle = createNormalStyle(pane);
Style style;
if (selected) {
style = selectedStyle;
} else {
style = normalStyle;
}
Style authorStyle = createAuthorStyle(pane, normalStyle);
Style hiliteStyle = createHiliteStyleStyle(pane, normalStyle, searchHiliteAttrs);
String author = entry.getAuthor();
AuthorLinker l = linkerSupport.getLinker(VCSHyperlinkSupport.AuthorLinker.class, id);
if(l == null) {
VCSKenaiAccessor.KenaiUser kenaiUser = getKenaiUser(author);
if (kenaiUser != null) {
l = new VCSHyperlinkSupport.AuthorLinker(kenaiUser, authorStyle, sd, author);
linkerSupport.add(l, id);
}
}
int pos = sd.getLength();
if(l != null) {
l.insertString(sd, selected ? style : null);
} else {
sd.insertString(sd.getLength(), author, style);
}
if (!selected) {
for (SearchHighlight highlight : highlights) {
if (highlight.getKind() == SearchHighlight.Kind.AUTHOR) {
int doclen = sd.getLength();
String highlightMessage = highlight.getSearchText();
String authorText = sd.getText(pos, doclen - pos).toLowerCase();
int idx = authorText.indexOf(highlightMessage);
if (idx > -1) {
sd.setCharacterAttributes(doclen - authorText.length() + idx, highlightMessage.length(), hiliteStyle, false);
}
}
}
}
}