Project

General

Profile

Download (11.4 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
        //multiple selections are not supported
142
        if(activePart!=null
143
                && thisPart!=null
144
                && !activePart.equals(thisPart)
145
                && selection instanceof IStructuredSelection
146
                && ((IStructuredSelection) selection).size()>1){
147
            showEmptyPage();
148
            return;
149
        }
150
        if(activePart==thisPart && WorkbenchUtility.getActiveEditorPart(partService)==null){
151
            showEmptyPage();
152
            return;
153
        }
154
        if (viewer != null && viewer.getControl()!= null && viewer.getInput() != null && !viewer.getControl().isDisposed()){
155
           try{
156
               viewer.getControl().setEnabled(true);
157
           }catch(SWTException e){
158
              logger.debug("Something went wrong for viewer.getControl().setEnabled(true) in " + this.getClass().getSimpleName(), e);
159
           }
160

    
161
        }
162

    
163

    
164
        if(previousSelection==null ||
165
                previousSelection!=selection){//skip redundant rendering of details view
166
            if(delaySelection==null){
167
                delaySelection = new DelaySelection(selection, activePart, thisPart);
168
            }
169
            delaySelection.setSelection(selection);
170
            delaySelection.setActivePart(activePart);
171
            delaySelection.setThisPart(thisPart);
172
            if(!isInDelay){
173
                isInDelay = true;
174
                sync.asyncExec(delaySelection);
175
                previousSelection = selection;
176
            }
177
        }
178
    }
179

    
180
    /** {@inheritDoc} */
181
    @Override
182
    public void changed(Object object) {
183
        if(selectionProvidingPart!=null){
184
            Object part = selectionProvidingPart.getObject();
185
            if(part instanceof IDirtyMarkable){
186
                ((IDirtyMarkable) part).changed(object);
187
            }
188
        }
189
    }
190

    
191
    public Viewer getViewer() {
192
        return viewer;
193
    }
194

    
195
    protected void showViewer(IStructuredSelection selection, MPart activePart, Viewer viewer){
196
        if(viewer!=null && viewer.getControl()!=null && !viewer.getControl().isDisposed()){
197
            Object element = selection.getFirstElement();
198
            Object part = createPartObject(activePart);
199
            viewer.getControl().setEnabled(true);
200
            if(selection.getFirstElement()!=null){
201
                if (element instanceof Taxon){
202
                    Taxon taxon = HibernateProxyHelper.deproxy(element, Taxon.class);
203
                    if (taxon.isMisapplication() || taxon.isProparteSynonym()){
204

    
205
                        if(part instanceof ITaxonEditor){
206
                            Taxon accepted = ((ITaxonEditor) part).getTaxon();
207
                            Set<TaxonRelationship> rels =  taxon.getTaxonRelations(accepted);
208

    
209
                            if (rels != null && rels.iterator().hasNext() && !taxon.equals(accepted)){
210
                                TaxonRelationship rel = rels.iterator().next();
211
                                if ((rel.getType().isAnyMisappliedName() || rel.getType().isAnySynonym())&& !rel.getFromTaxon().equals(((ITaxonEditor) part).getTaxon())){
212
                                    viewer.setInput(rel);
213
                                    selectionProvidingPart = activePart;
214
                                    return;
215
                                }
216
                            }
217
                        }
218

    
219

    
220
                    }
221
                }
222
                if (element instanceof Distribution && part instanceof IDistributionEditor){
223
                    ((DetailsViewerE4)viewer).setInput(element, part);
224
                }else{
225
                    viewer.setInput(element);
226
                    if (viewer instanceof DetailsViewerE4){
227
                    	((DetailsViewerE4)viewer).setDetailsEnabled(true);
228
                    }
229
                }
230
                selectionProvidingPart = activePart;
231
            }
232
        }
233
    }
234

    
235
    protected Object createPartObject(MPart activePart) {
236
        Object partObject = activePart;
237
        Object wrappedPart = WorkbenchUtility.getE4WrappedPart(activePart);
238
        if(wrappedPart!=null){
239
            partObject = wrappedPart;
240
        }
241
        return partObject;
242
    }
243

    
244
    protected void showEmptyPage() {
245
        if(viewer!=null && viewer.getControl()!=null && !viewer.getControl().isDisposed() ){
246
            viewer.setInput(null);
247
            try{
248
	            if (!viewer.getControl().isDisposed()){
249
	                viewer.getControl().setEnabled(false);
250
	            }
251
            }catch(SWTException e){
252
            	if (PreferencesUtil.isShowUpWidgetIsDisposedMessages() && e.getMessage().equals("Widget is disposed")){
253
                    MessagingUtils.errorDialog("Widget is disposed",
254
                            null,
255
                            MessagingUtils.WIDGET_IS_DISPOSED_MESSAGE,
256
                            null,
257
                            e,
258
                            true);
259

    
260
                }
261
            }
262

    
263
        }
264
        reset();
265
        if(thisPart!=null){
266
            thisPart.setLabel(getViewName());
267
        }
268
    }
269

    
270
    protected IStructuredSelection createSelection(Object selection) {
271
        if(selection==null){
272
            return null;
273
        }
274
        IStructuredSelection structuredSelection;
275
        if(!(selection instanceof IStructuredSelection)){
276
            structuredSelection = new StructuredSelection(selection);
277
        }
278
        else{
279
            structuredSelection = (IStructuredSelection) selection;
280
        }
281
        return structuredSelection;
282
    }
283

    
284
    /**
285
     * {@inheritDoc}
286
     */
287
    @Override
288
    public ConversationHolder getConversationHolder() {
289
        if(selectionProvidingPart != null && selectionProvidingPart instanceof IConversationEnabled) {
290
            return ((IConversationEnabled) selectionProvidingPart).getConversationHolder();
291
        }
292
        return null;
293
    }
294

    
295
    /**
296
     * {@inheritDoc}
297
     */
298
    @Override
299
    public boolean postOperation(CdmBase objectAffectedByOperation) {
300
        changed(objectAffectedByOperation);
301
        return true;
302
    }
303

    
304
    /**
305
     * {@inheritDoc}
306
     */
307
    @Override
308
    public boolean onComplete() {
309
        viewer.refresh();
310
        return true;
311
    }
312

    
313
    /**
314
     * {@inheritDoc}
315
     */
316
    @Override
317
    public MPart getSelectionProvidingPart() {
318
        return selectionProvidingPart;
319
    }
320

    
321
    @PreDestroy
322
    private void dispose() {
323
    }
324

    
325
    private void reset(){
326
        previousSelection = null;
327
        selectionProvidingPart = null;
328
        delaySelection = null;
329
        context.deactivate();
330
    }
331

    
332
    @PersistState
333
    private void persistState(){
334

    
335
    }
336

    
337
    /**
338
     * {@inheritDoc}
339
     */
340
    @Override
341
    public void update(CdmDataChangeMap arg0) {
342
    }
343

    
344
    /**
345
     * {@inheritDoc}
346
     */
347
    @Override
348
    public void forceDirty() {
349
    }
350

    
351
    protected abstract String getViewName();
352

    
353
}
(2-2/2)