/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
/**
 * @author Evgeniya G. Maenkova
 * @version $Revision$
 */
package javax.swing.undo;

import java.util.ListIterator;
import java.util.NoSuchElementException;
import java.util.Vector;

import org.apache.harmony.x.swing.Utilities;

public class CompoundEdit extends AbstractUndoableEdit {

    protected Vector<UndoableEdit> edits = new Vector<UndoableEdit>();

    /**
     * This is flag that indicates edits are still being added to it.
     * The method end() sets inProgress to false.
     */
    boolean inProgress = true;

    public boolean addEdit(final UndoableEdit anEdit) {
       if (!inProgress) {
           return false;
       }

       UndoableEdit last = lastEdit();

       if (last != null && (last.addEdit(anEdit) || anEdit.replaceEdit(last))) {
            return true;
       }

       return edits.add(anEdit);
    }

    protected UndoableEdit lastEdit() {
        UndoableEdit last = null;
        try {
            last = edits.lastElement();
        } catch (final NoSuchElementException e) {
        }

        return last;
    }

    /*
     * The format of the string is based on 1.5 release behavior
     * which can be revealed using the following code:
     *
     *  CompoundEdit obj = new CompoundEdit();
     *  obj.addEdit(new CompoundEdit());
     *  System.out.println(obj.toString());
     */
    public String toString() {
        String str = super.toString();
        str += " inProgress: " + inProgress;
        str += " edits: [";
        for (ListIterator li = edits.listIterator(); li.hasNext();) {
            str += li.next().toString() + ((li.hasNext()) ? "," : "");
        }
        str += "]";
        return str;
    }

    public String getUndoPresentationName() {
        UndoableEdit last = lastEdit();

        if (last != null) {
            String undoName = last.getUndoPresentationName();
            if (undoName.length() != 0) {
                return undoName;
            }
        }

        return super.getUndoPresentationName();
    }

    public String getRedoPresentationName() {
        UndoableEdit last = lastEdit();

        if (last != null) {
            String redoName = last.getRedoPresentationName();
            if (redoName.length() != 0) {
                return redoName;
            }
        }

        return super.getRedoPresentationName();
    }

    public String getPresentationName() {
        UndoableEdit last = lastEdit();

        if (last != null) {
            String name = last.getPresentationName();
            if (!Utilities.isEmptyString(name)) {
                return name;
            }
        }

        return super.getPresentationName();
    }

    public boolean isSignificant() {
        for (ListIterator li = edits.listIterator(); li.hasNext();) {
            if (((UndoableEdit)li.next()).isSignificant()) {
                return true;
            }
        }
        return false;
    }

    public boolean isInProgress() {
        return inProgress;
    }

    public boolean canUndo() {
        return !isInProgress() && super.canUndo();
    }

    public boolean canRedo() {
        return !isInProgress() && super.canRedo();
    }

    public void undo() {
        super.undo();
        for (ListIterator li = edits.listIterator(edits.size());
            li.hasPrevious();) {
            ((UndoableEdit)li.previous()).undo();
        }
    }

    public void redo() {
        super.redo();
        for (ListIterator li = edits.listIterator(); li.hasNext();) {
            ((UndoableEdit)li.next()).redo();
        }
    }

    public void end() {
        inProgress = false;
    }

    public void die() {
        super.die();
        for (ListIterator li = edits.listIterator(edits.size());
           li.hasPrevious();) {
            ((UndoableEdit)li.previous()).die();
        }
    }

}

@@ -1098,16 +1098,218 @@
 Edit %7B%0A%0A
+    /**%0A     * This field is added as performance optimization but not as%0A     * a guarantee of correct deserialization.%0A     */%0A    private static final long serialVersionUID = -6512679249930119683L;%0A%0A
     prot
@@ -1367,24 +1367,24 @@
 bleEdit%3E();%0A
-
 %0A    /**%0A   
@@ -1540,16 +1540,30 @@
  true;%0A%0A
+    @Override%0A
     publ
@@ -2355,24 +2355,38 @@
 ));%0A     */%0A
+    @Override%0A
     public S
@@ -2706,32 +2706,46 @@
 urn str;%0A    %7D%0A%0A
+    @Override%0A
     public Strin
@@ -3052,32 +3052,46 @@
 nName();%0A    %7D%0A%0A
+    @Override%0A
     public Strin
@@ -3398,32 +3398,46 @@
 nName();%0A    %7D%0A%0A
+    @Override%0A
     public Strin
@@ -3732,32 +3732,46 @@
 nName();%0A    %7D%0A%0A
+    @Override%0A
     public boole
@@ -4066,32 +4066,46 @@
 rogress;%0A    %7D%0A%0A
+    @Override%0A
     public boole
@@ -4169,32 +4169,46 @@
 nUndo();%0A    %7D%0A%0A
+    @Override%0A
     public boole
@@ -4272,32 +4272,46 @@
 nRedo();%0A    %7D%0A%0A
+    @Override%0A
     public void 
@@ -4498,32 +4498,46 @@
        %7D%0A    %7D%0A%0A
+    @Override%0A
     public void 
@@ -4751,32 +4751,46 @@
 = false;%0A    %7D%0A%0A
+    @Override%0A
     public void 

Patch for HARMONY- 3164 "[classlib][swing] j.s.undo.CompoundEdit has compiler warnings"