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

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

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

Element.getEndOffset介绍

暂无

代码示例

代码示例来源: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: 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 void handleToggleComment(Element elem, Document doc,
  2. String[] startEnd, boolean add) throws BadLocationException {
  3. int start = elem.getStartOffset();
  4. int end = elem.getEndOffset() - 1;
  5. if (add) {
  6. if (startEnd[1]!=null) {
  7. doc.insertString(end, startEnd[1], null);
  8. }
  9. doc.insertString(start, startEnd[0], null);
  10. }
  11. else {
  12. if (startEnd[1]!=null) {
  13. int temp = startEnd[1].length();
  14. doc.remove(end-temp, temp);
  15. }
  16. doc.remove(start, startEnd[0].length());
  17. }
  18. }

代码示例来源: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: fr.inria.wimmics/kggui

  1. public int getLineOfOffset(final JTextComponent textComponent, final int line) throws BadLocationException {
  2. final Document doc = textComponent.getDocument();
  3. final int lineCount = doc.getDefaultRootElement().getElementCount();
  4. if (line < 0) {
  5. throw new BadLocationException("Negative line", -1);
  6. } else if (line > lineCount) {
  7. throw new BadLocationException("No such line", doc.getLength() + 1);
  8. } else {
  9. Element map = doc.getDefaultRootElement();
  10. Element lineElem = map.getElement(line);
  11. return lineElem.getEndOffset();
  12. }
  13. }

代码示例来源: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: bobbylight/RSyntaxTextArea

  1. int tabSize)
  2. throws BadLocationException {
  3. int start = elem.getStartOffset();
  4. int end = elem.getEndOffset() - 1; // Why always true??
  5. doc.getText(start,end-start, s);
  6. int i = s.offset;

代码示例来源: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: Wimmics/corese

  1. public int getLineOfOffset(final JTextComponent textComponent, final int line) throws BadLocationException {
  2. final Document doc = textComponent.getDocument();
  3. final int lineCount = doc.getDefaultRootElement().getElementCount();
  4. if (line < 0) {
  5. throw new BadLocationException("Negative line", -1);
  6. } else if (line > lineCount) {
  7. throw new BadLocationException("No such line", doc.getLength() + 1);
  8. } else {
  9. Element map = doc.getDefaultRootElement();
  10. Element lineElem = map.getElement(line);
  11. return lineElem.getEndOffset();
  12. }
  13. }

代码示例来源: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: bobbylight/RSyntaxTextArea

  1. if(offs < curPara.getStartOffset()) {
  2. offs = Utilities.getParagraphElement(textArea, offs).
  3. getEndOffset() - 1;

代码示例来源: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. }

代码示例来源:origin: Spoutcraft/LegacyLauncher

  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: cmu-phil/tetrad

  1. private void removeLines() {
  2. Element root = getDocument().getDefaultRootElement();
  3. while (root.getElementCount() > maxLines) {
  4. Element firstLine = root.getElement(0);
  5. try {
  6. getDocument().remove(0, firstLine.getEndOffset());
  7. }
  8. catch (BadLocationException ble) {
  9. ble.printStackTrace();
  10. }
  11. }
  12. }
  13. }

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

  1. Element root = doc.getDefaultRootElement();
  2. int line = root.getElementIndex(offs);
  3. Element elem = root.getElement(line);
  4. int start = elem.getStartOffset();
  5. if (offs > start) {
  6. char ch = doc.charAt(offs);
  7. return -1;
  8. elem = root.getElement(--line);
  9. offs = elem.getEndOffset() - 1;
  10. while (prevWordStart == -1 && line > 0) {
  11. line--;
  12. elem = root.getElement(line);
  13. prevWordStart = getPreviousWordStartInLine(doc, elem,
  14. elem.getEndOffset());

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

  1. Element elem = root.getElement(line);
  2. int start = elem.getStartOffset();
  3. elem = root.getElement(endLine);
  4. int end = elem.getEndOffset();
  5. int lineCount = textArea.getLineCount();
  6. boolean movingLastLine = false;

相关文章