ref #6925 Update part titles
[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 import javax.inject.Inject;
16 import javax.inject.Named;
17
18 import org.eclipse.e4.core.di.annotations.Optional;
19 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
20 import org.eclipse.e4.ui.services.EMenuService;
21 import org.eclipse.e4.ui.services.IServiceConstants;
22 import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
23 import org.eclipse.jface.viewers.ISelection;
24 import org.eclipse.jface.viewers.ISelectionChangedListener;
25 import org.eclipse.jface.viewers.IStructuredSelection;
26 import org.eclipse.jface.viewers.ListViewer;
27 import org.eclipse.jface.viewers.StructuredSelection;
28 import org.eclipse.jface.viewers.ViewerSorter;
29 import org.eclipse.swt.widgets.Composite;
30 import org.eclipse.ui.IEditorInput;
31 import org.eclipse.ui.IEditorPart;
32
33 import eu.etaxonomy.cdm.model.taxon.Taxon;
34 import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
35 import eu.etaxonomy.taxeditor.bulkeditor.BulkEditor;
36 import eu.etaxonomy.taxeditor.editor.key.AbstractGraphKeyEditor;
37 import eu.etaxonomy.taxeditor.editor.l10n.Messages;
38 import eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4;
39 import eu.etaxonomy.taxeditor.editor.view.concept.ConceptContentProvider;
40 import eu.etaxonomy.taxeditor.editor.view.concept.ConceptLabelProvider;
41 import eu.etaxonomy.taxeditor.model.AbstractUtility;
42 import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
43 import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
44 import eu.etaxonomy.taxeditor.model.LineSelection;
45 import eu.etaxonomy.taxeditor.view.e4.AbstractCdmEditorPartE4;
46 import eu.etaxonomy.taxeditor.view.e4.details.DetailsPartE4;
47 import eu.etaxonomy.taxeditor.view.e4.supplementaldata.SupplementalDataPartE4;
48
49 /**
50 *
51 * @author pplitzner
52 * @date 21.08.2017
53 *
54 */
55 public class ConceptViewPartE4 extends AbstractCdmEditorPartE4
56 implements IPartContentHasDetails, IPartContentHasSupplementalData{
57
58 private ISelectionChangedListener selectionChangedListener;
59
60 @Inject
61 private ESelectionService selService;
62
63 @Inject
64 @Optional
65 public void selectionChanged(
66 @Named(IServiceConstants.ACTIVE_SELECTION)Object selection,
67 @Named(IServiceConstants.ACTIVE_PART)MPart activePart,
68 MPart thisPart){
69 if(activePart==thisPart){
70 return;
71 }
72
73 Object partObject = createPartObject(activePart);
74
75 if (partObject instanceof DetailsPartE4 || partObject instanceof SupplementalDataPartE4) {
76 // do not show empty page as these views are also used to edit the
77 // description selected in this view
78 return;
79 }
80 else if(partObject instanceof BulkEditor && selection instanceof LineSelection){
81 if(((LineSelection) selection).getFirstElement() instanceof Taxon){
82 showViewer((LineSelection) selection, activePart, viewer);
83 }
84 else{
85 showEmptyPage();
86 }
87 return;
88 }
89 else if(partObject instanceof AbstractGraphKeyEditor){
90 showEmptyPage();
91 }
92 else if(partObject instanceof TaxonNameEditorE4){
93 IEditorInput input = ((IEditorPart) partObject).getEditorInput();
94 showViewer(new StructuredSelection(input), activePart, viewer);
95 }
96 else {
97 showEmptyPage();
98 }
99
100 }
101
102 @PostConstruct
103 public void create(Composite parent, EMenuService menuService, MPart thisPart) {
104
105 this.thisPart = thisPart;
106
107 ConceptRelationViewer conceptViewer = new ConceptRelationViewer(parent);
108
109 conceptViewer.setContentProvider(new ConceptContentProvider());
110 conceptViewer.setLabelProvider(new ConceptLabelProvider());
111
112
113 conceptViewer.setSorter(new ViewerSorter());
114
115 viewer = conceptViewer;
116
117 // Propagate selection from viewer
118 selectionChangedListener = (event -> selService.setSelection(AbstractUtility.getElementsFromSelectionChangedEvent(event)));
119 viewer.addSelectionChangedListener(selectionChangedListener);
120
121 //create context menu
122 menuService.registerContextMenu(viewer.getControl(), "eu.etaxonomy.taxeditor.editor.popupmenu.conceptview");
123
124 }
125
126 @Override
127 public void changed(Object object) {
128 getViewer().refresh();
129 super.changed(object);
130 }
131
132 private class ConceptRelationViewer extends ListViewer {
133
134 public ConceptRelationViewer(Composite parent) {
135 super(parent);
136 }
137
138 @Override
139 public ISelection getSelection() {
140 ISelection selection = super.getSelection();
141 if(selection instanceof IStructuredSelection && !selection.isEmpty()){
142 Object firstElement = ((IStructuredSelection) selection).getFirstElement();
143 TaxonRelationship taxonRelationship = ((Entry<TaxonRelationship, Taxon>)firstElement).getKey();
144 return new StructuredSelection(taxonRelationship);
145
146 }
147 return selection;
148 }
149
150 @Override
151 public void setSelection(ISelection selection) {
152 super.setSelection(selection);
153 }
154
155 }
156
157 @Override
158 protected String getViewName() {
159 return Messages.ConceptViewPart_VIEWER_NAME;
160 }
161
162 }