public boolean gotoDeclaration(JTextComponent target) { try { Caret caret = target.getCaret(); int dotPos = caret.getDot(); BaseDocument doc = (BaseDocument) target.getDocument(); int[] idBlk = Utilities.getIdentifierBlock(doc, dotPos); ExtSyntaxSupport extSup = (ExtSyntaxSupport) doc.getSyntaxSupport(); if (idBlk != null) { int decPos = extSup.findDeclarationPosition(doc.getText(idBlk), idBlk[1]); if (decPos >= 0) { caret.setDot(decPos); return true; } } } catch (BadLocationException e) { } return false; }
public void actionPerformed(ActionEvent evt, JTextComponent target) { if (target != null) { if (!target.isEditable() || !target.isEnabled()) { target.getToolkit().beep(); return; } Caret caret = target.getCaret(); try { BaseDocument doc = (BaseDocument) target.getDocument(); int dotPos = caret.getDot(); int bolPos = Utilities.getRowStart(target, dotPos); int eolPos = Utilities.getRowEnd(target, dotPos); eolPos = Math.min(eolPos + 1, doc.getLength()); // include // '\n' doc.remove(bolPos, eolPos - bolPos); } catch (BadLocationException e) { target.getToolkit().beep(); } } } }
boolean replaceText( JTextComponent component, String text ) { BaseDocument doc = (BaseDocument)component.getDocument(); doc.atomicLock(); try { doc.remove( offset, length ); doc.insertString( offset, text, null); } catch( BadLocationException exc ) { return false; //not sucessfull } finally { doc.atomicUnlock(); } return true; }
@Override public void run() { try { classDoc.remove(startReplacement, lengthReplacement); classDoc.insertString(startReplacement, ";", null); // NOI18N } catch (BadLocationException e) { // Can't update } } });
public static int getPreviousWord(BaseDocument doc, int offset) throws BadLocationException { Finder prevWordFinder = (Finder)doc.getProperty(SettingsNames.PREVIOUS_WORD_FINDER); offset = doc.find(prevWordFinder, offset, 0); if (offset < 0) { offset = 0; } return offset; }
/** Runs the runnable under read lock. */ public void render(Runnable r) { readLock(); assert incrementThreadLocalLockDepth(); try { r.run(); } finally { assert decrementThreadLocalLockDepth(); readUnlock(); } }
public void actionPerformed(ActionEvent evt, JTextComponent target) { if (target != null) { if (!target.isEditable() || !target.isEnabled()) { target.getToolkit().beep(); return; } Caret caret = target.getCaret(); try { BaseDocument doc = (BaseDocument) target.getDocument(); int dotPos = caret.getDot(); int bolPos = Utilities.getRowStart(doc, dotPos); int wsPos = Utilities.getPreviousWord(target, dotPos); wsPos = (dotPos == bolPos) ? wsPos : Math.max(bolPos, wsPos); doc.remove(wsPos, dotPos - wsPos); } catch (BadLocationException e) { target.getToolkit().beep(); } } } }
public void actionPerformed(ActionEvent evt, JTextComponent target) { // shift-enter while editing aka startNewLineAction if (!target.isEditable() || !target.isEnabled()) { target.getToolkit().beep(); return; } BaseDocument doc = (BaseDocument)target.getDocument(); doc.atomicLock(); try { target.replaceSelection(""); //NOI18N Caret caret = target.getCaret(); // insert and remove '-' to remember caret // position int dotpos = caret.getDot(); doc.insertString(dotpos,"-",null); //NOI18N doc.remove(dotpos,1); int eolDot = Utilities.getRowEnd(target, caret.getDot()); int newDotPos = doc.getFormatter().indentNewLine(doc,eolDot); caret.setDot(newDotPos); } catch (BadLocationException ex) { ex.printStackTrace(); } finally{ doc.atomicUnlock(); } } }
public void actionPerformed(ActionEvent evt, JTextComponent target) { if (target != null) { if (!target.isEditable() || !target.isEnabled()) { target.getToolkit().beep(); return; } try { Caret caret = target.getCaret(); BaseDocument doc = (BaseDocument)target.getDocument(); // Format the current time. SimpleDateFormat formatter = new SimpleDateFormat(); Date currentTime = new Date(); String dateString = formatter.format(currentTime); doc.insertString(caret.getDot(), dateString, null); } catch (BadLocationException e) { target.getToolkit().beep(); } } } }
@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); } } }
public void actionPerformed(ActionEvent evt, JTextComponent target) { if (target != null) { if (!target.isEditable() || !target.isEnabled()) { target.getToolkit().beep(); return; EditorUI editorUI = Utilities.getEditorUI(target); Caret caret = target.getCaret(); final BaseDocument doc = Utilities.getDocument(target); if (caret.isSelectionVisible()) { target.replaceSelection(null); int dotPos = caret.getDot(); String s = editorUI.getWordMatch().getMatchWord(dotPos, direction); String prevWord = editorUI.getWordMatch().getPreviousWord(); if (s != null) { doc.atomicLock(); try { int pos = dotPos; if (prevWord != null && prevWord.length() > 0) { pos -= prevWord.length(); doc.remove(pos, prevWord.length()); doc.insertString(pos, s, null); } catch (BadLocationException e) { target.getToolkit().beep(); } finally { doc.atomicUnlock();
@SuppressWarnings("unchecked") private PPBlockInfo getBlock(final JTextComponent component) { try { final BaseDocument doc = (BaseDocument) component.getDocument(); final int lineNumber = Utilities.getLineOffset(doc, component.getCaret().getDot()); final ArrayList<PPLine> lineList = (ArrayList) doc.getProperty(DocumentPreprocessor.PREPROCESSOR_LINE_LIST); if (lineList != null) { PPLine myPpLine = null; if (lineNumber >= 0 && lineNumber < lineList.size()) { myPpLine = lineList.get(lineNumber); if (myPpLine != null) return myPpLine.getBlock(); } } } catch (BadLocationException ble) { } return null; }
public void run() { try { final BaseDocument doc = (BaseDocument)txt.getDocument(); final int s = Utilities.getRowStartFromLineOffset(doc, getSelectionStartLine(txt) - 1); final int e = Utilities.getRowEnd(txt, Utilities.getRowStartFromLineOffset(doc, getSelectionEndLine(txt) - 1)); doc.insertString(e, "\n//#enddebug", null); //NOI18N doc.insertString(s, "//#mdebug\n", null); //NOI18N txt.setSelectionEnd(txt.getSelectionStart()); } catch (BadLocationException ble) { ErrorManager.getDefault().notify(ble); } RecommentAction.actionPerformed(txt); } });
public void run() { try { final BaseDocument doc = (BaseDocument)txt.getDocument(); final int s = Utilities.getRowStartFromLineOffset(doc, getSelectionStartLine(txt) - 1); final int e = Utilities.getRowEnd(txt, Utilities.getRowStartFromLineOffset(doc, getSelectionEndLine(txt) - 1)); final String text = doc.getText(s, e-s); doc.insertString(e, "\n//#else\n" + text + "\n//#endif", null); //NOI18N doc.insertString(s, "//#if \n", null); //NOI18N txt.setSelectionStart(s + 6); txt.setSelectionEnd(s + 6); } catch (BadLocationException ble) { ErrorManager.getDefault().notify(ble); } RecommentAction.actionPerformed(txt); } });
if (getActiveParameterIndex() == -1) { // not showing active parm try { int fnwpos = Utilities.getFirstNonWhiteFwd(doc, offset + len); if (fnwpos > -1 && doc.getChars(fnwpos, 1)[0] == ')') { // NOI18N text = doc.getText(offset + len, fnwpos + 1 - offset - len); len = fnwpos + 1 - offset; doc.insertString(c.getCaretPosition(), paramName, null); substed = true; } catch (BadLocationException e) { String paramsText = null; try { int fnwpos = Utilities.getFirstNonWhiteFwd(doc, offset + len); if (fnwpos > -1 && fnwpos <= Utilities.getRowEnd(doc, offset + len) && doc.getChars(fnwpos, 1)[0] == '(') { // NOI18N paramsText = doc.getText(offset + len, fnwpos + 1 - offset - len); if (addSpace && paramsText.length() < 2) { text += ' '; if (fnwpos > -1 && doc.getChars(fnwpos, 1)[0] == ')') { // NOI18N paramsText = doc.getText(offset + len, fnwpos + 1 - offset - len); len = fnwpos + 1 - offset; if (params.size() > 0) { String insertText = text; if (!isDeclaration && getHint() == SubstitutionHint.DOT_TO_ARROW) { if (doc.getChars(insertOffset - 1, 1)[0] == '.') {// NOI18N insertOffset--; removeLength++;
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); } } }); }
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(); } } }
@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); } } });
public int[] getReformatBlock(JTextComponent target, String typedText) { BaseDocument doc = Utilities.getDocument(target); int dotPos = target.getCaret().getDot(); // don't reformat for DEL/BKSPC if(typedText.length() == 0 || typedText.charAt(0) == 8 || typedText.charAt(0) == 127) return null; if (doc != null) { try { int rstart = Utilities.getRowStart(doc, dotPos); if(dotPos - rstart > textLimit) { String preText = doc.getText(rstart, dotPos - rstart); int lastSpace = preText.lastIndexOf(' '); if(lastSpace > 0) { doc.remove(rstart+lastSpace, 1); doc.insertString(rstart+lastSpace, "\n", null); // NOI18N } } }catch(BadLocationException e) { e.printStackTrace(); } } return null; }
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); } } }); }