Project

General

Profile

Download (11.3 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
            view.setPrimaryStatusCompositeUuid((UUID)event.getSelectedObjects().get(1));
204
            refreshRelationshipView(view.getDirection());
205
            setSelectedTaxonRelUuid(null);
206
            updateControls();
207
        }
208
    }
209

    
210

    
211

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

    
221
        }
222

    
223
    }
224

    
225

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

    
237

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

    
248
    }
249

    
250

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

    
263
    }
264

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

    
275
        // top-level component properties
276
        setWidth("100.0%");
277
        setHeight("100.0%");
278

    
279
        // updateHorizontalLayout
280
        updateHorizontalLayout = buildUpdateHorizontalLayout();
281
        mainLayout.addComponent(updateHorizontalLayout);
282
        mainLayout.setComponentAlignment(updateHorizontalLayout, new Alignment(20));
283

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

    
293
        return mainLayout;
294
    }
295

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

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

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

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

    
330
        return updateHorizontalLayout;
331
    }
332

    
333

    
334

    
335
}
(2-2/6)