/
MachinePanelPresenter.java
309 lines (256 loc) · 10.9 KB
/
MachinePanelPresenter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*******************************************************************************
* Copyright (c) 2012-2016 Codenvy, S.A.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Codenvy, S.A. - initial API and implementation
*******************************************************************************/
package org.eclipse.che.ide.extension.machine.client.perspective.widgets.machine.panel;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.google.web.bindery.event.shared.EventBus;
import org.eclipse.che.api.core.model.machine.MachineStatus;
import org.eclipse.che.api.machine.gwt.client.MachineServiceClient;
import org.eclipse.che.api.machine.shared.dto.MachineDto;
import org.eclipse.che.api.promises.client.Operation;
import org.eclipse.che.api.promises.client.OperationException;
import org.eclipse.che.api.promises.client.Promise;
import org.eclipse.che.api.promises.client.PromiseError;
import org.eclipse.che.api.workspace.gwt.client.event.WorkspaceStartedEvent;
import org.eclipse.che.api.workspace.gwt.client.event.WorkspaceStartedHandler;
import org.eclipse.che.api.workspace.gwt.client.event.WorkspaceStoppedEvent;
import org.eclipse.che.api.workspace.gwt.client.event.WorkspaceStoppedHandler;
import org.eclipse.che.commons.annotation.Nullable;
import org.eclipse.che.ide.api.app.AppContext;
import org.eclipse.che.ide.api.event.ActivePartChangedEvent;
import org.eclipse.che.ide.api.event.ActivePartChangedHandler;
import org.eclipse.che.ide.api.parts.base.BasePresenter;
import org.eclipse.che.ide.extension.machine.client.MachineLocalizationConstant;
import org.eclipse.che.ide.extension.machine.client.MachineResources;
import org.eclipse.che.ide.extension.machine.client.inject.factories.EntityFactory;
import org.eclipse.che.ide.extension.machine.client.machine.Machine;
import org.eclipse.che.ide.extension.machine.client.machine.MachineStateEvent;
import org.eclipse.che.ide.extension.machine.client.perspective.widgets.machine.appliance.MachineAppliancePresenter;
import org.vectomatic.dom.svg.ui.SVGResource;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* The class contains business logic to control displaying of machines on special view.
*
* @author Dmitry Shnurenko
* @author Artem Zatsarynnyi
*/
@Singleton
public class MachinePanelPresenter extends BasePresenter implements MachinePanelView.ActionDelegate,
MachineStateEvent.Handler,
WorkspaceStartedHandler,
WorkspaceStoppedHandler,
ActivePartChangedHandler {
private final MachinePanelView view;
private final MachineServiceClient service;
private final EntityFactory entityFactory;
private final MachineLocalizationConstant locale;
private final MachineAppliancePresenter appliance;
private final MachineResources resources;
private final Map<String, MachineTreeNode> existingMachineNodes;
private final Map<String, Machine> cachedMachines;
private final MachineTreeNode rootNode;
private final List<MachineTreeNode> machineNodes;
private final AppContext appContext;
private org.eclipse.che.api.core.model.machine.Machine selectedMachine;
private boolean isMachineRunning;
@Inject
public MachinePanelPresenter(MachinePanelView view,
MachineServiceClient service,
EntityFactory entityFactory,
MachineLocalizationConstant locale,
MachineAppliancePresenter appliance,
EventBus eventBus,
MachineResources resources,
AppContext appContext) {
this.view = view;
this.view.setDelegate(this);
this.service = service;
this.entityFactory = entityFactory;
this.locale = locale;
this.appliance = appliance;
this.resources = resources;
this.appContext = appContext;
this.machineNodes = new ArrayList<>();
this.rootNode = entityFactory.createMachineNode(null, "root", machineNodes);
this.existingMachineNodes = new HashMap<>();
this.cachedMachines = new HashMap<>();
eventBus.addHandler(MachineStateEvent.TYPE, this);
eventBus.addHandler(WorkspaceStartedEvent.TYPE, this);
eventBus.addHandler(WorkspaceStoppedEvent.TYPE, this);
eventBus.addHandler(ActivePartChangedEvent.TYPE, this);
}
/** Gets all machines and adds them to special place on view. */
public Promise<List<MachineDto>> showMachines() {
return showMachines(appContext.getWorkspace().getId());
}
private Promise<List<MachineDto>> showMachines(String workspaceId) {
Promise<List<MachineDto>> machinesPromise = service.getMachines(workspaceId);
return machinesPromise.then(new Operation<List<MachineDto>>() {
@Override
public void apply(List<MachineDto> machines) throws OperationException {
machineNodes.clear();
if (machines.isEmpty()) {
appliance.showStub(locale.unavailableMachineInfo());
return;
}
for (MachineDto machine : machines) {
addNodeToTree(machine);
}
view.setData(rootNode);
selectFirstNode();
}
});
}
private void addNodeToTree(org.eclipse.che.api.core.model.machine.Machine machine) {
MachineTreeNode machineNode = entityFactory.createMachineNode(rootNode, machine, null);
existingMachineNodes.put(machine.getId(), machineNode);
if (!machineNodes.contains(machineNode)) {
machineNodes.add(machineNode);
}
}
private void selectFirstNode() {
if (!machineNodes.isEmpty()) {
view.selectNode(machineNodes.get(0));
}
}
/**
* Returns selected machine state.
*/
public org.eclipse.che.api.core.model.machine.Machine getSelectedMachineState() {
return selectedMachine;
}
/** {@inheritDoc} */
@Override
public void onMachineSelected(final MachineDto selectedMachine) {
this.selectedMachine = selectedMachine;
if (cachedMachines.containsKey(selectedMachine.getId())) {
appliance.showAppliance(cachedMachines.get(selectedMachine.getId()));
return;
}
service.getMachine(selectedMachine.getId()).then(new Operation<MachineDto>() {
@Override
public void apply(MachineDto machineDto) throws OperationException {
if (machineDto.getStatus() == MachineStatus.RUNNING) {
isMachineRunning = true;
Machine machine = entityFactory.createMachine(machineDto);
cachedMachines.put(selectedMachine.getId(), machine);
appliance.showAppliance(machine);
} else {
isMachineRunning = false;
// we show the loader for dev machine so this message isn't necessary for dev machine
if (!selectedMachine.getConfig().isDev()) {
appliance.showStub(locale.unavailableMachineStarting(selectedMachine.getConfig().getName()));
}
}
}
}).catchError(new Operation<PromiseError>() {
@Override
public void apply(PromiseError error) throws OperationException {
isMachineRunning = false;
appliance.showStub(locale.machineNotFound(selectedMachine.getId()));
}
});
}
/** {@inheritDoc} */
@NotNull
@Override
public String getTitle() {
return locale.machinePanelTitle();
}
/** {@inheritDoc} */
@Override
public void setVisible(boolean visible) {
view.setVisible(visible);
}
/** {@inheritDoc} */
@Override
public IsWidget getView() {
return view;
}
/** {@inheritDoc} */
@Nullable
@Override
public ImageResource getTitleImage() {
return null;
}
@Nullable
@Override
public SVGResource getTitleSVGImage() {
return resources.machinesPartIcon();
}
/** {@inheritDoc} */
@Nullable
@Override
public String getTitleToolTip() {
return locale.machinePanelTooltip();
}
/** {@inheritDoc} */
@Override
public void go(AcceptsOneWidget container) {
container.setWidget(view);
}
/** {@inheritDoc} */
@Override
public void onWorkspaceStarted(WorkspaceStartedEvent event) {
showMachines(event.getWorkspace().getId());
}
/** {@inheritDoc} */
@Override
public void onWorkspaceStopped(WorkspaceStoppedEvent event) {
showMachines(event.getWorkspace().getId());
}
@Override
public void onMachineCreating(MachineStateEvent event) {
isMachineRunning = false;
selectedMachine = event.getMachine();
addNodeToTree(selectedMachine);
view.setData(rootNode);
view.selectNode(existingMachineNodes.get(event.getMachine().getId()));
}
/** {@inheritDoc} */
@Override
public void onMachineRunning(final MachineStateEvent event) {
isMachineRunning = true;
selectedMachine = event.getMachine();
view.selectNode(existingMachineNodes.get(selectedMachine.getId()));
}
/**
* {@inheritDoc}
*/
@Override
public void onMachineDestroyed(MachineStateEvent event) {
org.eclipse.che.api.core.model.machine.Machine machine = event.getMachine();
MachineTreeNode deletedNode = existingMachineNodes.get(machine.getId());
machineNodes.remove(deletedNode);
existingMachineNodes.remove(machine.getId());
view.setData(rootNode);
selectFirstNode();
}
/**
* Returns <code>true</code> if selected machine running, and <code>false</code> if selected machine isn't running
*/
public boolean isMachineRunning() {
return isMachineRunning;
}
@Override
public void onActivePartChanged(ActivePartChangedEvent event) {
if ((event.getActivePart() instanceof MachinePanelPresenter)) {
showMachines();
}
}
}