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

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

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

Element.getElement介绍

暂无

代码示例

代码示例来源:origin: SonarSource/sonarqube

  1. protected String getTextLineNumber(int rowStartOffset) {
  2. Element root = component.getDocument().getDefaultRootElement();
  3. int index = root.getElementIndex(rowStartOffset);
  4. Element line = root.getElement(index);
  5. if (line.getStartOffset() == rowStartOffset)
  6. return String.valueOf(index + 1);
  7. else
  8. return "";
  9. }

代码示例来源:origin: bobbylight/RSyntaxTextArea

  1. private static Element getLineElem(Document d, int offs) {
  2. Element map = d.getDefaultRootElement();
  3. int index = map.getElementIndex(offs);
  4. Element elem = map.getElement(index);
  5. if ((offs>=elem.getStartOffset()) && (offs<elem.getEndOffset())) {
  6. return elem;
  7. }
  8. return null;
  9. }

代码示例来源:origin: bobbylight/RSyntaxTextArea

  1. private boolean getDoAdd(Document doc, Element map, int startLine,
  2. int endLine, String[] startEnd)
  3. throws BadLocationException {
  4. boolean doAdd = false;
  5. for (int i=startLine; i<=endLine; i++) {
  6. Element elem = map.getElement(i);
  7. int start = elem.getStartOffset();
  8. String t = doc.getText(start, elem.getEndOffset()-start-1);
  9. if (!t.startsWith(startEnd[0]) ||
  10. (startEnd[1]!=null && !t.endsWith(startEnd[1]))) {
  11. doAdd = true;
  12. break;
  13. }
  14. }
  15. return doAdd;
  16. }

代码示例来源:origin: ron190/jsql-injection

  1. /**
  2. * Get position of the beginning of the line.
  3. * @param line Index of the line
  4. * @return Offset of line
  5. * @throws BadLocationException
  6. */
  7. public int getLineStartOffset(int line) throws BadLocationException {
  8. Element map = this.getDocument().getDefaultRootElement();
  9. if (line < 0) {
  10. throw new BadLocationException("Negative line", -1);
  11. } else if (line >= map.getElementCount()) {
  12. throw new BadLocationException("No such line", this.getDocument().getLength() + 1);
  13. } else {
  14. Element lineElem = map.getElement(line);
  15. return lineElem.getStartOffset();
  16. }
  17. }

代码示例来源:origin: bobbylight/RSyntaxTextArea

  1. @Override
  2. public int yForLine(Rectangle alloc, int line) throws BadLocationException {
  3. return yForLineContaining(alloc,
  4. getElement().getElement(line).getStartOffset());
  5. //return alloc.y + getOffset(Y_AXIS, line);
  6. }

代码示例来源:origin: bobbylight/RSyntaxTextArea

  1. @Override
  2. protected int getWordEnd(RTextArea textArea, int offs)
  3. throws BadLocationException {
  4. RSyntaxDocument doc = (RSyntaxDocument)textArea.getDocument();
  5. Element root = doc.getDefaultRootElement();
  6. int line = root.getElementIndex(offs);
  7. Element elem = root.getElement(line);
  8. int end = elem.getEndOffset() - 1;
  9. int wordEnd = offs;
  10. while (wordEnd <= end) {
  11. if (!isIdentifierChar(doc.charAt(wordEnd))) {
  12. break;
  13. }
  14. wordEnd++;
  15. }
  16. return wordEnd;
  17. }

代码示例来源:origin: SKCraft/Launcher

  1. private void removeFromStart(Document document, Element root, int excess) {
  2. Element line = root.getElement(excess - 1);
  3. int end = line.getEndOffset();
  4. try {
  5. document.remove(0, end);
  6. } catch (BadLocationException ble) {
  7. System.out.println(ble);
  8. }
  9. }

代码示例来源:origin: bobbylight/RSyntaxTextArea

  1. /**
  2. * Returns the leading whitespace of a specific line in a document.
  3. *
  4. * @param doc The document.
  5. * @param offs The offset whose line to get the leading whitespace for.
  6. * @return The leading whitespace.
  7. * @throws BadLocationException If <code>offs</code> is not a valid offset
  8. * in the document.
  9. * @see #getLeadingWhitespace(String)
  10. */
  11. public static String getLeadingWhitespace(Document doc, int offs)
  12. throws BadLocationException {
  13. Element root = doc.getDefaultRootElement();
  14. int line = root.getElementIndex(offs);
  15. Element elem = root.getElement(line);
  16. int startOffs = elem.getStartOffset();
  17. int endOffs = elem.getEndOffset() - 1;
  18. String text = doc.getText(startOffs, endOffs-startOffs);
  19. return getLeadingWhitespace(text);
  20. }

代码示例来源:origin: bobbylight/RSyntaxTextArea

  1. /**
  2. * Makes our private <code>Segment s</code> point to the text in our
  3. * document referenced by the specified element. Note that
  4. * <code>line</code> MUST be a valid line number in the document.
  5. *
  6. * @param line The line number you want to get.
  7. */
  8. private void setSharedSegment(int line) {
  9. Element map = getDefaultRootElement();
  10. //int numLines = map.getElementCount();
  11. Element element = map.getElement(line);
  12. if (element==null) {
  13. throw new InternalError("Invalid line number: " + line);
  14. }
  15. int startOffset = element.getStartOffset();
  16. //int endOffset = (line==numLines-1 ?
  17. // element.getEndOffset()-1 : element.getEndOffset() - 1);
  18. int endOffset = element.getEndOffset()-1; // Why always "-1"?
  19. try {
  20. getText(startOffset, endOffset-startOffset, s);
  21. } catch (BadLocationException ble) {
  22. throw new InternalError("Text range not in document: " +
  23. startOffset + "-" + endOffset);
  24. }
  25. }

代码示例来源:origin: skylot/jadx

  1. @Nullable
  2. protected String getTextLineNumber(int rowStartOffset) {
  3. Element root = codeArea.getDocument().getDefaultRootElement();
  4. int index = root.getElementIndex(rowStartOffset);
  5. Element line = root.getElement(index);
  6. if (line.getStartOffset() != rowStartOffset) {
  7. return null;
  8. }
  9. int lineNumber = index + 1;
  10. if (useSourceLines) {
  11. Integer sourceLine = codeArea.getSourceLine(lineNumber);
  12. if (sourceLine == null) {
  13. return null;
  14. }
  15. return String.valueOf(sourceLine);
  16. }
  17. return String.valueOf(lineNumber);
  18. }

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

  1. public int getLineOffset( int iLine )
  2. {
  3. Element root = getDocument().getRootElements()[0];
  4. iLine = root.getElementCount() < iLine ? root.getElementCount() : iLine;
  5. Element elemAtLine = root.getElement( iLine );
  6. return elemAtLine == null ? 0 : elemAtLine.getStartOffset();
  7. }

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

  1. private int getLineStart(int i) throws BadLocationException {
  2. Element line = editor.getDocument().getDefaultRootElement().getElement(i);
  3. Rectangle result = editor.modelToView(line.getStartOffset());
  4. if (result != null) {
  5. return result.y;
  6. }
  7. return -1;
  8. }

代码示例来源:origin: bobbylight/RSyntaxTextArea

  1. @Override
  2. public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {
  3. try {
  4. // We use the elements instead of calling getLineOfOffset(),
  5. // etc. to speed things up just a tad (i.e. micro-optimize).
  6. Document document = textArea.getDocument();
  7. int caretPosition = textArea.getCaretPosition();
  8. Element map = document.getDefaultRootElement();
  9. int currentLineNum = map.getElementIndex(caretPosition);
  10. Element currentLineElement = map.getElement(currentLineNum);
  11. // Always take -1 as we don't want to remove the newline.
  12. int currentLineEnd = currentLineElement.getEndOffset()-1;
  13. if (caretPosition<currentLineEnd) {
  14. document.remove(caretPosition,
  15. currentLineEnd-caretPosition);
  16. }
  17. } catch (BadLocationException ble) {
  18. ble.printStackTrace();
  19. }
  20. }

代码示例来源:origin: SKCraft/SKMCLauncher

  1. private void removeFromStart(Document document, Element root) {
  2. Element line = root.getElement(0);
  3. int end = line.getEndOffset();
  4. try {
  5. document.remove(0, end);
  6. } catch (BadLocationException ble) {
  7. System.out.println(ble);
  8. }
  9. }

代码示例来源:origin: bobbylight/RSyntaxTextArea

  1. private void doError(SAXParseException e, ParserNotice.Level level) {
  2. int line = e.getLineNumber() - 1;
  3. Element root = doc.getDefaultRootElement();
  4. Element elem = root.getElement(line);
  5. int offs = elem.getStartOffset();
  6. int len = elem.getEndOffset() - offs;
  7. if (line==root.getElementCount()-1) {
  8. len++;
  9. }
  10. DefaultParserNotice pn = new DefaultParserNotice(XmlParser.this,
  11. e.getMessage(), line, offs, len);
  12. pn.setLevel(level);
  13. result.addNotice(pn);
  14. }

代码示例来源:origin: bobbylight/RSyntaxTextArea

  1. /**
  2. * Returns whether a parser notice contains the specified offset.
  3. *
  4. * @param notice The notice.
  5. * @param offs The offset.
  6. * @return Whether the notice contains the offset.
  7. */
  8. private boolean noticeContainsPosition(ParserNotice notice, int offs){
  9. if (notice.getKnowsOffsetAndLength()) {
  10. return notice.containsPosition(offs);
  11. }
  12. Document doc = textArea.getDocument();
  13. Element root = doc.getDefaultRootElement();
  14. int line = notice.getLine();
  15. if (line<0) { // Defensive against possible bad user-defined notices.
  16. return false;
  17. }
  18. Element elem = root.getElement(line);
  19. return elem != null && offs>=elem.getStartOffset() && offs<elem.getEndOffset();
  20. }

代码示例来源:origin: groovy/groovy-core

  1. public int findTabLocation(int offset) {
  2. // find first {
  3. boolean cont = true;
  4. while (offset > -1 && cont) {
  5. Element el = doc.getCharacterElement(offset);
  6. Object color =
  7. el.getAttributes().getAttribute(StyleConstants.Foreground);
  8. if (!COMMENT_COLOR.equals(color)) {
  9. cont = segment.array[offset] != '{' &&
  10. segment.array[offset] != '}';
  11. }
  12. offset -= cont ? 1 : 0;
  13. }
  14. if (offset > -1 && segment.array[offset] == '{') {
  15. while (offset > -1 &&
  16. !Character.isWhitespace(segment.array[offset--])) {
  17. }
  18. }
  19. int index = offset < 0 || segment.array[offset] == '}' ? -4 : 0;
  20. if (offset > -1) {
  21. Element top = doc.getDefaultRootElement();
  22. offset = top.getElement(top.getElementIndex(offset)).getStartOffset();
  23. while (Character.isWhitespace(segment.array[offset++])) {
  24. index++;
  25. }
  26. }
  27. return index;
  28. }
  29. }

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

  1. /** Return start offset of the line
  2. * @param lineIndex line index starting from 0
  3. * @return start position of the line or -1 if lineIndex was invalid
  4. */
  5. public static int getRowStartFromLineOffset(BaseDocument doc, int lineIndex) {
  6. Element lineRoot = doc.getParagraphElement(0).getParentElement();
  7. if (lineIndex < 0 || lineIndex >= lineRoot.getElementCount()) {
  8. return -1; // invalid line number
  9. } else {
  10. return lineRoot.getElement(lineIndex).getStartOffset();
  11. }
  12. }

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

  1. private int getLineStart(int i) throws BadLocationException {
  2. Element line = editor.getDocument().getDefaultRootElement().getElement(i);
  3. Rectangle result = editor.modelToView(line.getStartOffset());
  4. if (result != null) {
  5. return result.y;
  6. }
  7. return -1;
  8. }

代码示例来源:origin: bobbylight/RSyntaxTextArea

  1. @Override
  2. public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {
  3. int offs = textArea.getCaretPosition();
  4. int endOffs = 0;
  5. try {
  6. if (textArea.getLineWrap()) {
  7. // Must check per character, since one logical line may be
  8. // many physical lines.
  9. // FIXME: Replace Utilities call with custom version to
  10. // cut down on all of the modelToViews, as each call causes
  11. // a getTokenList => expensive!
  12. endOffs = Utilities.getRowEnd(textArea, offs);
  13. }
  14. else {
  15. Element root = textArea.getDocument().getDefaultRootElement();
  16. int line = root.getElementIndex(offs);
  17. endOffs = root.getElement(line).getEndOffset() - 1;
  18. }
  19. if (select) {
  20. textArea.moveCaretPosition(endOffs);
  21. }
  22. else {
  23. textArea.setCaretPosition(endOffs);
  24. }
  25. } catch (Exception ex) {
  26. UIManager.getLookAndFeel().provideErrorFeedback(textArea);
  27. }
  28. }

相关文章