Exemple #1
0
  public void saveCaretInfo() {
    if (!buffer.isLoaded()) return;

    buffer.setIntegerProperty(Buffer.CARET, textArea.getCaretPosition());

    CaretInfo caretInfo = caretsForPath.get(buffer.getPath());
    if (caretInfo == null) {
      caretInfo = new CaretInfo();
      caretsForPath.put(buffer.getPath(), caretInfo);
    }
    caretInfo.caret = textArea.getCaretPosition();

    Selection[] selection = textArea.getSelection();
    for (int i = 0; i < selection.length; i++) selection[i] = (Selection) selection[i].clone();
    buffer.setProperty(Buffer.SELECTION, selection);
    caretInfo.selection = selection;

    caretInfo.rectangularSelection = textArea.isRectangularSelectionEnabled();
    caretInfo.multipleSelection = textArea.isMultipleSelectionEnabled();

    buffer.setIntegerProperty(Buffer.SCROLL_VERT, textArea.getFirstPhysicalLine());
    caretInfo.scrollVert = textArea.getFirstPhysicalLine();
    buffer.setIntegerProperty(Buffer.SCROLL_HORIZ, textArea.getHorizontalOffset());
    caretInfo.scrollHoriz = textArea.getHorizontalOffset();
    if (!buffer.isUntitled()) {
      BufferHistory.setEntry(
          buffer.getPath(),
          textArea.getCaretPosition(),
          (Selection[]) buffer.getProperty(Buffer.SELECTION),
          buffer.getStringProperty(JEditBuffer.ENCODING),
          buffer.getMode().getName());
    }
  }
 /**
  * Applies given caret/selection state to the editor. Editor text must have been set up
  * previously.
  */
 public static void setCaretsAndSelection(Editor editor, CaretAndSelectionState caretsState) {
   CaretModel caretModel = editor.getCaretModel();
   if (caretModel.supportsMultipleCarets()) {
     List<CaretState> states = new ArrayList<CaretState>(caretsState.carets.size());
     for (CaretInfo caret : caretsState.carets) {
       states.add(
           new CaretState(
               caret.position == null
                   ? null
                   : editor.offsetToLogicalPosition(caret.getCaretOffset(editor.getDocument())),
               caret.selection == null
                   ? null
                   : editor.offsetToLogicalPosition(caret.selection.getStartOffset()),
               caret.selection == null
                   ? null
                   : editor.offsetToLogicalPosition(caret.selection.getEndOffset())));
     }
     caretModel.setCaretsAndSelections(states);
   } else {
     assertEquals("Multiple carets are not supported by the model", 1, caretsState.carets.size());
     CaretInfo caret = caretsState.carets.get(0);
     if (caret.position != null) {
       caretModel.moveToOffset(caret.getCaretOffset(editor.getDocument()));
     }
     if (caret.selection != null) {
       editor
           .getSelectionModel()
           .setSelection(caret.selection.getStartOffset(), caret.selection.getEndOffset());
     } else {
       editor.getSelectionModel().removeSelection();
     }
   }
   if (caretsState.blockSelection != null) {
     editor
         .getSelectionModel()
         .setBlockSelection(
             editor.offsetToLogicalPosition(caretsState.blockSelection.getStartOffset()),
             editor.offsetToLogicalPosition(caretsState.blockSelection.getEndOffset()));
   }
 }