((BaseDocument) doc).runAtomic(update); } else { update.run();
protected void substituteText(JTextComponent c, final int offset, final int len, String toAdd) { final BaseDocument doc = (BaseDocument) c.getDocument(); CharSequence prefix = getSubstitutionText(); String text = prefix.toString(); if(toAdd != null) { text += toAdd; } final String finalText = text; doc.runAtomic(new Runnable() { @Override public void run() { try { Position position = doc.createPosition(offset); doc.remove(offset, len); doc.insertString(position.getOffset(), finalText.toString(), null); } catch (BadLocationException ex) { Exceptions.printStackTrace(ex); } } }); }
void updateSchemaLocation(Document doc, final int offset, final String namespace, final String schemaLocation) { BaseDocument baseDoc = (BaseDocument) doc; final XMLSyntaxSupport syntaxSupport = (XMLSyntaxSupport) baseDoc.getSyntaxSupport(); baseDoc.runAtomic(new Runnable() { @Override public void run() { try { SyntaxElement element = syntaxSupport.getElementChain(offset); if (element instanceof StartTag) { Attr attr = ((StartTag) element).getAttributeNode("xsi:schemaLocation"); //NOI18N if (attr != null) { String val = attr.getValue(); if (val.indexOf(namespace) == -1) { attr.setValue(val + "\n " + namespace + " " + schemaLocation); //NOI18N } } } } catch (BadLocationException ex) { Exceptions.printStackTrace(ex); } } }); }
static void replaceText(JTextComponent textComponent, final int offset, final int length, final String text) { if (!textComponent.isEditable()) { return; } final Document document = textComponent.getDocument(); Runnable r = new Runnable() { public void run() { try { if (length > 0) { document.remove(offset, length); } document.insertString(offset, text, null); } catch (BadLocationException ble) { ErrorManager.getDefault().notify(ble); } } }; if (document instanceof BaseDocument) { ((BaseDocument)document).runAtomic(r); } else { r.run(); } } }
if (text != null) { doc.runAtomic(new Runnable() {
private void doSubstitute(final JTextComponent component, String toAdd, final int backOffset) { final BaseDocument doc = (BaseDocument) component.getDocument(); final int caretOffset = component.getCaretPosition(); final String value = getText() + (toAdd != null && (!toAdd.equals("/") || (toAdd.equals("/") && !getText().endsWith(toAdd))) ? toAdd : ""); //NOI18N // Update the text doc.runAtomic(new Runnable() { @Override public void run() { try { String pfx = doc.getText(anchor, caretOffset - anchor); doc.remove(caretOffset - pfx.length(), pfx.length()); doc.insertString(caretOffset - pfx.length(), value, null); component.setCaretPosition(component.getCaretPosition() - backOffset); } catch (BadLocationException e) { Exceptions.printStackTrace(e); } } }); }
private void runTransaction() { Runnable run = new Runnable() { public void run() { try { transaction(); } catch (Exception ex) { // throw RuntimeException to inform runAtomic that // run() hasn't finished correctly throw new FileModelTransactionException(ex); } } }; try { getDoc().runAtomic(run); } catch (FileModelTransactionException ex) { SceneManager.error("Transaction failed.", ex.getCause()); //NOI18N } }
private boolean copyDocument(final BaseDocument src, final BaseDocument dest) { final boolean[] retVal = new boolean[]{true}; src.readLock(); try{ dest.runAtomic(new Runnable() { @Override public void run() { try { String docText = src.getText(0, src.getLength()); dest.insertString(0, docText, null); } catch(BadLocationException ble) { Exceptions.printStackTrace(ble); retVal[0] = false; } } }); } finally { src.readUnlock(); } return retVal[0]; }
public static void insert(String s, final JTextComponent target) throws BadLocationException { Document doc = target.getDocument(); if (doc != null && doc instanceof BaseDocument) { final String str = (s == null) ? "" : s; final BaseDocument baseDoc = (BaseDocument) doc; final Reformat formatter = Reformat.get(baseDoc); Runnable edit = new Runnable() { public void run() { try { int start = insert(str, target, baseDoc); // format the inserted text if (start >= 0) { int end = start + str.length(); formatter.reformat(start, end); } } catch (BadLocationException e) { Exceptions.printStackTrace(e); } } }; formatter.lock(); try { baseDoc.runAtomic(edit); }finally { formatter.unlock(); } } }
@Override public void actionPerformed(ActionEvent evt, JTextComponent target) { final AtomicBoolean processedHere = new AtomicBoolean(false); if (target != null) { if (!target.isEditable() || !target.isEnabled() || !(target.getDocument() instanceof BaseDocument)) { target.getToolkit().beep(); return; } final int caretOffset = Utilities.isSelectionShowing(target) ? target.getSelectionStart() : target.getCaretPosition(); final BaseDocument doc = (BaseDocument) target.getDocument(); doc.runAtomic(new Runnable() { @Override public void run() { performCustomAction(doc, caretOffset, processedHere); } }); if (!processedHere.get()) { performDefaultAction(evt, target); } } }
doc.runAtomic(new Runnable() { public @Override void run() { try {
doc.runAtomic(new Runnable() {
final BaseDocument bdoc = org.netbeans.editor.Utilities.getDocument(target); if (bdoc != null) { bdoc.runAtomic(new Runnable() { public void run() { DocumentUtilities.setTypingModification(bdoc, true);
try { final boolean [] ok = new boolean [] { false }; ((BaseDocument) doc).runAtomic(new Runnable() { public void run() { try {
reformat.lock(); try { ((BaseDocument) doc).runAtomic(new Runnable() { @Override public void run() {
final CodeTemplateManager ctm = CodeTemplateManager.get(doc); if (ctm != null) { doc.runAtomic(new Runnable() { public void run() { try {
public static void reformatFile(final DataObject dataObject) throws IOException { assert dataObject != null; EditorCookie ec = dataObject.getCookie(EditorCookie.class); assert ec != null : "No editorcookie for " + dataObject; Document doc = ec.openDocument(); assert doc instanceof BaseDocument; // reformat final BaseDocument baseDoc = (BaseDocument) doc; final Reformat reformat = Reformat.get(baseDoc); reformat.lock(); try { // seems to be synchronous but no info in javadoc baseDoc.runAtomic(new Runnable() { @Override public void run() { try { reformat.reformat(0, baseDoc.getLength()); } catch (BadLocationException ex) { LOGGER.log(Level.INFO, "Cannot reformat file " + dataObject.getName(), ex); } } }); } finally { reformat.unlock(); } // save saveFile(dataObject); }
private static String reformat(String content, final BaseDocument doc) throws BadLocationException { doc.remove(0, doc.getLength()); doc.insertString(0, content, null); final Reformat reformat = Reformat.get(doc); reformat.lock(); try { doc.runAtomic(new Runnable() { @Override public void run() { try { reformat.reformat(0, doc.getLength()); } catch (BadLocationException ex) { Exceptions.printStackTrace(ex); } } }); } finally { reformat.unlock(); } return doc.getText(0, doc.getLength()); }
reformat.lock(); try { doc.runAtomic(new Runnable() { public void run() { try {
final boolean _lineSelection = lineSelection; doc.runAtomic(new Runnable() {