@Override public void paintComponent(Graphics g) { super.paintComponent(g); // starting position in document int start = textEditor.viewToModel(getViewport().getViewPosition()); // end position in document int end = textEditor.viewToModel(new Point(10, getViewport().getViewPosition().y + (int) textEditor.getVisibleRect().getHeight()) ); // translate offsets to lines Document doc = textEditor.getDocument(); int startline = doc.getDefaultRootElement().getElementIndex(start) + 1; int endline = doc.getDefaultRootElement().getElementIndex(end) + 1; Font f = textEditor.getFont(); int fontHeight = g.getFontMetrics(f).getHeight(); int fontDesc = g.getFontMetrics(f).getDescent(); int startingY = -1; try { startingY = textEditor.modelToView(start).y + fontHeight - fontDesc; } catch (BadLocationException e1) { System.err.println(e1.getMessage()); } g.setFont(f); for (int line = startline, y = startingY; line <= endline; y += fontHeight, line++) { String lineNumber = StringGroovyMethods.padLeft(Integer.toString(line), 4, " "); g.drawString(lineNumber, 0, y); } } }
@Override public void run() { try { sourceCodeArea.getHighlighter().removeAllHighlights(); if (node == null) { return; } int startOffset = sourceCodeArea.getLineStartOffset(node.getBeginLine() - 1) + node.getBeginColumn() - 1; int end = sourceCodeArea.getLineStartOffset(node.getEndLine() - 1) + node.getEndColumn(); sourceCodeArea.getHighlighter().addHighlight(startOffset, end, new DefaultHighlighter.DefaultHighlightPainter(HIGHLIGHT_COLOR)); sourceCodeArea.moveCaretPosition(startOffset); } catch (BadLocationException exc) { throw new IllegalStateException(exc.getMessage()); } } });
/** * Add a text at the end of textpane. * @param string Text to add */ public void append(String string) { try { Document doc = this.getDocument(); doc.insertString(doc.getLength(), string, null); } catch (BadLocationException e) { LOGGER.error(e.getMessage(), e); } }
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); } }
/** * Add a colored string to the textpane, measure prompt at the same time. * @param string Text to append * @param color Color of text * @param isAddingPrompt Should we measure prompt length? */ private void appendPrompt(String string, Color color, boolean isAddingPrompt) { try { StyleConstants.setForeground(this.style, color); this.styledDocument.insertString(this.styledDocument.getLength(), string, this.style); if (isAddingPrompt) { this.prompt += string; } } catch (BadLocationException e) { LOGGER.error(e.getMessage(), e); } }
LOGGER.error(e.getMessage(), e); return;
str, null); } catch (BadLocationException e) { throw new IllegalArgumentException(e.getMessage()); } finally { undoManager.endInternalAtomicEdit();
/** * We override this method because the super version gives us an entirely * new <code>Document</code>, thus requiring us to re-attach our Undo * manager. With this version we just replace the text. */ @Override public void read(Reader in, Object desc) throws IOException { RTextAreaEditorKit kit = (RTextAreaEditorKit)getUI().getEditorKit(this); setText(null); Document doc = getDocument(); if (desc != null) { doc.putProperty(Document.StreamDescriptionProperty, desc); } try { // NOTE: Resets the "line separator" property. kit.read(in, doc, 0); } catch (BadLocationException e) { throw new IOException(e.getMessage()); } }
LOGGER.error(e.getMessage(), e);
if (evt.getKeyCode() == 10) { try { int offset=ta.getLineOfOffset(ta.getCaretPosition()); int start=ta.getLineStartOffset(offset); int end=ta.getLineEndOffset(offset); System.out.println("Text: "+ta.getText(start, (end-start))); } catch (BadLocationException ex) { System.out.println(ex.getMessage()); } }
public static Position createPosition(Document doc, int offset) { try { return doc.createPosition(offset); } catch (BadLocationException ex) { throw new IndexOutOfBoundsException(ex.getMessage()); } }
@Override public void actionPerformed( final ActionEvent event ) { try { final int caret = getInsertCaret(); _piperDocument.insertString( caret, "\n// Load a Piper file containing a partial Pipeline\nload ", null ); } catch ( BadLocationException blE ) { LOGGER.error( blE.getMessage() ); } _runButton.setEnabled( false ); } }
void addBlockFoldInfo(CppFoldRecord foldRecord) { if (foldRecord != null) { try { blockFoldInfos.add(new BlockFoldInfo(foldRecord)); } catch (BadLocationException ex) { if (log.isLoggable(Level.FINE)){ log.log(Level.FINE, "CFM.addBlockFoldInfo: Got BadLocationException\n {0}", // NOI18N ex.getMessage()); } } } } }
private IOException convert(BadLocationException e) { IOException ioe = new IOException(e.getMessage()); ioe.setStackTrace(e.getStackTrace()); return ioe; }
private IOException toIOException(BadLocationException e) { IOException ioe = new IOException(e.getMessage()); ioe.setStackTrace(e.getStackTrace()); return ioe; }
private void scrollToSearchResult(ArrayList<String> toHighlight, JTextPane textPane) { if (toHighlight.size() == 0) { return; } try { StyledDocument logDetailsDocument = textPane.getStyledDocument(); String text = logDetailsDocument.getText(0, logDetailsDocument.getLength()); String string = toHighlight.get(0); textPane.setCaretPosition(Math.max(text.indexOf(string), 0)); } catch (BadLocationException e) { LOGGER.warn("Cant scroll to content, wrong location: " + e.getMessage()); } }
public Object highlight(int start, int end, HighlightPainter painter) { try { Object highlightReference = getHighlighter().addHighlight(start, end, painter); this.setCaretPosition(start); return highlightReference; } catch (BadLocationException e) { log.error(e.getMessage(), e); } return null; }
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 makeCaretVisible(JTextComponent textComponent) { int pos = textComponent.getCaretPosition(); try { Rectangle rec = textComponent.getUI().modelToView(textComponent, pos); if (rec != null) { Point p = SwingUtilities.convertPoint(textComponent, rec.x, rec.y, this); scrollRectToVisible(new Rectangle(p.x, p.y, rec.width, rec.height)); } } catch (BadLocationException blex) { LOG.log(Level.INFO, blex.getMessage(), blex); } }
protected void highlight(int start, int end) { Highlighter hilite = this.getHighlighter(); HighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(Color.LIGHT_GRAY); try { // DOBIN removeAllHighlights(); hilite.addHighlight(start, end, painter); this.setCaretPosition(start); } catch (BadLocationException e) { log.error(e.getMessage(), e); } }