View Javadoc

1   package nl.tudelft.goal.ut2004.visualizer.gui.action;
2   
3   import java.awt.Component;
4   import java.awt.Graphics;
5   import java.awt.event.ActionEvent;
6   import java.util.Collection;
7   
8   import javax.swing.AbstractAction;
9   import javax.swing.Action;
10  import javax.swing.Icon;
11  import javax.swing.JDialog;
12  
13  import nl.tudelft.goal.ut2004.visualizer.connection.EnvironmentService;
14  import nl.tudelft.goal.ut2004.visualizer.controller.ServerController;
15  import nl.tudelft.goal.ut2004.visualizer.gui.dialogs.ChangeGameSpeedDialog;
16  import nl.tudelft.goal.ut2004.visualizer.util.CollectionEventAdaptor;
17  
18  import cz.cuni.amis.pogamut.ut2004.server.IUT2004Server;
19  import cz.cuni.amis.utils.flag.FlagListener;
20  
21  /**
22   * Action that is active or inactive depending on the state of the connection to
23   * the server. When the action is active it will launch a dialoque. This
24   * dialoque will be hidden when the connection to the server is lost.
25   * 
26   * @author mpkorstanje
27   * 
28   */
29  public class ShowServerEnvironmentDependentDialogueAction extends
30  		ShowDialogueAction {
31  
32  	/**
33  	 * Constructs the action.
34  	 * 
35  	 * @param dialog
36  	 *            to show when the action is used.
37  	 * @param name
38  	 *            this action.
39  	 * @param description
40  	 *            to show when a user hovers on this action.
41  	 */
42  	public ShowServerEnvironmentDependentDialogueAction(final JDialog dialog,
43  			String name, String description) {
44  		super(dialog, name, description);
45  
46  		ServerController controller = ServerController.getInstance();
47  		controller.getServerDefinition().getServerFlag()
48  				.addListener(new ServerListener());
49  		controller.getEnvironmentData().getEnvironments()
50  				.addCollectionListener(new CollectionListener());
51  
52  		setEnabled(controller.getServer() != null);
53  	}
54  
55  	private boolean environmentsAvailable = false;
56  	private boolean serverAvailable = false;
57  
58  	private void checkStatus() {
59  		if (environmentsAvailable && serverAvailable) {
60  			setEnabled(true);
61  		} else {
62  			setEnabled(false);
63  			dialog.setVisible(false);
64  		}
65  	}
66  
67  	private class CollectionListener extends
68  			CollectionEventAdaptor<EnvironmentService> {
69  		@Override
70  		public void postAddEvent(Collection<EnvironmentService> alreadyAdded,
71  				Collection<EnvironmentService> whereWereAdded) {
72  			environmentsAvailable = !whereWereAdded.isEmpty();
73  			checkStatus() ;
74  		}
75  
76  		@Override
77  		public void postRemoveEvent(
78  				Collection<EnvironmentService> alreadyRemoved,
79  				Collection<EnvironmentService> whereWereRemoved) {
80  			environmentsAvailable = !whereWereRemoved.isEmpty();
81  			checkStatus() ;
82  		}
83  	}
84  
85  	private class ServerListener implements FlagListener<IUT2004Server> {
86  		@Override
87  		public void flagChanged(IUT2004Server changedValue) {
88  			serverAvailable = changedValue != null;
89  			checkStatus() ;
90  		}
91  	}
92  }