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
16
17
18
19
20
21
22 public class ShowServerEnvironmentDependentDialogueAction extends
23 ShowDialogueAction {
24
25
26
27
28
29
30
31
32
33
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 }