2626import org .eclipse .che .ide .api .command .CommandRemovedEvent ;
2727import org .eclipse .che .ide .api .command .CommandUpdatedEvent ;
2828import org .eclipse .che .ide .api .component .WsAgentComponent ;
29+ import org .eclipse .che .ide .api .machine .events .AbstractWsAgentStateHandler ;
30+ import org .eclipse .che .ide .api .machine .events .WsAgentStateEvent ;
2931
3032import java .util .HashMap ;
3133import java .util .Map ;
@@ -52,7 +54,6 @@ public class ExecuteCommandActionManager implements WsAgentComponent {
5254 private final GoalPopUpGroupFactory goalPopUpGroupFactory ;
5355 private final ExecuteCommandActionFactory commandActionFactory ;
5456 private final CommandGoalRegistry goalRegistry ;
55- private final EventBus eventBus ;
5657
5758 /** Map of command's name to an appropriate {@link ExecuteCommandAction}. */
5859 private final Map <String , Action > commandActions ;
@@ -73,43 +74,69 @@ public ExecuteCommandActionManager(CommandManager commandManager,
7374 this .goalPopUpGroupFactory = goalPopUpGroupFactory ;
7475 this .commandActionFactory = commandActionFactory ;
7576 this .goalRegistry = goalRegistry ;
76- this .eventBus = eventBus ;
7777
7878 commandActions = new HashMap <>();
7979 goalPopUpGroups = new HashMap <>();
80- }
81-
82- @ Override
83- public void start (Callback <WsAgentComponent , Exception > callback ) {
84- callback .onSuccess (this );
8580
8681 eventBus .addHandler (CommandAddedEvent .getType (), e -> addAction (e .getCommand ()));
8782 eventBus .addHandler (CommandRemovedEvent .getType (), e -> removeAction (e .getCommand ()));
88- eventBus .addHandler (CommandsLoadedEvent .getType (), e -> {
89- commandManager .getCommands ().forEach (this ::removeAction );
90- commandManager .getCommands ().forEach (this ::addAction );
91- });
83+
9284 eventBus .addHandler (CommandUpdatedEvent .getType (), e -> {
9385 removeAction (e .getInitialCommand ());
9486 addAction (e .getUpdatedCommand ());
9587 });
9688
89+ eventBus .addHandler (WsAgentStateEvent .TYPE , new AbstractWsAgentStateHandler () {
90+ @ Override
91+ public void onWsAgentStopped (WsAgentStateEvent event ) {
92+ disposeActions ();
93+ }
94+ });
95+
96+ eventBus .addHandler (CommandsLoadedEvent .getType (), e -> {
97+ disposeActions ();
98+ registerActions ();
99+ });
100+
97101 actionManager .registerAction (COMMANDS_ACTION_GROUP_ID_PREFIX , commandsActionGroup );
98102
99103 // inject 'Commands' menu into context menus
100104 ((DefaultActionGroup )actionManager .getAction (GROUP_MAIN_CONTEXT_MENU )).add (commandsActionGroup );
101105 ((DefaultActionGroup )actionManager .getAction (GROUP_EDITOR_TAB_CONTEXT_MENU )).add (commandsActionGroup );
102106 ((DefaultActionGroup )actionManager .getAction (GROUP_CONSOLES_TREE_CONTEXT_MENU )).add (commandsActionGroup );
107+ }
108+
109+ @ Override
110+ public void start (Callback <WsAgentComponent , Exception > callback ) {
111+ callback .onSuccess (this );
112+ }
113+
114+ /**
115+ * Fetch registered action from command manager and constructs actions which should be
116+ * registered in action manager and context menus.
117+ */
118+ private void registerActions () {
119+ commandManager .getCommands ().forEach (ExecuteCommandActionManager .this ::addAction );
120+ }
103121
104- commandManager .getCommands ().forEach (this ::addAction );
122+ /**
123+ * Packet dispose of registered actions and remove all action groups.
124+ * This action need to be called for example when workspace is stopped.
125+ */
126+ private void disposeActions () {
127+ commandActions .values ().forEach (ExecuteCommandActionManager .this ::removeAction );
128+ goalPopUpGroups .values ().forEach (ExecuteCommandActionManager .this ::removeAction );
129+
130+ commandActions .clear ();
131+ goalPopUpGroups .clear ();
105132 }
106133
107134 /**
108135 * Creates action for executing the given command and
109136 * adds created action to the appropriate action group.
110137 */
111138 private void addAction (CommandImpl command ) {
112- final ExecuteCommandAction action = commandActionFactory .create (command );
139+ ExecuteCommandAction action = commandActionFactory .create (command );
113140
114141 actionManager .registerAction (COMMAND_ACTION_ID_PREFIX + command .getName (), action );
115142 commandActions .put (command .getName (), action );
@@ -123,7 +150,6 @@ private void addAction(CommandImpl command) {
123150 */
124151 private DefaultActionGroup getActionGroupForCommand (CommandImpl command ) {
125152 String goalId = command .getGoal ();
126-
127153 if (isNullOrEmpty (goalId )) {
128154 goalId = goalRegistry .getDefaultGoal ().getId ();
129155 }
@@ -132,7 +158,6 @@ private DefaultActionGroup getActionGroupForCommand(CommandImpl command) {
132158
133159 if (commandGoalPopUpGroup == null ) {
134160 commandGoalPopUpGroup = goalPopUpGroupFactory .create (goalId );
135-
136161 actionManager .registerAction (GOAL_ACTION_GROUP_ID_PREFIX + goalId , commandGoalPopUpGroup );
137162 goalPopUpGroups .put (goalId , commandGoalPopUpGroup );
138163
@@ -142,18 +167,30 @@ private DefaultActionGroup getActionGroupForCommand(CommandImpl command) {
142167 return commandGoalPopUpGroup ;
143168 }
144169
170+ /**
171+ * Removes actual action and dispose it from the action manager.
172+ */
173+ private void removeAction (Action commandAction ) {
174+ String commandActionId = actionManager .getId (commandAction );
175+
176+ if (commandActionId != null ) {
177+ if (actionManager .isGroup (commandActionId )) {
178+ commandsActionGroup .remove (commandAction );
179+ }
180+
181+ actionManager .unregisterAction (commandActionId );
182+ }
183+ }
184+
145185 /**
146186 * Removes action for executing the given command and
147187 * removes the appropriate action group in case it's empty.
148188 */
149189 private void removeAction (CommandImpl command ) {
150- final Action commandAction = commandActions .remove (command .getName ());
190+ Action commandAction = commandActions .remove (command .getName ());
151191
152192 if (commandAction != null ) {
153- final String commandActionId = actionManager .getId (commandAction );
154- if (commandActionId != null ) {
155- actionManager .unregisterAction (commandActionId );
156- }
193+ removeAction (commandAction );
157194
158195 // remove action from it's action group
159196 String goalId = command .getGoal ();
@@ -162,17 +199,13 @@ private void removeAction(CommandImpl command) {
162199 }
163200
164201 // remove action group if it's empty
165- final DefaultActionGroup goalPopUpGroup = goalPopUpGroups .remove (goalId );
202+ DefaultActionGroup goalPopUpGroup = goalPopUpGroups .remove (goalId );
166203
167204 if (goalPopUpGroup != null ) {
168205 goalPopUpGroup .remove (commandAction );
169206
170207 if (goalPopUpGroup .getChildrenCount () == 0 ) {
171- final String goalActionId = actionManager .getId (goalPopUpGroup );
172- if (goalActionId != null ) {
173- actionManager .unregisterAction (goalActionId );
174- }
175- commandsActionGroup .remove (goalPopUpGroup );
208+ removeAction (goalPopUpGroup );
176209 }
177210 }
178211 }
0 commit comments