import javax.swing.AbstractAction;
import javax.swing.Action;
+import net.sf.openrocket.document.events.DocumentChangeEvent;
+import net.sf.openrocket.document.events.DocumentChangeListener;
+import net.sf.openrocket.document.events.SimulationChangeEvent;
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;
public static final int UNDO_MARGIN = 10;
+ public static final String SIMULATION_NAME_PREFIX = "Simulation ";
+
private final Rocket rocket;
private final Configuration configuration;
private LinkedList<String> undoDescription = new LinkedList<String>();
private String nextDescription = null;
+ private String storedDescription = null;
private File file = null;
private final StorageOptions storageOptions = new StorageOptions();
+ private final List<DocumentChangeListener> listeners =
+ new ArrayList<DocumentChangeListener>();
+
/* These must be initialized after undo history is set up. */
private final UndoRedoAction undoAction;
private final UndoRedoAction redoAction;
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);
rocket.addComponentChangeListener(this);
-
-
}
}
public void addSimulation(Simulation simulation) {
simulations.add(simulation);
+ fireDocumentChangeEvent(new SimulationChangeEvent(simulation));
}
public void addSimulation(Simulation simulation, int n) {
simulations.add(n, simulation);
+ fireDocumentChangeEvent(new SimulationChangeEvent(simulation));
}
public void removeSimulation(Simulation simulation) {
simulations.remove(simulation);
+ fireDocumentChangeEvent(new SimulationChangeEvent(simulation));
}
public Simulation removeSimulation(int n) {
- return simulations.remove(n);
+ Simulation simulation = simulations.remove(n);
+ fireDocumentChangeEvent(new SimulationChangeEvent(simulation));
+ return simulation;
}
+ /**
+ * Return a unique name suitable for the next simulation. The name begins
+ * with {@link #SIMULATION_NAME_PREFIX} and has a unique number larger than any
+ * previous simulation.
+ *
+ * @return the new name.
+ */
+ public String getNextSimulationName() {
+ // Generate unique name for the simulation
+ int maxValue = 0;
+ for (Simulation s: simulations) {
+ String name = s.getName();
+ if (name.startsWith(SIMULATION_NAME_PREFIX)) {
+ try {
+ maxValue = Math.max(maxValue,
+ Integer.parseInt(name.substring(SIMULATION_NAME_PREFIX.length())));
+ } catch (NumberFormatException ignore) { }
+ }
+ }
+ return SIMULATION_NAME_PREFIX + (maxValue+1);
+ }
/**
}
+ /**
+ * 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;
}
}
+ /**
+ * 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) {
+ /////// Listeners
+
+ public void addDocumentChangeListener(DocumentChangeListener listener) {
+ listeners.add(listener);
+ }
+
+ public void removeDocumentChangeListener(DocumentChangeListener listener) {
+ listeners.remove(listener);
+ }
+
+ protected void fireDocumentChangeEvent(DocumentChangeEvent event) {
+ DocumentChangeListener[] array = listeners.toArray(new DocumentChangeListener[0]);
+ for (DocumentChangeListener l: array) {
+ l.documentChanged(event);
+ }
+ }
+
+
/**
break;
default:
- throw new RuntimeException("EEEK!");
+ throw new BugException("illegal type="+type);
}
if (desc != null)