javax.swing.text.Element.getDocument()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(10.5k)|赞(0)|评价(0)|浏览(137)

本文整理了Java中javax.swing.text.Element.getDocument()方法的一些代码示例,展示了Element.getDocument()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Element.getDocument()方法的具体详情如下:
包路径:javax.swing.text.Element
类名称:Element
方法名:getDocument

Element.getDocument介绍

暂无

代码示例

代码示例来源:origin: org.netbeans.api/org-netbeans-modules-editor-document

@Override
public Document getDocument() {
  return parent.getDocument();
}

代码示例来源:origin: robotframework/SwingLibrary

public Document getDocument() {
  return element.getDocument();
}

代码示例来源:origin: org.netbeans.api/org-netbeans-modules-editor-document

AbstractPositionElement(Element parent, int startOffset, int endOffset) {
  this(
      parent,
      createPosition(parent.getDocument(), startOffset),
      createPosition(parent.getDocument(), endOffset)
  );
}

代码示例来源:origin: org.netbeans.api/org-netbeans-modules-editor-document

CustomElement(Element parent, int startOffset, int endOffset) {
  super(parent, startOffset, endOffset);
  CharSequenceUtilities.checkIndexesValid(startOffset, endOffset,
      parent.getDocument().getLength() + 1);
}

代码示例来源:origin: omegat-org/omegat

@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();
  }
}

代码示例来源:origin: net.sf.kerner-utils/kerner-utils

/**
   * 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;
    }
  }
}

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-team-commons

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);
      }
    }
  }
};

代码示例来源:origin: chatty/chatty

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("'");

代码示例来源:origin: chatty/chatty

/**
 * 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("]");
}

代码示例来源:origin: nz.ac.waikato.cms.weka.thirdparty/bounce

/**
   * 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;
  }
}

代码示例来源:origin: org.tentackle/tentackle-swing

/**
 * 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);
}

代码示例来源:origin: pentaho/pentaho-reporting

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;
}

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-team-commons

@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);
  }
}

代码示例来源:origin: nz.ac.waikato.cms.weka.thirdparty/bounce

/**
 * 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;
}

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-team-commons

@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);
    }
  }
}

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-team-commons

@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

代码示例来源:origin: org.gosu-lang.gosu/gosu-editor

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 );
   }
  }
 }
}

代码示例来源:origin: net.sf.squirrel-sql.thirdparty-non-maven/org-netbeans-modules-editor-lib

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);
  }
}

代码示例来源:origin: org.gosu-lang.gosu/gosu-lab

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 );
   }
  }
 }
}

代码示例来源:origin: pentaho/pentaho-reporting

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;
}

相关文章