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

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

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

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

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

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

    
218

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

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

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

    
259
                }
260
            }
261

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

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

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

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

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

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

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

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

    
331
    @PersistState
332
    private void persistState(){
333

    
334
    }
335

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

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

    
350
    protected abstract String getViewName();
351

    
352
}
(2-2/2)