Exemplo n.º 1
0
  /**
   * Whenever an UndoableEdit happens the edit will either be absorbed by the current compound edit
   * or a new compound edit will be started
   */
  @Override
  public void undoableEditHappened(UndoableEditEvent e) {
    //  Start a new compound edit

    AbstractDocument.DefaultDocumentEvent docEvt = (DefaultDocumentEvent) e.getEdit();

    if (compoundEdit == null) {
      compoundEdit = startCompoundEdit(e.getEdit());
      startCombine = false;
      updateDirty();
      return;
    }

    int editLine = ((SyntaxDocument) docEvt.getDocument()).getLineNumberAt(docEvt.getOffset());

    //  Check for an incremental edit or backspace.
    //  The Change in Caret position and Document length should both be
    //  either 1 or -1.
    if ((startCombine || Math.abs(docEvt.getLength()) == 1) && editLine == lastLine) {
      compoundEdit.addEdit(e.getEdit());
      startCombine = false;
      updateDirty();
      return;
    }

    //  Not incremental edit, end previous edit and start a new one
    lastLine = editLine;

    compoundEdit.end();
    compoundEdit = startCompoundEdit(e.getEdit());

    updateDirty();
  }
 /** Efface le noeud DOM, et son parent s'il devient vide. */
 private void effacerNoeud(final boolean annulation, final CompoundEdit cedit1) {
   CompoundEdit cedit = null;
   if (!annulation) {
     final FormUndoableEdit fedit =
         new FormUndoableEdit(FormUndoableEdit.TypeEdition.SUPPRIMER, this);
     if (cedit1 != null) cedit1.addEdit(fedit);
     else {
       cedit = new CompoundEdit();
       cedit.addEdit(fedit);
     }
   }
   final Element elparent = (Element) affParent.getNoeud();
   if (attribut) {
     final String nom = cfg.nomAttribut(refNoeud);
     final String espace = cfg.espaceAttribut(refNoeud);
     elparent.removeAttributeNS(espace, nom);
   } else {
     final Node textNode = noeud.getNextSibling();
     elparent.removeChild(noeud);
     if (textNode.getNodeType() == Node.TEXT_NODE) elparent.removeChild(textNode);
   }
   noeud = null;
   if (!annulation) affParent.testEffacementParent(cedit1 != null ? cedit1 : cedit);
   else lireEnfants();
   doc.textPane.miseAJourArbre();
   if (cedit1 == null && cedit != null) {
     cedit.end();
     doc.textPane.addEdit(cedit);
   }
 }
  @Override
  public void process() {

    // put all the edits together
    CompoundEdit edit = new CompoundEdit();

    boolean removeAnnotation = removeMatched.isSelected();

    Pattern pattern =
        caseInsensitive.isSelected()
            ? Pattern.compile(patternField.getText(), Pattern.CASE_INSENSITIVE)
            : Pattern.compile(patternField.getText());

    BasicPatternMatcher<Note> matcher = new BasicPatternMatcher<Note>(Note.class, pattern);

    Annotation target = (Annotation) annotationComboBox.getSelectedItem();

    List<Annotation> createdAnnotations = new ArrayList<Annotation>();
    List<Annotation> removedAnnotations = new ArrayList<Annotation>();

    for (AnnotatedEntity entity : getSelectionController().getSelection().getEntities()) {

      for (Annotation annotation : entity.getAnnotations()) {

        String matched = annotation.accept(matcher);

        if (!matched.isEmpty()) {
          if (target instanceof StringAnnotation) {
            createdAnnotations.add(((StringAnnotation) target).getInstance(matched));
            if (removeAnnotation) {
              removedAnnotations.add(annotation);
            }
          } else if (target.getClass().equals(Charge.class)) {
            Charge charge = (Charge) target.newInstance();
            charge.setValue(Double.parseDouble(matched));
            createdAnnotations.add(charge);
            if (removeAnnotation) {
              removedAnnotations.add(annotation);
            }
          }
        }
      }

      edit.addEdit(new AddAnnotationEdit(entity, createdAnnotations));
      for (Annotation a : removedAnnotations) {
        edit.addEdit(new RemoveAnnotationEdit(entity, a));
        entity.removeAnnotation(a);
      }
      entity.addAnnotations(createdAnnotations);
      createdAnnotations.clear();
      removedAnnotations.clear();
    }

    // inform the compound edit that we've finished editing
    edit.end();

    addEdit(edit);
  }
Exemplo n.º 4
0
 @Override
 public UndoableEdit updateTransform(JEnvironment env) {
   JDocumentViewer viewer = getDocument().getViewer();
   JRequest req = viewer.getCurrentRequest();
   CompoundEdit cEdit = new CompoundEdit();
   if (req.isAltDown
       && req.getSelectionMode() == req.GROUP_MODE
       && addingTransform.getType() == AffineTransform.TYPE_TRANSLATION) {
     cEdit.addEdit(new JDuplicateObjectEdit(viewer, this));
   }
   cEdit.addEdit(new JTransformImageObjectEdit(viewer, this, addingTransform));
   addingTransform = null;
   cEdit.end();
   return cEdit;
 }
Exemplo n.º 5
0
  public synchronized boolean addEdit(UndoableEdit edit) {
    if (DEBUG) System.out.println("ChangeManager.addEdit( " + edit + ")");
    boolean result = false;
    validated = false;

    if (compoundEdit == null) {
      if (DEBUG) System.out.println("super.addEdit( " + edit + ")");

      if (edit instanceof UndoableDesignerEdit
          && !(editToBeUndone() instanceof UndoableDesignerEdit)) {
        super.discardAllEdits();
      } else if (!(edit instanceof UndoableDesignerEdit)
          && editToBeUndone() instanceof UndoableDesignerEdit) {
        super.discardAllEdits();
      }

      if (edit instanceof UndoablePluginEditAbstact
          && !(editToBeUndone() instanceof UndoablePluginEditAbstact)) {
        super.discardAllEdits();
      } else if (!(edit instanceof UndoablePluginEditAbstact)
          && editToBeUndone() instanceof UndoablePluginEditAbstact) {
        super.discardAllEdits();
      }

      result = super.addEdit(edit);

      fireStateChanged(new ChangeEvent(this));
    } else {
      result = compoundEdit.addEdit(edit);
    }

    return result;
  }
Exemplo n.º 6
0
  /**
   * Unabbreviate the journal name of the given entry.
   *
   * @param entry The entry to be treated.
   * @param fieldName The field name (e.g. "journal")
   * @param ce If the entry is changed, add an edit to this compound.
   * @return true if the entry was changed, false otherwise.
   */
  public boolean unabbreviate(
      BibDatabase database, BibEntry entry, String fieldName, CompoundEdit ce) {
    if (!entry.hasField(fieldName)) {
      return false;
    }
    String text = entry.getFieldOptional(fieldName).get();
    String origText = text;
    if (database != null) {
      text = database.resolveForStrings(text);
    }

    if (!journalAbbreviationRepository.isKnownName(text)) {
      return false; // cannot do anything if it is not known
    }

    if (!journalAbbreviationRepository.isAbbreviatedName(text)) {
      return false; // cannot unabbreviate unabbreviated name.
    }

    Abbreviation abbreviation =
        journalAbbreviationRepository.getAbbreviation(text).get(); // must be here
    String newText = abbreviation.getName();
    entry.setField(fieldName, newText);
    ce.addEdit(new UndoableFieldChange(entry, fieldName, origText, newText));
    return true;
  }
Exemplo n.º 7
0
  public void endCompound() {
    if (DEBUG) System.out.println("ChangeManager.endCompound()");

    compoundEdit.end();

    if ((editToBeUndone() instanceof UndoableDesignerEdit) && compoundEdit.isSignificant()) {
      super.discardAllEdits();
    } else if ((editToBeUndone() instanceof UndoablePluginEditAbstact)
        && compoundEdit.isSignificant()) {
      super.discardAllEdits();
    }

    super.addEdit(compoundEdit);

    compoundEdit = null;

    fireStateChanged(new ChangeEvent(this));
  }
Exemplo n.º 8
0
 /** Die Methode schliest den dummy Compound falls es ein "Space oder Enter" gedrueckt wird */
 public void keyTyped(KeyEvent e) {
   char keyChar = e.getKeyChar();
   if (keyChar == ' ' || keyChar == '\n') {
     // Falls Req Req Compound schliessen und an
     // Req Manager uebergeben
     if (ReqOROpt) {
       if (myReqCompound.isInProgress()) {
         myReqCompound.end();
         myReqField.addEdit(myReqCompound);
       }
       // sonst Opt Compound schliessen und an
       // Opt Manager uebergeben
     } else {
       if (myOptCompound.isInProgress()) {
         myOptCompound.end();
         myOptField.addEdit(myOptCompound);
       }
     }
   }
 }
Exemplo n.º 9
0
  @Override
  public void redo() throws CannotRedoException {
    restoreSelections();

    if (canRedo()) {
      super.redo();
    } else {
      die();
    }

    editor.requestFocusInWindow();
  }
Exemplo n.º 10
0
    @Override
    public void undo() throws CannotUndoException {
      //  End the edit so future edits don't get absorbed by this edit

      if (compoundEdit != null) {
        compoundEdit.end();
      }

      super.undo();

      //  Always start a new compound edit after an undo

      compoundEdit = null;
    }
 @SuppressWarnings("unchecked")
 private static CompoundEdit reset(
     final ComputedTrajectorySet cts, final BroomPromptModel broom, final boolean outPosition) {
   final RockSet<Pos> ipos = cts.getInitialPos();
   final RockSet<Vel> ivel = cts.getInitialVel();
   // store the initial state:
   final PosMemento[] pm = new PosMemento[RockSet.ROCKS_PER_SET];
   for (int i16 = RockSet.ROCKS_PER_SET - 1; i16 >= 0; i16--)
     pm[i16] = new PosMemento(ipos, i16, ipos.getRock(i16).p());
   final IndexMemento bi = new IndexMemento(broom, broom.getIdx16());
   final HandleMemento bh = new HandleMemento(broom, broom.getOutTurn());
   final XYMemento bxy = new XYMemento(broom, broom.getBroom());
   final SplitMemento bs = new SplitMemento(broom, broom.getSplitTimeMillis().getValue());
   final boolean preS = cts.getSuspended();
   cts.setSuspended(true);
   try {
     // reset:
     RockSet.allZero(ivel);
     broom.setIdx16(-1);
     if (outPosition) RockSetUtils.allOut(ipos);
     else RockSetUtils.allHome(ipos);
     broom.setIdx16(1);
     broom.setBroom(new Point2D.Double(0, 0));
     broom.getSplitTimeMillis().setValue(3300);
   } finally {
     cts.setSuspended(preS);
   }
   // create a compound edit
   final CompoundEdit ce = new CompoundEdit();
   ce.addEdit(new UndoableMemento(new SuspendMemento(cts, preS), new SuspendMemento(cts, true)));
   for (int i16 = RockSet.ROCKS_PER_SET - 1; i16 >= 0; i16--)
     ce.addEdit(new UndoableMemento(pm[i16], new PosMemento(ipos, i16, ipos.getRock(i16).p())));
   ce.addEdit(new UndoableMemento(bi, new IndexMemento(broom, broom.getIdx16())));
   ce.addEdit(new UndoableMemento(bh, new HandleMemento(broom, broom.getOutTurn())));
   ce.addEdit(new UndoableMemento(bxy, new XYMemento(broom, broom.getBroom())));
   ce.addEdit(
       new UndoableMemento(bs, new SplitMemento(broom, broom.getSplitTimeMillis().getValue())));
   ce.addEdit(new UndoableMemento(new SuspendMemento(cts, true), new SuspendMemento(cts, preS)));
   ce.end();
   return ce;
 }
Exemplo n.º 12
0
  /*
   **  Each CompoundEdit will store a group of related incremental edits
   **  (ie. each character typed or backspaced is an incremental edit)
   */
  private CompoundEdit startCompoundEdit(UndoableEdit anEdit) {
    //  Track Caret and Document information of this compound edit
    // AbstractDocument.DefaultDocumentEvent docEvt = (DefaultDocumentEvent) anEdit;

    //  The compound edit is used to store incremental edits

    compoundEdit = new MyCompoundEdit();
    compoundEdit.addEdit(anEdit);

    //  The compound edit is added to the UndoManager. All incremental
    //  edits stored in the compound edit will be undone/redone at once

    addEdit(compoundEdit);

    return compoundEdit;
  }
Exemplo n.º 13
0
  public void undo() throws CannotUndoException {
    restoreSelections();

    listener.finishCurrentEdit();

    if (canUndo()) {
      super.undo();
    } else {
      die();
    }

    if (adapter != null) {
      adapter.updateModel();
    }

    editor.requestFocusInWindow();
    editor.selectAll();
  }
Exemplo n.º 14
0
 public void redo() throws CannotUndoException {
   super.redo();
   isUnDone = false;
 }
Exemplo n.º 15
0
 public void undo() throws CannotUndoException {
   super.undo();
   isUnDone = true;
 }
Exemplo n.º 16
0
  private void handleMoveInstruction(
      String script,
      GosuRefactorUtil.MoveInstruction moveInstruction,
      int startClip,
      int endClip,
      boolean up) {
    // Do nothing if we are moving down beyond the last position in the script
    if (endClip == script.length() - 1 && !up) {
      return;
    }

    if (endClip < script.length()) {
      endClip = endClip + 1;
    }

    String movedCodeAsString = script.substring(startClip, endClip);
    if (!movedCodeAsString.endsWith("\n")) {
      movedCodeAsString += "\n";
    }
    int offset = moveInstruction.position;

    if (up) {
      offset = TextComponentUtil.getLineStart(script, offset);
    } else {
      offset = TextComponentUtil.getLineEnd(script, offset) + 1 - movedCodeAsString.length();
    }

    StringBuilder movedCode = new StringBuilder();

    int caretPosition = _gsEditor.getEditor().getCaretPosition();
    int selectionStart = _gsEditor.getEditor().getSelectionStart();
    int selectionEnd = _gsEditor.getEditor().getSelectionEnd();

    int currentPosition = startClip;

    if (moveInstruction.indent) {
      String[] strings = movedCodeAsString.split("\n");
      for (String str : strings) {

        if (caretPosition > currentPosition) {
          caretPosition += 2;
        }
        if (selectionStart > currentPosition) {
          selectionStart += 2;
        }
        if (selectionEnd > currentPosition) {
          selectionEnd += 2;
        }

        movedCode.append("  ");
        movedCode.append(str);
        movedCode.append("\n");
        currentPosition += str.length() + 1;
      }
    } else if (moveInstruction.outdent && movedCodeAsString.startsWith("  ")) {
      String[] strings = movedCodeAsString.split("\n");
      for (String str : strings) {
        if (str.startsWith("  ")) {
          if (caretPosition > currentPosition) {
            caretPosition -= 2;
          }
          if (selectionStart > currentPosition) {
            selectionStart -= 2;
          }
          if (selectionEnd > currentPosition) {
            selectionEnd -= 2;
          }
          str = str.substring(2, str.length());
        }

        movedCode.append(str);
        movedCode.append("\n");
        currentPosition += str.length() + 1;
      }
    } else {
      movedCode.append(movedCodeAsString);
    }

    CompoundEdit undoAtom = _gsEditor.getUndoManager().getUndoAtom();
    if (undoAtom != null && undoAtom.getPresentationName().equals("Script Change")) {
      _gsEditor.getUndoManager().endUndoAtom();
    }
    _gsEditor.getUndoManager().beginUndoAtom("moveLine");
    try {

      _gsEditor.getEditor().getDocument().remove(startClip, endClip - startClip);
      _gsEditor.getEditor().getDocument().insertString(offset, movedCode.toString(), null);

      _gsEditor.getEditor().setSelectionStart(selectionStart - (startClip - offset));
      _gsEditor.getEditor().setSelectionEnd(selectionEnd - (startClip - offset));
      _gsEditor.getEditor().getCaret().moveDot(caretPosition - (startClip - offset));

    } catch (BadLocationException e) {
      throw new RuntimeException(e);
    } finally {
      _gsEditor.getUndoManager().endUndoAtom();
    }
  }