View Javadoc

1   package nl.tudelft.goal.ut2004.visualizer.gui.action;
2   
3   import java.awt.event.ActionEvent;
4   import javax.swing.AbstractAction;
5   import javax.swing.Action;
6   
7   import nl.tudelft.goal.ut2004.visualizer.controller.ServerController;
8   import nl.tudelft.goal.ut2004.visualizer.gui.widgets.MapBox;
9   import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.ChangeMap;
10  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.MapList;
11  import cz.cuni.amis.pogamut.ut2004.server.IUT2004Server;
12  import cz.cuni.amis.utils.flag.FlagListener;
13  
14  public class ChangeMapAction extends AbstractAction {
15  
16  	private final MapBox mapSelection;
17  
18  	public ChangeMapAction(MapBox box) {
19  		this.mapSelection = box;
20  
21  		putValue(Action.SHORT_DESCRIPTION, "Change the current map");
22  		putValue(Action.NAME, "Change map");
23  		ServerController controller = ServerController.getInstance();
24  		controller.getServerDefinition().getServerFlag()
25  				.addListener(new FlagListener<IUT2004Server>() {
26  
27  					@Override
28  					public void flagChanged(IUT2004Server changedValue) {
29  						if (changedValue == null) {
30  							setEnabled(false);
31  						} else {
32  							setEnabled(true);
33  
34  						}
35  					}
36  				});
37  
38  		IUT2004Server server = controller.getServer();
39  		if (server == null) {
40  			setEnabled(false);
41  			return;
42  		}
43  	}
44  
45  	@Override
46  	public void actionPerformed(ActionEvent arg0) {
47  
48  		ServerController controller = ServerController.getInstance();
49  		IUT2004Server server = controller.getServer();
50  		assert server != null;
51  
52  		// Sending command directly rather then using the provided method.
53  		// provided method restarts server which conflicts with the restarting
54  		// server definition.
55  		// server.setGameMap((String) mapSelection.getSelecteditem());
56  		MapList selected = mapSelection.getSelected();
57  		if (selected != null)
58  			server.getAct().act(new ChangeMap().setMapName(selected.getName()));
59  	}
60  
61  }