/**
   * Tell the edits in the given range (inclusive) to die, and remove them from edits. from > to is
   * a no-op.
   */
  protected void trimEdits(int from, int to) {
    if (from <= to) {
      //          System.out.println("Trimming " + from + " " + to + " with index " +
      //                           indexOfNextAdd);
      for (int i = to; from <= i; i--) {
        UndoableEdit e = (UndoableEdit) edits.elementAt(i);
        //              System.out.println("JUM: Discarding " +
        //                                 e.getUndoPresentationName());
        e.die();
        // PENDING(rjrjr) when Vector supports range deletion (JDK
        // 1.2) , we can optimize the next line considerably.
        edits.removeElementAt(i);
      }

      if (indexOfNextAdd > to) {
        //              System.out.print("...right...");
        indexOfNextAdd -= to - from + 1;
      } else if (indexOfNextAdd >= from) {
        //              System.out.println("...mid...");
        indexOfNextAdd = from;
      }

      //          System.out.println("new index " + indexOfNextAdd);
    }
  }
 /**
  * Undo after some positions have been garbage collected.
  *
  * @throws BadLocationException
  */
 public void testUndoGC() throws BadLocationException {
   content.insertString(0, "012345678");
   Vector<WeakReference<Position>> pos = new Vector<WeakReference<Position>>(10);
   ReferenceQueue<Position> rq = new ReferenceQueue<Position>();
   for (int i = 0; i < content.length(); i += 2) {
     pos.add(new WeakReference<Position>(content.createPosition(i), rq));
   }
   int count = 0;
   int i;
   for (i = 0; i < 100; i++) {
     System.gc();
     Reference<?> r;
     if ((r = rq.poll()) != null) {
       pos.remove(r);
       count++;
       if (pos.size() == 0) {
         break;
       }
     }
   }
   // This call causes all the garbage collected positions to be
   // removed from the internal list
   UndoableEdit ue = content.remove(0, 5);
   assertEquals("5678", content.getString(0, content.length() - 1));
   // Test (it shouldn't fail with any NullPointerException)
   ue.undo();
   assertEquals("012345678", content.getString(0, content.length() - 1));
 }
 /** Redoes all changes from indexOfNextAdd to edit. Updates indexOfNextAdd accordingly. */
 protected void redoTo(UndoableEdit edit) throws CannotRedoException {
   boolean done = false;
   while (!done) {
     UndoableEdit next = (UndoableEdit) edits.elementAt(indexOfNextAdd++);
     next.redo();
     done = next == edit;
   }
 }
 public boolean addEdit(UndoableEdit anEdit) {
   final boolean absorbed = super.addEdit(anEdit);
   if (!absorbed && !isInProgress() && anEdit instanceof FormUndoableEdit) {
     final UndoableEdit ledit = lastEdit();
     if (ledit != null && ledit instanceof FormUndoableEdit) return (ledit.addEdit(anEdit));
   }
   return (absorbed);
 }
  /** Returns the the next significant edit to be undone if undo is called. May return null */
  protected UndoableEdit editToBeUndone() {
    int i = indexOfNextAdd;
    while (i > 0) {
      UndoableEdit edit = (UndoableEdit) edits.elementAt(--i);
      if (edit.isSignificant()) {
        return edit;
      }
    }

    return null;
  }
 /** Empty the undo manager, sending each edit a die message in the process. */
 public synchronized void discardAllEdits() {
   Enumeration cursor = edits.elements();
   while (cursor.hasMoreElements()) {
     UndoableEdit e = (UndoableEdit) cursor.nextElement();
     e.die();
   }
   edits = new Vector(limit);
   indexOfNextAdd = 0;
   // PENDING(rjrjr) when vector grows a removeRange() method
   // (expected in JDK 1.2), trimEdits() will be nice and
   // efficient, and this method can call that instead.
 }
Esempio n. 7
0
  private void localize() {

    LocaleManager localeManager = LocaleManager.getInstance();
    UndoManager undoManager = UndoManager.getInstance();

    UndoableEdit undoableEdit = undoManager.editToBeRedone();

    if (undoableEdit == null) {
      setText(localeManager.getValue(CAPTION_LOCALIZE_KEY));
    } else {
      setText(undoableEdit.getRedoPresentationName());
    }
  }
  /** Returns the the next significant edit to be redone if redo is called. May return null */
  protected UndoableEdit editToBeRedone() {
    int count = edits.size();
    int i = indexOfNextAdd;

    while (i < count) {
      UndoableEdit edit = (UndoableEdit) edits.elementAt(i++);
      if (edit.isSignificant()) {
        return edit;
      }
    }

    return null;
  }
  /**
   * Overridden to preserve usual semantics: returns true if an undo operation would be successful
   * now, false otherwise
   */
  public synchronized boolean canUndo() {

    //            System.out.println("giugiu affichage de edits : ");
    //            for (int i = 0; i < this.edits.size(); i++)
    //                System.out.println("\t" + this.edits.elementAt(i).hashCode());

    if (this.isInProgress()) {
      UndoableEdit edit = editToBeUndone();
      //            System.out.println("canUndo : " + (edit != null && edit.canUndo()));
      //            System.out.println("edit : " + edit.hashCode() + (edit != null ? edit.canUndo()
      // : "beuh"));// + " : " + edit.getClass());
      return edit != null && edit.canUndo();
    } else {
      //            System.out.println("undo aieee");
      return super.canUndo();
    }
  }
Esempio n. 10
0
  public void undoableEditHappened(UndoableEditEvent e) {
    UndoableEdit edit = e.getEdit();
    if (edit instanceof AbstractDocument.DefaultDocumentEvent) {
      AbstractDocument.DefaultDocumentEvent event = (AbstractDocument.DefaultDocumentEvent) edit;
      int start = event.getOffset();
      int len = event.getLength();
      Debug.log(9, "undoableEditHappened " + start + "," + len);
      boolean isNeedStart = false;
      if (event.getType().equals(DocumentEvent.EventType.CHANGE)
          || event.getType().equals(DocumentEvent.EventType.INSERT)) {
        try {
          String text = event.getDocument().getText(start, len);
          if (text.contains("\n")) isNeedStart = true;
        } catch (BadLocationException e1) {
          e1.printStackTrace();
        }
      }

      if (current == null) {
        isNeedStart = true;
      } else if (lastEditName == null || !lastEditName.equals(edit.getPresentationName())) {
        isNeedStart = true;
      }

      while (pointer < edits.size() - 1) {
        edits.remove(edits.size() - 1);
        isNeedStart = true;
      }
      if (isNeedStart) {
        createCompoundEdit();
      }

      current.addEdit(edit);
      lastEditName = edit.getPresentationName();

      refreshControls();
    }
  }
  /** add an undoableEdit to the manager */
  public synchronized boolean addEdit(UndoableEdit anEdit) {
    System.out.println("appel à addEdit : " + anEdit);
    //        System.out.println("addEdit .. " + (anEdit != null ? anEdit.getClass() : "") );
    //        System.out.println("\tisInProgress ? " + this.isInProgress());
    //        System.out.println("\tgroup mode   ? " + this.groupActivated);
    //
    //        System.out.println("affichage de edits : ");
    //        for (int i = 0; i < this.edits.size(); i++)
    //            System.out.println("\t" + this.edits.elementAt(i).getClass());

    if (!this.isInProgress()) return false;

    System.out.println("groupActivated : " + this.groupActivated);

    if (this.groupActivated) {
      if (!(this.editToBeUndone() instanceof UndoManager)) {
        System.out.println("\teditToBeUndone is NOT NOT an UndoManager");
        UndoManager newManager = new UndoManager();
        newManager.setLimit(this.limit); // attention, la limite peut poser de graves problemes
        // super.addEdit(newManager);
        this.edits.add(newManager);

        indexOfNextAdd = edits.size();
        System.out.println("\tindexOfNextAdd : " + indexOfNextAdd);
      }

      if (this.editToBeUndone() instanceof UndoManager) {
        System.out.println("\teditToBeUndone is an UndoManager");
        ((UndoManager) this.editToBeUndone()).addEdit(anEdit);
        //                System.out.println("\tindexOfNextAdd : " + indexOfNextAdd);

        //        System.out.println("gaga affichage de edits : ");
        //        for (int i = 0; i < this.edits.size(); i++)
        //            System.out.println("\t" + this.edits.elementAt(i).getClass());

        return true;
      } else {
        //                System.out.println("\teditToBeUndone is NOT an UndoManager");

        if (this.edits.size() > 0) {
          System.out.println("\ta");
          ((UndoManager) this.edits.elementAt(this.edits.size() - 1)).addEdit(anEdit);
        } else {
          System.out.println("\tb");
          this.edits.add(anEdit);
        }
        return true;
      }
      // throw new RuntimeException("gros probleme dans l'undoManager"); }
    } else {
      System.out.println("\tajout simple");
      boolean retVal;

      // Trim from the indexOfNextAdd to the end, as we'll
      // never reach these edits once the new one is added.
      trimEdits(indexOfNextAdd, edits.size() - 1);

      ///////////////
      if (!this.isInProgress()) {
        return false;
      } else {
        UndoableEdit last = lastEdit();

        // If this is the first subedit received, just add it.
        // Otherwise, give the last one a chance to absorb the new
        // one.  If it won't, give the new one a chance to absorb
        // the last one.

        System.out.println("\t\tlast : " + (last == null ? "null" : last.getClass()));
        if (last == null) {
          System.out.println("\t\taddElement");
          edits.addElement(anEdit);
        } else // if (last instanceof UndoManager)//!last.addEdit(anEdit) || )
        {
          System.out.println("\t\ti");
          if (anEdit.replaceEdit(last)) {
            edits.removeElementAt(edits.size() - 1);
            System.out.println("\t\tj");
          }
          System.out.println(
              "il ajoute quand meme ce connard !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
          edits.addElement(anEdit);
        }

        retVal = true;
      }
      ///////////////

      if (this.isInProgress()) {
        retVal = true;
      }

      // Maybe super added this edit, maybe it didn't (perhaps
      // an in progress compound edit took it instead. Or perhaps
      // this UndoManager is no longer in progress). So make sure
      // the indexOfNextAdd is pointed at the right place.
      indexOfNextAdd = edits.size();

      System.out.println("indexofNextAdd : " + indexOfNextAdd);

      // Enforce the limit
      trimForLimit();

      return retVal;
    }
  }