1
|
/**
|
2
|
* Copyright (C) 2015 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.cdm.vaadin.component.taxon;
|
10
|
|
11
|
import java.util.Arrays;
|
12
|
import java.util.UUID;
|
13
|
|
14
|
import org.json.JSONException;
|
15
|
|
16
|
import com.vaadin.annotations.AutoGenerated;
|
17
|
import com.vaadin.ui.Alignment;
|
18
|
import com.vaadin.ui.Button;
|
19
|
import com.vaadin.ui.Button.ClickEvent;
|
20
|
import com.vaadin.ui.Button.ClickListener;
|
21
|
import com.vaadin.ui.CustomComponent;
|
22
|
import com.vaadin.ui.HorizontalLayout;
|
23
|
import com.vaadin.ui.Notification;
|
24
|
import com.vaadin.ui.Notification.Type;
|
25
|
import com.vaadin.ui.VerticalLayout;
|
26
|
|
27
|
import eu.etaxonomy.cdm.vaadin.container.IdUuidName;
|
28
|
import eu.etaxonomy.cdm.vaadin.jscomponent.D3ConceptRelationshipTree;
|
29
|
import eu.etaxonomy.cdm.vaadin.jscomponent.D3ConceptRelationshipTree.Direction;
|
30
|
import eu.etaxonomy.cdm.vaadin.session.BasicEvent;
|
31
|
import eu.etaxonomy.cdm.vaadin.session.CdmChangeEvent;
|
32
|
import eu.etaxonomy.cdm.vaadin.session.CdmChangeEvent.Action;
|
33
|
import eu.etaxonomy.cdm.vaadin.session.IBasicEventListener;
|
34
|
import eu.etaxonomy.cdm.vaadin.session.ICdmChangeListener;
|
35
|
import eu.etaxonomy.cdm.vaadin.session.ISelectionListener;
|
36
|
import eu.etaxonomy.cdm.vaadin.session.SelectionEvent;
|
37
|
import eu.etaxonomy.cdm.vaadin.util.CdmVaadinSessionUtilities;
|
38
|
import eu.etaxonomy.cdm.vaadin.util.CdmVaadinUtilities;
|
39
|
import eu.etaxonomy.cdm.vaadin.view.ConceptRelationshipView;
|
40
|
|
41
|
/**
|
42
|
* @author cmathew
|
43
|
* @date 9 Apr 2015
|
44
|
*
|
45
|
*/
|
46
|
public class ConceptRelationshipComposite extends CustomComponent implements ISelectionListener, ICdmChangeListener, IBasicEventListener {
|
47
|
|
48
|
/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */
|
49
|
|
50
|
@AutoGenerated
|
51
|
private VerticalLayout mainLayout;
|
52
|
@AutoGenerated
|
53
|
private D3ConceptRelationshipTree d3ConceptRelationShipTree;
|
54
|
@AutoGenerated
|
55
|
private HorizontalLayout updateHorizontalLayout;
|
56
|
@AutoGenerated
|
57
|
private Button deleteButton;
|
58
|
@AutoGenerated
|
59
|
private Button editButton;
|
60
|
@AutoGenerated
|
61
|
private Button newButton;
|
62
|
private final IConceptRelationshipComponentListener listener;
|
63
|
|
64
|
private IdUuidName fromTaxonIun;
|
65
|
private UUID selectedTaxonRelUuid;
|
66
|
|
67
|
private ConceptRelationshipView view;
|
68
|
|
69
|
public static final String CREATE_NEW_CR_TITLE = "Create New Concept Relationship";
|
70
|
public static final String EDIT_CR_TITLE = "Edit Concept Relationship";
|
71
|
public static final String DELETE_CR_TITLE = "Delete Concept Relationship";
|
72
|
|
73
|
public static final String UPDATE_START_ID = "cr-update-start";
|
74
|
public static final String UPDATE_END_ID = "cr-update-end";
|
75
|
|
76
|
/**
|
77
|
* The constructor should first build the main layout, set the
|
78
|
* composition root and then do any custom initialization.
|
79
|
*
|
80
|
* The constructor will not be automatically regenerated by the
|
81
|
* visual editor.
|
82
|
*/
|
83
|
public ConceptRelationshipComposite() {
|
84
|
buildMainLayout();
|
85
|
setCompositionRoot(mainLayout);
|
86
|
|
87
|
CdmVaadinSessionUtilities.getCurrentSelectionService().register(this);
|
88
|
CdmVaadinSessionUtilities.getCurrentCdmDataChangeService().register(this);
|
89
|
CdmVaadinSessionUtilities.getCurrentBasicEventService().register(this);
|
90
|
listener = new ConceptRelationshipPresenter(d3ConceptRelationShipTree);
|
91
|
|
92
|
addUIListeners();
|
93
|
init();
|
94
|
}
|
95
|
|
96
|
public void setView(ConceptRelationshipView view) {
|
97
|
this.view = view;
|
98
|
}
|
99
|
|
100
|
private void init() {
|
101
|
enableControls(false);
|
102
|
initD3ConceptRelationShipTree();
|
103
|
}
|
104
|
|
105
|
private void initD3ConceptRelationShipTree() {
|
106
|
d3ConceptRelationShipTree.setImmediate(true);
|
107
|
d3ConceptRelationShipTree.setConceptRelComposite(this);
|
108
|
}
|
109
|
|
110
|
private void addUIListeners() {
|
111
|
addNewButtonListener();
|
112
|
addEditButtonListener();
|
113
|
addDeleteButtonListener();
|
114
|
}
|
115
|
|
116
|
private void addNewButtonListener() {
|
117
|
newButton.addClickListener(new ClickListener() {
|
118
|
|
119
|
@Override
|
120
|
public void buttonClick(ClickEvent event) {
|
121
|
EditConceptRelationshipComposite.showInDialog(CREATE_NEW_CR_TITLE,
|
122
|
fromTaxonIun,
|
123
|
null,
|
124
|
null,
|
125
|
Action.Create,
|
126
|
view.getDirection());
|
127
|
setSelectedTaxonRelUuid(null);
|
128
|
}
|
129
|
});
|
130
|
}
|
131
|
|
132
|
private void addEditButtonListener() {
|
133
|
editButton.addClickListener(new ClickListener() {
|
134
|
|
135
|
@Override
|
136
|
public void buttonClick(ClickEvent event) {
|
137
|
EditConceptRelationshipComposite.showInDialog(EDIT_CR_TITLE,
|
138
|
fromTaxonIun,
|
139
|
selectedTaxonRelUuid,
|
140
|
Action.Update,
|
141
|
view.getDirection());
|
142
|
setSelectedTaxonRelUuid(null);
|
143
|
}
|
144
|
});
|
145
|
}
|
146
|
|
147
|
private void addDeleteButtonListener() {
|
148
|
deleteButton.addClickListener(new ClickListener() {
|
149
|
|
150
|
@Override
|
151
|
public void buttonClick(ClickEvent event) {
|
152
|
EditConceptRelationshipComposite.showInDialog(DELETE_CR_TITLE,
|
153
|
fromTaxonIun,
|
154
|
selectedTaxonRelUuid,
|
155
|
Action.Delete,
|
156
|
view.getDirection());
|
157
|
setSelectedTaxonRelUuid(null);
|
158
|
}
|
159
|
});
|
160
|
}
|
161
|
|
162
|
|
163
|
private void refreshRelationshipView(Direction direction) {
|
164
|
if(fromTaxonIun != null) {
|
165
|
try {
|
166
|
listener.refreshRelationshipView(fromTaxonIun, direction);
|
167
|
} catch (JSONException e) {
|
168
|
Notification.show("Error generating concept relation JSON", e.getMessage(), Type.WARNING_MESSAGE);
|
169
|
}
|
170
|
}
|
171
|
}
|
172
|
|
173
|
public void setSelectedTaxonRelUuid(UUID selectedTaxonRelUuid) {
|
174
|
this.selectedTaxonRelUuid = selectedTaxonRelUuid;
|
175
|
updateControls();
|
176
|
}
|
177
|
|
178
|
private void enableControls(boolean enabled) {
|
179
|
CdmVaadinUtilities.setEnabled(this, enabled, Arrays.asList(d3ConceptRelationShipTree));
|
180
|
}
|
181
|
|
182
|
private void updateControls() {
|
183
|
enableControls(false);
|
184
|
if(fromTaxonIun != null) {
|
185
|
newButton.setEnabled(true);
|
186
|
}
|
187
|
if(selectedTaxonRelUuid != null) {
|
188
|
editButton.setEnabled(true);
|
189
|
deleteButton.setEnabled(true);
|
190
|
}
|
191
|
}
|
192
|
|
193
|
/* (non-Javadoc)
|
194
|
* @see eu.etaxonomy.cdm.vaadin.session.ISelectionListener#onSelect(eu.etaxonomy.cdm.vaadin.session.SelectionEvent)
|
195
|
*/
|
196
|
@Override
|
197
|
public void onSelect(SelectionEvent event) {
|
198
|
if(event.getSourceType().equals(StatusComposite.class)) {
|
199
|
fromTaxonIun = (IdUuidName)event.getSelectedObjects().get(0);
|
200
|
if(fromTaxonIun != null) {
|
201
|
view.setPrimaryStatusCompositeUuid((StatusComposite)event.getSource());
|
202
|
refreshRelationshipView(view.getDirection());
|
203
|
setSelectedTaxonRelUuid(null);
|
204
|
} else {
|
205
|
listener.clearRelationshipView();
|
206
|
}
|
207
|
updateControls();
|
208
|
|
209
|
}
|
210
|
}
|
211
|
|
212
|
|
213
|
|
214
|
/* (non-Javadoc)
|
215
|
* @see eu.etaxonomy.cdm.vaadin.session.ICdmChangeListener#onCreate(eu.etaxonomy.cdm.vaadin.session.CdmChangeEvent)
|
216
|
*/
|
217
|
@Override
|
218
|
public void onCreate(CdmChangeEvent event) {
|
219
|
if(event.getSourceType().equals(EditConceptRelationshipComposite.class)) {
|
220
|
setSelectedTaxonRelUuid(null);
|
221
|
refreshRelationshipView(view.getDirection());
|
222
|
|
223
|
}
|
224
|
|
225
|
}
|
226
|
|
227
|
|
228
|
/* (non-Javadoc)
|
229
|
* @see eu.etaxonomy.cdm.vaadin.session.ICdmChangeListener#onUpdate(eu.etaxonomy.cdm.vaadin.session.CdmChangeEvent)
|
230
|
*/
|
231
|
@Override
|
232
|
public void onUpdate(CdmChangeEvent event) {
|
233
|
if(event.getSourceType().equals(EditConceptRelationshipComposite.class)) {
|
234
|
setSelectedTaxonRelUuid(null);
|
235
|
refreshRelationshipView(view.getDirection());
|
236
|
}
|
237
|
}
|
238
|
|
239
|
|
240
|
/* (non-Javadoc)
|
241
|
* @see eu.etaxonomy.cdm.vaadin.session.ICdmChangeListener#onDelete(eu.etaxonomy.cdm.vaadin.session.CdmChangeEvent)
|
242
|
*/
|
243
|
@Override
|
244
|
public void onDelete(CdmChangeEvent event) {
|
245
|
if(event.getSourceType().equals(EditConceptRelationshipComposite.class)) {
|
246
|
setSelectedTaxonRelUuid(null);
|
247
|
refreshRelationshipView(view.getDirection());
|
248
|
}
|
249
|
|
250
|
}
|
251
|
|
252
|
|
253
|
/* (non-Javadoc)
|
254
|
* @see eu.etaxonomy.cdm.vaadin.session.IBasicEventListener#onAction(eu.etaxonomy.cdm.vaadin.session.BasicEvent)
|
255
|
*/
|
256
|
@Override
|
257
|
public void onAction(BasicEvent event) {
|
258
|
if(ConceptRelationshipComposite.UPDATE_START_ID.equals(event.getEventId())) {
|
259
|
enableControls(false);
|
260
|
}
|
261
|
if(ConceptRelationshipComposite.UPDATE_END_ID.equals(event.getEventId())) {
|
262
|
updateControls();
|
263
|
}
|
264
|
|
265
|
}
|
266
|
|
267
|
@AutoGenerated
|
268
|
private VerticalLayout buildMainLayout() {
|
269
|
// common part: create layout
|
270
|
mainLayout = new VerticalLayout();
|
271
|
mainLayout.setImmediate(false);
|
272
|
mainLayout.setWidth("100%");
|
273
|
mainLayout.setHeight("100%");
|
274
|
mainLayout.setMargin(true);
|
275
|
mainLayout.setSpacing(true);
|
276
|
|
277
|
// top-level component properties
|
278
|
setWidth("100.0%");
|
279
|
setHeight("100.0%");
|
280
|
|
281
|
// updateHorizontalLayout
|
282
|
updateHorizontalLayout = buildUpdateHorizontalLayout();
|
283
|
mainLayout.addComponent(updateHorizontalLayout);
|
284
|
mainLayout.setComponentAlignment(updateHorizontalLayout, new Alignment(20));
|
285
|
|
286
|
// d3ConceptRelationShipTree
|
287
|
d3ConceptRelationShipTree = new D3ConceptRelationshipTree();
|
288
|
d3ConceptRelationShipTree.setImmediate(false);
|
289
|
d3ConceptRelationShipTree.setWidth("100.0%");
|
290
|
d3ConceptRelationShipTree.setHeight("-1px");
|
291
|
mainLayout.addComponent(d3ConceptRelationShipTree);
|
292
|
mainLayout.setExpandRatio(d3ConceptRelationShipTree, 1.0f);
|
293
|
mainLayout.setComponentAlignment(d3ConceptRelationShipTree, new Alignment(20));
|
294
|
|
295
|
return mainLayout;
|
296
|
}
|
297
|
|
298
|
@AutoGenerated
|
299
|
private HorizontalLayout buildUpdateHorizontalLayout() {
|
300
|
// common part: create layout
|
301
|
updateHorizontalLayout = new HorizontalLayout();
|
302
|
updateHorizontalLayout.setImmediate(false);
|
303
|
updateHorizontalLayout.setWidth("-1px");
|
304
|
updateHorizontalLayout.setHeight("-1px");
|
305
|
updateHorizontalLayout.setMargin(true);
|
306
|
updateHorizontalLayout.setSpacing(true);
|
307
|
|
308
|
// newButton
|
309
|
newButton = new Button();
|
310
|
newButton.setCaption("new");
|
311
|
newButton.setImmediate(true);
|
312
|
newButton.setWidth("-1px");
|
313
|
newButton.setHeight("-1px");
|
314
|
updateHorizontalLayout.addComponent(newButton);
|
315
|
|
316
|
// editButton
|
317
|
editButton = new Button();
|
318
|
editButton.setCaption("edit");
|
319
|
editButton.setImmediate(true);
|
320
|
editButton.setWidth("-1px");
|
321
|
editButton.setHeight("-1px");
|
322
|
updateHorizontalLayout.addComponent(editButton);
|
323
|
|
324
|
// deleteButton
|
325
|
deleteButton = new Button();
|
326
|
deleteButton.setCaption("delete");
|
327
|
deleteButton.setImmediate(true);
|
328
|
deleteButton.setWidth("-1px");
|
329
|
deleteButton.setHeight("-1px");
|
330
|
updateHorizontalLayout.addComponent(deleteButton);
|
331
|
|
332
|
return updateHorizontalLayout;
|
333
|
}
|
334
|
|
335
|
|
336
|
|
337
|
}
|