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 String getPartialInput() { try { return document.getText(startReadLen, document.getLength() - startReadLen); } catch (BadLocationException e) { logger.error("Failed to retrieve partial input text", e); return ""; } }
@Override public void run() { try { docContentRef.set(document.getText(0, document.getLength())); } catch (BadLocationException ex) { bleRef.set(ex); } }
String html = "<html>..."; JTextPane pane = new JTextPane(); pane.setContentType("text/html"); pane.setText(html); StyledDocument doc = pane.getStyledDocument(); try { System.out.println("Text: " + doc.getText(0, doc.getLength())); } catch (BadLocationException ex) { Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex); }
@Override public void run() { try { int position = doc.getText(0, doc.getLength()).indexOf(find); // if element wasn't found - it isn't likely new project with sample index.html page and // there is probably not wished to have it changed, so don't do it if (position >= 0) { doc.insertString(position, enhanceBy + "\n", null); //NOI18N } } catch (BadLocationException ex) { Exceptions.printStackTrace(ex); } } });
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()); } }
StyledDocument document = textPane.getStyledDocument(); Style highlight = document.addStyle("highlight", null); StyleConstants.setBackground(highlight, Color.YELLOW); String text = document.getText(0, document.getLength()); while ((index = text.indexOf(myWord, index)) >= 0) { document.setCharacterAttributes(index, myWord.length(), highlight, false); index += myWord.length(); }
public void run() { // Part of #33165 - the following code is wrapped by doc.render() try { int p = position.getOffset(); if (p >= doc.getLength()) { retStringArray[0] = ""; } else { retStringArray[0] = doc.getText(position.getOffset(), getLength()); } } catch (BadLocationException ex) { Logger.getLogger(DocumentLine.class.getName()).log(Level.WARNING, null, ex); retStringArray[0] = null; } // End of the code wrapped by doc.render() } }
private String convert(InputStream rtfDocumentInputStream) throws IOException { RTFEditorKit aRtfEditorkit = new RTFEditorKit(); StyledDocument styledDoc = new DefaultStyledDocument(); String textDocument; try { aRtfEditorkit.read(rtfDocumentInputStream, styledDoc, 0); textDocument = styledDoc.getText(0, styledDoc.getLength()); } catch (BadLocationException e) { throw new IOException("Error during parsing"); } return textDocument; }
/** * Map document position to contents of whole line at that position. * SHOULD be combined with addrFromLine to givew: * Address viewToModel(pos) */ private String getLineAt(int pos) { // By default a "paragraph" is a line Element pp = styledDoc.getParagraphElement(pos); int startPos = pp.getStartOffset(); int endPos = pp.getEndOffset(); int length = endPos - startPos; String line = null; try { line = styledDoc.getText(startPos, length); } catch (Exception x) { } return line; }
/** Finds the text contained in this range. * @return the text * @exception IOException if any I/O problem occurred during document loading (if that was necessary) * @exception BadLocationException if the positions are out of the bounds of the document */ public String getText() throws BadLocationException, IOException { StyledDocument doc = begin.getCloneableEditorSupport().openDocument(); int p1 = begin.getOffset(); int p2 = end.getOffset(); return doc.getText(p1, p2 - p1); }
@Override public Figure createTextArea(double x, double y, double w, double h, StyledDocument doc, Map<AttributeKey, Object> attributes) { SVGTextAreaFigure figure = new SVGTextAreaFigure(); figure.setBounds(new Point2D.Double(x,y),new Point2D.Double(x+w,y+h)); try { figure.setText(doc.getText(0, doc.getLength())); } catch (BadLocationException e) { InternalError ex = new InternalError(e.getMessage()); ex.initCause(e); throw ex; } figure.setAttributes(attributes); return figure; }
public void run() { // Part of #33165 - the following code is wrapped by doc.render() int lineNumber = getLineNumber(); int lineStart = NbDocument.findLineOffset(doc, lineNumber); // #24434: Check whether the next line exists // (the current one could be the last one). int lineEnd; if ((lineNumber + 1) >= NbDocument.findLineRootElement(doc).getElementCount()) { lineEnd = doc.getLength(); } else { lineEnd = NbDocument.findLineOffset(doc, lineNumber + 1); } try { retStringArray[0] = doc.getText(lineStart, lineEnd - lineStart); } catch (BadLocationException ex) { Logger.getLogger(DocumentLine.class.getName()).log(Level.WARNING, null, ex); retStringArray[0] = null; } // End of the code wrapped by doc.render() } }
/** Finds the text contained in this range. * @return the text * @exception IOException if any I/O problem occurred during document loading (if that was necessary) * @exception BadLocationException if the positions are out of the bounds of the document */ public String getText() throws BadLocationException, IOException { StyledDocument doc = begin.getCloneableEditorSupport().openDocument(); int p1 = begin.getOffset(); int p2 = end.getOffset(); return doc.getText(p1, p2 - p1); }
public static void highlightSearchResult( OtrosJTextWithRulerScrollPane<JTextPane> otrosJTextWithRulerScrollPane, PluginableElementsContainer<MessageColorizer> colorizersContainer, Theme theme) { MessageUpdateUtils messageUpdateUtils = new MessageUpdateUtils(); StyledDocument styledDocument = otrosJTextWithRulerScrollPane.getjTextComponent().getStyledDocument(); String text; try { text = styledDocument.getText(0, styledDocument.getLength()); } catch (BadLocationException e) { LOGGER.error("Cant get document text for log details view: ", e); return; } MessageColorizer messageColorizer = colorizersContainer.getElement(SearchResultColorizer.class.getName()); List<MessageFragmentStyle> messageFragmentStyles = new ArrayList<>(); if (messageColorizer != null) { messageFragmentStyles.addAll(messageUpdateUtils.colorizeMessageWithTimeLimit(text, 0, messageColorizer, 10)); } markSearchResult(messageFragmentStyles, otrosJTextWithRulerScrollPane, theme); }
/** Finds the text contained in this range. * @return the text * @exception IOException if any I/O problem occurred during document loading (if that was necessary) * @exception BadLocationException if the positions are out of the bounds of the document */ public String getText() throws BadLocationException, IOException { StyledDocument doc = begin.getCloneableEditorSupport().openDocument(); int p1 = begin.getOffset(); int p2 = end.getOffset(); return doc.getText(p1, p2 - p1); }
private void registerForIssueLinks(final JTextPane pane, final Link issueLink, final IssueRefProvider issueIdProvider) { pane.removeMouseMotionListener(motionListener); try { String text = ""; try { text = pane.getStyledDocument().getText(0, pane.getStyledDocument().getLength()); } catch (BadLocationException ex) { Support.LOG.log(Level.INFO, null, ex); } registerLinkIntern(pane, issueIdProvider.getIssueRefSpans(text), issueLink); } catch (Exception ex) { Exceptions.printStackTrace(ex); } pane.addMouseMotionListener(motionListener); }
static int getRowFirstNonWhite(StyledDocument doc, int offset) throws BadLocationException { Element lineElement = doc.getParagraphElement(offset); int start = lineElement.getStartOffset(); while (start + 1 < lineElement.getEndOffset()) { try { if (doc.getText(start, 1).charAt(0) != ' ') { break; } } catch (BadLocationException ex) { throw (BadLocationException) new BadLocationException( "calling getText(" + start + ", " + (start + 1) + ") on doc of length: " + doc.getLength(), start).initCause(ex); } start++; } return start; }
@Override public Figure createText(Point2D.Double[] coordinates, double[] rotates, StyledDocument text, Map<AttributeKey, Object> a) { SVGTextFigure figure = new SVGTextFigure(); figure.setCoordinates(coordinates); figure.setRotates(rotates); try { figure.setText(text.getText(0, text.getLength())); } catch (BadLocationException e) { InternalError ex = new InternalError(e.getMessage()); ex.initCause(e); throw ex; } figure.setAttributes(a); return figure; }
doc.render(new Runnable() { public void run() { // Part of #33165 - the following code is wrapped by doc.render() int lineNumber = getLineNumber(); int lineStart = NbDocument.findLineOffset(doc, lineNumber); // #24434: Check whether the next line exists // (the current one could be the last one). int lineEnd; if((lineNumber + 1) >= NbDocument.findLineRootElement(doc).getElementCount()) { lineEnd = doc.getLength(); } else { lineEnd = NbDocument.findLineOffset(doc, lineNumber + 1); } try { retStringArray[0] = doc.getText(lineStart, lineEnd - lineStart); } catch (BadLocationException ex) { ErrorManager.getDefault ().notify ( ErrorManager.EXCEPTION, ex); retStringArray[0] = null; } // End of the code wrapped by doc.render() }}); return retStringArray[0];