/** 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);
  }
Exemple #3
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;
 }
  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;
  }
  /**
   * 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();
  }
  /**
   * 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;
  }
  /*
   **  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;
  }
 @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;
 }