Merge branch 'develop' into bulkNat
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / concept / e4 / ConceptViewPartE4.java
1 /**
2 * Copyright (C) 2007 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
10 package eu.etaxonomy.taxeditor.editor.view.concept.e4;
11
12 import java.util.Map.Entry;
13
14 import javax.annotation.PostConstruct;
15
16 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
17 import org.eclipse.e4.ui.services.EMenuService;
18 import org.eclipse.jface.viewers.ISelection;
19 import org.eclipse.jface.viewers.IStructuredSelection;
20 import org.eclipse.jface.viewers.ListViewer;
21 import org.eclipse.jface.viewers.StructuredSelection;
22 import org.eclipse.jface.viewers.ViewerSorter;
23 import org.eclipse.swt.widgets.Composite;
24
25 import eu.etaxonomy.cdm.model.taxon.Taxon;
26 import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
27 import eu.etaxonomy.taxeditor.editor.key.e4.AbstractGraphKeyEditorE4;
28 import eu.etaxonomy.taxeditor.editor.l10n.Messages;
29 import eu.etaxonomy.taxeditor.editor.view.concept.ConceptContentProvider;
30 import eu.etaxonomy.taxeditor.editor.view.concept.ConceptLabelProvider;
31 import eu.etaxonomy.taxeditor.editor.view.descriptive.e4.FactualDataPartE4;
32 import eu.etaxonomy.taxeditor.editor.view.media.e4.MediaViewPartE4;
33 import eu.etaxonomy.taxeditor.model.AbstractUtility;
34 import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
35 import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
36 import eu.etaxonomy.taxeditor.view.e4.AbstractCdmEditorPartE4;
37 import eu.etaxonomy.taxeditor.view.e4.details.DetailsPartE4;
38 import eu.etaxonomy.taxeditor.view.e4.supplementaldata.SupplementalDataPartE4;
39
40 /**
41 *
42 * @author pplitzner
43 * @date 21.08.2017
44 *
45 */
46 public class ConceptViewPartE4 extends AbstractCdmEditorPartE4
47 implements IPartContentHasDetails, IPartContentHasSupplementalData{
48
49 @Override
50 public void selectionChanged_internal(Object selection, MPart activePart, MPart thisPart) {
51 if(activePart==thisPart){
52 return;
53 }
54 if (!viewer.getControl().isDisposed()){
55 viewer.getControl().setEnabled(true);
56 }
57 Object partObject = createPartObject(activePart);
58 IStructuredSelection structuredSelection = createSelection(selection);
59
60 if (partObject instanceof DetailsPartE4 || partObject instanceof SupplementalDataPartE4
61 || partObject instanceof FactualDataPartE4|| partObject instanceof MediaViewPartE4) {
62 // do not show empty page as these views are also used to edit the
63 // description selected in this view
64 return;
65 }
66 else if(partObject instanceof AbstractGraphKeyEditorE4){
67 showEmptyPage();
68 }
69 else if(selection!=null){
70 showViewer(structuredSelection, activePart, viewer);
71 }
72 else {
73 showEmptyPage();
74 }
75
76 }
77
78 @PostConstruct
79 public void create(Composite parent, EMenuService menuService, MPart thisPart) {
80
81 this.thisPart = thisPart;
82
83 ConceptRelationViewer conceptViewer = new ConceptRelationViewer(parent);
84
85 conceptViewer.setContentProvider(new ConceptContentProvider());
86 conceptViewer.setLabelProvider(new ConceptLabelProvider());
87
88
89 conceptViewer.setSorter(new ViewerSorter());
90
91 viewer = conceptViewer;
92
93 // Propagate selection from viewer
94 selectionChangedListener = (event -> selService.setSelection(AbstractUtility.getElementsFromSelectionChangedEvent(event)));
95 viewer.addSelectionChangedListener(selectionChangedListener);
96
97 //create context menu
98 menuService.registerContextMenu(viewer.getControl(), "eu.etaxonomy.taxeditor.editor.popupmenu.conceptview");
99
100 }
101
102 @Override
103 public void changed(Object object) {
104 getViewer().refresh();
105 super.changed(object);
106 }
107
108 private class ConceptRelationViewer extends ListViewer {
109
110 public ConceptRelationViewer(Composite parent) {
111 super(parent);
112 }
113
114 @Override
115 public ISelection getSelection() {
116 ISelection selection = super.getSelection();
117 if(selection instanceof IStructuredSelection && !selection.isEmpty()){
118 Object firstElement = ((IStructuredSelection) selection).getFirstElement();
119 TaxonRelationship taxonRelationship = ((Entry<TaxonRelationship, Taxon>)firstElement).getKey();
120 return new StructuredSelection(taxonRelationship);
121
122 }
123 return selection;
124 }
125
126 @Override
127 public void setSelection(ISelection selection) {
128 super.setSelection(selection);
129 }
130
131 }
132
133 @Override
134 protected String getViewName() {
135 return Messages.ConceptViewPart_VIEWER_NAME;
136 }
137
138 }