@Override public Document getDocument() { return parent.getDocument(); }
public Document getDocument() { return element.getDocument(); }
AbstractPositionElement(Element parent, int startOffset, int endOffset) { this( parent, createPosition(parent.getDocument(), startOffset), createPosition(parent.getDocument(), endOffset) ); }
CustomElement(Element parent, int startOffset, int endOffset) { super(parent, startOffset, endOffset); CharSequenceUtilities.checkIndexesValid(startOffset, endOffset, parent.getDocument().getLength() + 1); }
@Override public void paint(Graphics g, Shape a) { // draw text super.paint(g, a); if (!(getElement().getDocument() instanceof Document3)) { // document didn't created yet return; } if (fontHeight == 0) { FontMetrics fm = g.getFontMetrics(); fontHeight = fm.getHeight(); } }
/** * If necessary, wrap the text into multiple lines. * * @param lines * line array in which to store the wrapped lines * @param elem * the document element containing the text content */ protected void wrap(final List<String> lines, final Element elem) { final int p1 = elem.getEndOffset(); final Document doc = elem.getDocument(); for (int p0 = elem.getStartOffset(); p0 < p1;) { final int p = calculateBreakPosition(doc, p0, p1); try { lines.add(doc.getText(p0, p - p0)); } catch (final BadLocationException e) { throw new Error("Can't get line text. p0=" + p0 + " p=" + p); } p0 = (p == p0) ? p1 : p; } } }
private void showMenu(MouseEvent e) { if (e.isPopupTrigger()) { try { Element elem = element(e); if (elem.getAttributes().getAttribute(HyperlinkSupport.STACKTRACE_ATTRIBUTE) != null) { String stackFrame = elem.getDocument().getText(elem.getStartOffset(), elem.getEndOffset() - elem.getStartOffset()); JPopupMenu menu = new JPopupMenu(); menu.add(new StackTraceAction(stackFrame, false)); menu.add(new StackTraceAction(stackFrame, true)); menu.show((JTextPane)e.getSource(), e.getX(), e.getY()); } } catch(Exception ex) { Support.LOG.log(Level.SEVERE, null, ex); } } } };
result.append("<html><body style='font-weight:normal;border:1px solid #000;padding:3px 5px 3px 5px;'>"); try { String text = e.getDocument().getText(e.getStartOffset(), e.getEndOffset() - e.getStartOffset()); text = text.replace("\n", "\\n"); // Make linebreaks visible result.append("'").append(text).append("'");
/** * Output the text of the subelements of the given element. * * @param element */ public static void debugContents(Element element, StringBuilder b) { Document doc = element.getDocument(); b.append("["); if (element.isLeaf()) { try { String text = doc.getText( element.getStartOffset(), element.getEndOffset() - element.getStartOffset()); b.append("'").append(text).append("'"); } catch (BadLocationException ex) { System.out.println("Bad location"); } } else { for (int i = 0; i < element.getElementCount(); i++) { Element child = element.getElement(i); debugContents(child, b); } } b.append("]"); }
/** * Creates the XML View. * * @param elem * the root element. * @return the XMLView */ public View create(Element elem) { try { return new XMLView(new XMLScanner(elem.getDocument()), context, elem); } catch (IOException e) { // Instead of an IOException, this will return null if the // XMLView could not be instantiated. // Should never happen. } return null; } }
/** * Creates a view (FieldView) based on an element. * * @param elem the element * @return the view */ @Override public View create(Element elem) { Document doc = elem.getDocument(); Object i18nFlag = doc.getProperty("i18n"/*AbstractDocument.I18NProperty*/); if ((i18nFlag != null) && i18nFlag.equals(Boolean.TRUE)) { // To support bidirectional text, we build a more heavyweight // representation of the field. String kind = elem.getName(); if (kind != null) { if (kind.equals(AbstractDocument.ContentElementName)) { return new GlyphView(elem); } else if (kind.equals(AbstractDocument.ParagraphElementName)) { return new I18nFieldView(elem); } } // this shouldn't happen, should probably throw in this case. } return new TFieldView(elem); }
private boolean isInvisible( final javax.swing.text.Element textElement ) { final HTMLDocument htmlDocument = (HTMLDocument) textElement.getDocument(); final StyleSheet sheet = htmlDocument.getStyleSheet(); final AttributeSet attr = computeStyle( textElement, sheet ); final Object o = attr.getAttribute( CSS.Attribute.DISPLAY ); if ( "none".equals( String.valueOf( o ) ) ) { return true; } final Object tag = findTag( textElement.getAttributes() ); if ( tag == HTML.Tag.COMMENT ) { return true; } if ( tag == HTML.Tag.SCRIPT ) { return true; } if ( tag == HTML.Tag.HEAD ) { return true; } return false; }
@Override public void mouseClicked(MouseEvent e) { try { if (SwingUtilities.isLeftMouseButton(e)) { Element elem = element(e); AttributeSet as = elem.getAttributes(); TypeLink action = (TypeLink) as.getAttribute(HyperlinkSupport.TYPE_ATTRIBUTE); if (action != null) { try { String name = elem.getDocument().getText(elem.getStartOffset(), elem.getEndOffset() - elem.getStartOffset()); int idx = name.lastIndexOf("."); if(idx > -1 && name.length() > idx) { name = name.substring(idx + 1); } action.jumpTo(name); } catch(BadLocationException ex) { Support.LOG.log(Level.SEVERE, null, ex); } } } else if (SwingUtilities.isRightMouseButton(e)) { popupMenu.clickPoint.setLocation(e.getPoint()); popupMenu.pane = (JTextPane)e.getSource(); popupMenu.show((JTextPane)e.getSource(), e.getPoint().x, e.getPoint().y); } } catch(Exception ex) { Support.LOG.log(Level.SEVERE, null, ex); } }
/** * Calculate the width of the line represented by the given element. It is * assumed that the font and font metrics are up-to-date. */ private int getLineWidth(Element line) { int p0 = line.getStartOffset(); int p1 = line.getEndOffset(); int w; Segment s = SegmentCache.getSharedSegment(); try { line.getDocument().getText(p0, p1 - p0, s); w = Utilities.getTabbedTextWidth(s, metrics, tabBase, this, p0); } catch (BadLocationException ble) { w = 0; } SegmentCache.releaseSharedSegment(s); return w; }
@Override public void mouseClicked(MouseEvent e) { try { if (SwingUtilities.isLeftMouseButton(e)) { JTextPane pane = (JTextPane)e.getSource(); StyledDocument doc = pane.getStyledDocument(); Element elem = doc.getCharacterElement(pane.viewToModel(e.getPoint())); AttributeSet as = elem.getAttributes(); Link link = (Link)as.getAttribute(LINK_ATTRIBUTE); if (link != null) { link.onClick(elem.getDocument().getText(elem.getStartOffset(), elem.getEndOffset() - elem.getStartOffset())); } } } catch(Exception ex) { Support.LOG.log(Level.SEVERE, null, ex); } } }
@Override public void mouseClicked(MouseEvent e) { try { if (SwingUtilities.isLeftMouseButton(e)) { Element elem = element(e); AttributeSet as = elem.getAttributes(); StackTraceAction stacktraceAction = (StackTraceAction) as.getAttribute(HyperlinkSupport.STACKTRACE_ATTRIBUTE); if (stacktraceAction != null) { try { StackTraceAction.openStackTrace(elem.getDocument().getText(elem.getStartOffset(), elem.getEndOffset() - elem.getStartOffset()), false); } catch(Exception ex) { Support.LOG.log(Level.SEVERE, null, ex); } } } } catch(Exception ex) { Support.LOG.log(Level.SEVERE, null, ex); } } @Override
private void fixCloseBraceIfNecessary( String previousLine ) throws BadLocationException { Element root = _editor.getDocument().getRootElements()[0]; int iStart = _editor.getCaretPosition(); Element line = root.getElement( root.getElementIndex( iStart ) ); int iEnd = line.getEndOffset(); if( iStart < _editor.getDocument().getLength() ) { String strLine = line.getDocument().getText( iStart, iEnd - iStart ); if( strLine.trim().startsWith( "}" ) ) { int offset = strLine.indexOf( '}' ); boolean previousLineWasOpenBrace = previousLine.trim().endsWith( "{" ); if( previousLineWasOpenBrace ) { _editor.getDocument().insertString( iStart, "\n", null ); offset += 1; } parseAndWaitForParser(); _editor.setCaretPosition( iStart + offset ); _handleBraceRightNow( _editor.getCaretPosition(), false ); if( previousLineWasOpenBrace ) { _editor.setCaretPosition( iStart ); } } } }
public FragmentView(Element elem, int offset, int length){ super(elem); try { Document doc = elem.getDocument(); this.startPos = doc.createPosition(super.getStartOffset() + offset); this.endPos = doc.createPosition(startPos.getOffset() + length); } catch (BadLocationException e) { ErrorManager.getDefault().notify(e); } }
private void fixCloseBraceIfNecessary( String previousLine ) throws BadLocationException { Element root = getEditor().getDocument().getRootElements()[0]; int iStart = getEditor().getCaretPosition(); Element line = root.getElement( root.getElementIndex( iStart ) ); int iEnd = line.getEndOffset(); if( iStart < getEditor().getDocument().getLength() ) { String strLine = line.getDocument().getText( iStart, iEnd - iStart ); if( strLine.trim().startsWith( "}" ) ) { int offset = strLine.indexOf( '}' ); boolean previousLineWasOpenBrace = previousLine.trim().endsWith( "{" ); if( previousLineWasOpenBrace ) { getEditor().getDocument().insertString( iStart, "\n", null ); offset += 1; } parseAndWaitForParser(); getEditor().setCaretPosition( iStart + offset ); _handleBraceRightNow( getEditor().getCaretPosition(), false ); if( previousLineWasOpenBrace ) { getEditor().setCaretPosition( iStart ); } } } }
private Element process( final javax.swing.text.Element textElement ) throws BadLocationException { if ( textElement.isLeaf() ) { final int endOffset = textElement.getEndOffset(); final int startOffset = textElement.getStartOffset(); final String text = textElement.getDocument().getText( startOffset, endOffset - startOffset ); final Element result = new Element(); result.setElementType( LabelType.INSTANCE ); result.setAttribute( AttributeNames.Core.NAMESPACE, AttributeNames.Core.VALUE, text ); configureStyle( textElement.getAttributes(), result ); return result; } final Band band = new Band(); configureStyle( textElement.getAttributes(), band ); configureBand( textElement, band ); final int size = textElement.getElementCount(); for ( int i = 0; i < size; i++ ) { final Element element = process( textElement.getElement( i ) ); band.addElement( element ); } return band; }