View Javadoc

1   package nl.tudelft.goal.ut2004.visualizer.gui.action;
2   
3   import java.awt.event.ActionEvent;
4   import java.awt.event.ActionListener;
5   
6   import javax.swing.JSpinner;
7   import javax.swing.event.ChangeEvent;
8   import javax.swing.event.ChangeListener;
9   
10  import nl.tudelft.goal.ut2004.visualizer.controller.ServerController;
11  
12  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.SetGameSpeed;
13  import cz.cuni.amis.pogamut.ut2004.server.IUT2004Server;
14  
15  public class SetSpeedAction implements ActionListener, ChangeListener {
16  
17  	private final JSpinner speedSelection;
18  
19  	public SetSpeedAction(JSpinner speedSelection) {
20  		this.speedSelection = speedSelection;
21  	}
22  
23  	@Override
24  	public void actionPerformed(ActionEvent e) {
25  		setSpeed();
26  	}
27  
28  	@Override
29  	public void stateChanged(ChangeEvent arg0) {
30  		setSpeed();
31  	}
32  
33  	private void setSpeed() {
34  		ServerController controller = ServerController.getInstance();
35  		IUT2004Server server = controller.getServer();
36  		assert server != null;
37  
38  		double speed = (Double) speedSelection.getValue();
39  
40  		// Limiting speed to 10. Documentation of indicates
41  		// SetGameSpeed indicates this is reasonable.
42  		speed = Math.max(0.1, Math.min(10, speed));
43  
44  		SetGameSpeed setSpeed = new SetGameSpeed(speed);
45  		server.getAct().act(setSpeed);
46  	}
47  
48  }