Refine search
findReplaceCount = 0; int pos = textComponent.getSelectedText() == null ? textComponent.getCaretPosition() : textComponent.getSelectionStart(); Document doc = textComponent.getDocument(); try { doc.remove(pos, find.length()); doc.insertString(pos, replace, attributeSet); ble.printStackTrace();
public void append(JTextPane l, String text) { try { l.getDocument().insertString( l.getDocument().getLength(), (l.getDocument().getLength() == 0 ? "" : "\n") + text, null ); } catch (BadLocationException e) { LOGGER.error(e.getMessage(), e); } }
/** * Get position of the beginning of the line. * @param line Index of the line * @return Offset of line * @throws BadLocationException */ public int getLineStartOffset(int line) throws BadLocationException { Element map = this.getDocument().getDefaultRootElement(); if (line < 0) { throw new BadLocationException("Negative line", -1); } else if (line >= map.getElementCount()) { throw new BadLocationException("No such line", this.getDocument().getLength() + 1); } else { Element lineElem = map.getElement(line); return lineElem.getStartOffset(); } }
public int getLineOfOffset(final JTextComponent textComponent, final int line) throws BadLocationException { final Document doc = textComponent.getDocument(); final int lineCount = doc.getDefaultRootElement().getElementCount(); if (line < 0) { throw new BadLocationException("Negative line", -1); } else if (line > lineCount) { throw new BadLocationException("No such line", doc.getLength() + 1); } else { Element map = doc.getDefaultRootElement(); Element lineElem = map.getElement(line); return lineElem.getEndOffset(); } }
Element map = doc.getDefaultRootElement(); int lineNum = map.getElementIndex(caretPos); Element line = map.getElement(lineNum); int start = line.getStartOffset(); int end = line.getEndOffset()-1; // Why always "-1"? int len = end-start; String s = doc.getText(start, len); ble.printStackTrace();
g.setFont(font!=null ? font : textComponent.getFont()); Document doc = textComponent.getDocument(); rootElement = doc.getDefaultRootElement(); numDocLines = rootElement.getElementCount(); // The number of lines in our document. currentDocLineNumber = 0; // The line number of the document we're currently on. int startingOffset = 0; // Used when a line is so long it has to be wrapped. Element currentLine = rootElement.getElement(currentDocLineNumber); int currentLineStart = currentLine.getStartOffset(); int currentLineEnd = currentLine.getEndOffset(); doc.getText(currentLineStart+startingOffset, currentLineEnd-(currentLineStart+startingOffset), currentLineSeg); } catch (BadLocationException ble) { currentPos++; try { doc.getText(currentLineStart+startingOffset, currentPos, currentLineSeg); } catch (BadLocationException ble) { System.err.println(ble); System.err.println("==> Range: " + (currentLineStart+startingOffset) + " - " + (currentLineStart+startingOffset+currentPos)); ble.printStackTrace(); return Printable.NO_SUCH_PAGE;
int mark = textArea.getCaret().getMark(); Document doc = textArea.getDocument(); Element root = doc.getDefaultRootElement(); int startLine = root.getElementIndex(Math.min(dot, mark)); int endLine = root.getElementIndex(Math.max(dot, mark)); Element elem = root.getElement(endLine); if (dot == elem.getStartOffset() || mark == elem.getStartOffset()) { moveCount--; ble.printStackTrace(); UIManager.getLookAndFeel().provideErrorFeedback(textArea); return;
public String call() { Document doc = component.getDocument(); try { return doc.getText(0, doc.getLength()); } catch (BadLocationException e) { e.printStackTrace(); return ""; } } });
@Override public void actionPerformedImpl(ActionEvent e, RTextArea textArea) { try { // We use the elements instead of calling getLineOfOffset(), // etc. to speed things up just a tad (i.e. micro-optimize). Document document = textArea.getDocument(); int caretPosition = textArea.getCaretPosition(); Element map = document.getDefaultRootElement(); int currentLineNum = map.getElementIndex(caretPosition); Element currentLineElement = map.getElement(currentLineNum); // Always take -1 as we don't want to remove the newline. int currentLineEnd = currentLineElement.getEndOffset()-1; if (caretPosition<currentLineEnd) { document.remove(caretPosition, currentLineEnd-caretPosition); } } catch (BadLocationException ble) { ble.printStackTrace(); } }
@Override public void keyPressed(KeyEvent keyEvent) { try { final Element root = this.terminal.getDocument().getDefaultRootElement(); final int caretPosition = this.terminal.getCaretPosition(); command[0] = this.terminal.getText( root.getElement(lineNumber).getStartOffset(), root.getElement(lineNumber).getEndOffset() - root.getElement(lineNumber).getStartOffset() ).replace(this.terminal.getPrompt(), ""); terminalCommand.setCaretPosition(terminalCommand.getDocument().getLength()); terminalCommand.action( command[0], this.terminal.getDocument().remove( root.getElement(lineNumber).getStartOffset() + this.terminal.getPrompt().length(), command[0].length() - 1 LOGGER.error(e.getMessage(), e);
protected String getLine() { try { return area.getText(startPos, area.getDocument().getLength() - startPos); } catch (BadLocationException e) { e.printStackTrace(); } return null; }
private String getWord(int offset) { try { int start = Utilities.getWordStart(textComponent, offset); int end = Utilities.getWordEnd(textComponent, offset); return textComponent.getDocument().getText(start, end - start + 1).trim(); } catch (BadLocationException e) { e.printStackTrace(); } return null; }
/** * Get index of line for current offset (generally cursor position). * @param offset Position on the line * @return Index of the line * @throws BadLocationException */ public int getLineOfOffset(int offset) throws BadLocationException { String errorMsg = "Can't translate offset to line"; Document doc = this.getDocument(); if (offset < 0) { throw new BadLocationException(errorMsg, -1); } else if (offset > doc.getLength()) { throw new BadLocationException(errorMsg, doc.getLength() + 1); } else { Element map = doc.getDefaultRootElement(); return map.getElementIndex(offset); } }
public void clear() { UISpecAssert.assertTrue(isEditable()); Document document = jTextComponent.getDocument(); try { document.remove(0, document.getLength()); } catch (BadLocationException e) { AssertAdapter.fail("Clear failed: " + e.getMessage()); } }
private void search(DocumentEvent documentEvent) { try { String word = documentEvent.getDocument().getText(0, documentEvent.getDocument().getLength()); if (word.isEmpty()) { nextAction.setEnabled(false); previousAction.setEnabled(false); searchPanel.reset(); textComponent.getHighlighter().removeAllHighlights(); return; } search(word); } catch (BadLocationException e) { e.printStackTrace(); } }
Element line = getLineElem(doc, offs); if (line == null) { throw new BadLocationException("No word at " + offs, offs); int lineStart = line.getStartOffset(); if (offs==lineStart) { // Start of the line. return offs; int endOffs = Math.min(offs+1, doc.getLength()); String s = doc.getText(lineStart, endOffs-lineStart); if(s != null && s.length() > 0) { int i = s.length() - 1;
/** * Reads the single character at the current position in the document. */ @Override public int read() { if(position>=document.getLength()) { return -1; // Read past end of document. } try { document.getText((int)position,1, segment); position++; return segment.array[segment.offset]; } catch (BadLocationException ble) { /* Should never happen?? */ ble.printStackTrace(); return -1; } }
private void addCom(String text){ AttributeSet attribute = attributes.get("0"); try{ int len = doc.getLength(); doc.insertString(len, text, attribute); }catch (BadLocationException e){ e.printStackTrace(); } chat.setCaretPosition(chat.getDocument().getLength()); }
protected void replaceText(int start, int end, String replacement) { try { area.getDocument().remove(start, end - start); area.getDocument().insertString(start, replacement, inputStyle); } catch (BadLocationException e) { e.printStackTrace(); } }
public void actionPerformed(ActionEvent ae) { JTextComponent tComp = (JTextComponent) ae.getSource(); if (tComp.getDocument() instanceof StyledDocument) { doc = (StyledDocument) tComp.getDocument(); try { doc.getText(0, doc.getLength(), segment); } catch (Exception e) { // should NEVER reach here e.printStackTrace(); } int offset = tComp.getCaretPosition(); int index = findTabLocation(offset); buffer.delete(0, buffer.length()); buffer.append('\n'); if (index > -1) { for (int i = 0; i < index + 4; i++) { buffer.append(' '); } } try { doc.insertString(offset, buffer.toString(), doc.getDefaultRootElement().getAttributes()); } catch (BadLocationException ble) { ble.printStackTrace(); } } }