This repository has been archived by the owner on Nov 12, 2020. It is now read-only.
/
AbstractEntityView.java
363 lines (297 loc) · 11.9 KB
/
AbstractEntityView.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
/*
* JBoss, Home of Professional Open Source
* Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors
* as indicated by the @author tags. All rights reserved.
* See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This copyrighted material is made available to anyone wishing to use,
* modify, copy, or redistribute it subject to the terms and conditions
* of the GNU Lesser General Public License, v. 2.1.
* This program is distributed in the hope that it will be useful, but WITHOUT A
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License,
* v.2.1 along with this distribution; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
package org.jboss.as.console.client.shared.viewframework;
import com.google.gwt.dom.client.Style;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import org.jboss.as.console.client.Console;
import org.jboss.as.console.client.core.SuspendableViewImpl;
import org.jboss.as.console.client.widgets.forms.AddressBinding;
import org.jboss.as.console.client.widgets.forms.ApplicationMetaData;
import org.jboss.as.console.client.widgets.forms.FormMetaData;
import org.jboss.as.console.client.widgets.forms.PropertyBinding;
import org.jboss.ballroom.client.widgets.forms.Form;
import org.jboss.ballroom.client.widgets.forms.FormAdapter;
import org.jboss.ballroom.client.widgets.forms.FormItem;
import org.jboss.ballroom.client.widgets.forms.ObservableFormItem;
import org.jboss.ballroom.client.widgets.tables.DefaultCellTable;
import org.jboss.ballroom.client.widgets.tabs.FakeTabPanel;
import org.jboss.ballroom.client.widgets.tools.ToolStrip;
import org.jboss.dmr.client.ModelNode;
import java.util.Collections;
import java.util.EnumSet;
import java.util.List;
/**
* An abstract view class with a full EntityEditor. This class assembles the editor and reacts to
* FrameworkView callbacks.
*
* @author Stan Silvert
* @author Heiko Braun
*/
public abstract class AbstractEntityView<T> extends SuspendableViewImpl
implements FrameworkView, FrameworkPresenter, FormItemObserver {
protected Class<?> beanType;
protected EnumSet<FrameworkButton> hideButtons;
protected FormMetaData formMetaData;
protected AddressBinding address;
protected EntityEditor<T> entityEditor;
protected EntityDetails<T> entityDetails;
private String description = null;
public AbstractEntityView(Class<?> beanType, ApplicationMetaData propertyMetaData) {
this(beanType, propertyMetaData, EnumSet.noneOf(FrameworkButton.class));
}
public AbstractEntityView(Class<?> beanType, ApplicationMetaData propertyMetaData, EnumSet<FrameworkButton> hideButtons) {
this.beanType = beanType;
this.hideButtons = hideButtons;
formMetaData = propertyMetaData.getFormMetaData(beanType);
address = propertyMetaData.getBeanMetaData(beanType).getAddress();
}
public void setDescription(String description) {
this.description = description;
}
/**
* Get the FormMetaData for the Entity.
* @return The FormMetaData.
*/
protected FormMetaData getFormMetaData() {
return this.formMetaData;
}
/**
* Get the DMR address for this entity.
* @return The address.
*/
protected AddressBinding getAddress() {
return this.address;
}
/**
* Get the EntityToDmrBridge for the Entity.
* @return The bridge.
*/
public abstract EntityToDmrBridge<T> getEntityBridge();
/**
* Create the table with the desired columns for the Entity.
* @return The table.
*/
protected abstract DefaultCellTable<T> makeEntityTable();
/**
* Create the form with fields used for creating a new Entity.
* @return The Console.CONSTANTS.common_label_add() form.
*/
protected abstract FormAdapter<T> makeAddEntityForm();
/**
* Get the plural name of the Entity to be displayed.
* @return The Entity name.
*/
protected abstract String getEntityDisplayName();
/**
* This is the default view assembly routine. It simly creates a single
* top level tab and adds the editors contents to it.<p/>
* Feel free to override this method in order to create more sophisticated
* view compositions.
*
* @return
*/
@Override
public Widget createWidget() {
LayoutPanel layout = new LayoutPanel();
// Top Most Tab
FakeTabPanel titleBar = new FakeTabPanel(getEntityDisplayName());
layout.add(titleBar);
VerticalPanel panel = new VerticalPanel();
panel.setStyleName("rhs-content-panel");
ScrollPanel scrollPanel = new ScrollPanel(panel);
layout.add(scrollPanel);
entityEditor = makeEntityEditor();
entityEditor.setDescription(description);
entityEditor.setTools(createToolStrip());
Widget editorWidget = entityEditor.setIncludeTools(false).asWidget();
panel.add(editorWidget);
/*ToolStrip tools = createToolStrip();
if(tools.hasButtons())
{
layout.add(tools);
layout.setWidgetTopHeight(titleBar, 0, Style.Unit.PX, 40, Style.Unit.PX);
layout.setWidgetTopHeight(tools, 40, Style.Unit.PX, 30, Style.Unit.PX);
layout.setWidgetTopHeight(scrollPanel, 70, Style.Unit.PX, 100, Style.Unit.PCT);
}
else
{
layout.setWidgetTopHeight(titleBar, 0, Style.Unit.PX, 40, Style.Unit.PX);
layout.setWidgetTopHeight(scrollPanel, 40, Style.Unit.PX, 100, Style.Unit.PCT);
} */
layout.setWidgetTopHeight(titleBar, 0, Style.Unit.PX, 40, Style.Unit.PX);
layout.setWidgetTopHeight(scrollPanel, 40, Style.Unit.PX, 100, Style.Unit.PCT);
return layout;
}
/**
* Creates a default widgets without the top most tab.
* It can be easily embedded into a TablayoutPanel for instance.
*
* @return
*/
public Widget createEmbeddableWidget() {
LayoutPanel layout = new LayoutPanel();
layout.setStyleName("fill-layout");
VerticalPanel panel = new VerticalPanel();
panel.setStyleName("rhs-content-panel");
ScrollPanel scrollPanel = new ScrollPanel(panel);
layout.add(scrollPanel);
entityEditor = makeEntityEditor();
entityEditor.setDescription(description);
entityEditor.setTools(createToolStrip());
Widget editorWidget = entityEditor.setIncludeTools(false).asWidget();
panel.add(editorWidget);
/*ToolStrip tools = createToolStrip();
if(tools.hasButtons())
{
layout.add(tools);
layout.setWidgetTopHeight(tools, 0, Style.Unit.PX, 28, Style.Unit.PX);
layout.setWidgetTopHeight(scrollPanel, 28, Style.Unit.PX, 100, Style.Unit.PCT);
}
else
{
layout.setWidgetTopHeight(scrollPanel, 0, Style.Unit.PX, 100, Style.Unit.PCT);
} */
layout.setWidgetTopHeight(scrollPanel, 0, Style.Unit.PX, 100, Style.Unit.PCT);
return layout;
}
protected ToolStrip createToolStrip() {
return entityEditor.createTools();
}
/**
* Create the EntityEditor with the following pieces:
* - A title obtained from getPluralEntityName()
* - A table obtained from makeEntityTable()
* - An AddEntityWindow with a form from makeAddEntityForm()
* - An EntityDetails with a form from makeEditEntityDetailsForm()
*
* @return The EntityEditor
*/
protected EntityEditor<T> makeEntityEditor() {
entityDetails = new EntityDetails<T>(
this,
getEntityDisplayName(),
makeEditEntityDetailsForm(),
address,
hideButtons
);
EntityPopupWindow<T> window = makeAddEntityPopup();
DefaultCellTable<T> table = makeEntityTable();
return new EntityEditor<T>(this, getEntityDisplayName(), window, table, entityDetails, hideButtons);
}
protected EntityPopupWindow<T> makeAddEntityPopup() {
String title = Console.CONSTANTS.common_label_add() + " " + getEntityDisplayName();
return new AddEntityWindow<T>(
title,
makeAddEntityForm(),
getAddress(),
getEntityBridge()
);
}
/**
* Creates an details for for the Entity. This method will add all the Entity's Attributes to
* the form in a 2-column format. If you desire a different layout or you want to exclude
* some attributes, you should override this method.
*
* @return The details form.
*/
protected FormAdapter<T> makeEditEntityDetailsForm() {
FormAdapter<T> formAdapter = null;
if (getFormMetaData().hasTabs()) {
TabbedFormLayoutPanel tabbedPanel = new TabbedFormLayoutPanel(beanType, getFormMetaData(), hideButtons, this);
tabbedPanel.setAdditionalViews(
provideAdditionalTabs(beanType, getFormMetaData(), this)
);
formAdapter = tabbedPanel;
} else {
formAdapter = makeSimpleForm();
}
return formAdapter;
}
/**
* In case a tabbed form layout is used, implementations
* can provide additional custom tabs by overriding this method.
*
* @param beanType
* @param formMetaData
* @param presenter
*/
protected List<SingleEntityView<T>> provideAdditionalTabs(
Class<?> beanType, FormMetaData formMetaData,
FrameworkPresenter presenter)
{
// nada by default
return Collections.EMPTY_LIST;
}
private FormAdapter<T> makeSimpleForm() {
Form<T> form = new Form(beanType);
form.setNumColumns(2);
FormMetaData attributes = getFormMetaData();
// add base items to form
FormItem[][] items = new FormItem[attributes.getBaseAttributes().size()][];
int i=0;
for (PropertyBinding attrib : attributes.getBaseAttributes()) {
items[i++] = attrib.getFormItemForEdit(this);
}
form.setFields(items);
// add grouped items to form
for (String subgroup : attributes.getGroupNames()) {
FormItem[][] groupItems = new FormItem[attributes.getGroupedAttribtes(subgroup).size()][];
int j=0;
for (PropertyBinding attrib : attributes.getGroupedAttribtes(subgroup)) {
groupItems[j++] = attrib.getFormItemForEdit(this);
}
form.setFieldsInGroup(subgroup, groupItems);
}
return form;
}
/**
* Called when the user requests details to be edited.
* @param isEnabled
*/
@Override
public void setEditingEnabled(boolean isEnabled) {
entityEditor.setEditingEnabled(isEnabled);
}
@Override
public void initialLoad() {
getEntityBridge().loadEntities(null);
}
public void initialLoad(ModelNode baseAddress) {
getEntityBridge().loadEntities(null, baseAddress);
}
/**
* Called whenever there is a change to any Entity
*/
@Override
public void refresh() {
EntityToDmrBridge entityBridge = getEntityBridge();
T lastEntityEdited = null;
if (entityBridge.getNameOfLastEdited() != null) {
// Look up by name.
lastEntityEdited = (T)entityBridge.findEntity(entityBridge.getNameOfLastEdited());
}
entityEditor.updateEntityList(entityBridge.getEntityList(), lastEntityEdited);
}
@Override
public void itemAction(Action action, ObservableFormItem item) {
}
}