Project

General

Profile

Download (11 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.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.view.ConceptRelationshipView;
40
import eu.etaxonomy.cdm.vaadin.view.IConceptRelationshipComponentListener;
41

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

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

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

    
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
            }
130
        });
131
    }
132

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

    
136
            @Override
137
            public void buttonClick(ClickEvent event) {
138
                EditConceptRelationshipComposite.showInDialog(EDIT_CR_TITLE,
139
                        fromTaxonIun,
140
                        selectedTaxonRelUuid,
141
                        Action.Update,
142
                        view.getDirection());
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
            }
158
        });
159
    }
160

    
161

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

    
172
    public void setSelectedTaxonRelUuid(UUID selectedTaxonRelUuid) {
173
        this.selectedTaxonRelUuid = selectedTaxonRelUuid;
174
        updateControls();
175
    }
176

    
177
    private void enableControls(boolean enabled) {
178
        newButton.setEnabled(enabled);
179
        editButton.setEnabled(enabled);
180
        deleteButton.setEnabled(enabled);
181
    }
182

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

    
194
    /* (non-Javadoc)
195
     * @see eu.etaxonomy.cdm.vaadin.session.ISelectionListener#onSelect(eu.etaxonomy.cdm.vaadin.session.SelectionEvent)
196
     */
197
    @Override
198
    public void onSelect(SelectionEvent event) {
199
        if(event.getSourceType().equals(StatusComposite.class)) {
200
            fromTaxonIun = (IdUuidName)event.getSelectedObjects().get(0);
201
            view.setPrimaryStatusCompositeUuid((UUID)event.getSelectedObjects().get(1));
202
            refreshRelationshipView(view.getDirection());
203
            updateControls();
204
        }
205
    }
206

    
207

    
208

    
209
    /* (non-Javadoc)
210
     * @see eu.etaxonomy.cdm.vaadin.session.ICdmChangeListener#onCreate(eu.etaxonomy.cdm.vaadin.session.CdmChangeEvent)
211
     */
212
    @Override
213
    public void onCreate(CdmChangeEvent event) {
214
        if(event.getSourceType().equals(EditConceptRelationshipComposite.class)) {
215
            setSelectedTaxonRelUuid(null);
216
            refreshRelationshipView(view.getDirection());
217

    
218
        }
219

    
220
    }
221

    
222

    
223
    /* (non-Javadoc)
224
     * @see eu.etaxonomy.cdm.vaadin.session.ICdmChangeListener#onUpdate(eu.etaxonomy.cdm.vaadin.session.CdmChangeEvent)
225
     */
226
    @Override
227
    public void onUpdate(CdmChangeEvent event) {
228
        if(event.getSourceType().equals(EditConceptRelationshipComposite.class)) {
229
            setSelectedTaxonRelUuid(null);
230
            refreshRelationshipView(view.getDirection());
231
        }
232
    }
233

    
234

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

    
245
    }
246

    
247

    
248
    /* (non-Javadoc)
249
     * @see eu.etaxonomy.cdm.vaadin.session.IBasicEventListener#onAction(eu.etaxonomy.cdm.vaadin.session.BasicEvent)
250
     */
251
    @Override
252
    public void onAction(BasicEvent event) {
253
        if(ConceptRelationshipComposite.UPDATE_START_ID.equals(event.getEventId())) {
254
            enableControls(false);
255
        }
256
        if(ConceptRelationshipComposite.UPDATE_END_ID.equals(event.getEventId())) {
257
            updateControls();
258
        }
259

    
260
    }
261

    
262
    @AutoGenerated
263
    private VerticalLayout buildMainLayout() {
264
        // common part: create layout
265
        mainLayout = new VerticalLayout();
266
        mainLayout.setImmediate(false);
267
        mainLayout.setWidth("100%");
268
        mainLayout.setHeight("100%");
269
        mainLayout.setMargin(true);
270
        mainLayout.setSpacing(true);
271

    
272
        // top-level component properties
273
        setWidth("100.0%");
274
        setHeight("100.0%");
275

    
276
        // updateHorizontalLayout
277
        updateHorizontalLayout = buildUpdateHorizontalLayout();
278
        mainLayout.addComponent(updateHorizontalLayout);
279
        mainLayout.setComponentAlignment(updateHorizontalLayout, new Alignment(20));
280

    
281
        // d3ConceptRelationShipTree
282
        d3ConceptRelationShipTree = new D3ConceptRelationshipTree();
283
        d3ConceptRelationShipTree.setImmediate(false);
284
        d3ConceptRelationShipTree.setWidth("-1px");
285
        d3ConceptRelationShipTree.setHeight("-1px");
286
        mainLayout.addComponent(d3ConceptRelationShipTree);
287
        mainLayout.setExpandRatio(d3ConceptRelationShipTree, 1.0f);
288
        mainLayout.setComponentAlignment(d3ConceptRelationShipTree, new Alignment(20));
289

    
290
        return mainLayout;
291
    }
292

    
293
    @AutoGenerated
294
    private HorizontalLayout buildUpdateHorizontalLayout() {
295
        // common part: create layout
296
        updateHorizontalLayout = new HorizontalLayout();
297
        updateHorizontalLayout.setImmediate(false);
298
        updateHorizontalLayout.setWidth("-1px");
299
        updateHorizontalLayout.setHeight("-1px");
300
        updateHorizontalLayout.setMargin(true);
301
        updateHorizontalLayout.setSpacing(true);
302

    
303
        // newButton
304
        newButton = new Button();
305
        newButton.setCaption("new");
306
        newButton.setImmediate(true);
307
        newButton.setWidth("-1px");
308
        newButton.setHeight("-1px");
309
        updateHorizontalLayout.addComponent(newButton);
310

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

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

    
327
        return updateHorizontalLayout;
328
    }
329

    
330

    
331

    
332
}
(2-2/6)