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