Merge branch 'develop' into bulkEditorE4
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / view / e4 / AbstractCdmEditorPartE4.java
1 /**
2 * Copyright (C) 2017 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 package eu.etaxonomy.taxeditor.view.e4;
10
11 import java.util.Set;
12
13 import javax.annotation.PreDestroy;
14 import javax.inject.Inject;
15 import javax.inject.Named;
16
17 import org.eclipse.e4.core.di.annotations.Optional;
18 import org.eclipse.e4.ui.di.PersistState;
19 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
20 import org.eclipse.e4.ui.services.IServiceConstants;
21 import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
22 import org.eclipse.jface.viewers.ISelectionChangedListener;
23 import org.eclipse.jface.viewers.IStructuredSelection;
24 import org.eclipse.jface.viewers.StructuredSelection;
25 import org.eclipse.jface.viewers.Viewer;
26 import org.eclipse.swt.widgets.Display;
27 import org.eclipse.ui.IEditorPart;
28
29 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
30 import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
31 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
32 import eu.etaxonomy.cdm.model.common.CdmBase;
33 import eu.etaxonomy.cdm.model.taxon.Taxon;
34 import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
35 import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
36 import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
37 import eu.etaxonomy.taxeditor.editor.ITaxonEditor;
38 import eu.etaxonomy.taxeditor.model.AbstractUtility;
39 import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
40 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
41 import eu.etaxonomy.taxeditor.workbench.WorkbenchUtility;
42 import eu.etaxonomy.taxeditor.workbench.part.ISelectionElementEditingPart;
43
44 /**
45 * @author pplitzner
46 * @since Aug 10, 2017
47 *
48 */
49 public abstract class AbstractCdmEditorPartE4
50 implements IConversationEnabled, IDirtyMarkable, ISelectionElementEditingPart, IPostOperationEnabled{
51
52 private DelaySelection delaySelection = null;
53 /**
54 * This is the monitor for the DelaySelection runnable.
55 * If it is <code>true</code> then it is currently delaying a selection.
56 */
57 private boolean isInDelay;
58
59
60 /**
61 * This class invokes internal_selectionChanged() in a separate thread.
62 * This allows an asynchronous and/or delayed handling of selection changes
63 */
64 private class DelaySelection implements Runnable{
65 private Object selection;
66 private MPart activePart;
67 private MPart thisPart;
68
69 public DelaySelection(Object selection, MPart activePart, MPart thisPart) {
70 super();
71 this.selection = selection;
72 this.activePart= activePart;
73 this.thisPart = thisPart;
74 }
75
76 @Override
77 public void run() {
78 try{
79 selectionChanged_internal(selection, activePart, thisPart);
80 }
81 finally{
82 isInDelay = false;
83 }
84 }
85
86 public synchronized void setSelection(Object selection) {
87 this.selection = selection;
88 }
89
90 public synchronized void setActivePart(MPart activePart) {
91 this.activePart = activePart;
92 }
93
94 public synchronized void setThisPart(MPart thisPart) {
95 this.thisPart = thisPart;
96 }
97
98 }
99
100 protected abstract void selectionChanged_internal(Object selection, MPart activePart, MPart thisPart);
101
102 @Inject
103 @Optional
104 public void selectionChanged(
105 @Named(IServiceConstants.ACTIVE_SELECTION)Object selection,
106 @Named(IServiceConstants.ACTIVE_PART)MPart activePart,
107 MPart thisPart){
108 Object savablePart = WorkbenchUtility.findSavablePart(this);
109 if(savablePart==null){
110 showEmptyPage();
111 }
112 if(delaySelection==null){
113 delaySelection = new DelaySelection(selection, activePart, thisPart);
114 }
115 delaySelection.setSelection(selection);
116 delaySelection.setActivePart(activePart);
117 delaySelection.setThisPart(thisPart);
118 if(!isInDelay){
119 isInDelay = true;
120 Display.getCurrent().asyncExec(delaySelection);
121 }
122 }
123
124 protected Viewer viewer;
125
126 protected MPart thisPart;
127
128 protected MPart selectionProvidingPart;
129
130 protected ISelectionChangedListener selectionChangedListener;
131
132 @Inject
133 protected ESelectionService selService;
134
135 /** {@inheritDoc} */
136 @Override
137 public void changed(Object object) {
138 if(selectionProvidingPart!=null){
139 Object part = selectionProvidingPart.getObject();
140 if(part instanceof IDirtyMarkable){
141 ((IDirtyMarkable) part).changed(object);
142 }
143 else {
144 IEditorPart editor = AbstractUtility.getActiveEditor();
145 if (editor != null && editor instanceof IDirtyMarkable) {
146 ((IDirtyMarkable) editor).changed(object);
147 }
148 }
149 }
150 if(object!=null){
151 createPartTitle(object);
152 }
153 }
154
155 protected void createPartTitle(Object selectedObject){
156 if(thisPart!=null){
157 if(selectedObject!=null){
158 thisPart.setLabel(getViewName()+": "+selectedObject); //$NON-NLS-1$
159 }
160 else{
161 thisPart.setLabel(getViewName());
162 }
163 }
164 }
165
166 public Viewer getViewer() {
167 return viewer;
168 }
169
170 protected void showViewer(IStructuredSelection selection, MPart activePart, Viewer viewer){
171 if(viewer!=null && viewer.getControl()!=null && !viewer.getControl().isDisposed()){
172 Object element = selection.getFirstElement();
173 if(selection.getFirstElement()!=null){
174 if (element instanceof Taxon){
175 Taxon taxon = HibernateProxyHelper.deproxy(element, Taxon.class);
176 if (taxon.isMisapplication()){
177 Object part = createPartObject(activePart);
178 if(part instanceof ITaxonEditor){
179 Taxon accepted = ((ITaxonEditor) part).getTaxon();
180
181 // Taxon accepted= ((ITaxonEditor)activePart).getTaxon();
182 Set<TaxonRelationship> rels = taxon.getTaxonRelations(accepted);
183
184 if (rels.iterator().hasNext()){
185 TaxonRelationship rel = rels.iterator().next();
186 if (rel.getType().equals(TaxonRelationshipType.MISAPPLIED_NAME_FOR()) && !rel.getFromTaxon().equals(((ITaxonEditor) part).getTaxon())){
187 viewer.setInput(rel);
188
189 return;
190 }
191 }
192 }
193
194
195 }
196 }
197 viewer.setInput(element);
198
199 createPartTitle(element);
200
201 }
202 selectionProvidingPart = activePart;
203 }
204 }
205
206 protected Object createPartObject(MPart activePart) {
207 Object partObject = activePart;
208 Object wrappedPart = WorkbenchUtility.getE4WrappedPart(activePart);
209 if(wrappedPart!=null){
210 partObject = wrappedPart;
211 }
212 return partObject;
213 }
214
215 protected void showEmptyPage() {
216 if(viewer!=null && viewer.getControl()!=null && viewer.getControl().isDisposed()){
217 viewer.setInput(null);
218 }
219 selectionProvidingPart = null;
220 }
221
222 protected IStructuredSelection createSelection(Object selection) {
223 if(selection==null){
224 return null;
225 }
226 IStructuredSelection structuredSelection;
227 if(!(selection instanceof IStructuredSelection)){
228 structuredSelection = new StructuredSelection(selection);
229 }
230 else{
231 structuredSelection = (IStructuredSelection) selection;
232 }
233 return structuredSelection;
234 }
235
236 /**
237 * {@inheritDoc}
238 */
239 @Override
240 public ConversationHolder getConversationHolder() {
241 if(selectionProvidingPart != null && selectionProvidingPart instanceof IConversationEnabled) {
242 return ((IConversationEnabled) selectionProvidingPart).getConversationHolder();
243 }
244 return null;
245 }
246
247 /**
248 * {@inheritDoc}
249 */
250 @Override
251 public boolean postOperation(CdmBase objectAffectedByOperation) {
252 changed(objectAffectedByOperation);
253 return true;
254 }
255
256 /**
257 * {@inheritDoc}
258 */
259 @Override
260 public boolean onComplete() {
261 viewer.refresh();
262 return true;
263 }
264
265 /**
266 * {@inheritDoc}
267 */
268 @Override
269 public Object getSelectionProvidingPart() {
270 return selectionProvidingPart;
271 }
272
273 @PreDestroy
274 private void dispose() {
275 }
276
277 @PersistState
278 private void persistState(){
279
280 }
281
282 /**
283 * {@inheritDoc}
284 */
285 @Override
286 public void update(CdmDataChangeMap arg0) {
287 }
288
289 /**
290 * {@inheritDoc}
291 */
292 @Override
293 public void forceDirty() {
294 }
295
296 protected abstract String getViewName();
297
298 }