-
Notifications
You must be signed in to change notification settings - Fork 186
/
AbstractTableDetailsLayout.java
346 lines (281 loc) · 11.8 KB
/
AbstractTableDetailsLayout.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
/**
* Copyright (c) 2015 Bosch Software Innovations GmbH 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
*/
package org.eclipse.hawkbit.ui.common.detailslayout;
import org.eclipse.hawkbit.repository.model.NamedEntity;
import org.eclipse.hawkbit.ui.SpPermissionChecker;
import org.eclipse.hawkbit.ui.common.builder.LabelBuilder;
import org.eclipse.hawkbit.ui.common.table.BaseEntityEventType;
import org.eclipse.hawkbit.ui.common.table.BaseUIEntityEvent;
import org.eclipse.hawkbit.ui.common.tagdetails.AbstractTagToken;
import org.eclipse.hawkbit.ui.components.SPUIComponentProvider;
import org.eclipse.hawkbit.ui.decorators.SPUIButtonStyleNoBorder;
import org.eclipse.hawkbit.ui.management.state.ManagementUIState;
import org.eclipse.hawkbit.ui.utils.HawkbitCommonUtil;
import org.eclipse.hawkbit.ui.utils.SPDateTimeUtil;
import org.eclipse.hawkbit.ui.utils.SPUIStyleDefinitions;
import org.eclipse.hawkbit.ui.utils.UIComponentIdProvider;
import org.eclipse.hawkbit.ui.utils.UIMessageIdProvider;
import org.eclipse.hawkbit.ui.utils.VaadinMessageSource;
import org.vaadin.spring.events.EventBus.UIEventBus;
import com.vaadin.server.FontAwesome;
import com.vaadin.shared.ui.label.ContentMode;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.TabSheet;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
/**
* Abstract Layout to show the entity details.
*
* @param <T>
*/
public abstract class AbstractTableDetailsLayout<T extends NamedEntity> extends VerticalLayout {
private static final long serialVersionUID = 1L;
private final VaadinMessageSource i18n;
private final SpPermissionChecker permissionChecker;
private T selectedBaseEntity;
private Label caption;
private Button editButton;
private Button manageMetadataBtn;
private TabSheet detailsTab;
private final VerticalLayout detailsLayout;
private final VerticalLayout descriptionLayout;
private final VerticalLayout logLayout;
private final VerticalLayout attributesLayout;
private final VerticalLayout tagsLayout;
private final ManagementUIState managementUIState;
private HorizontalLayout nameEditLayout;
protected AbstractTableDetailsLayout(final VaadinMessageSource i18n, final UIEventBus eventBus,
final SpPermissionChecker permissionChecker, final ManagementUIState managementUIState) {
this.i18n = i18n;
this.permissionChecker = permissionChecker;
this.managementUIState = managementUIState;
detailsLayout = createTabLayout();
descriptionLayout = createTabLayout();
logLayout = createTabLayout();
attributesLayout = createTabLayout();
tagsLayout = createTabLayout();
createComponents();
buildLayout();
if (doSubscribeToEventBus()) {
eventBus.subscribe(this);
}
}
/**
* Subscribes the view to the eventBus. Method has to be overriden (return
* false) if the view does not contain any listener to avoid Vaadin blowing
* up our logs with warnings.
*/
protected boolean doSubscribeToEventBus() {
return true;
}
public void setSelectedBaseEntity(final T selectedBaseEntity) {
this.selectedBaseEntity = selectedBaseEntity;
}
protected final VerticalLayout createTabLayout() {
final VerticalLayout tabLayout = SPUIComponentProvider.getDetailTabLayout();
tabLayout.addStyleName("details-layout");
return tabLayout;
}
protected SpPermissionChecker getPermissionChecker() {
return permissionChecker;
}
protected VaadinMessageSource getI18n() {
return i18n;
}
protected T getSelectedBaseEntity() {
return selectedBaseEntity;
}
/**
* Default implementation to handle an entity event.
*
* @param baseEntityEvent
* the event
*/
protected void onBaseEntityEvent(final BaseUIEntityEvent<T> baseEntityEvent) {
final BaseEntityEventType eventType = baseEntityEvent.getEventType();
if (BaseEntityEventType.SELECTED_ENTITY == eventType || BaseEntityEventType.UPDATED_ENTITY == eventType
|| BaseEntityEventType.REMOVE_ENTITY == eventType) {
UI.getCurrent().access(() -> populateData(baseEntityEvent.getEntity()));
} else if (BaseEntityEventType.MINIMIZED == eventType) {
UI.getCurrent().access(() -> setVisible(true));
} else if (BaseEntityEventType.MAXIMIZED == eventType) {
UI.getCurrent().access(() -> setVisible(false));
}
}
protected void setName(final String headerCaption, final String value) {
caption.setValue(HawkbitCommonUtil.getSoftwareModuleName(headerCaption, value));
}
/**
* Restores the tables and tabs displayed on the view based on the selected
* entity.
*/
protected void restoreState() {
populateData(getSelectedBaseEntity());
if (onLoadIsTableMaximized()) {
setVisible(false);
}
}
protected void populateTags(final AbstractTagToken<?> tagToken) {
getTagsLayout().removeAllComponents();
if (getSelectedBaseEntity() == null) {
return;
}
getTagsLayout().addComponent(tagToken.getTagPanel());
}
protected void populateLog() {
logLayout.removeAllComponents();
logLayout.addComponent(SPUIComponentProvider.createNameValueLabel(i18n.getMessage("label.created.at"),
SPDateTimeUtil.formatCreatedAt(selectedBaseEntity)));
logLayout.addComponent(SPUIComponentProvider.createCreatedByLabel(i18n, selectedBaseEntity));
logLayout.addComponent(SPUIComponentProvider.createNameValueLabel(i18n.getMessage("label.modified.date"),
SPDateTimeUtil.formatLastModifiedAt(selectedBaseEntity)));
logLayout.addComponent(SPUIComponentProvider.createLastModifiedByLabel(i18n, selectedBaseEntity));
}
protected void updateDescriptionLayout(final String description) {
descriptionLayout.removeAllComponents();
final Label descLabel = SPUIComponentProvider.createNameValueLabel("", description == null ? "" : description);
/**
* By default text will be truncated based on layout width. So removing
* it as we need full description.
*/
descLabel.removeStyleName("label-style");
descLabel.setId(UIComponentIdProvider.DETAILS_DESCRIPTION_LABEL_ID);
descriptionLayout.addComponent(descLabel);
}
protected VerticalLayout getLogLayout() {
return logLayout;
}
protected VerticalLayout getAttributesLayout() {
return attributesLayout;
}
protected VerticalLayout getDescriptionLayout() {
return descriptionLayout;
}
protected VerticalLayout getDetailsLayout() {
return detailsLayout;
}
protected VerticalLayout getTagsLayout() {
return tagsLayout;
}
protected TabSheet getDetailsTab() {
return detailsTab;
}
protected ManagementUIState getManagementUIState() {
return managementUIState;
}
protected void createComponents() {
caption = createHeaderCaption();
caption.setImmediate(true);
caption.setContentMode(ContentMode.HTML);
caption.setId(getDetailsHeaderCaptionId());
editButton = SPUIComponentProvider.getButton("", "", i18n.getMessage(UIMessageIdProvider.TOOLTIP_UPDATE), null,
false, FontAwesome.PENCIL_SQUARE_O, SPUIButtonStyleNoBorder.class);
editButton.setId(getEditButtonId());
editButton.addClickListener(this::onEdit);
editButton.setEnabled(false);
manageMetadataBtn = SPUIComponentProvider.getButton("", "",
i18n.getMessage(UIMessageIdProvider.TOOLTIP_METADATA_ICON), null, false, FontAwesome.LIST_ALT,
SPUIButtonStyleNoBorder.class);
manageMetadataBtn.setId(getMetadataButtonId());
manageMetadataBtn.setDescription(i18n.getMessage(UIMessageIdProvider.TOOLTIP_METADATA_ICON));
manageMetadataBtn.addClickListener(this::showMetadata);
manageMetadataBtn.setEnabled(false);
detailsTab = SPUIComponentProvider.getDetailsTabSheet();
detailsTab.setImmediate(true);
detailsTab.setWidth(98, Unit.PERCENTAGE);
detailsTab.setHeight(90, Unit.PERCENTAGE);
detailsTab.addStyleName(SPUIStyleDefinitions.DETAILS_LAYOUT_STYLE);
detailsTab.setId(getTabSheetId());
}
protected void buildLayout() {
nameEditLayout = new HorizontalLayout();
nameEditLayout.setWidth(100.0F, Unit.PERCENTAGE);
nameEditLayout.addComponent(caption);
nameEditLayout.setComponentAlignment(caption, Alignment.TOP_LEFT);
if (hasEditPermission()) {
nameEditLayout.addComponent(editButton);
nameEditLayout.setComponentAlignment(editButton, Alignment.TOP_RIGHT);
nameEditLayout.addComponent(manageMetadataBtn);
nameEditLayout.setComponentAlignment(manageMetadataBtn, Alignment.TOP_RIGHT);
}
nameEditLayout.setExpandRatio(caption, 1.0F);
nameEditLayout.addStyleName(SPUIStyleDefinitions.WIDGET_TITLE);
addComponent(nameEditLayout);
setComponentAlignment(nameEditLayout, Alignment.TOP_CENTER);
addComponent(detailsTab);
setComponentAlignment(nameEditLayout, Alignment.TOP_CENTER);
setSizeFull();
setHeightUndefined();
addStyleName(SPUIStyleDefinitions.WIDGET_STYLE);
}
private Label createHeaderCaption() {
return new LabelBuilder().name(getDefaultCaption()).buildCaptionLabel();
}
/**
* If there is no data in table (i.e. no row selected), then disable the
* edit button. If row is selected, enable edit button.
*/
protected void populateData(final T selectedBaseEntity) {
this.selectedBaseEntity = selectedBaseEntity;
editButton.setEnabled(selectedBaseEntity != null);
manageMetadataBtn.setEnabled(selectedBaseEntity != null);
if (selectedBaseEntity == null) {
setName(getDefaultCaption(), "");
} else {
setName(getDefaultCaption(), getName());
}
populateLog();
populateDescription();
populateDetailsWidget();
}
private void populateDescription() {
if (selectedBaseEntity != null) {
updateDescriptionLayout(selectedBaseEntity.getDescription());
} else {
updateDescriptionLayout(null);
}
}
protected Long getSelectedBaseEntityId() {
return selectedBaseEntity == null ? null : selectedBaseEntity.getId();
}
protected String getName() {
return getSelectedBaseEntity().getName();
}
protected abstract void populateDetailsWidget();
protected abstract void populateMetadataDetails();
/**
* Default caption of header to be displayed when no data row selected in
* table.
*
* @return String
*/
protected abstract String getDefaultCaption();
/**
* Click listener for edit button.
*
* @param event
*/
protected abstract void onEdit(Button.ClickEvent event);
protected abstract String getEditButtonId();
protected abstract String getMetadataButtonId();
protected abstract boolean onLoadIsTableMaximized();
protected abstract String getTabSheetId();
protected abstract boolean hasEditPermission();
protected abstract String getDetailsHeaderCaptionId();
protected abstract void showMetadata(Button.ClickEvent event);
public HorizontalLayout getNameEditLayout() {
return nameEditLayout;
}
public Button getEditButton() {
return editButton;
}
}