Project

General

Profile

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

    
41
/**
42
 * @author cmathew
43
 * @date 9 Apr 2015
44
 *
45
 */
46
public class ConceptRelationshipComposite extends CustomComponent implements ISelectionListener, ICdmChangeListener {
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

    
63
    private final IConceptRelationshipComponentListener listener;
64

    
65
    private IdUuidName fromTaxonIun;
66
    private UUID selectedTaxonRelUuid;
67

    
68
    private ConceptRelationshipView view;
69

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

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

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

    
88
        CdmVaadinSessionUtilities.getCurrentSelectionService().register(this);
89
        CdmVaadinSessionUtilities.getCurrentCdmDataChangeService().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
        editButton.setEnabled(false);
102
        deleteButton.setEnabled(false);
103
        initD3ConceptRelationShipTree();
104
    }
105

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

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

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

    
120
            @Override
121
            public void buttonClick(ClickEvent event) {
122
               EditConceptRelationshipComposite.showInDialog(CREATE_NEW_CR_TITLE,
123
                       fromTaxonIun,
124
                       null,
125
                       null,
126
                       Action.Create);
127
               CdmVaadinSessionUtilities.getCurrentBasicEventService()
128
               .fireBasicEvent(new BasicEvent(UPDATE_START_ID, ConceptRelationshipComposite.class), false);
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
                CdmVaadinSessionUtilities.getCurrentBasicEventService()
143
                .fireBasicEvent(new BasicEvent(UPDATE_START_ID, ConceptRelationshipComposite.class), false);
144
            }
145
        });
146
    }
147

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

    
151
            @Override
152
            public void buttonClick(ClickEvent event) {
153
                EditConceptRelationshipComposite.showInDialog(DELETE_CR_TITLE,
154
                        fromTaxonIun,
155
                        selectedTaxonRelUuid,
156
                        Action.Delete);
157
                CdmVaadinSessionUtilities.getCurrentBasicEventService()
158
                .fireBasicEvent(new BasicEvent(UPDATE_START_ID, ConceptRelationshipComposite.class), false);
159
            }
160
        });
161
    }
162

    
163

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

    
174
    public void setSelectedTaxonRelUuid(UUID selectedTaxonRelUuid) {
175
        this.selectedTaxonRelUuid = selectedTaxonRelUuid;
176
        if(selectedTaxonRelUuid == null) {
177
            editButton.setEnabled(false);
178
            deleteButton.setEnabled(false);
179
        } else {
180
            editButton.setEnabled(true);
181
            deleteButton.setEnabled(true);
182
        }
183
    }
184

    
185
    /* (non-Javadoc)
186
     * @see eu.etaxonomy.cdm.vaadin.session.ISelectionListener#onSelect(eu.etaxonomy.cdm.vaadin.session.SelectionEvent)
187
     */
188
    @Override
189
    public void onSelect(SelectionEvent event) {
190
        if(event.getSourceType().equals(StatusComposite.class)) {
191
            fromTaxonIun = (IdUuidName)event.getSelectedObjects().get(0);
192
            view.setPrimaryStatusCompositeUuid((UUID)event.getSelectedObjects().get(1));
193
            refreshRelationshipView(view.getDirection());
194
        }
195
    }
196

    
197

    
198

    
199
    /* (non-Javadoc)
200
     * @see eu.etaxonomy.cdm.vaadin.session.ICdmChangeListener#onCreate(eu.etaxonomy.cdm.vaadin.session.CdmChangeEvent)
201
     */
202
    @Override
203
    public void onCreate(CdmChangeEvent event) {
204
        if(event.getSourceType().equals(EditConceptRelationshipComposite.class)) {
205
            setSelectedTaxonRelUuid(null);
206
            refreshRelationshipView(view.getDirection());
207

    
208
        }
209

    
210
    }
211

    
212

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

    
223
    }
224

    
225

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

    
236
    }
237
    @AutoGenerated
238
    private VerticalLayout buildMainLayout() {
239
        // common part: create layout
240
        mainLayout = new VerticalLayout();
241
        mainLayout.setImmediate(false);
242
        mainLayout.setWidth("100%");
243
        mainLayout.setHeight("100%");
244
        mainLayout.setMargin(true);
245
        mainLayout.setSpacing(true);
246

    
247
        // top-level component properties
248
        setWidth("100.0%");
249
        setHeight("100.0%");
250

    
251
        // updateHorizontalLayout
252
        updateHorizontalLayout = buildUpdateHorizontalLayout();
253
        mainLayout.addComponent(updateHorizontalLayout);
254
        mainLayout.setComponentAlignment(updateHorizontalLayout, new Alignment(20));
255

    
256
        // d3ConceptRelationShipTree
257
        d3ConceptRelationShipTree = new D3ConceptRelationshipTree();
258
        d3ConceptRelationShipTree.setImmediate(false);
259
        d3ConceptRelationShipTree.setWidth("-1px");
260
        d3ConceptRelationShipTree.setHeight("-1px");
261
        mainLayout.addComponent(d3ConceptRelationShipTree);
262
        mainLayout.setExpandRatio(d3ConceptRelationShipTree, 1.0f);
263
        mainLayout.setComponentAlignment(d3ConceptRelationShipTree, new Alignment(20));
264

    
265
        return mainLayout;
266
    }
267

    
268
    @AutoGenerated
269
    private HorizontalLayout buildUpdateHorizontalLayout() {
270
        // common part: create layout
271
        updateHorizontalLayout = new HorizontalLayout();
272
        updateHorizontalLayout.setImmediate(false);
273
        updateHorizontalLayout.setWidth("-1px");
274
        updateHorizontalLayout.setHeight("-1px");
275
        updateHorizontalLayout.setMargin(true);
276
        updateHorizontalLayout.setSpacing(true);
277

    
278
        // newButton
279
        newButton = new Button();
280
        newButton.setCaption("new");
281
        newButton.setImmediate(true);
282
        newButton.setWidth("-1px");
283
        newButton.setHeight("-1px");
284
        updateHorizontalLayout.addComponent(newButton);
285

    
286
        // editButton
287
        editButton = new Button();
288
        editButton.setCaption("edit");
289
        editButton.setImmediate(true);
290
        editButton.setWidth("-1px");
291
        editButton.setHeight("-1px");
292
        updateHorizontalLayout.addComponent(editButton);
293

    
294
        // deleteButton
295
        deleteButton = new Button();
296
        deleteButton.setCaption("delete");
297
        deleteButton.setImmediate(true);
298
        deleteButton.setWidth("-1px");
299
        deleteButton.setHeight("-1px");
300
        updateHorizontalLayout.addComponent(deleteButton);
301

    
302
        return updateHorizontalLayout;
303
    }
304

    
305

    
306
}
(2-2/6)