Project

General

Profile

Download (19.9 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
 * Copyright (C) 2015 EDIT
4
 * European Distributed Institute of Taxonomy
5
 * http://www.e-taxonomy.eu
6
 *
7
 * The contents of this file are subject to the Mozilla Public License Version 1.1
8
 * See LICENSE.TXT at the top of this package for the full license terms.
9
 */
10
package eu.etaxonomy.cdm.vaadin.component;
11

    
12
import java.sql.SQLException;
13
import java.util.Arrays;
14
import java.util.Map;
15
import java.util.UUID;
16

    
17
import com.vaadin.annotations.AutoGenerated;
18
import com.vaadin.data.Container.Hierarchical;
19
import com.vaadin.data.Validator.EmptyValueException;
20
import com.vaadin.data.util.sqlcontainer.RowId;
21
import com.vaadin.event.Transferable;
22
import com.vaadin.event.dd.DragAndDropEvent;
23
import com.vaadin.event.dd.DropHandler;
24
import com.vaadin.event.dd.acceptcriteria.AcceptAll;
25
import com.vaadin.event.dd.acceptcriteria.AcceptCriterion;
26
import com.vaadin.server.Page;
27
import com.vaadin.ui.Alignment;
28
import com.vaadin.ui.Button;
29
import com.vaadin.ui.Button.ClickEvent;
30
import com.vaadin.ui.Button.ClickListener;
31
import com.vaadin.ui.ComboBox;
32
import com.vaadin.ui.CustomComponent;
33
import com.vaadin.ui.DragAndDropWrapper;
34
import com.vaadin.ui.HorizontalLayout;
35
import com.vaadin.ui.Label;
36
import com.vaadin.ui.Notification;
37
import com.vaadin.ui.Notification.Type;
38
import com.vaadin.ui.TextField;
39
import com.vaadin.ui.TreeTable;
40
import com.vaadin.ui.UI;
41
import com.vaadin.ui.VerticalLayout;
42
import com.vaadin.ui.Window;
43

    
44
import eu.etaxonomy.cdm.vaadin.container.IdUuidName;
45
import eu.etaxonomy.cdm.vaadin.container.LeafNodeTaxonContainer;
46
import eu.etaxonomy.cdm.vaadin.presenter.EditConceptRelationshipPresenter;
47
import eu.etaxonomy.cdm.vaadin.session.BasicEvent;
48
import eu.etaxonomy.cdm.vaadin.session.CdmChangeEvent;
49
import eu.etaxonomy.cdm.vaadin.session.CdmChangeEvent.Action;
50
import eu.etaxonomy.cdm.vaadin.util.CdmVaadinOperation;
51
import eu.etaxonomy.cdm.vaadin.util.CdmVaadinSessionUtilities;
52
import eu.etaxonomy.cdm.vaadin.util.CdmVaadinUtilities;
53

    
54
/**
55
 * @author cmathew
56
 * @date 13 Apr 2015
57
 *
58
 */
59
public class EditConceptRelationshipComposite extends CustomComponent {
60

    
61
    /*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */
62

    
63
    @AutoGenerated
64
    private VerticalLayout mainLayout;
65
    @AutoGenerated
66
    private CdmProgressComponent cdmProgressComponent;
67
    @AutoGenerated
68
    private HorizontalLayout saveCancelHLayout;
69
    @AutoGenerated
70
    private Button cancelButton;
71
    @AutoGenerated
72
    private Button saveButton;
73
    @AutoGenerated
74
    private HorizontalLayout horizontalLayout;
75
    @AutoGenerated
76
    private VerticalLayout typeVLayout;
77
    @AutoGenerated
78
    private ComboBox conceptRComboBox;
79
    @AutoGenerated
80
    private Label typeLabel;
81
    @AutoGenerated
82
    private VerticalLayout fromTaxonVLayout;
83
    @AutoGenerated
84
    private Label fromTaxonValue;
85
    @AutoGenerated
86
    private Label fromTaxonLabel;
87
    private VerticalLayout toTaxonVLayout;
88
    private Label toTaxonLabeL;
89
    private TextField toTaxonTextField;
90
    private DragAndDropWrapper toTaxonLayoutWrapper;
91

    
92
    private final EditConceptRelationshipPresenter presenter;
93

    
94
    private IdUuidName fromTaxonIun, taxonRTypeIun, toTaxonIun;
95
    private UUID relUuid;
96
    private Window window;
97

    
98
    private final static String CHOOSE_TREL_TYPE = "Choose Type ...";
99
    private final static String DRAG_TAXON_HINT = "Drag Taxon here ...";
100

    
101

    
102
    private Action action;
103

    
104
    public EditConceptRelationshipComposite(IdUuidName fromTaxonIdUuidName,
105
            IdUuidName taxonRTypeIdUuidName,
106
            IdUuidName toTaxonIdUuidName,
107
            Action action) {
108
        this();
109
        init(fromTaxonIdUuidName, taxonRTypeIdUuidName, toTaxonIdUuidName, action);
110

    
111
    }
112

    
113
    public EditConceptRelationshipComposite(IdUuidName fromTaxonIun,
114
            UUID relUuid,
115
            Action action) {
116
        this();
117
        this.relUuid = relUuid;
118
        Map<String, IdUuidName> map = presenter.getRelTypeToTaxonIunMap(fromTaxonIun.getUuid(), relUuid);
119
        taxonRTypeIun = map.get(EditConceptRelationshipPresenter.REL_TYPE_KEY);
120
        toTaxonIun = map.get(EditConceptRelationshipPresenter.TO_TAXON_KEY);
121
        init(fromTaxonIun, taxonRTypeIun, toTaxonIun, action);
122

    
123
    }
124
    /**
125
     * The constructor should first build the main layout, set the
126
     * composition root and then do any custom initialization.
127
     *
128
     * The constructor will not be automatically regenerated by the
129
     * visual editor.
130
     */
131
    public EditConceptRelationshipComposite() {
132

    
133
        buildMainLayout();
134
        buildToTaxon();
135
        setCompositionRoot(mainLayout);
136

    
137
        this.presenter = new EditConceptRelationshipPresenter();
138
        addUIListeners();
139

    
140
    }
141

    
142
    public void init(IdUuidName fromTaxonIdUuidName,
143
            IdUuidName taxonRTypeIdUuidName,
144
            IdUuidName toTaxonIdUuidName,
145
            Action action) {
146

    
147
        this.fromTaxonIun = fromTaxonIdUuidName;
148
        this.taxonRTypeIun = taxonRTypeIdUuidName;
149
        this.toTaxonIun = toTaxonIdUuidName;
150
        this.action = action;
151

    
152
        initFromTaxonLabel();
153
        initConceptRComboBox();
154
        initToTaxon();
155
    }
156

    
157
    public void setWindow(Window window) {
158
        this.window = window;
159
    }
160
    private void initFromTaxonLabel() {
161
        fromTaxonValue.setValue(fromTaxonIun.getName());
162
    }
163

    
164
    private void initConceptRComboBox() {
165
        conceptRComboBox.setImmediate(true);
166
        conceptRComboBox.setItemCaptionPropertyId("titleCache");
167
        try {
168
            conceptRComboBox.setContainerDataSource(presenter.loadTaxonRelationshipTypeContainer());
169
        } catch (SQLException e) {
170
            // TODO Auto-generated catch block
171
            e.printStackTrace();
172
        }
173
        if(taxonRTypeIun == null) {
174
            conceptRComboBox.setInputPrompt(CHOOSE_TREL_TYPE);
175
        } else {
176
            conceptRComboBox.setValue(new RowId(taxonRTypeIun.getId()));
177
        }
178

    
179
        if(action == Action.Delete) {
180
            conceptRComboBox.setReadOnly(true);
181
            saveButton.setCaption("ok");
182
        }
183
    }
184

    
185
    private void buildToTaxon() {
186
        toTaxonVLayout = buildToTaxonVLayout();
187
        toTaxonLayoutWrapper = new DragAndDropWrapper(toTaxonVLayout);
188
        toTaxonLayoutWrapper.setImmediate(false);
189
        toTaxonLayoutWrapper.setWidth("-1px");
190
        toTaxonLayoutWrapper.setHeight("-1px");
191
        toTaxonTextField.setReadOnly(true);
192

    
193
        toTaxonLayoutWrapper.setDropHandler(new DropHandler() {
194

    
195
            @Override
196
            public AcceptCriterion getAcceptCriterion() {
197
                return AcceptAll.get();
198
            }
199

    
200
            @Override
201
            public void drop(DragAndDropEvent event) {
202
                // Wrapper for the object that is dragged
203
                Transferable t = event.getTransferable();
204

    
205
                // Make sure the drag source is a status composite tree table
206
                if (action != Action.Delete && t.getSourceComponent() instanceof TreeTable) {
207
                    TreeTable table = (TreeTable)t.getSourceComponent();
208
                    Hierarchical containerDataSource = table.getContainerDataSource();
209
                    if(containerDataSource instanceof LeafNodeTaxonContainer) {
210
                        LeafNodeTaxonContainer lntc = (LeafNodeTaxonContainer)containerDataSource;
211
                        Object sourceItemId = t.getData("itemId");
212
                        String toName = (String)lntc.getProperty(sourceItemId, LeafNodeTaxonContainer.NAME_ID).getValue();
213
                        toTaxonIun = new IdUuidName(sourceItemId,
214
                                lntc.getUuid(sourceItemId),
215
                                toName);
216
                        toTaxonTextField.setReadOnly(false);
217
                        toTaxonTextField.setValue(toName);
218
                        toTaxonTextField.setReadOnly(true);
219
                    }
220
                }
221
            }
222
        });
223
        horizontalLayout.addComponent(toTaxonLayoutWrapper);
224
        horizontalLayout.setComponentAlignment(toTaxonLayoutWrapper, new Alignment(48));
225

    
226
    }
227

    
228
    private void initToTaxon() {
229
        toTaxonTextField.setReadOnly(false);
230
        if(toTaxonIun == null) {
231
            toTaxonTextField.setValue(DRAG_TAXON_HINT);
232
        } else {
233
            toTaxonTextField.setValue(toTaxonIun.getName());
234
        }
235
        toTaxonTextField.setReadOnly(true);
236
    }
237

    
238
    private void addUIListeners() {
239
        addSaveButtonListener();
240
        addCancelButtonListener();
241

    
242
    }
243

    
244
    private void addSaveButtonListener() {
245
        saveButton.addClickListener(new ClickListener() {
246

    
247
            @Override
248
            public void buttonClick(ClickEvent event) {
249

    
250

    
251
                try {
252
                    conceptRComboBox.validate();
253
                    toTaxonTextField.validate();
254
                    if(toTaxonIun == null) {
255
                        // FIXME: Not efficient - figure out a way
256
                        // of validation including the null check
257
                        throw new EmptyValueException("");
258
                    }
259
                } catch (EmptyValueException e) {
260
                    Notification notification = new Notification("Invalid input", "Neither Relationship Type nor To Taxon can be empty", Type.WARNING_MESSAGE);
261
                    notification.setDelayMsec(2000);
262
                    notification.show(Page.getCurrent());
263
                    return;
264
                }
265

    
266
                CdmVaadinUtilities.setEnabled(mainLayout, false, null);
267

    
268
                try {
269
                    CdmVaadinUtilities.exec(new CdmVaadinOperation(200, cdmProgressComponent) {
270
                        @Override
271
                        public boolean execute() {
272
                            UUID relTypeUuid = presenter.getTaxonRTypeContainer().getUuid(conceptRComboBox.getValue());
273
                            switch(action) {
274
                            case Create:
275
                                setProgress("Saving New Concept Relationship");
276
                                presenter.createRelationship(fromTaxonIun.getUuid(), relTypeUuid, toTaxonIun.getUuid());
277
                                registerDelayedEvent(new CdmChangeEvent(Action.Create, Arrays.asList((Object)relTypeUuid), EditConceptRelationshipComposite.class));
278
                                break;
279
                            case Update:
280
                                setProgress("Update Concept Relationship");
281
                                presenter.updateRelationship(fromTaxonIun.getUuid(), relUuid, relTypeUuid, toTaxonIun.getUuid());
282
                                registerDelayedEvent(new CdmChangeEvent(Action.Update, Arrays.asList((Object)relTypeUuid), EditConceptRelationshipComposite.class));
283
                                break;
284
                            case Delete:
285
                                setProgress("Deleting Concept Relationship");
286
                                presenter.deleteRelationship(fromTaxonIun.getUuid(), relUuid);
287
                                registerDelayedEvent(new CdmChangeEvent(Action.Delete, Arrays.asList((Object)relTypeUuid), EditConceptRelationshipComposite.class));
288
                                break;
289
                            default:
290

    
291
                            }
292
                            return true;
293
                        }
294

    
295
                        @Override
296
                        public void postOpUIUpdate(boolean success) {
297
                            if(success) {
298
                                if(window != null) {
299
                                    UI.getCurrent().removeWindow(window);
300
                                }
301
                            } else {
302
                                CdmVaadinUtilities.setEnabled(mainLayout, true, null);
303
                            }
304
                        }
305
                    });
306
                } finally {
307
                    CdmVaadinSessionUtilities.getCurrentBasicEventService()
308
                    .fireBasicEvent(new BasicEvent(ConceptRelationshipComposite.UPDATE_END_ID, EditConceptRelationshipComposite.class), false);
309
                }
310
            }
311
        });
312
    }
313

    
314
    private void addCancelButtonListener() {
315
        cancelButton.addClickListener(new ClickListener() {
316

    
317
            @Override
318
            public void buttonClick(ClickEvent event) {
319
                if(window != null) {
320
                    UI.getCurrent().removeWindow(window);
321
                }
322
                CdmVaadinSessionUtilities.getCurrentBasicEventService()
323
                .fireBasicEvent(new BasicEvent(ConceptRelationshipComposite.UPDATE_END_ID, EditConceptRelationshipComposite.class), true);
324
            }
325
        });
326
    }
327

    
328

    
329
    private static void showInDialog(String windowTitle,
330
            EditConceptRelationshipComposite ecrc) {
331
        Window dialog = new Window(windowTitle);
332
        dialog.setModal(false);
333
        dialog.setClosable(false);
334
        dialog.setResizable(false);
335
        UI.getCurrent().addWindow(dialog);
336
        ecrc.setWindow(dialog);
337
        dialog.setContent(ecrc);
338
    }
339

    
340
    public static void showInDialog(String windowTitle,
341
            IdUuidName fromTaxonIun,
342
            IdUuidName taxonRTypeIun,
343
            IdUuidName toTaxonIun,
344
            Action action) {
345
        EditConceptRelationshipComposite ecrc = new EditConceptRelationshipComposite(fromTaxonIun, taxonRTypeIun, toTaxonIun,action);
346
        showInDialog(windowTitle, ecrc);
347
    }
348

    
349
    public static void showInDialog(String windowTitle,
350
            IdUuidName fromTaxonIun,
351
            UUID relUuid,
352
            Action action) {
353
        EditConceptRelationshipComposite ecrc = new EditConceptRelationshipComposite(fromTaxonIun, relUuid,action);
354
        showInDialog(windowTitle, ecrc);
355
    }
356

    
357
    private VerticalLayout buildToTaxonVLayout() {
358
        // common part: create layout
359
        toTaxonVLayout = new VerticalLayout();
360
        toTaxonVLayout.setImmediate(false);
361
        toTaxonVLayout.setWidth("-1px");
362
        toTaxonVLayout.setHeight("-1px");
363
        toTaxonVLayout.setMargin(false);
364
        toTaxonVLayout.setSpacing(true);
365

    
366
        // toTaxonLabeL
367
        toTaxonLabeL = new Label();
368
        toTaxonLabeL.setImmediate(false);
369
        toTaxonLabeL.setWidth("-1px");
370
        toTaxonLabeL.setHeight("-1px");
371
        toTaxonLabeL.setValue("To Taxon");
372
        toTaxonVLayout.addComponent(toTaxonLabeL);
373
        toTaxonVLayout.setComponentAlignment(toTaxonLabeL, new Alignment(48));
374

    
375
        // toTaxonTextField
376
        toTaxonTextField = new TextField();
377
        toTaxonTextField.setImmediate(false);
378
        toTaxonTextField.setWidth("-1px");
379
        toTaxonTextField.setHeight("-1px");
380
        toTaxonTextField.setInvalidAllowed(false);
381
        toTaxonTextField.setRequired(true);
382
        toTaxonTextField.setInputPrompt("Drag Taxon here ...");
383
        toTaxonVLayout.addComponent(toTaxonTextField);
384
        toTaxonVLayout.setComponentAlignment(toTaxonTextField, new Alignment(48));
385

    
386
        return toTaxonVLayout;
387
    }
388

    
389
    @AutoGenerated
390
    private VerticalLayout buildMainLayout() {
391
        // common part: create layout
392
        mainLayout = new VerticalLayout();
393
        mainLayout.setImmediate(false);
394
        mainLayout.setWidth("602px");
395
        mainLayout.setHeight("170px");
396
        mainLayout.setMargin(false);
397
        mainLayout.setSpacing(true);
398

    
399
        // top-level component properties
400
        setWidth("602px");
401
        setHeight("170px");
402

    
403
        // horizontalLayout
404
        horizontalLayout = buildHorizontalLayout();
405
        mainLayout.addComponent(horizontalLayout);
406

    
407
        // saveCancelHLayout
408
        saveCancelHLayout = buildSaveCancelHLayout();
409
        mainLayout.addComponent(saveCancelHLayout);
410
        mainLayout.setComponentAlignment(saveCancelHLayout, new Alignment(48));
411

    
412
        // cdmProgressComponent
413
        cdmProgressComponent = new CdmProgressComponent();
414
        cdmProgressComponent.setImmediate(false);
415
        cdmProgressComponent.setWidth("-1px");
416
        cdmProgressComponent.setHeight("-1px");
417
        mainLayout.addComponent(cdmProgressComponent);
418
        mainLayout.setComponentAlignment(cdmProgressComponent, new Alignment(48));
419

    
420
        return mainLayout;
421
    }
422

    
423
    @AutoGenerated
424
    private HorizontalLayout buildHorizontalLayout() {
425
        // common part: create layout
426
        horizontalLayout = new HorizontalLayout();
427
        horizontalLayout.setImmediate(false);
428
        horizontalLayout.setWidth("602px");
429
        horizontalLayout.setHeight("-1px");
430
        horizontalLayout.setMargin(true);
431
        horizontalLayout.setSpacing(true);
432

    
433
        // fromTaxonVLayout
434
        fromTaxonVLayout = buildFromTaxonVLayout();
435
        horizontalLayout.addComponent(fromTaxonVLayout);
436
        horizontalLayout.setComponentAlignment(fromTaxonVLayout, new Alignment(48));
437

    
438
        // typeVLayout
439
        typeVLayout = buildTypeVLayout();
440
        horizontalLayout.addComponent(typeVLayout);
441
        horizontalLayout.setExpandRatio(typeVLayout, 1.0f);
442
        horizontalLayout.setComponentAlignment(typeVLayout, new Alignment(48));
443

    
444
        return horizontalLayout;
445
    }
446

    
447
    @AutoGenerated
448
    private VerticalLayout buildFromTaxonVLayout() {
449
        // common part: create layout
450
        fromTaxonVLayout = new VerticalLayout();
451
        fromTaxonVLayout.setImmediate(false);
452
        fromTaxonVLayout.setWidth("-1px");
453
        fromTaxonVLayout.setHeight("-1px");
454
        fromTaxonVLayout.setMargin(false);
455
        fromTaxonVLayout.setSpacing(true);
456

    
457
        // fromTaxonLabel
458
        fromTaxonLabel = new Label();
459
        fromTaxonLabel.setImmediate(false);
460
        fromTaxonLabel.setWidth("-1px");
461
        fromTaxonLabel.setHeight("-1px");
462
        fromTaxonLabel.setValue("From Taxon");
463
        fromTaxonVLayout.addComponent(fromTaxonLabel);
464
        fromTaxonVLayout.setComponentAlignment(fromTaxonLabel, new Alignment(48));
465

    
466
        // fromTaxonValue
467
        fromTaxonValue = new Label();
468
        fromTaxonValue.setImmediate(false);
469
        fromTaxonValue.setWidth("-1px");
470
        fromTaxonValue.setHeight("-1px");
471
        fromTaxonValue.setValue("Taxon Name");
472
        fromTaxonVLayout.addComponent(fromTaxonValue);
473
        fromTaxonVLayout.setComponentAlignment(fromTaxonValue, new Alignment(48));
474

    
475
        return fromTaxonVLayout;
476
    }
477

    
478
    @AutoGenerated
479
    private VerticalLayout buildTypeVLayout() {
480
        // common part: create layout
481
        typeVLayout = new VerticalLayout();
482
        typeVLayout.setImmediate(false);
483
        typeVLayout.setWidth("-1px");
484
        typeVLayout.setHeight("-1px");
485
        typeVLayout.setMargin(false);
486
        typeVLayout.setSpacing(true);
487

    
488
        // typeLabel
489
        typeLabel = new Label();
490
        typeLabel.setImmediate(false);
491
        typeLabel.setWidth("-1px");
492
        typeLabel.setHeight("-1px");
493
        typeLabel.setValue("Type");
494
        typeVLayout.addComponent(typeLabel);
495
        typeVLayout.setComponentAlignment(typeLabel, new Alignment(48));
496

    
497
        // conceptRComboBox
498
        conceptRComboBox = new ComboBox();
499
        conceptRComboBox.setImmediate(false);
500
        conceptRComboBox.setWidth("-1px");
501
        conceptRComboBox.setHeight("-1px");
502
        conceptRComboBox.setRequired(true);
503
        typeVLayout.addComponent(conceptRComboBox);
504
        typeVLayout.setComponentAlignment(conceptRComboBox, new Alignment(48));
505

    
506
        return typeVLayout;
507
    }
508

    
509
    @AutoGenerated
510
    private HorizontalLayout buildSaveCancelHLayout() {
511
        // common part: create layout
512
        saveCancelHLayout = new HorizontalLayout();
513
        saveCancelHLayout.setImmediate(false);
514
        saveCancelHLayout.setWidth("-1px");
515
        saveCancelHLayout.setHeight("-1px");
516
        saveCancelHLayout.setMargin(false);
517
        saveCancelHLayout.setSpacing(true);
518

    
519
        // saveButton
520
        saveButton = new Button();
521
        saveButton.setCaption("save");
522
        saveButton.setImmediate(true);
523
        saveButton.setWidth("-1px");
524
        saveButton.setHeight("-1px");
525
        saveCancelHLayout.addComponent(saveButton);
526

    
527
        // cancelButton
528
        cancelButton = new Button();
529
        cancelButton.setCaption("cancel");
530
        cancelButton.setImmediate(true);
531
        cancelButton.setWidth("-1px");
532
        cancelButton.setHeight("-1px");
533
        saveCancelHLayout.addComponent(cancelButton);
534

    
535
        return saveCancelHLayout;
536
    }
537

    
538
}
(3-3/6)