org.netbeans.editor.Utilities类的使用及代码示例

x33g5p2x  于2022-02-01 转载在 其他  
字(12.4k)|赞(0)|评价(0)|浏览(128)

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

Utilities介绍

[英]Various useful editor functions. Some of the methods have the same names and signatures like in javax.swing.Utilities but there is also many other useful methods. All the methods are static so there's no reason to instantiate Utilities. All the methods working with the document rely on that it is locked against modification so they don't acquire document read/write lock by themselves to guarantee the full thread safety of the execution. It's the user's task to lock the document appropriately before using methods described here. Most of the methods require org.netbeans.editor.BaseDocument instance not just the javax.swing.text.Document. The reason for that is to mark that the methods work on BaseDocument instances only, not on generic documents. To convert the Document to BaseDocument the simple conversion (BaseDocument)target.getDocument() can be done or the method getDocument(target) can be called. There are also other conversion methods like getEditorUI(), getKit() or getKitClass().
[中]各种有用的编辑器功能。有些方法的名称和签名与javax中的相同。摆动但是还有很多其他有用的方法。所有方法都是静态的,因此没有理由实例化实用程序。所有处理文档的方法都依赖于文档被锁定以防修改,因此它们不会自行获取文档读/写锁,以确保执行的全线程安全。在使用本文描述的方法之前,用户的任务是适当地锁定文档。大多数方法都需要组织。上网本。编辑BaseDocument实例,而不仅仅是javax。摆动文本文件这样做的原因是要标记这些方法只在BaseDocument实例上工作,而不在通用文档上工作。要将文档转换为BaseDocument,请选择简单转换(BaseDocument)目标。可以执行getDocument(),也可以调用getDocument(目标)方法。还有其他转换方法,如getEditorUI()、getKit()或getKitClass()。

代码示例

代码示例来源:origin: net.java.abeille/abeille

public void actionPerformed(ActionEvent evt, JTextComponent target) {
    if (target != null) {
      if (!target.isEditable() || !target.isEnabled()) {
        target.getToolkit().beep();
        return;
      }
      Caret caret = target.getCaret();
      try {
        BaseDocument doc = (BaseDocument) target.getDocument();
        int dotPos = caret.getDot();
        int bolPos = Utilities.getRowStart(target, dotPos);
        int eolPos = Utilities.getRowEnd(target, dotPos);
        eolPos = Math.min(eolPos + 1, doc.getLength()); // include
                                // '\n'
        doc.remove(bolPos, eolPos - bolPos);
      } catch (BadLocationException e) {
        target.getToolkit().beep();
      }
    }
  }
}

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

public JMenuItem getPopupMenuItem(JTextComponent target) {
  EditorUI ui = Utilities.getEditorUI(target);
  try {
    return ui.getDocument().getAnnotations().createMenu(Utilities.getKit(target), Utilities.getLineOffset(ui.getDocument(),target.getCaret().getDot()));
  } catch (BadLocationException ex) {
    return null;
  }
}

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

protected boolean hasTextBefore(JTextComponent target, String typedText) {
  BaseDocument doc = Utilities.getDocument(target);
  int dotPos = target.getCaret().getDot();
  try {
    int fnw = Utilities.getRowFirstNonWhite(doc, dotPos);
    return dotPos != fnw+typedText.length();
  } catch (BadLocationException e) {
    return false;
  }
}

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

public static int getFirstNonWhiteRow(BaseDocument doc, int offset, boolean downDir)
throws BadLocationException {
  if (isRowWhite(doc, offset)) {
    if (downDir) { // search down for non-white line
      offset = getFirstNonWhiteFwd(doc, offset);
    } else { // search up for non-white line
      offset = getFirstNonWhiteBwd(doc, offset);
    }
  }
  return offset;
}

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

/** Reformat the line around the given position. */
public static void reformatLine(BaseDocument doc, int pos)
throws BadLocationException {
  int lineStart = getRowStart(doc, pos);
  int lineEnd = getRowEnd(doc, pos);
  reformat(doc, lineStart, lineEnd);
}

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

public static String getLine(final JTextComponent component, final int offset) {
  final BaseDocument workingDocument = org.netbeans.editor.Utilities.getDocument(component);
  try {
    final int lineStartOffset = org.netbeans.editor.Utilities.getRowStart(workingDocument, offset);
    final int lineEndOffset = org.netbeans.editor.Utilities.getRowEnd(workingDocument, offset);
    return String.valueOf(workingDocument.getChars(lineStartOffset, lineEndOffset - lineStartOffset));
  } catch (BadLocationException ble) {
    return "";
  }
}

代码示例来源:origin: net.java.abeille/abeille

public void actionPerformed(ActionEvent evt, JTextComponent target) {
    if (target != null) {
      if (!target.isEditable() || !target.isEnabled()) {
        target.getToolkit().beep();
        return;
      }
      Caret caret = target.getCaret();
      try {
        BaseDocument doc = (BaseDocument) target.getDocument();
        int dotPos = caret.getDot();
        int bolPos = Utilities.getRowStart(doc, dotPos);
        int wsPos = Utilities.getPreviousWord(target, dotPos);
        wsPos = (dotPos == bolPos) ? wsPos : Math.max(bolPos, wsPos);
        doc.remove(wsPos, dotPos - wsPos);
      } catch (BadLocationException e) {
        target.getToolkit().beep();
      }
    }
  }
}

代码示例来源:origin: net.java.abeille/abeille

public void actionPerformed(ActionEvent evt, JTextComponent target) {
    if (target != null) {
      Caret caret = target.getCaret();
      try {
        int pos = Utilities.getRowFirstNonWhite((BaseDocument) target.getDocument(), caret.getDot());
        if (pos >= 0) {
          if (select) {
            caret.moveDot(pos);
          }
          else {
            caret.setDot(pos);
          }
        }
      } catch (BadLocationException e) {
        target.getToolkit().beep();
      }
    }
  }
}

代码示例来源:origin: net.java.abeille/abeille

public void actionPerformed(ActionEvent evt, JTextComponent target) {
    if (target != null) {
      Caret caret = target.getCaret();
      try {
        int pos = Utilities.getRowLastNonWhite((BaseDocument) target.getDocument(), caret.getDot());
        if (pos >= 0) {
          if (select) {
            caret.moveDot(pos);
          }
          else {
            caret.setDot(pos);
          }
        }
      } catch (BadLocationException e) {
        target.getToolkit().beep();
      }
    }
  }
}

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

public void actionPerformed(ActionEvent evt, JTextComponent target) {
  if (target != null) {
    if (!target.isEditable() || !target.isEnabled()) {
      target.getToolkit().beep();
      return;
    EditorUI editorUI = Utilities.getEditorUI(target);
    Caret caret = target.getCaret();
    final BaseDocument doc = Utilities.getDocument(target);
    if (caret.isSelectionVisible()) {
      target.replaceSelection(null);
    int dotPos = caret.getDot();
    String s = editorUI.getWordMatch().getMatchWord(dotPos, direction);
    String prevWord = editorUI.getWordMatch().getPreviousWord();
    if (s != null) {
      doc.atomicLock();
      try {
        int pos = dotPos;
        if (prevWord != null && prevWord.length() > 0) {
          pos -= prevWord.length();
          doc.remove(pos, prevWord.length());
        doc.insertString(pos, s, null);
      } catch (BadLocationException e) {
        target.getToolkit().beep();
      } finally {
        doc.atomicUnlock();

代码示例来源:origin: net.java.abeille/abeille

public void actionPerformed(ActionEvent evt, JTextComponent target) {
    if (target != null) {
      try {
        Caret caret = target.getCaret();
        BaseDocument doc = Utilities.getDocument(target);
        int dotPos = caret.getDot();
        ExtSyntaxSupport sup = (ExtSyntaxSupport) doc.getSyntaxSupport();
        if (dotPos > 0) {
          int[] matchBlk = sup.findMatchingBlock(dotPos - 1, false);
          if (matchBlk != null) {
            if (select) {
              caret.moveDot(matchBlk[1]);
            }
            else {
              caret.setDot(matchBlk[1]);
            }
          }
        }
      } catch (BadLocationException e) {
        target.getToolkit().beep();
      }
    }
  }
}

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

@SuppressWarnings("unchecked")
private PPBlockInfo getBlock(final JTextComponent component) {
  try {
    final BaseDocument doc = (BaseDocument) component.getDocument();
    final int lineNumber = Utilities.getLineOffset(doc, component.getCaret().getDot());
    final ArrayList<PPLine> lineList = (ArrayList) doc.getProperty(DocumentPreprocessor.PREPROCESSOR_LINE_LIST);
    if (lineList != null) {
      PPLine myPpLine = null;
      if (lineNumber >= 0 && lineNumber < lineList.size()) {
        myPpLine = lineList.get(lineNumber);
        if (myPpLine != null)
          return myPpLine.getBlock();
      }
    }
  } catch (BadLocationException ble) {
  }
  return null;
}

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

public void run() {
    try {
      final BaseDocument doc = (BaseDocument)txt.getDocument();
      final int s = Utilities.getRowStartFromLineOffset(doc, getSelectionStartLine(txt) - 1);
      final int e = Utilities.getRowEnd(txt, Utilities.getRowStartFromLineOffset(doc, getSelectionEndLine(txt) - 1));
      doc.insertString(e, "\n//#enddebug", null); //NOI18N
      doc.insertString(s, "//#mdebug\n", null); //NOI18N
      txt.setSelectionEnd(txt.getSelectionStart());
    } catch (BadLocationException ble) {
      ErrorManager.getDefault().notify(ble);
    }
    RecommentAction.actionPerformed(txt);
  }
});

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

public void run() {
    try {
      final BaseDocument doc = (BaseDocument)txt.getDocument();
      final int s = Utilities.getRowStartFromLineOffset(doc, getSelectionStartLine(txt) - 1);
      final int e = Utilities.getRowEnd(txt, Utilities.getRowStartFromLineOffset(doc, getSelectionEndLine(txt) - 1));
      final String text = doc.getText(s, e-s);
      doc.insertString(e,  "\n//#else\n" + text + "\n//#endif", null); //NOI18N
      doc.insertString(s, "//#if \n", null); //NOI18N
      txt.setSelectionStart(s + 6);
      txt.setSelectionEnd(s + 6);
    } catch (BadLocationException ble) {
      ErrorManager.getDefault().notify(ble);
    }
    RecommentAction.actionPerformed(txt);
  }
});

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-cnd-completion

if (getActiveParameterIndex() == -1) { // not showing active parm
  try {
    int fnwpos = Utilities.getFirstNonWhiteFwd(doc, offset + len);
    if (fnwpos > -1 && doc.getChars(fnwpos, 1)[0] == ')') { // NOI18N
      text = doc.getText(offset + len, fnwpos + 1 - offset - len);
      len = fnwpos + 1 - offset;
      try {
        doc.insertString(c.getCaretPosition(), paramName, null);
        substed = true;
      } catch (BadLocationException e) {
boolean addSpace = CodeStyle.getDefault(doc).spaceAfterComma();
try {
  if (addSpace && (ind == 0 || (offset > 0 && Character.isWhitespace(DocumentUtilities.getText(doc, offset - 1, 1).charAt(0))))) {
    addSpace = false;
String paramsText = null;
try {
  int fnwpos = Utilities.getFirstNonWhiteFwd(doc, offset + len);
  if (fnwpos > -1 && fnwpos <= Utilities.getRowEnd(doc, offset + len) && doc.getChars(fnwpos, 1)[0] == '(') { // NOI18N
    int fnwpos = Utilities.getFirstNonWhiteFwd(doc, offset + len);
c.setCaretPosition(insertOffset + insertText.length());

代码示例来源:origin: net.java.abeille/abeille

/**
 * Get the starting position of the row.
 * 
 * @param c
 *            text component to operate on
 * @param offset
 *            position in document where to start searching
 * @return position of the start of the row or -1 for invalid position
 */
public static int getRowStart(JTextComponent c, int offset) throws BadLocationException {
  return getRowStart((BaseDocument) c.getDocument(), offset, 0);
}

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-ruby-rhtml

private boolean handleInsertion(BaseDocument doc, Caret caret, char c) {
  int dotPos = caret.getDot();
      String s = doc.getText(dotPos-2, 2);
      if ("%=".equals(s) && dotPos >= 3) { // NOI18N
        s = doc.getText(dotPos-3, 3);
        doc.insertString(dotPos, "  ", null);
        caret.setDot(dotPos+1);
        return true;
        if (token.id() == RhtmlTokenId.HTML && doc.getText(dotPos-1, 1).charAt(0) == '<') {
          int first = Utilities.getFirstNonWhiteFwd(doc, dotPos, Utilities.getRowEnd(doc, dotPos));
          if (first == -1) {
            doc.insertString(dotPos, "%%>", null); // NOI18N
            caret.setDot(dotPos+1);
            return true;
            CharSequence suffix = DocumentUtilities.getText(doc, tokenPos, 2);
            if (CharSequenceUtilities.textEquals(suffix, "%>")) { // NOI18N
              caret.setDot(dotPos+1);
            int first = Utilities.getFirstNonWhiteFwd(doc, dotPos, Utilities.getRowEnd(doc, dotPos));
            if (first == -1) {
              doc.insertString(dotPos, "%%>", null); // NOI18N

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-web-jsf

JTextComponent target = Utilities.getFocusedComponent();
if (target == null || target.getDocument() != bdoc)
  return null;
ExtSyntaxSupport sup = (ExtSyntaxSupport)bdoc.getSyntaxSupport();
TokenItem token = sup.getTokenChain(offset, offset+1);
if (token == null || token.getTokenID().getNumericID() != JSFEditorUtilities.XML_TEXT)

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-cnd-completion

@Override
public void performClickAction(Document originalDoc, final int offset, final HyperlinkType type) {
  if (originalDoc == null) {
    return;
  }
  final Document doc = originalDoc;
  final JTextComponent target = Utilities.getFocusedComponent();
  if (target == null || target.getDocument() != doc) {
    return;
  }
  Runnable run = new Runnable() {
    @Override
    public void run() {
      int[] span = CsmMacroExpansion.getMacroExpansionSpan(doc, offset, false);
      if (type == HyperlinkType.ALT_HYPERLINK && (span != null && span[0] != span[1])) {
        // in this mode we open MacroView
        CsmMacroExpansion.showMacroExpansionView(doc, offset);
      } else {
        performAction(doc, target, offset, type);
      }
    }
  };
  if (hyperLinkTask != null) {
    hyperLinkTask.cancel();
  }
  hyperLinkTask = CsmModelAccessor.getModel().enqueue(run, "Following hyperlink");// NOI18N
}

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

public void actionPerformed(ActionEvent evt, JTextComponent target) {
    if (target != null) {
      try {
        Caret caret = target.getCaret();
        BaseDocument doc = Utilities.getDocument(target);
        int caretLine = Utilities.getLineOffset(doc, caret.getDot());
        AnnotationDesc aDesc = doc.getAnnotations().activateNextAnnotation(caretLine);
      } catch (BadLocationException e) {
        e.printStackTrace();
      }
    }
  }
}

相关文章

微信公众号

最新文章

更多