コード例 #1
0
  /**
   * Finds the next instance of the string/regular expression specified from the caret position. If
   * a match is found, it is selected in this text area.
   *
   * @param textArea The text area in which to search.
   * @param context What to search for and all search options.
   * @return Whether a match was found (and thus selected).
   * @throws PatternSyntaxException If this is a regular expression search but the search text is an
   *     invalid regular expression.
   * @see #replace(RTextArea, SearchContext)
   * @see #replaceAll(RTextArea, SearchContext)
   */
  public static boolean find(JTextArea textArea, SearchContext context) {

    String text = context.getSearchFor();
    if (text == null || text.length() == 0) {
      return false;
    }

    // Be smart about what position we're "starting" at.  We don't want
    // to find a match in the currently selected text (if any), so we
    // start searching AFTER the selection if searching forward, and
    // BEFORE the selection if searching backward.
    Caret c = textArea.getCaret();
    boolean forward = context.getSearchForward();
    int start = forward ? Math.max(c.getDot(), c.getMark()) : Math.min(c.getDot(), c.getMark());

    String findIn = getFindInText(textArea, start, forward);
    if (findIn == null || findIn.length() == 0) return false;

    // Find the next location of the text we're searching for.
    if (!context.isRegularExpression()) {
      int pos =
          getNextMatchPos(text, findIn, forward, context.getMatchCase(), context.getWholeWord());
      findIn = null; // May help garbage collecting.
      if (pos != -1) {
        // Without this, if JTextArea isn't in focus, selection
        // won't appear selected.
        c.setSelectionVisible(true);
        pos = forward ? start + pos : pos;
        selectAndPossiblyCenter(textArea, pos, pos + text.length());
        return true;
      }
    } else {
      // Regex matches can have varying widths.  The returned point's
      // x- and y-values represent the start and end indices of the
      // match in findIn.
      Point regExPos =
          getNextMatchPosRegEx(
              text, findIn, forward, context.getMatchCase(), context.getWholeWord());
      findIn = null; // May help garbage collecting.
      if (regExPos != null) {
        // Without this, if JTextArea isn't in focus, selection
        // won't appear selected.
        c.setSelectionVisible(true);
        if (forward) {
          regExPos.translate(start, start);
        }
        selectAndPossiblyCenter(textArea, regExPos.x, regExPos.y);
        return true;
      }
    }

    // No match.
    return false;
  }
コード例 #2
0
ファイル: Utils.java プロジェクト: tacolalab/forstudents
 /**
  * Replaces the currently selected content with new content represented by the given StyledText.
  * If there is no selection this amounts to an insert of the given text. If there is no
  * replacement text this amounts to a removal of the current selection. The replacement text will
  * have the attributes currently defined for input at the point of insertion. If the document is
  * not editable, beep and return
  *
  * @param content the content to replace the selection with
  * @see StyledText#insert
  */
 public static void replaceSelection(Word word, StyledText content) {
   Document doc = word.workspace.getDocument();
   String text;
   Caret caret = word.workspace.getCaret();
   int insertPos = 0;
   int i;
   int contentSize;
   if (doc != null) {
     try {
       int p0 = Math.min(caret.getDot(), caret.getMark());
       int p1 = Math.max(caret.getDot(), caret.getMark());
       // if there is any selection
       if (p0 != p1) {
         doc.remove(p0, p1 - p0);
       }
       // insert the content
       if (content != null) {
         content.insert(doc, p0);
       }
     } catch (BadLocationException ble) {
       javax.swing.UIManager.getLookAndFeel().provideErrorFeedback(word.workspace);
       return;
     }
   }
 }
コード例 #3
0
ファイル: EditFrame.java プロジェクト: BrotherPhil/ROPE
  @Override
  public void internalFrameActivated(InternalFrameEvent e) {
    super.internalFrameActivated(e);

    mainFrame.pasteItem.setEnabled(canPaste());

    Caret caret = sourceArea.getCaret();
    doCaretUpdate(caret.getDot(), caret.getMark());

    setUndoItem();
    setRedoItem();
  }
コード例 #4
0
ファイル: EditorKit.java プロジェクト: niknah/SikuliX-2014
    public void actionPerformed(JTextComponent text) {
      indentationLogic = ((EditorPane) text).getIndentationLogic();
      StyledDocument doc = (StyledDocument) text.getDocument();
      Element map = doc.getDefaultRootElement();
      Caret c = text.getCaret();
      int dot = c.getDot();
      int mark = c.getMark();
      int line1 = map.getElementIndex(dot);

      if (dot != mark) {
        int line2 = map.getElementIndex(mark);
        int begin = Math.min(line1, line2);
        int end = Math.max(line1, line2);
        Element elem;
        try {
          for (line1 = begin; line1 < end; line1++) {
            elem = map.getElement(line1);
            handleDecreaseIndent(line1, elem, doc);
          }
          elem = map.getElement(end);
          int start = elem.getStartOffset();
          if (Math.max(c.getDot(), c.getMark()) != start) {
            handleDecreaseIndent(end, elem, doc);
          }
        } catch (BadLocationException ble) {
          Debug.error(me + "Problem while de-indenting line\n%s", ble.getMessage());
          UIManager.getLookAndFeel().provideErrorFeedback(text);
        }
      } else {
        Element elem = map.getElement(line1);
        try {
          handleDecreaseIndent(line1, elem, doc);
        } catch (BadLocationException ble) {
          Debug.error(me + "Problem while de-indenting line\n%s", ble.getMessage());
          UIManager.getLookAndFeel().provideErrorFeedback(text);
        }
      }
    }
コード例 #5
0
ファイル: EditorKit.java プロジェクト: niknah/SikuliX-2014
    public void actionPerformed(JTextComponent text) {
      indentationLogic = ((EditorPane) text).getIndentationLogic();
      boolean indentError = false;
      Document doc = text.getDocument();
      Element map = doc.getDefaultRootElement();
      String tabWhitespace = PreferencesUser.getInstance().getTabWhitespace();
      Caret c = text.getCaret();
      int dot = c.getDot();
      int mark = c.getMark();
      int dotLine = map.getElementIndex(dot);
      int markLine = map.getElementIndex(mark);

      if (dotLine != markLine) {
        int first = Math.min(dotLine, markLine);
        int last = Math.max(dotLine, markLine);
        Element elem;
        int start;
        try {
          for (int i = first; i < last; i++) {
            elem = map.getElement(i);
            start = elem.getStartOffset();
            doc.insertString(start, tabWhitespace, null);
          }
          elem = map.getElement(last);
          start = elem.getStartOffset();
          if (Math.max(c.getDot(), c.getMark()) != start) {
            doc.insertString(start, tabWhitespace, null);
          }
        } catch (BadLocationException ble) {
          Debug.error(me + "Problem while indenting line\n%s", ble.getMessage());
          UIManager.getLookAndFeel().provideErrorFeedback(text);
        }
      } else {
        text.replaceSelection(tabWhitespace);
      }
    }
コード例 #6
0
ファイル: EditorKit.java プロジェクト: niknah/SikuliX-2014
    @Override
    public void actionPerformed(ActionEvent e) {
      JTextComponent textArea = (JTextComponent) e.getSource();

      Caret caret = textArea.getCaret();
      int dot = caret.getDot();

      /*
       * Move to the beginning/end of selection on a "non-shifted"
       * left- or right-keypress.  We shouldn't have to worry about
       * navigation filters as, if one is being used, it let us get
       * to that position before.
       */
      if (!select) {
        switch (direction) {
          case SwingConstants.EAST:
            int mark = caret.getMark();
            if (dot != mark) {
              caret.setDot(Math.max(dot, mark));
              return;
            }
            break;
          case SwingConstants.WEST:
            mark = caret.getMark();
            if (dot != mark) {
              caret.setDot(Math.min(dot, mark));
              return;
            }
            break;
          default:
        }
      }

      Position.Bias[] bias = new Position.Bias[1];
      Point magicPosition = caret.getMagicCaretPosition();

      try {

        if (magicPosition == null
            && (direction == SwingConstants.NORTH || direction == SwingConstants.SOUTH)) {
          Rectangle r = textArea.modelToView(dot);
          magicPosition = new Point(r.x, r.y);
        }

        NavigationFilter filter = textArea.getNavigationFilter();

        if (filter != null) {
          dot =
              filter.getNextVisualPositionFrom(
                  textArea, dot, Position.Bias.Forward, direction, bias);
        } else {
          if (direction == SwingConstants.NORTH || direction == SwingConstants.SOUTH) {
            dot = getNSVisualPosition((EditorPane) textArea, dot, direction);
          } else {
            dot =
                textArea
                    .getUI()
                    .getNextVisualPositionFrom(
                        textArea, dot, Position.Bias.Forward, direction, bias);
          }
        }
        if (select) {
          caret.moveDot(dot);
        } else {
          caret.setDot(dot);
        }

        if (magicPosition != null
            && (direction == SwingConstants.NORTH || direction == SwingConstants.SOUTH)) {
          caret.setMagicCaretPosition(magicPosition);
        }

      } catch (BadLocationException ble) {
        Debug.error(me + "Problem while trying to move caret\n%s", ble.getMessage());
      }
    }
コード例 #7
0
 /**
  * Makes the caret's dot and mark the same location so that, for the next search in the specified
  * direction, a match will be found even if it was within the original dot and mark's selection.
  *
  * @param textArea The text area.
  * @param forward Whether the search will be forward through the document (<code>false</code>
  *     means backward).
  * @return The new dot and mark position.
  */
 private static int makeMarkAndDotEqual(JTextArea textArea, boolean forward) {
   Caret c = textArea.getCaret();
   int val = forward ? Math.min(c.getDot(), c.getMark()) : Math.max(c.getDot(), c.getMark());
   c.setDot(val);
   return val;
 }