Updates for 0.9.5
[debian/openrocket] / src / net / sf / openrocket / document / OpenRocketDocument.java
index 24c144cdfb755ad4ec791541689eecfa2ec34cdb..83c4b0b7ebeba0c15f5860bafde4c75a161e07a8 100644 (file)
@@ -17,6 +17,7 @@ import net.sf.openrocket.rocketcomponent.ComponentChangeEvent;
 import net.sf.openrocket.rocketcomponent.ComponentChangeListener;
 import net.sf.openrocket.rocketcomponent.Configuration;
 import net.sf.openrocket.rocketcomponent.Rocket;
+import net.sf.openrocket.util.BugException;
 import net.sf.openrocket.util.Icons;
 
 
@@ -45,6 +46,7 @@ public class OpenRocketDocument implements ComponentChangeListener {
        private LinkedList<String> undoDescription = new LinkedList<String>();
        
        private String nextDescription = null;
+       private String storedDescription = null;
        
        
        private File file = null;
@@ -70,9 +72,7 @@ public class OpenRocketDocument implements ComponentChangeListener {
                this.configuration = configuration;
                this.rocket = configuration.getRocket();
                
-               undoHistory.add(rocket.copy());
-               undoDescription.add(null);
-               undoPosition = 0;
+               clearUndo();
                
                undoAction = new UndoRedoAction(UndoRedoAction.UNDO);
                redoAction = new UndoRedoAction(UndoRedoAction.REDO);
@@ -234,6 +234,31 @@ public class OpenRocketDocument implements ComponentChangeListener {
        }
 
        
+       /**
+        * Start a time-limited undoable operation.  After the operation {@link #stopUndo()}
+        * must be called, which will restore the previous undo description into effect.
+        * Only one level of start-stop undo descriptions is supported, i.e. start-stop
+        * undo cannot be nested, and no other undo operations may be called between
+        * the start and stop calls.
+        * 
+        * @param description   Description of the following undoable operations.
+        */
+       public void startUndo(String description) {
+               storedDescription = nextDescription;
+               addUndoPosition(description);
+       }
+       
+       /**
+        * End the previous time-limited undoable operation.  This must be called after
+        * {@link #startUndo(String)} has been called before any other undo operations are
+        * performed.
+        */
+       public void stopUndo() {
+               addUndoPosition(storedDescription);
+               storedDescription = null;
+       }
+       
+       
        public Action getUndoAction() {
                return undoAction;
        }
@@ -244,6 +269,24 @@ public class OpenRocketDocument implements ComponentChangeListener {
        }
        
        
+       /**
+        * Clear the undo history.
+        */
+       public void clearUndo() {
+               undoHistory.clear();
+               undoDescription.clear();
+               
+               undoHistory.add(rocket.copy());
+               undoDescription.add(null);
+               undoPosition = 0;
+               
+               if (undoAction != null)
+                       undoAction.setAllValues();
+               if (redoAction != null)
+                       redoAction.setAllValues();
+       }
+       
+       
        @Override
        public void componentChanged(ComponentChangeEvent e) {
                
@@ -407,7 +450,7 @@ public class OpenRocketDocument implements ComponentChangeListener {
                                break;
                                
                        default:
-                               throw new RuntimeException("EEEK!");
+                               throw new BugException("illegal type="+type);
                        }
                        
                        if (desc != null)