JEditorPane pane = new JEditorPane(); /* ... Other stuff ... */ public void append(String s) { try { Document doc = pane.getDocument(); doc.insertString(doc.getLength(), s, null); } catch(BadLocationException exc) { exc.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(); } } }
public void paint(Graphics g) { if (isVisible()) { try { JTextComponent component = getComponent(); Rectangle r = component.getUI().modelToView(component, getDot()); Color c = g.getColor(); g.setColor(component.getBackground()); g.setXORMode(component.getCaretColor()); r.setBounds(r.x, r.y, g.getFontMetrics().charWidth('w'), g.getFontMetrics().getHeight()); g.fillRect(r.x, r.y, r.width, r.height); g.setPaintMode(); g.setColor(c); } catch (BadLocationException e) { e.printStackTrace(); } } }
/** * 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 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; }
private String getRowText(int offset) { try { int rowStart = Utilities.getRowStart(textComponent, offset); int rowEnd = Utilities.getRowEnd(textComponent, offset); return textComponent.getDocument().getText(rowStart, rowEnd - rowStart + 1); } catch (BadLocationException e) { e.printStackTrace(); } return null; } }
@Override public void mousePressed(MouseEvent e) { if (bookmarkingEnabled && bookmarkIcon!=null) { try { int line = viewToModelLine(e.getPoint()); if (line>-1) { toggleBookmark(line); } } catch (BadLocationException ble) { ble.printStackTrace(); // Never happens } } }
private void scrollToWord(int offset) { try { textComponent.scrollRectToVisible(textComponent.modelToView(offset)); } catch (BadLocationException e) { e.printStackTrace(); } }
private int rowAtPoint(Point p) { int line = 0; try { int offs = textArea.viewToModel(p); if (offs>-1) { line = textArea.getLineOfOffset(offs); } } catch (BadLocationException ble) { ble.printStackTrace(); // Never happens } return line; }
/** * {@inheritDoc} */ @Override public String toString() { try { return doc.getText(start, length()); } catch (BadLocationException ble) { // Never happens ble.printStackTrace(); return ""; } }
private void writeToken(Object obj, StyledDocument docu) { try { if (obj instanceof Operator) { addOperators(obj, docu); } else { writeOperand(obj, docu); } } catch (BadLocationException e) { e.printStackTrace(); } }
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(); } }
@Override public void actionPerformedImpl(ActionEvent e, RTextArea textArea) { Gutter gutter = RSyntaxUtilities.getGutter(textArea); if (gutter!=null) { int line = textArea.getCaretLineNumber(); try { gutter.toggleBookmark(line); } catch (BadLocationException ble) { // Never happens UIManager.getLookAndFeel(). provideErrorFeedback(textArea); ble.printStackTrace(); } } }
private StyledDocument getDocument(InputStream inputStream, String encoding) { StyledDocument docu = new DefaultStyledDocument(); if (inputStream != null) { String data = getStringOfStream(inputStream, encoding); try { docu.insertString(0, data, null); } catch (BadLocationException e) { e.printStackTrace(); } } return docu; }
private void changeHighlighter(int index, Highlighter.HighlightPainter newPainter) { Highlighter highlighter = textComponent.getHighlighter(); Highlighter.Highlight highLight = highlights.get(index); highlighter.removeHighlight(highLight); try { highlighter.addHighlight(highLight.getStartOffset(), highLight.getEndOffset(), newPainter); highlights.set(index, highlighter.getHighlights()[highlighter.getHighlights().length - 1]); } catch (BadLocationException e) { e.printStackTrace(); } }
/** * Returns the last visible offset in this text area. This may not be the * length of the document if code folding is enabled. * * @return The last visible offset in this text area. */ public int getLastVisibleOffset() { if (isCodeFoldingEnabled()) { int lastVisibleLine = foldManager.getLastVisibleLine(); if (lastVisibleLine<getLineCount()-1) { // Not the last line try { return getLineEndOffset(lastVisibleLine) - 1; } catch (BadLocationException ble) { // Never happens ble.printStackTrace(); } } } return getDocument().getLength(); }
@Override public void removeUpdate(DocumentEvent e) { // Removing text from the visible line of a folded Fold causes that // Fold to unfold. We only need to check the removal offset since // that's the new caret position. int offs = e.getOffset(); try { int lastLineModified = textArea.getLineOfOffset(offs); //System.out.println(">>> " + lastLineModified); Fold fold = getFoldForLine(lastLineModified); //System.out.println("&&& " + fold); if (fold!=null && fold.isCollapsed()) { fold.toggleCollapsedState(); } } catch (BadLocationException ble) { ble.printStackTrace(); // Never happens } }
@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(); } }