Exemple #1
0
  public void removeTrailingWhitespace() {
    int end = textArea.getDocument().getLength();

    // remove trailing empty lines
    int realEnd = end;
    if (eolAt(end - 1)) {
      while (eolAt(end - 2) || getText(end - 2, end - 1).equals("\r")) end--;
      if (end < realEnd) textArea.replaceRange("", end - 1, realEnd - 1);
    }

    // remove trailing white space from each line
    for (int i = textArea.getLineCount() - 1; i >= 0; i--)
      try {
        int start = textArea.getLineStartOffset(i);
        if (eolAt(end - 1)) end--;
        if (start == end) continue;
        String line = getText(start, end);
        realEnd = end;
        while (start < end && Character.isWhitespace(line.charAt(end - start - 1))) end--;
        if (end < realEnd) textArea.replaceRange("", end, realEnd);
        end = start;
      } catch (BadLocationException e) {
        /* cannot happen */
      }
  }
Exemple #2
0
  private void search(int direction) {
    String searchText = searchField.getText();
    if (searchText == null || searchText.length() == 0 || rTextArea.getText() == null) {
      return;
    }

    boolean forward = direction >= 0;
    boolean matchCase = matchCaseCB.isSelected();
    boolean regex = regexCB.isSelected();
    boolean wholeWord = wholeWordCB.isSelected();

    if (markAllCB.isSelected()) {
      rTextArea.markAll(searchText, matchCase, wholeWord, regex);
    } else {
      rTextArea.clearMarkAllHighlights();
    }

    SearchContext context = new SearchContext();
    context.setSearchFor(searchText);
    context.setMatchCase(matchCase);
    context.setRegularExpression(regex);
    context.setSearchForward(forward);
    context.setWholeWord(wholeWord);

    // TODO hack: move cursor before previous search for not jump to next occurrence
    if (direction == 0 && !COLOR_BG_ERROR.equals(searchField.getBackground())) {
      try {
        int caretPos = rTextArea.getCaretPosition();
        int lineNum = rTextArea.getLineOfOffset(caretPos) - 1;
        if (lineNum > 1) {
          rTextArea.setCaretPosition(rTextArea.getLineStartOffset(lineNum));
        }
      } catch (BadLocationException e) {
        LOG.error("Caret move error", e);
      }
    }

    boolean found = SearchEngine.find(rTextArea, context);
    if (!found) {
      int pos =
          SearchEngine.getNextMatchPos(
              searchText, rTextArea.getText(), forward, matchCase, wholeWord);
      if (pos != -1) {
        rTextArea.setCaretPosition(forward ? 0 : rTextArea.getDocument().getLength() - 1);
        search(direction);
        searchField.setBackground(COLOR_BG_WARN);
        return;
      }
      searchField.setBackground(COLOR_BG_ERROR);
    } else {
      searchField.setBackground(COLOR_BG_NORMAL);
    }
  }
 /**
  * Marque une ligne du texte avec une icône.
  *
  * @param line Numéro de la ligne du texte.
  * @param icon Nom de l'icône à utiliser (une icône indiquant une erreur par défaut).
  */
 public void signalLine(int line, String icon) {
   Gutter gutter = scrollPane.getGutter();
   gutter.setBookmarkingEnabled(true);
   try {
     textArea.setCaretPosition(textArea.getLineStartOffset(line - 1));
     scrollPane
         .getGutter()
         .addLineTrackingIcon(
             line - 1,
             Macros.getIcon(icon == null ? "org/javascool/widgets/icons/error.png" : icon));
   } catch (Exception e) {
   }
 }