-
Notifications
You must be signed in to change notification settings - Fork 1
/
AppSelectionDialog.java
336 lines (284 loc) · 11 KB
/
AppSelectionDialog.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package org.iplantc.core.client.pipelines.views.dialogs;
import java.util.ArrayList;
import org.iplantc.core.client.pipelines.Constants;
import org.iplantc.core.client.pipelines.I18N;
import org.iplantc.core.jsonutil.JsonUtil;
import org.iplantc.core.uiapplications.client.events.AnalysisCategorySelectedEvent;
import org.iplantc.core.uiapplications.client.events.AnalysisCategorySelectedEventHandler;
import org.iplantc.core.uiapplications.client.models.Analysis;
import org.iplantc.core.uiapplications.client.models.AnalysisGroup;
import org.iplantc.core.uiapplications.client.services.AppTemplateServiceFacade;
import org.iplantc.core.uiapplications.client.views.panels.AbstractCatalogCategoryPanel;
import org.iplantc.core.uiapplications.client.views.panels.BaseCatalogMainPanel;
import org.iplantc.core.uiapplications.client.events.AppSearchResultSelectedEvent;
import org.iplantc.core.uiapplications.client.events.AppSearchResultSelectedEventHandler;
import org.iplantc.core.uicommons.client.ErrorHandler;
import org.iplantc.core.uicommons.client.events.EventBus;
import org.iplantc.de.client.DeCommonI18N;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.BorderLayoutEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.event.WindowEvent;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Status;
import com.extjs.gxt.ui.client.widget.button.ButtonBar;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.toolbar.FillToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;
/**
* Lets the user choose an app and click "add" to add it to some other UI component. The "add" button
* leaves the dialog open, so multiple apps can be added in one session by using the "add" button
* repeatedly.
*
* @author hariolf
*
*/
public class AppSelectionDialog extends Dialog {
private static final String ADD = YES; // button id for the add button
private final AbstractCatalogCategoryPanel categoryPanel;
private BaseCatalogMainPanel appsListPanel;
private ArrayList<HandlerRegistration> handlers;
private final AppTemplateServiceFacade service;
private ContentPanel mainPanel;
private BorderLayoutData dataWest;
private BorderLayoutData dataCenter;
private Status countLabel;
private Status lastAppLabel;
private final Command cmdAdd;
/**
*
* @param categoryPanel
* @param service
* @param cmdAdd called when the add button is clicked
*/
public AppSelectionDialog(AbstractCatalogCategoryPanel categoryPanel,
AppTemplateServiceFacade service, Command cmdAdd) {
this.categoryPanel = categoryPanel;
this.service = service;
this.cmdAdd = cmdAdd;
init();
}
private void init() {
addBeforeShowListener();
setHeading(I18N.DISPLAY.selectWindowTitle());
setSize(800, 400);
setModal(true);
setButtons(ADD + CLOSE);
// make the yes button into an add button
getButtonById(ADD).setText(DeCommonI18N.DISPLAY.add());
initHandlers();
initMainPanel();
initLayout();
initAppListPanel();
countLabel = new Status();
countLabel.setBox(true);
lastAppLabel = new Status();
lastAppLabel.setBox(true);
updateAddButton();
compose();
addAfterHideListener();
}
private void initAppListPanel() {
appsListPanel = new BaseCatalogMainPanel(Constants.CLIENT.tagAppSelectDialog(), service);
appsListPanel.setSize(400, 400);
appsListPanel.addGridSelectionChangeListener(buildGridChangeListener());
}
private void initMainPanel() {
mainPanel = new ContentPanel();
mainPanel.setHeaderVisible(false);
mainPanel.setSize(700, 400);
}
private void addAfterHideListener() {
addListener(Events.Hide, new Listener<WindowEvent>() {
@Override
public void handleEvent(WindowEvent be) {
for (HandlerRegistration handler : handlers) {
handler.removeHandler();
}
}
});
}
private void addBeforeShowListener() {
addListener(Events.BeforeShow, new Listener<ComponentEvent>() {
@Override
public void handleEvent(ComponentEvent be) {
categoryPanel.selectDefault();
}
});
}
private Listener<BaseEvent> buildGridChangeListener() {
return new Listener<BaseEvent>() {
@Override
public void handleEvent(BaseEvent be) {
updateAddButton();
}
};
}
/**
* Enables or disables the add button depending on the current selection
*/
private void updateAddButton() {
getButtonById(ADD).setEnabled(appsListPanel.getSelectedApp() != null);
}
private BorderLayoutData initLayoutRegion(LayoutRegion region, float size, boolean collapsible) {
BorderLayoutData ret = new BorderLayoutData(region);
if (size > 0) {
ret.setSize(size);
}
ret.setCollapsible(collapsible);
ret.setSplit(true);
return ret;
}
private void initLayout() {
BorderLayout layout = new BorderLayout();
// make sure we re-draw when a panel expands
layout.addListener(Events.Expand, new Listener<BorderLayoutEvent>() {
@Override
public void handleEvent(BorderLayoutEvent be) {
layout();
}
});
mainPanel.setLayout(layout);
dataWest = initLayoutRegion(LayoutRegion.WEST, getWestWidth(), true);
dataCenter = initLayoutRegion(LayoutRegion.CENTER, 400, false);
}
/**
* {@inheritDoc}
*/
@Override
protected void onRender(Element parent, int pos) {
super.onRender(parent, pos);
add(mainPanel);
compose();
}
@Override
protected void onResize(int width, int height) {
super.onResize(width, height);
resizeContents(getInnerWidth(), getInnerHeight());
}
@Override
protected void onAfterLayout() {
super.onAfterLayout();
resizeContents(getInnerWidth(), getInnerHeight());
}
/**
* Resizes this panel's inner tree panel.
*
* @param width
* @param height
*/
private void resizeContents(int width, int height) {
if (mainPanel != null) {
mainPanel.setWidth(width);
mainPanel.setHeight(height);
}
}
private void compose() {
mainPanel.add(categoryPanel, dataWest);
mainPanel.add(appsListPanel, dataCenter);
add(mainPanel);
ToolBar statusBar = new ToolBar();
statusBar.add(countLabel);
statusBar.add(lastAppLabel);
ButtonBar buttonBar = getButtonBar();
buttonBar.insert(countLabel, 0);
buttonBar.insert(lastAppLabel, 1);
buttonBar.insert(new FillToolItem(), 2);
}
protected int getWestWidth() {
return 220;
}
private void initHandlers() {
EventBus eventbus = EventBus.getInstance();
handlers = new ArrayList<HandlerRegistration>();
handlers.add(eventbus.addHandler(AnalysisCategorySelectedEvent.TYPE,
new AnalysisCategorySelectedEventHandlerImpl()));
getButtonById(ADD).addSelectionListener(new SelectionListener<ButtonEvent>() {
@Override
public void componentSelected(ButtonEvent ce) {
Analysis app = appsListPanel.getSelectedApp();
if (app != null) {
cmdAdd.execute();
}
}
});
handlers.add(eventbus.addHandler(AppSearchResultSelectedEvent.TYPE,
new AppSearchResultSelectedEventHandler() {
@Override
public void onSelection(AppSearchResultSelectedEvent event) {
if (Constants.CLIENT.tagAppSelectDialog().equals(event.getSourceTag())) {
categoryPanel.selectCategory(event.getCategoryId());
appsListPanel.selectTool(event.getAppId());
}
}
}));
}
/**
* Updates the app count and last app added.
*
* @param appCount number of apps
* @param lastAppName name of the app that was added last, or null
*/
public void updateStatusBar(int appCount, String lastAppName) {
countLabel.setText(appCount == 1 ? I18N.DISPLAY
.appCountSingular() : I18N.DISPLAY
.appCountPlural(appCount));
lastAppLabel.setText(I18N.DISPLAY
.lastApp(lastAppName == null ? I18N.DISPLAY
.lastAppNotDefined() : lastAppName));
}
/**
* Returns the selected app or null if nothing selected
*
* @return the selected app
*/
public Analysis getSelectedApp() {
return appsListPanel.getSelectedApp();
}
private class AnalysisCategorySelectedEventHandlerImpl implements
AnalysisCategorySelectedEventHandler {
@Override
public void onSelection(AnalysisCategorySelectedEvent event) {
if (event.getSourcePanel() == categoryPanel && appsListPanel != null) {
appsListPanel.setHeading(event.getGroup().getName());
updateAnalysesListing(event.getGroup());
}
}
}
private void updateAnalysesListing(final AnalysisGroup group) {
appsListPanel.mask(DeCommonI18N.DISPLAY.loadingMask());
service.getAnalysis(group.getId(), new AsyncCallback<String>() {
@Override
public void onSuccess(String result) {
ArrayList<Analysis> analyses = new ArrayList<Analysis>();
JSONArray templates = JsonUtil.getArray(JsonUtil.getObject(result), "templates"); //$NON-NLS-1$
if (templates != null) {
for (int i = 0; i < templates.size(); i++) {
Analysis analysis = new Analysis(JsonUtil.getObjectAt(templates, i));
analyses.add(analysis);
}
}
appsListPanel.seed(analyses, group);
appsListPanel.unmask();
// updateAddButton();
}
@Override
public void onFailure(Throwable caught) {
ErrorHandler.post(caught);
appsListPanel.unmask();
}
});
}
}