timers: try to be more disciplined about starting/stopping the timer

git-svn-id: https://micropolis.googlecode.com/svn/trunk/micropolis-java@724 d9718cc8-9f43-0410-858b-315f434eb58c
This commit is contained in:
jason@long.name 2013-07-05 12:49:40 +00:00
parent 5270d072b8
commit 94dc4ea7be

View file

@ -305,9 +305,12 @@ public class MainWindow extends JFrame
{ {
if (needsSaved()) if (needsSaved())
{ {
try { boolean timerEnabled = isTimerActive();
stopTimer(); if (timerEnabled) {
stopTimer();
}
try {
int rv = JOptionPane.showConfirmDialog( int rv = JOptionPane.showConfirmDialog(
this, this,
strings.getString("main.save_query"), strings.getString("main.save_query"),
@ -325,7 +328,7 @@ public class MainWindow extends JFrame
} }
} }
finally { finally {
startTimer(); if (timerEnabled) { startTimer(); }
} }
} }
return true; return true;
@ -750,7 +753,10 @@ public class MainWindow extends JFrame
static final String EXTENSION = "cty"; static final String EXTENSION = "cty";
private boolean onSaveCityAsClicked() private boolean onSaveCityAsClicked()
{ {
stopTimer(); boolean timerEnabled = isTimerActive();
if (timerEnabled) {
stopTimer();
}
try try
{ {
JFileChooser fc = new JFileChooser(); JFileChooser fc = new JFileChooser();
@ -775,7 +781,7 @@ public class MainWindow extends JFrame
} }
finally finally
{ {
startTimer(); if (timerEnabled) { startTimer(); }
} }
return false; return false;
} }
@ -787,16 +793,20 @@ public class MainWindow extends JFrame
return; return;
} }
boolean timerEnabled = isTimerActive();
if (timerEnabled) {
stopTimer();
}
try try
{ {
JFileChooser fc = new JFileChooser(); JFileChooser fc = new JFileChooser();
FileNameExtensionFilter filter1 = new FileNameExtensionFilter(strings.getString("cty_file"), EXTENSION); FileNameExtensionFilter filter1 = new FileNameExtensionFilter(strings.getString("cty_file"), EXTENSION);
fc.setFileFilter(filter1); fc.setFileFilter(filter1);
stopTimer(); assert !isTimerActive();
int rv = fc.showOpenDialog(this);
startTimer();
int rv = fc.showOpenDialog(this);
if (rv == JFileChooser.APPROVE_OPTION) { if (rv == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile(); File file = fc.getSelectedFile();
Micropolis newEngine = new Micropolis(); Micropolis newEngine = new Micropolis();
@ -812,6 +822,11 @@ public class MainWindow extends JFrame
JOptionPane.showMessageDialog(this, e, strings.getString("main.error_caption"), JOptionPane.showMessageDialog(this, e, strings.getString("main.error_caption"),
JOptionPane.ERROR_MESSAGE); JOptionPane.ERROR_MESSAGE);
} }
finally {
if (timerEnabled) {
startTimer();
}
}
} }
private JToggleButton makeToolBtn(final MicropolisTool tool) private JToggleButton makeToolBtn(final MicropolisTool tool)
@ -945,9 +960,16 @@ public class MainWindow extends JFrame
public void doNewCity(boolean firstTime) public void doNewCity(boolean firstTime)
{ {
stopTimer(); boolean timerEnabled = isTimerActive();
if (timerEnabled) {
stopTimer();
}
new NewCityDialog(this, !firstTime).setVisible(true); new NewCityDialog(this, !firstTime).setVisible(true);
startTimer();
if (timerEnabled) {
startTimer();
}
} }
void doQueryTool(int xpos, int ypos) void doQueryTool(int xpos, int ypos)
@ -1169,7 +1191,6 @@ public class MainWindow extends JFrame
engine.animate(); engine.animate();
if (!engine.autoBudget && engine.isBudgetTime()) if (!engine.autoBudget && engine.isBudgetTime())
{ {
stopTimer(); //redundant
showAutoBudget(); showAutoBudget();
return; return;
} }
@ -1256,7 +1277,10 @@ public class MainWindow extends JFrame
//implements EarthquakeListener //implements EarthquakeListener
public void earthquakeStarted() public void earthquakeStarted()
{ {
stopTimer(); if (isTimerActive()) {
stopTimer();
}
currentEarthquake = new EarthquakeStepper(); currentEarthquake = new EarthquakeStepper();
currentEarthquake.oneStep(); currentEarthquake.oneStep();
startTimer(); startTimer();
@ -1270,6 +1294,8 @@ public class MainWindow extends JFrame
private void stopTimer() private void stopTimer()
{ {
assert isTimerActive();
if (simTimer != null) { if (simTimer != null) {
simTimer.stop(); simTimer.stop();
simTimer = null; simTimer = null;
@ -1287,7 +1313,9 @@ public class MainWindow extends JFrame
private void onWindowClosed(WindowEvent ev) private void onWindowClosed(WindowEvent ev)
{ {
stopTimer(); if (isTimerActive()) {
stopTimer();
}
} }
private void onDifficultyClicked(int newDifficulty) private void onDifficultyClicked(int newDifficulty)
@ -1297,7 +1325,10 @@ public class MainWindow extends JFrame
private void onPriorityClicked(Speed newSpeed) private void onPriorityClicked(Speed newSpeed)
{ {
stopTimer(); if (isTimerActive()) {
stopTimer();
}
getEngine().setSpeed(newSpeed); getEngine().setSpeed(newSpeed);
startTimer(); startTimer();
} }
@ -1436,11 +1467,18 @@ public class MainWindow extends JFrame
private void showBudgetWindow(boolean isEndOfYear) private void showBudgetWindow(boolean isEndOfYear)
{ {
stopTimer(); boolean timerEnabled = isTimerActive();
if (timerEnabled) {
stopTimer();
}
BudgetDialog dlg = new BudgetDialog(this, getEngine()); BudgetDialog dlg = new BudgetDialog(this, getEngine());
dlg.setModal(true); dlg.setModal(true);
dlg.setVisible(true); dlg.setVisible(true);
startTimer();
if (timerEnabled) {
startTimer();
}
} }
private JMenuItem makeMapStateMenuItem(String caption, final MapState state) private JMenuItem makeMapStateMenuItem(String caption, final MapState state)