Example #1
0
 // This is a workaround for a java bug causing the caret to jump to 0
 // on focus gain since the value is recalculated. bug #4740914 (rejected)
 protected void processFocusEvent(FocusEvent e) {
   if (e.getID() == FocusEvent.FOCUS_GAINED) {
     Caret c = getCaret();
     int cd = getCaret().getDot();
     int cm = getCaret().getMark();
     super.processFocusEvent(e);
     // Assumes this won't go out of bounds (e.g. text didn't change).
     // This is normally a safe assumption, since it seems like the value can't change.
     c.setDot(cm);
     c.moveDot(cd);
   } else super.processFocusEvent(e);
 }
  /**
   * Perform the goto operation.
   *
   * @return whether the dialog should be made invisible or not
   */
  protected boolean performGoto() {
    JTextComponent c = EditorRegistry.lastFocusedComponent();
    if (c != null) {
      try {
        int line = Integer.parseInt(getGotoValueText());

        // issue 188976
        if (line == 0) line = 1;
        // end of issue 188976

        BaseDocument doc = Utilities.getDocument(c);
        if (doc != null) {
          int rowCount = Utilities.getRowCount(doc);
          if (line > rowCount) line = rowCount;

          // Obtain the offset where to jump
          int pos = Utilities.getRowStartFromLineOffset(doc, line - 1);

          BaseKit kit = Utilities.getKit(c);
          if (kit != null) {
            Action a = kit.getActionByName(ExtKit.gotoAction);
            if (a instanceof ExtKit.GotoAction) {
              pos = ((ExtKit.GotoAction) a).getOffsetFromLine(doc, line - 1);
            }
          }

          if (pos != -1) {
            Caret caret = c.getCaret();
            caret.setDot(pos);
          } else {
            c.getToolkit().beep();
            return false;
          }
        }
      } catch (NumberFormatException e) {
        c.getToolkit().beep();
        return false;
      }
    }
    return true;
  }
Example #3
0
 public void setCaretPosition(int position) {
   Caret c = textView.getCaret();
   // move the caret
   c.setDot(position);
 }
Example #4
0
    @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());
      }
    }
 /**
  * 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;
 }
 @Override
 public void actionPerformed(ActionEvent e, JTextComponent target) {
   Caret caret = target.getCaret();
   int prev = getPreviousWordPosition(target.getDocument(), caret.getDot());
   caret.setDot(prev);
 }