/
EntityGridLoadListener.java
114 lines (100 loc) · 3.91 KB
/
EntityGridLoadListener.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
/*******************************************************************************
* 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.api.client.ui.grid;
import com.extjs.gxt.ui.client.data.LoadEvent;
import com.extjs.gxt.ui.client.data.Loader;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.button.Button;
import org.eclipse.kapua.app.console.module.api.client.util.KapuaLoadListener;
import org.eclipse.kapua.app.console.module.api.shared.model.GwtEntityModel;
import java.util.List;
public class EntityGridLoadListener<M extends GwtEntityModel> extends KapuaLoadListener {
private EntityGrid<M> entityGrid;
private ListStore<M> entityStore;
private Button searchButton;
private Button resetButton;
private List<M> selectedEntities;
private boolean keepSelectedOnLoad = true;
public EntityGridLoadListener(EntityGrid<M> entityGrid, ListStore<M> entityStore) {
this.entityGrid = entityGrid;
this.entityStore = entityStore;
}
public EntityGridLoadListener(EntityGrid<M> entityGrid, ListStore<M> entityStore, Button searchButton, Button resetButton) {
this.entityGrid = entityGrid;
this.entityStore = entityStore;
this.searchButton = searchButton;
this.resetButton = resetButton;
}
/**
* Keeps track of the selected item in the {@link EntityGrid} before the invocation of {@link Loader#load()}.
*
* @param le The {@link LoadEvent}
*/
@Override
public void loaderBeforeLoad(LoadEvent le) {
selectedEntities = entityGrid.getSelectionModel().getSelectedItems();
if (searchButton != null && resetButton != null) {
searchButton.disable();
resetButton.disable();
}
}
/**
* If {@link #keepSelectedOnLoad} is {@code true}, selects all entities which where selected before {@link Loader#load()}
* invocation if they are present in the new result set.
*
* @param le The {@link LoadEvent}.
*/
@Override
public void loaderLoad(LoadEvent le) {
if (isKeepSelectedOnLoad() && !selectedEntities.isEmpty()) {
for (M e : entityStore.getModels()) {
for (M se : selectedEntities) {
if (se.getId().equals(e.getId())) {
entityGrid.getSelectionModel().select(e, true);
break;
}
}
}
}
if (searchButton != null && resetButton != null) {
searchButton.enable();
resetButton.enable();
}
entityGrid.loaded();
}
@Override
public void loaderLoadException(LoadEvent le) {
entityGrid.loaded();
if (searchButton != null && resetButton != null) {
searchButton.enable();
resetButton.enable();
}
super.loaderLoadException(le);
}
/**
* Returns whether or not the currently selected entities will be kept selected after the {@link Loader#load()}
*
* @return {@code true} if enabled,{@code false} otherwise.
*/
public boolean isKeepSelectedOnLoad() {
return keepSelectedOnLoad;
}
/**
* Enables or disables the feature of remembering the selected items after the {@link Loader#load()} event.
*
* @param keepSelectedOnLoad {@code true} to enable, {@code false} to disable.
*/
public void setKeepSelectedOnLoad(boolean keepSelectedOnLoad) {
this.keepSelectedOnLoad = keepSelectedOnLoad;
}
}