/
DeviceTagAddDialog.java
173 lines (143 loc) · 6.83 KB
/
DeviceTagAddDialog.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
/*******************************************************************************
* Copyright (c) 2011, 2020 Eurotech and/or its affiliates and others
*
* 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:
* Eurotech - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua.app.console.module.device.client.device.tag;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;
import org.eclipse.kapua.app.console.module.api.client.GwtKapuaErrorCode;
import org.eclipse.kapua.app.console.module.api.client.GwtKapuaException;
import org.eclipse.kapua.app.console.module.api.client.ui.dialog.entity.EntityAddEditDialog;
import org.eclipse.kapua.app.console.module.api.client.ui.panel.FormPanel;
import org.eclipse.kapua.app.console.module.api.client.util.DialogUtils;
import org.eclipse.kapua.app.console.module.api.client.util.FailureHandler;
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.service.GwtDeviceService;
import org.eclipse.kapua.app.console.module.device.shared.service.GwtDeviceServiceAsync;
import org.eclipse.kapua.app.console.module.tag.shared.model.GwtTag;
import org.eclipse.kapua.app.console.module.tag.shared.service.GwtTagService;
import org.eclipse.kapua.app.console.module.tag.shared.service.GwtTagServiceAsync;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class DeviceTagAddDialog extends EntityAddEditDialog {
private static final ConsoleDeviceMessages MSGS = GWT.create(ConsoleDeviceMessages.class);
private static final GwtTagServiceAsync GWT_TAG_SERVICE = GWT.create(GwtTagService.class);
private static final GwtDeviceServiceAsync GWT_DEVICE_SERVICE = GWT.create(GwtDeviceService.class);
private ComboBox<GwtTag> tagsCombo;
private GwtDevice selectedDevice;
public DeviceTagAddDialog(GwtSession currentSession, GwtDevice selectedDevice) {
super(currentSession);
this.selectedDevice = selectedDevice;
DialogUtils.resizeDialog(this, 400, 200);
}
@Override
public void submit() {
String tagId = tagsCombo.getValue().getId();
GWT_DEVICE_SERVICE.addDeviceTag(xsrfToken, selectedDevice.getScopeId(), selectedDevice.getId(), tagId, new AsyncCallback<Void>() {
@Override
public void onSuccess(Void gwtAccessPermission) {
exitStatus = true;
exitMessage = MSGS.dialogDeviceTagAddConfirmation();
hide();
}
@Override
public void onFailure(Throwable cause) {
unmask();
submitButton.enable();
cancelButton.enable();
status.hide();
exitStatus = false;
if (!isPermissionErrorMessage(cause)) {
if (cause instanceof GwtKapuaException) {
GwtKapuaException gwtCause = (GwtKapuaException) cause;
if (gwtCause.getCode().equals(GwtKapuaErrorCode.DUPLICATE_NAME)) {
tagsCombo.markInvalid(gwtCause.getMessage());
} else if (gwtCause.getCode().equals(GwtKapuaErrorCode.ENTITY_NOT_FOUND)) {
tagsCombo.markInvalid(gwtCause.getMessage());
}
}
FailureHandler.handleFormException(formPanel, cause);
}
}
});
}
@Override
public String getHeaderMessage() {
return MSGS.dialogDeviceTagAddHeader();
}
@Override
public String getInfoMessage() {
return MSGS.dialogDeviceTagAddInfo();
}
@Override
public void createBody() {
submitButton.disable();
FormPanel formPanel = new FormPanel(FORM_LABEL_WIDTH);
Listener<BaseEvent> comboBoxListener = new Listener<BaseEvent>() {
@Override
public void handleEvent(BaseEvent be) {
DeviceTagAddDialog.this.formPanel.fireEvent(Events.OnClick);
}
};
//
// Tags
tagsCombo = new ComboBox<GwtTag>();
tagsCombo.setStore(new ListStore<GwtTag>());
tagsCombo.setEditable(false);
tagsCombo.setTypeAhead(false);
tagsCombo.setAllowBlank(false);
tagsCombo.disable();
tagsCombo.setEmptyText(MSGS.dialogDeviceTagAddFieldTagEmptyText());
tagsCombo.setFieldLabel("* " + MSGS.dialogDeviceTagAddFieldTag());
tagsCombo.setToolTip(MSGS.deviceFormTagTooltip());
tagsCombo.setTriggerAction(TriggerAction.ALL);
tagsCombo.setDisplayField("tagName");
tagsCombo.setTemplate("<tpl for=\".\"><div role=\"listitem\" class=\"x-combo-list-item\" title={tagName}>{tagName}</div></tpl>");
tagsCombo.addListener(Events.Select, comboBoxListener);
GWT_TAG_SERVICE.findAll(selectedDevice.getScopeId(), new AsyncCallback<List<GwtTag>>() {
@Override
public void onFailure(Throwable caught) {
exitStatus = false;
if (!isPermissionErrorMessage(caught)) {
exitMessage = MSGS.dialogDeviceTagAddFieldTagLoadingError(caught.getLocalizedMessage());
}
hide();
}
@Override
public void onSuccess(List<GwtTag> result) {
Collections.sort(result, new Comparator<GwtTag>() {
@Override
public int compare(GwtTag tag1, GwtTag tag2) {
return tag1.getTagName().compareTo(tag2.getTagName());
}
});
tagsCombo.getStore().add(result);
tagsCombo.setEmptyText(result.isEmpty() ? MSGS.dialogDeviceTagAddFieldTagEmptyTextNoTags() : MSGS.dialogDeviceTagAddFieldTagEmptyText());
tagsCombo.enable();
}
});
formPanel.add(tagsCombo);
bodyPanel.add(formPanel);
}
@Override
protected void onRender(Element parent, int pos) {
super.onRender(parent, pos);
}
}