Project

General

Profile

Download (11.4 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.util.Arrays;
13
import java.util.UUID;
14

    
15
import org.json.JSONException;
16

    
17
import com.vaadin.annotations.AutoGenerated;
18
import com.vaadin.ui.Alignment;
19
import com.vaadin.ui.Button;
20
import com.vaadin.ui.Button.ClickEvent;
21
import com.vaadin.ui.Button.ClickListener;
22
import com.vaadin.ui.CustomComponent;
23
import com.vaadin.ui.HorizontalLayout;
24
import com.vaadin.ui.Notification;
25
import com.vaadin.ui.Notification.Type;
26
import com.vaadin.ui.VerticalLayout;
27

    
28
import eu.etaxonomy.cdm.vaadin.container.IdUuidName;
29
import eu.etaxonomy.cdm.vaadin.jscomponent.D3ConceptRelationshipTree;
30
import eu.etaxonomy.cdm.vaadin.jscomponent.D3ConceptRelationshipTree.Direction;
31
import eu.etaxonomy.cdm.vaadin.presenter.ConceptRelationshipPresenter;
32
import eu.etaxonomy.cdm.vaadin.session.BasicEvent;
33
import eu.etaxonomy.cdm.vaadin.session.CdmChangeEvent;
34
import eu.etaxonomy.cdm.vaadin.session.CdmChangeEvent.Action;
35
import eu.etaxonomy.cdm.vaadin.session.IBasicEventListener;
36
import eu.etaxonomy.cdm.vaadin.session.ICdmChangeListener;
37
import eu.etaxonomy.cdm.vaadin.session.ISelectionListener;
38
import eu.etaxonomy.cdm.vaadin.session.SelectionEvent;
39
import eu.etaxonomy.cdm.vaadin.util.CdmVaadinSessionUtilities;
40
import eu.etaxonomy.cdm.vaadin.util.CdmVaadinUtilities;
41
import eu.etaxonomy.cdm.vaadin.view.ConceptRelationshipView;
42
import eu.etaxonomy.cdm.vaadin.view.IConceptRelationshipComponentListener;
43

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

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

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

    
67
    private IdUuidName fromTaxonIun;
68
    private UUID selectedTaxonRelUuid;
69

    
70
    private ConceptRelationshipView view;
71

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
165

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

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

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

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

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

    
212
        }
213
    }
214

    
215

    
216

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

    
226
        }
227

    
228
    }
229

    
230

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

    
242

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

    
253
    }
254

    
255

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

    
268
    }
269

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

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

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

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

    
298
        return mainLayout;
299
    }
300

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

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

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

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

    
335
        return updateHorizontalLayout;
336
    }
337

    
338

    
339

    
340
}
(2-2/7)