public static void checkSteno(
     Settings settingsObj, StenoData stenoObj, javax.swing.JTextArea ta) {
   if (settingsObj.getStenoActivated()) {
     int caret = ta.getCaretPosition();
     if (caret > 2) {
       try {
         String text = ta.getText();
         int longest = stenoObj.retrieveLongestAbbrLength();
         int start = caret - longest - 1;
         int start2 = text.lastIndexOf(System.lineSeparator(), caret - 2);
         if (start2 > start) {
           start = start2;
         }
         String searchstring = text.substring(start, caret);
         String abbr = stenoObj.findAbbreviationFromText(searchstring);
         if (abbr != null) {
           String longword = stenoObj.getStenoWord(abbr);
           if (longword != null) {
             StringBuilder sb = new StringBuilder(text);
             sb.replace(caret - abbr.length(), caret, longword);
             ta.setText(sb.toString());
             caret = caret - abbr.length() + longword.length();
             ta.setCaretPosition(caret);
           } else {
             if (!ta.getName().equals("jTextAreaAuthor")) {
               ta.replaceSelection("\t");
             }
           }
         } else {
           if (!ta.getName().equals("jTextAreaAuthor")) {
             ta.replaceSelection("\t");
           }
         }
       } catch (IndexOutOfBoundsException | IllegalArgumentException ex) {
       }
     } else {
       if (!ta.getName().equals("jTextAreaAuthor")) {
         ta.replaceSelection("\t");
       }
     }
   } else {
     if (!ta.getName().equals("jTextAreaAuthor")) {
       ta.replaceSelection("\t");
     }
   }
 }
 @Override
 public void replaceSelection(String content) {
   getDocument().removeDocumentListener(LWTextAreaPeer.this);
   super.replaceSelection(content);
   // post only one text event in this case
   postTextEvent();
   getDocument().addDocumentListener(LWTextAreaPeer.this);
 }
 public void replace() {
   try {
     find();
     if (select_start != -1) txt.replaceSelection(textR.getText());
   } catch (NullPointerException e) {
     System.out.print("Null Pointer Exception: " + e);
   }
 }
Beispiel #4
0
  /**
   * Finds the next instance of the regular expression specified from the caret position. If a match
   * is found, it is replaced with the specified replacement string.
   *
   * @param textArea The text area in which to search.
   * @param toFind The regular expression to search for.
   * @param replaceWith The string to replace the found regex with.
   * @param forward Whether to search forward from the caret position or backward from it.
   * @param matchCase Whether the search should be case-sensitive.
   * @param wholeWord Whether there should be spaces or tabs on either side of the match.
   * @return Whether a match was found (and thus replaced).
   * @throws PatternSyntaxException If <code>toFind</code> is not a valid regular expression.
   * @throws IndexOutOfBoundsException If <code>replaceWith</code> references an invalid group (less
   *     than zero or greater than the number of groups matched).
   * @see #replace
   * @see #find
   */
  protected static boolean regexReplace(
      JTextArea textArea,
      String toFind,
      String replaceWith,
      boolean forward,
      boolean matchCase,
      boolean wholeWord)
      throws PatternSyntaxException {

    // Be smart about what position we're "starting" at. For example,
    // if they are searching backwards and there is a selection such that
    // the dot is past the mark, and the selection is the text for which
    // you're searching, this search will find and return the current
    // selection. So, in that case we start at the beginning of the
    // selection.
    Caret c = textArea.getCaret();
    int start = makeMarkAndDotEqual(textArea, forward);

    String findIn = getFindInText(textArea, start, forward);
    if (findIn == null) return false;

    // Find the next location of the text we're searching for.
    RegExReplaceInfo info =
        getRegExReplaceInfo(toFind, findIn, forward, matchCase, wholeWord, replaceWith);

    findIn = null; // May help garbage collecting.

    // If a match was found, do the replace and return!
    if (info != null) {

      // Without this, if JTextArea isn't in focus, selection won't
      // appear selected.
      c.setSelectionVisible(true);

      int matchStart = info.getStartIndex();
      int matchEnd = info.getEndIndex();
      if (forward) {
        matchStart += start;
        matchEnd += start;
      }
      selectAndPossiblyCenter(textArea, matchStart, matchEnd);
      textArea.replaceSelection(info.getReplacement());

      return true;
    }

    // No match.
    return false;
  }
Beispiel #5
0
 public void deleteMenuAction(ActionEvent event) {
   if (sourceArea != null) {
     sourceArea.replaceSelection("");
   }
 }