Project

General

Profile

Download (11 KB) Statistics
| Branch: | Tag: | Revision:
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.apache.log4j.Logger;
18
import org.eclipse.e4.core.contexts.IEclipseContext;
19
import org.eclipse.e4.core.di.annotations.Optional;
20
import org.eclipse.e4.ui.di.PersistState;
21
import org.eclipse.e4.ui.di.UISynchronize;
22
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
23
import org.eclipse.e4.ui.services.IServiceConstants;
24
import org.eclipse.e4.ui.workbench.modeling.EPartService;
25
import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
26
import org.eclipse.jface.viewers.ISelectionChangedListener;
27
import org.eclipse.jface.viewers.IStructuredSelection;
28
import org.eclipse.jface.viewers.StructuredSelection;
29
import org.eclipse.jface.viewers.Viewer;
30
import org.eclipse.swt.SWTException;
31

    
32
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
33
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
34
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
35
import eu.etaxonomy.cdm.model.common.CdmBase;
36
import eu.etaxonomy.cdm.model.description.Distribution;
37
import eu.etaxonomy.cdm.model.taxon.Taxon;
38
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
39
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
40
import eu.etaxonomy.taxeditor.editor.IDistributionEditor;
41
import eu.etaxonomy.taxeditor.editor.ITaxonEditor;
42
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
43
import eu.etaxonomy.taxeditor.model.MessagingUtils;
44
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
45
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
46
import eu.etaxonomy.taxeditor.view.e4.details.DetailsViewerE4;
47
import eu.etaxonomy.taxeditor.workbench.WorkbenchUtility;
48
import eu.etaxonomy.taxeditor.workbench.part.ISelectionElementEditingPart;
49

    
50
/**
51
 * @author pplitzner
52
 * @since Aug 10, 2017
53
 *
54
 */
55
public abstract class AbstractCdmEditorPartE4
56
        implements IConversationEnabled, IDirtyMarkable, ISelectionElementEditingPart, IPostOperationEnabled{
57

    
58
    private DelaySelection delaySelection = null;
59
    /**
60
     * This is the monitor for the DelaySelection runnable.
61
     * If it is <code>true</code> then it is currently delaying a selection.
62
     */
63
    private boolean isInDelay;
64
    private static final Logger logger = Logger.getLogger(AbstractCdmEditorPartE4.class);
65

    
66
    /**
67
     * This class invokes internal_selectionChanged() in a separate thread.
68
     * This allows an asynchronous and/or delayed handling of selection changes
69
     */
70
    private class DelaySelection implements Runnable{
71
        private Object selection;
72
        private MPart activePart;
73
        private MPart thisPart;
74

    
75
        public DelaySelection(Object selection, MPart activePart, MPart thisPart) {
76
            super();
77
            this.selection = selection;
78
            this.activePart= activePart;
79
            this.thisPart = thisPart;
80
        }
81

    
82
        @Override
83
        public void run() {
84
            try{
85
                selectionChanged_internal(selection, activePart, thisPart);
86
            }
87
            finally{
88
                isInDelay = false;
89
            }
90
        }
91

    
92
        public synchronized void setSelection(Object selection) {
93
            this.selection = selection;
94
        }
95

    
96
        public synchronized void setActivePart(MPart activePart) {
97
            this.activePart = activePart;
98
        }
99

    
100
        public synchronized void setThisPart(MPart thisPart) {
101
            this.thisPart = thisPart;
102
        }
103

    
104
    }
105

    
106
    protected Viewer viewer;
107

    
108
    protected MPart thisPart;
109

    
110
    protected MPart selectionProvidingPart;
111

    
112
    protected Object previousSelection;
113

    
114
    public Object getPreviousSelection() {
115
        return previousSelection;
116
    }
117

    
118
    public void setPreviousSelection(Object previousSelection) {
119
        this.previousSelection = previousSelection;
120
    }
121

    
122
    protected ISelectionChangedListener selectionChangedListener;
123

    
124
    public ISelectionChangedListener getSelectionChangedListener() {
125
        return selectionChangedListener;
126
    }
127

    
128
    @Inject
129
    protected ESelectionService selService;
130

    
131
    @Inject
132
    protected IEclipseContext context;
133

    
134
    protected abstract void selectionChanged_internal(Object selection, MPart activePart, MPart thisPart);
135

    
136
    @Inject
137
    public void selectionChanged(
138
            @Optional@Named(IServiceConstants.ACTIVE_SELECTION)Object selection,
139
            @Optional@Named(IServiceConstants.ACTIVE_PART)MPart activePart,
140
            MPart thisPart, UISynchronize sync, EPartService partService){
141
        if(activePart==thisPart && WorkbenchUtility.getActiveEditorPart(partService)==null){
142
            showEmptyPage();
143
            return;
144
        }
145
        if (viewer != null && viewer.getControl()!= null && viewer.getInput() != null && !viewer.getControl().isDisposed()){
146
           try{
147
               viewer.getControl().setEnabled(true);
148
           }catch(SWTException e){
149
              logger.debug("Something went wrong for viewer.getControl().setEnabled(true) in " + this.getClass().getSimpleName(), e);
150
           }
151

    
152
        }
153

    
154

    
155
        if(previousSelection==null ||
156
                previousSelection!=selection){//skip redundant rendering of details view
157
            if(delaySelection==null){
158
                delaySelection = new DelaySelection(selection, activePart, thisPart);
159
            }
160
            delaySelection.setSelection(selection);
161
            delaySelection.setActivePart(activePart);
162
            delaySelection.setThisPart(thisPart);
163
            if(!isInDelay){
164
                isInDelay = true;
165
                sync.asyncExec(delaySelection);
166
                previousSelection = selection;
167
            }
168
        }
169
    }
170

    
171
    /** {@inheritDoc} */
172
    @Override
173
    public void changed(Object object) {
174
        if(selectionProvidingPart!=null){
175
            Object part = selectionProvidingPart.getObject();
176
            if(part instanceof IDirtyMarkable){
177
                ((IDirtyMarkable) part).changed(object);
178
            }
179
        }
180
    }
181

    
182
    public Viewer getViewer() {
183
        return viewer;
184
    }
185

    
186
    protected void showViewer(IStructuredSelection selection, MPart activePart, Viewer viewer){
187
        if(viewer!=null && viewer.getControl()!=null && !viewer.getControl().isDisposed()){
188
            Object element = selection.getFirstElement();
189
            Object part = createPartObject(activePart);
190
            viewer.getControl().setEnabled(true);
191
            if(selection.getFirstElement()!=null){
192
                if (element instanceof Taxon){
193
                    Taxon taxon = HibernateProxyHelper.deproxy(element, Taxon.class);
194
                    if (taxon.isMisapplication() || taxon.isProparteSynonym()){
195

    
196
                        if(part instanceof ITaxonEditor){
197
                            Taxon accepted = ((ITaxonEditor) part).getTaxon();
198
                            Set<TaxonRelationship> rels =  taxon.getTaxonRelations(accepted);
199

    
200
                            if (rels != null && rels.iterator().hasNext()){
201
                                TaxonRelationship rel = rels.iterator().next();
202
                                if ((rel.getType().isAnyMisappliedName() || rel.getType().isAnySynonym())&& !rel.getFromTaxon().equals(((ITaxonEditor) part).getTaxon())){
203
                                    viewer.setInput(rel);
204

    
205
                                    return;
206
                                }
207
                            }
208
                        }
209

    
210

    
211
                    }
212
                }
213
                if (element instanceof Distribution && part instanceof IDistributionEditor){
214
                    ((DetailsViewerE4)viewer).setInput(element, part);
215
                }else{
216
                    viewer.setInput(element);
217
                    if (viewer instanceof DetailsViewerE4){
218
                    	((DetailsViewerE4)viewer).setDetailsEnabled(true);
219
                    }
220
                }
221
                selectionProvidingPart = activePart;
222
            }
223
        }
224
    }
225

    
226
    protected Object createPartObject(MPart activePart) {
227
        Object partObject = activePart;
228
        Object wrappedPart = WorkbenchUtility.getE4WrappedPart(activePart);
229
        if(wrappedPart!=null){
230
            partObject = wrappedPart;
231
        }
232
        return partObject;
233
    }
234

    
235
    protected void showEmptyPage() {
236
        if(viewer!=null && viewer.getControl()!=null && !viewer.getControl().isDisposed() ){
237
            viewer.setInput(null);
238
            try{
239
	            if (!viewer.getControl().isDisposed()){
240
	                viewer.getControl().setEnabled(false);
241
	            }
242
            }catch(SWTException e){
243
            	if (PreferencesUtil.isShowUpWidgetIsDisposedMessages() && e.getMessage().equals("Widget is disposed")){
244
                    MessagingUtils.errorDialog("Widget is disposed",
245
                            null,
246
                            MessagingUtils.WIDGET_IS_DISPOSED_MESSAGE,
247
                            null,
248
                            e,
249
                            true);
250

    
251
                }
252
            }
253

    
254
        }
255
        reset();
256
        if(thisPart!=null){
257
            thisPart.setLabel(getViewName());
258
        }
259
    }
260

    
261
    protected IStructuredSelection createSelection(Object selection) {
262
        if(selection==null){
263
            return null;
264
        }
265
        IStructuredSelection structuredSelection;
266
        if(!(selection instanceof IStructuredSelection)){
267
            structuredSelection = new StructuredSelection(selection);
268
        }
269
        else{
270
            structuredSelection = (IStructuredSelection) selection;
271
        }
272
        return structuredSelection;
273
    }
274

    
275
    /**
276
     * {@inheritDoc}
277
     */
278
    @Override
279
    public ConversationHolder getConversationHolder() {
280
        if(selectionProvidingPart != null && selectionProvidingPart instanceof IConversationEnabled) {
281
            return ((IConversationEnabled) selectionProvidingPart).getConversationHolder();
282
        }
283
        return null;
284
    }
285

    
286
    /**
287
     * {@inheritDoc}
288
     */
289
    @Override
290
    public boolean postOperation(CdmBase objectAffectedByOperation) {
291
        changed(objectAffectedByOperation);
292
        return true;
293
    }
294

    
295
    /**
296
     * {@inheritDoc}
297
     */
298
    @Override
299
    public boolean onComplete() {
300
        viewer.refresh();
301
        return true;
302
    }
303

    
304
    /**
305
     * {@inheritDoc}
306
     */
307
    @Override
308
    public MPart getSelectionProvidingPart() {
309
        return selectionProvidingPart;
310
    }
311

    
312
    @PreDestroy
313
    private void dispose() {
314
    }
315

    
316
    private void reset(){
317
        previousSelection = null;
318
        selectionProvidingPart = null;
319
        delaySelection = null;
320
        context.deactivate();
321
    }
322

    
323
    @PersistState
324
    private void persistState(){
325

    
326
    }
327

    
328
    /**
329
     * {@inheritDoc}
330
     */
331
    @Override
332
    public void update(CdmDataChangeMap arg0) {
333
    }
334

    
335
    /**
336
     * {@inheritDoc}
337
     */
338
    @Override
339
    public void forceDirty() {
340
    }
341

    
342
    protected abstract String getViewName();
343

    
344
}
(2-2/2)