/
DeviceTabHistory.java
397 lines (332 loc) · 15.5 KB
/
DeviceTabHistory.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*******************************************************************************
* Copyright (c) 2017, 2021 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Eurotech - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua.app.console.module.device.client.device.history;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.Style.SortDir;
import com.extjs.gxt.ui.client.data.BasePagingLoadConfig;
import com.extjs.gxt.ui.client.data.BasePagingLoadResult;
import com.extjs.gxt.ui.client.data.BasePagingLoader;
import com.extjs.gxt.ui.client.data.LoadEvent;
import com.extjs.gxt.ui.client.data.PagingLoadConfig;
import com.extjs.gxt.ui.client.data.PagingLoadResult;
import com.extjs.gxt.ui.client.data.RpcProxy;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.IconButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.Text;
import com.extjs.gxt.ui.client.widget.button.ToolButton;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridSelectionModel;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.toolbar.FillToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.LabelToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.SeparatorToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.extjs.gxt.ui.client.widget.treegrid.TreeGridCellRenderer;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.http.client.URL;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import org.eclipse.kapua.app.console.module.api.client.messages.ConsoleMessages;
import org.eclipse.kapua.app.console.module.api.client.resources.icons.IconSet;
import org.eclipse.kapua.app.console.module.api.client.resources.icons.KapuaIcon;
import org.eclipse.kapua.app.console.module.api.client.ui.button.KapuaButton;
import org.eclipse.kapua.app.console.module.api.client.ui.button.RefreshButton;
import org.eclipse.kapua.app.console.module.api.client.ui.tab.KapuaTabItem;
import org.eclipse.kapua.app.console.module.api.client.ui.widget.DateRangeSelector;
import org.eclipse.kapua.app.console.module.api.client.ui.widget.DateRangeSelectorListener;
import org.eclipse.kapua.app.console.module.api.client.ui.widget.KapuaPagingToolBar;
import org.eclipse.kapua.app.console.module.api.client.ui.widget.KapuaPagingToolbarMessages;
import org.eclipse.kapua.app.console.module.api.client.util.FailureHandler;
import org.eclipse.kapua.app.console.module.api.client.util.KapuaLoadListener;
import org.eclipse.kapua.app.console.module.api.shared.model.session.GwtSession;
import org.eclipse.kapua.app.console.module.device.client.messages.ConsoleDeviceMessages;
import org.eclipse.kapua.app.console.module.device.shared.model.GwtDevice;
import org.eclipse.kapua.app.console.module.device.shared.model.GwtDeviceEvent;
import org.eclipse.kapua.app.console.module.device.shared.service.GwtDeviceService;
import org.eclipse.kapua.app.console.module.device.shared.service.GwtDeviceServiceAsync;
import java.util.ArrayList;
import java.util.List;
public class DeviceTabHistory extends KapuaTabItem<GwtDevice> {
private static final ConsoleMessages MSGS = GWT.create(ConsoleMessages.class);
private static final ConsoleDeviceMessages DEVICES_MSGS = GWT.create(ConsoleDeviceMessages.class);
private final GwtDeviceServiceAsync gwtDeviceService = GWT.create(GwtDeviceService.class);
private static final String EVENT = "event";
private static final int DEVICE_PAGE_SIZE = 250;
private boolean initialized;
private ToolBar toolBar;
private KapuaButton refreshButton;
private KapuaButton export;
private DateRangeSelector dateRangeSelector;
private Grid<GwtDeviceEvent> grid;
private KapuaPagingToolBar pagingToolBar;
private BasePagingLoader<PagingLoadResult<GwtDeviceEvent>> loader;
protected boolean refreshProcess;
public DeviceTabHistory(GwtSession currentSession) {
super(currentSession, DEVICES_MSGS.tabHistory(), new KapuaIcon(IconSet.HISTORY));
initialized = false;
setEnabled(false);
}
@Override
public void setEntity(GwtDevice gwtDevice) {
super.setEntity(gwtDevice);
setEnabled(gwtDevice != null);
}
@Override
protected void onRender(Element parent, int index) {
super.onRender(parent, index);
setLayout(new FitLayout());
setBorders(false);
// init components
initToolBar();
initGrid();
ContentPanel devicesHistoryPanel = new ContentPanel();
devicesHistoryPanel.setBorders(false);
devicesHistoryPanel.setBodyBorder(true);
devicesHistoryPanel.setHeaderVisible(false);
devicesHistoryPanel.setLayout(new FitLayout());
devicesHistoryPanel.setTopComponent(toolBar);
devicesHistoryPanel.add(grid);
devicesHistoryPanel.setBottomComponent(pagingToolBar);
add(devicesHistoryPanel);
initialized = true;
loader.load();
pagingToolBar.enable();
}
private void initToolBar() {
toolBar = new ToolBar();
toolBar.setBorders(true);
//
// Refresh Button
refreshButton = new RefreshButton(new SelectionListener<ButtonEvent>() {
@Override
public void componentSelected(ButtonEvent ce) {
if (!refreshProcess) {
refreshProcess = true;
reload();
refreshProcess = false;
}
}
});
refreshButton.setEnabled(true);
toolBar.add(refreshButton);
toolBar.add(new SeparatorToolItem());
export = new KapuaButton(MSGS.exportToCSV(), new KapuaIcon(IconSet.FILE_TEXT_O),
new SelectionListener<ButtonEvent>() {
@Override
public void componentSelected(ButtonEvent be) {
export("csv");
}
});
toolBar.add(export);
toolBar.add(new SeparatorToolItem());
dateRangeSelector = new DateRangeSelector();
dateRangeSelector.setListener(new DateRangeSelectorListener() {
@Override
public void onUpdate() {
grid.getView().refresh(true);
reload();
}
});
toolBar.add(new FillToolItem());
toolBar.add(new LabelToolItem(MSGS.dataDateRange()));
toolBar.add(dateRangeSelector);
toolBar.disable();
}
private void initGrid() {
List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
ColumnConfig column = new ColumnConfig("receivedOnFormatted", DEVICES_MSGS.deviceEventTime(), 75);
column.setSortable(false);
column.setAlignment(HorizontalAlignment.CENTER);
columns.add(column);
column = new ColumnConfig("eventType", DEVICES_MSGS.deviceEventType(), 50);
column.setSortable(false);
column.setAlignment(HorizontalAlignment.CENTER);
columns.add(column);
TreeGridCellRenderer<GwtDeviceEvent> eventMessageRenderer = new TreeGridCellRenderer<GwtDeviceEvent>() {
@Override
public Object render(final GwtDeviceEvent model, String property, ColumnData config, int rowIndex, int colIndex, ListStore<GwtDeviceEvent> store, Grid<GwtDeviceEvent> grid) {
final HorizontalPanel horizontalPanel = new HorizontalPanel();
final Text cellText = new Text();
cellText.setStyleName("x-grid3-cell");
cellText.setText(model.getUnescapedEventMessage());
// Search button
final ToolButton searchButton = new ToolButton("x-tool-search", new SelectionListener<IconButtonEvent>() {
@Override
public void componentSelected(IconButtonEvent ce) {
final EventMessageDetailsDialog dialog = new EventMessageDetailsDialog(model.getReceivedOnFormatted(), model.getEventType(), model.getUnescapedEventMessage());
dialog.show();
}
});
searchButton.setStyleAttribute("margin-right", "5px");
searchButton.setToolTip(MSGS.showDetails());
horizontalPanel.add(searchButton);
horizontalPanel.add(cellText);
searchButton.hide();
// Show search button on mouse over
horizontalPanel.addDomHandler(new MouseOverHandler() {
@Override
public void onMouseOver(MouseOverEvent arg0) {
if (model.getUnescapedEventMessage() != null && !model.getUnescapedEventMessage().isEmpty()) {
searchButton.show();
}
}
}, MouseOverEvent.getType());
// Hide search button on mouse out
horizontalPanel.addDomHandler(new MouseOutHandler() {
@Override
public void onMouseOut(MouseOutEvent arg0) {
searchButton.hide();
}
}, MouseOutEvent.getType());
return horizontalPanel;
}
};
column = new ColumnConfig("actionType", DEVICES_MSGS.deviceEventActionType(), 50);
column.setSortable(false);
column.setAlignment(HorizontalAlignment.CENTER);
columns.add(column);
column = new ColumnConfig("responseCode", DEVICES_MSGS.deviceEventResponseCode(), 50);
column.setSortable(false);
column.setAlignment(HorizontalAlignment.CENTER);
columns.add(column);
column = new ColumnConfig("eventMessage", DEVICES_MSGS.deviceEventMessage(), 200);
column.setSortable(false);
column.setAlignment(HorizontalAlignment.LEFT);
column.setRenderer(eventMessageRenderer);
columns.add(column);
// loader and store
RpcProxy<PagingLoadResult<GwtDeviceEvent>> proxy = new RpcProxy<PagingLoadResult<GwtDeviceEvent>>() {
@Override
public void load(Object loadConfig, AsyncCallback<PagingLoadResult<GwtDeviceEvent>> callback) {
if (selectedEntity != null) {
PagingLoadConfig pagingConfig = (BasePagingLoadConfig) loadConfig;
((BasePagingLoadConfig) pagingConfig).setLimit(DEVICE_PAGE_SIZE);
gwtDeviceService.findDeviceEvents(pagingConfig,
selectedEntity,
dateRangeSelector.getStartDate(),
dateRangeSelector.getEndDate(),
callback);
} else {
callback.onSuccess(new BasePagingLoadResult<GwtDeviceEvent>(new ArrayList<GwtDeviceEvent>()));
}
}
};
loader = new BasePagingLoader<PagingLoadResult<GwtDeviceEvent>>(proxy);
loader.setSortDir(SortDir.DESC);
loader.setSortField("receivedOnFormatted");
loader.setRemoteSort(true);
loader.addLoadListener(new DataLoadListener());
ListStore<GwtDeviceEvent> store = new ListStore<GwtDeviceEvent>(loader);
grid = new Grid<GwtDeviceEvent>(store, new ColumnModel(columns));
grid.setBorders(false);
grid.setStateful(false);
grid.setLoadMask(true);
grid.setStripeRows(true);
grid.setTrackMouseOver(false);
grid.setAutoExpandColumn("eventMessage");
grid.disableTextSelection(false);
grid.getView().setAutoFill(true);
grid.getView().setForceFit(true);
grid.getView().setEmptyText(MSGS.gridNoResultReceived(EVENT));
pagingToolBar = new KapuaPagingToolBar(DEVICE_PAGE_SIZE);
pagingToolBar.setKapuaPagingToolbarMessages(getKapuaPagingToolbarMessages());
pagingToolBar.bind(loader);
GridSelectionModel<GwtDeviceEvent> selectionModel = new GridSelectionModel<GwtDeviceEvent>();
selectionModel.setSelectionMode(SelectionMode.SINGLE);
grid.setSelectionModel(selectionModel);
}
protected KapuaPagingToolbarMessages getKapuaPagingToolbarMessages() {
return new KapuaPagingToolbarMessages() {
@Override
public String pagingToolbarShowingPost() {
return MSGS.specificPagingToolbarShowingPost(EVENT);
}
@Override
public String pagingToolbarNoResult() {
return MSGS.specificPagingToolbarNoResult(EVENT);
}
};
}
// --------------------------------------------------------------------------------------
//
// Device Event List Management
//
// --------------------------------------------------------------------------------------
@Override
public void doRefresh() {
if (initialized) {
if (selectedEntity == null) {
// clear the table
grid.getStore().removeAll();
} else {
loader.load();
}
}
}
public void reload() {
loader.load();
}
private void export(String format) {
StringBuilder sbUrl = new StringBuilder();
sbUrl.append("exporter_device_event?");
sbUrl.append("format=")
.append(format)
.append("&scopeId=")
.append(URL.encodeQueryString(currentSession.getSelectedAccountId()))
.append("&deviceId=")
.append(URL.encodeQueryString(selectedEntity.getId()))
.append("&startDate=")
.append(dateRangeSelector.getStartDate().getTime())
.append("&endDate=")
.append(dateRangeSelector.getEndDate().getTime());
Window.open(sbUrl.toString(), "_blank", "location=no");
}
// --------------------------------------------------------------------------------------
//
// Data Load Listener
//
// --------------------------------------------------------------------------------------
private class DataLoadListener extends KapuaLoadListener {
public DataLoadListener() {
}
@Override
public void loaderLoad(LoadEvent le) {
if (le.exception != null) {
FailureHandler.handle(le.exception);
}
toolBar.enable();
pagingToolBar.enable();
export.enable();
refreshButton.enable();
}
@Override
public void loaderBeforeLoad(LoadEvent le) {
super.loaderBeforeLoad(le);
export.disable();
refreshButton.disable();
}
}
}