Package javax.swing.text

Examples of javax.swing.text.Caret


    public void mousePressed(MouseEvent e) {
      // WORKAROUND:  Since JTextComponent only updates the caret
      // location on mouse clicked and released, we'll do it on dragged
      // events when the left mouse button is clicked.
      if ((e.getModifiers() & MouseEvent.BUTTON1_MASK) != 0) {
        Caret caret = getCaret();
        dot = caret.getDot();
        mark = caret.getMark();
        fireCaretUpdate(this);
      }
    }
View Full Code Here


   * @param textArea The text area to operate on.
   * @throws BadLocationException If something bad happens.
   */
  public void invoke(RSyntaxTextArea textArea) throws BadLocationException {

    Caret c = textArea.getCaret();
    int dot = c.getDot();
    int mark = c.getMark();
    int p0 = Math.min(dot, mark);
    int p1 = Math.max(dot, mark);
    RSyntaxDocument doc = (RSyntaxDocument)textArea.getDocument();
    Element map = doc.getDefaultRootElement();

View Full Code Here

                view.getStartOffset();
          break;
        }
        RSyntaxTextArea target = (RSyntaxTextArea)view.
                          getContainer();
        Caret c = (target != null) ? target.getCaret() : null;
        // YECK! Ideally, the x location from the magic caret
        // position would be passed in.
        Point mcp;
        if (c != null)
          mcp = c.getMagicCaretPosition();
        else
          mcp = null;
        int x;
        if (mcp == null) {
          Rectangle loc = target.modelToView(pos);
View Full Code Here

   * @param e The event.
   */
  public void actionPerformed(ActionEvent e) {

    // Don't do anything if they are selecting text.
    Caret c = textArea.getCaret();
    if (c.getDot()!=c.getMark()) {
      return;
    }

    RSyntaxDocument doc = (RSyntaxDocument)textArea.getDocument();
    //long time = System.currentTimeMillis();
    doc.readLock();
    try {

      // Remove old highlights
      removeHighlights();

      // Get the token at the caret position.
      int line = textArea.getCaretLineNumber();
      Token tokenList = textArea.getTokenListForLine(line);
      int dot = c.getDot();
      Token t = RSyntaxUtilities.getTokenAtOffset(tokenList, dot);
      if (t==null /* EOL */ || !isValidType(t) || isNonWordChar(t)) {
        // Try to the "left" of the caret.
        dot--;
        try {
View Full Code Here

    // 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();
    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 (regex==false) {
      int pos = getNextMatchPos(text, findIn, forward,
                      matchCase, wholeWord);
      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;
        c.setDot(pos);
        c.moveDot(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, matchCase, wholeWord);
      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);
        }
        c.setDot(regExPos.x);
        c.moveDot(regExPos.y);
        return true;
      }
    }

    // No match.
View Full Code Here

   *        document (<code>false</code> means backward).
   * @return The new dot and mark position.
   */
  protected 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;
  }
View Full Code Here

    // 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;
      }
      c.setDot(matchStart);
      c.moveDot(matchEnd);
      textArea.replaceSelection(info.getReplacement());

      return true;

    }
View Full Code Here

  protected void insertCompletion(Completion c) {
    System.out.println("ENTRE: " + c.getReplacementText());
    JTextComponent textComp = getTextComponent();
    String alreadyEntered = c.getAlreadyEntered(textComp);
    hidePopupWindow();
    Caret caret = textComp.getCaret();

    int dot = caret.getDot();
    int len = alreadyEntered.length();
    int start = dot-len;

    Object itemObj = ((MatchableShorthandCompletion) c).getItem().getObject();
    String token = null;
    if (itemObj instanceof Project) {
      try {
        start = textComp.getDocument().getText(0, dot).lastIndexOf("$") + 1;
      }   catch (Exception exc) {
        exc.printStackTrace();
      }
    }
    if (itemObj instanceof Task) {
      try {
        start = textComp.getDocument().getText(0, dot).lastIndexOf("#") + 1;
      }   catch (Exception exc) {
        exc.printStackTrace();
      }
    }


    String replacement = getReplacementText(c, textComp.getDocument(), start, len);

    caret.setDot(start);
    caret.moveDot(dot);
    textComp.replaceSelection(replacement);

    if (isParameterAssistanceEnabled() &&
            (c instanceof ParameterizedCompletion)) {
      ParameterizedCompletion pc = (ParameterizedCompletion)c;
View Full Code Here

    return null; // In a token type we can't auto-complete from.
  }

  private int getClosestTokenType(RSyntaxDocument doc, JTextComponent comp) {
    Caret caret = comp.getCaret();
    int dot = caret.getDot();

    String textUntilCaret = "";
    try {
      textUntilCaret = doc.getText(0, dot);
    } catch (BadLocationException e) {
View Full Code Here

        break;
      default:
        return token;
    }

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

    String textUntilCaret = "";
    try {
      textUntilCaret = doc.getText(0, dot);
    } catch (BadLocationException e) {
View Full Code Here

TOP

Related Classes of javax.swing.text.Caret

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.