View Javadoc

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