Project

General

Profile

Download (11.4 KB) Statistics
| Branch: | Tag: | Revision:
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;
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.presenter.ConceptRelationshipPresenter;
31
import eu.etaxonomy.cdm.vaadin.session.BasicEvent;
32
import eu.etaxonomy.cdm.vaadin.session.CdmChangeEvent;
33
import eu.etaxonomy.cdm.vaadin.session.CdmChangeEvent.Action;
34
import eu.etaxonomy.cdm.vaadin.session.IBasicEventListener;
35
import eu.etaxonomy.cdm.vaadin.session.ICdmChangeListener;
36
import eu.etaxonomy.cdm.vaadin.session.ISelectionListener;
37
import eu.etaxonomy.cdm.vaadin.session.SelectionEvent;
38
import eu.etaxonomy.cdm.vaadin.util.CdmVaadinSessionUtilities;
39
import eu.etaxonomy.cdm.vaadin.util.CdmVaadinUtilities;
40
import eu.etaxonomy.cdm.vaadin.view.ConceptRelationshipView;
41
import eu.etaxonomy.cdm.vaadin.view.IConceptRelationshipComponentListener;
42

    
43
/**
44
 * @author cmathew
45
 * @date 9 Apr 2015
46
 *
47
 */
48
public class ConceptRelationshipComposite extends CustomComponent implements ISelectionListener, ICdmChangeListener, IBasicEventListener {
49

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

    
52
    @AutoGenerated
53
    private VerticalLayout mainLayout;
54
    @AutoGenerated
55
    private D3ConceptRelationshipTree d3ConceptRelationShipTree;
56
    @AutoGenerated
57
    private HorizontalLayout updateHorizontalLayout;
58
    @AutoGenerated
59
    private Button deleteButton;
60
    @AutoGenerated
61
    private Button editButton;
62
    @AutoGenerated
63
    private Button newButton;
64
    private final IConceptRelationshipComponentListener listener;
65

    
66
    private IdUuidName fromTaxonIun;
67
    private UUID selectedTaxonRelUuid;
68

    
69
    private ConceptRelationshipView view;
70

    
71
    public static final String CREATE_NEW_CR_TITLE = "Create New Concept Relationship";
72
    public static final String EDIT_CR_TITLE = "Edit Concept Relationship";
73
    public static final String DELETE_CR_TITLE = "Delete Concept Relationship";
74

    
75
    public static final String UPDATE_START_ID = "cr-update-start";
76
    public static final String UPDATE_END_ID = "cr-update-end";
77

    
78
    /**
79
     * The constructor should first build the main layout, set the
80
     * composition root and then do any custom initialization.
81
     *
82
     * The constructor will not be automatically regenerated by the
83
     * visual editor.
84
     */
85
    public ConceptRelationshipComposite() {
86
        buildMainLayout();
87
        setCompositionRoot(mainLayout);
88

    
89
        CdmVaadinSessionUtilities.getCurrentSelectionService().register(this);
90
        CdmVaadinSessionUtilities.getCurrentCdmDataChangeService().register(this);
91
        CdmVaadinSessionUtilities.getCurrentBasicEventService().register(this);
92
        listener = new ConceptRelationshipPresenter(d3ConceptRelationShipTree);
93

    
94
        addUIListeners();
95
        init();
96
    }
97

    
98
    public void setView(ConceptRelationshipView view) {
99
        this.view = view;
100
    }
101

    
102
    private void init() {
103
        enableControls(false);
104
        initD3ConceptRelationShipTree();
105
    }
106

    
107
    private void initD3ConceptRelationShipTree() {
108
        d3ConceptRelationShipTree.setImmediate(true);
109
        d3ConceptRelationShipTree.setConceptRelComposite(this);
110
    }
111

    
112
    private void addUIListeners() {
113
        addNewButtonListener();
114
        addEditButtonListener();
115
        addDeleteButtonListener();
116
    }
117

    
118
    private void addNewButtonListener() {
119
        newButton.addClickListener(new ClickListener() {
120

    
121
            @Override
122
            public void buttonClick(ClickEvent event) {
123
               EditConceptRelationshipComposite.showInDialog(CREATE_NEW_CR_TITLE,
124
                       fromTaxonIun,
125
                       null,
126
                       null,
127
                       Action.Create,
128
                       view.getDirection());
129
               setSelectedTaxonRelUuid(null);
130
            }
131
        });
132
    }
133

    
134
    private void addEditButtonListener() {
135
        editButton.addClickListener(new ClickListener() {
136

    
137
            @Override
138
            public void buttonClick(ClickEvent event) {
139
                EditConceptRelationshipComposite.showInDialog(EDIT_CR_TITLE,
140
                        fromTaxonIun,
141
                        selectedTaxonRelUuid,
142
                        Action.Update,
143
                        view.getDirection());
144
                setSelectedTaxonRelUuid(null);
145
            }
146
        });
147
    }
148

    
149
    private void addDeleteButtonListener() {
150
        deleteButton.addClickListener(new ClickListener() {
151

    
152
            @Override
153
            public void buttonClick(ClickEvent event) {
154
                EditConceptRelationshipComposite.showInDialog(DELETE_CR_TITLE,
155
                        fromTaxonIun,
156
                        selectedTaxonRelUuid,
157
                        Action.Delete,
158
                        view.getDirection());
159
                setSelectedTaxonRelUuid(null);
160
            }
161
        });
162
    }
163

    
164

    
165
    private void refreshRelationshipView(Direction direction) {
166
        if(fromTaxonIun != null) {
167
            try {
168
                listener.refreshRelationshipView(fromTaxonIun, direction);
169
            } catch (JSONException e) {
170
                Notification.show("Error generating concept relation JSON",  e.getMessage(), Type.WARNING_MESSAGE);
171
            }
172
        }
173
    }
174

    
175
    public void setSelectedTaxonRelUuid(UUID selectedTaxonRelUuid) {
176
        this.selectedTaxonRelUuid = selectedTaxonRelUuid;
177
        updateControls();
178
    }
179

    
180
    private void enableControls(boolean enabled) {
181
        CdmVaadinUtilities.setEnabled(this, enabled, Arrays.asList(d3ConceptRelationShipTree));
182
    }
183

    
184
    private void updateControls() {
185
        enableControls(false);
186
        if(fromTaxonIun != null) {
187
            newButton.setEnabled(true);
188
        }
189
        if(selectedTaxonRelUuid != null) {
190
            editButton.setEnabled(true);
191
            deleteButton.setEnabled(true);
192
        }
193
    }
194

    
195
    /* (non-Javadoc)
196
     * @see eu.etaxonomy.cdm.vaadin.session.ISelectionListener#onSelect(eu.etaxonomy.cdm.vaadin.session.SelectionEvent)
197
     */
198
    @Override
199
    public void onSelect(SelectionEvent event) {
200
        if(event.getSourceType().equals(StatusComposite.class)) {
201
            fromTaxonIun = (IdUuidName)event.getSelectedObjects().get(0);
202
            if(fromTaxonIun != null) {
203
                view.setPrimaryStatusCompositeUuid((StatusComposite)event.getSource());
204
                refreshRelationshipView(view.getDirection());
205
                setSelectedTaxonRelUuid(null);
206
            } else {
207
                listener.clearRelationshipView();
208
            }
209
            updateControls();
210

    
211
        }
212
    }
213

    
214

    
215

    
216
    /* (non-Javadoc)
217
     * @see eu.etaxonomy.cdm.vaadin.session.ICdmChangeListener#onCreate(eu.etaxonomy.cdm.vaadin.session.CdmChangeEvent)
218
     */
219
    @Override
220
    public void onCreate(CdmChangeEvent event) {
221
        if(event.getSourceType().equals(EditConceptRelationshipComposite.class)) {
222
            setSelectedTaxonRelUuid(null);
223
            refreshRelationshipView(view.getDirection());
224

    
225
        }
226

    
227
    }
228

    
229

    
230
    /* (non-Javadoc)
231
     * @see eu.etaxonomy.cdm.vaadin.session.ICdmChangeListener#onUpdate(eu.etaxonomy.cdm.vaadin.session.CdmChangeEvent)
232
     */
233
    @Override
234
    public void onUpdate(CdmChangeEvent event) {
235
        if(event.getSourceType().equals(EditConceptRelationshipComposite.class)) {
236
            setSelectedTaxonRelUuid(null);
237
            refreshRelationshipView(view.getDirection());
238
        }
239
    }
240

    
241

    
242
    /* (non-Javadoc)
243
     * @see eu.etaxonomy.cdm.vaadin.session.ICdmChangeListener#onDelete(eu.etaxonomy.cdm.vaadin.session.CdmChangeEvent)
244
     */
245
    @Override
246
    public void onDelete(CdmChangeEvent event) {
247
        if(event.getSourceType().equals(EditConceptRelationshipComposite.class)) {
248
            setSelectedTaxonRelUuid(null);
249
            refreshRelationshipView(view.getDirection());
250
        }
251

    
252
    }
253

    
254

    
255
    /* (non-Javadoc)
256
     * @see eu.etaxonomy.cdm.vaadin.session.IBasicEventListener#onAction(eu.etaxonomy.cdm.vaadin.session.BasicEvent)
257
     */
258
    @Override
259
    public void onAction(BasicEvent event) {
260
        if(ConceptRelationshipComposite.UPDATE_START_ID.equals(event.getEventId())) {
261
            enableControls(false);
262
        }
263
        if(ConceptRelationshipComposite.UPDATE_END_ID.equals(event.getEventId())) {
264
            updateControls();
265
        }
266

    
267
    }
268

    
269
    @AutoGenerated
270
    private VerticalLayout buildMainLayout() {
271
        // common part: create layout
272
        mainLayout = new VerticalLayout();
273
        mainLayout.setImmediate(false);
274
        mainLayout.setWidth("100%");
275
        mainLayout.setHeight("100%");
276
        mainLayout.setMargin(true);
277
        mainLayout.setSpacing(true);
278

    
279
        // top-level component properties
280
        setWidth("100.0%");
281
        setHeight("100.0%");
282

    
283
        // updateHorizontalLayout
284
        updateHorizontalLayout = buildUpdateHorizontalLayout();
285
        mainLayout.addComponent(updateHorizontalLayout);
286
        mainLayout.setComponentAlignment(updateHorizontalLayout, new Alignment(20));
287

    
288
        // d3ConceptRelationShipTree
289
        d3ConceptRelationShipTree = new D3ConceptRelationshipTree();
290
        d3ConceptRelationShipTree.setImmediate(false);
291
        d3ConceptRelationShipTree.setWidth("100.0%");
292
        d3ConceptRelationShipTree.setHeight("-1px");
293
        mainLayout.addComponent(d3ConceptRelationShipTree);
294
        mainLayout.setExpandRatio(d3ConceptRelationShipTree, 1.0f);
295
        mainLayout.setComponentAlignment(d3ConceptRelationShipTree, new Alignment(20));
296

    
297
        return mainLayout;
298
    }
299

    
300
    @AutoGenerated
301
    private HorizontalLayout buildUpdateHorizontalLayout() {
302
        // common part: create layout
303
        updateHorizontalLayout = new HorizontalLayout();
304
        updateHorizontalLayout.setImmediate(false);
305
        updateHorizontalLayout.setWidth("-1px");
306
        updateHorizontalLayout.setHeight("-1px");
307
        updateHorizontalLayout.setMargin(true);
308
        updateHorizontalLayout.setSpacing(true);
309

    
310
        // newButton
311
        newButton = new Button();
312
        newButton.setCaption("new");
313
        newButton.setImmediate(true);
314
        newButton.setWidth("-1px");
315
        newButton.setHeight("-1px");
316
        updateHorizontalLayout.addComponent(newButton);
317

    
318
        // editButton
319
        editButton = new Button();
320
        editButton.setCaption("edit");
321
        editButton.setImmediate(true);
322
        editButton.setWidth("-1px");
323
        editButton.setHeight("-1px");
324
        updateHorizontalLayout.addComponent(editButton);
325

    
326
        // deleteButton
327
        deleteButton = new Button();
328
        deleteButton.setCaption("delete");
329
        deleteButton.setImmediate(true);
330
        deleteButton.setWidth("-1px");
331
        deleteButton.setHeight("-1px");
332
        updateHorizontalLayout.addComponent(deleteButton);
333

    
334
        return updateHorizontalLayout;
335
    }
336

    
337

    
338

    
339
}
(2-2/7)