Project

General

Profile

Download (11.7 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.ISelection;
27
import org.eclipse.jface.viewers.ISelectionChangedListener;
28
import org.eclipse.jface.viewers.IStructuredSelection;
29
import org.eclipse.jface.viewers.StructuredSelection;
30
import org.eclipse.jface.viewers.Viewer;
31
import org.eclipse.swt.SWTException;
32

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

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

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

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

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

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

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

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

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

    
105
    }
106

    
107
    protected Viewer viewer;
108

    
109
    protected MPart thisPart;
110

    
111
    protected MPart selectionProvidingPart;
112

    
113
    protected Object previousSelection;
114

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

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

    
123
    protected ISelectionChangedListener selectionChangedListener;
124

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

    
129
    @Inject
130
    protected ESelectionService selService;
131

    
132
    @Inject
133
    protected IEclipseContext context;
134

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

    
137
    @Inject
138
    public void selectionChanged(
139
            @Optional@Named(IServiceConstants.ACTIVE_SELECTION)Object selection,
140
            @Optional@Named(IServiceConstants.ACTIVE_PART)MPart activePart,
141
            MPart thisPart, UISynchronize sync, EPartService partService){
142
        //multiple selections are not supported
143
        if(activePart!=null
144
                && thisPart!=null
145
                && !activePart.equals(thisPart)
146
                && selection instanceof IStructuredSelection
147
                && ((IStructuredSelection) selection).size()>1){
148
            showEmptyPage();
149
            return;
150
        }
151
        if(activePart==thisPart && WorkbenchUtility.getActiveEditorPart(partService)==null){
152
            showEmptyPage();
153
            return;
154
        }
155
        if (viewer != null && viewer.getControl()!= null && viewer.getInput() != null && !viewer.getControl().isDisposed()){
156
           try{
157
               viewer.getControl().setEnabled(true);
158
           }catch(SWTException e){
159
              logger.debug("Something went wrong for viewer.getControl().setEnabled(true) in " + this.getClass().getSimpleName(), e);
160
           }
161

    
162
        }
163

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

    
186
    /** {@inheritDoc} */
187
    @Override
188
    public void changed(Object object) {
189
        if(selectionProvidingPart!=null){
190
            Object part = selectionProvidingPart.getObject();
191
            if(part instanceof IDirtyMarkable){
192
                ((IDirtyMarkable) part).changed(object);
193
            }
194
        }
195
    }
196

    
197
    public Viewer getViewer() {
198
        return viewer;
199
    }
200

    
201
    protected void showViewer(IStructuredSelection selection, MPart activePart, Viewer viewer){
202
        if(viewer!=null && viewer.getControl()!=null && !viewer.getControl().isDisposed()){
203
            Object element = selection.getFirstElement();
204
            Object part = createPartObject(activePart);
205
            viewer.getControl().setEnabled(true);
206
            if(selection.getFirstElement()!=null){
207
                if (element instanceof Taxon){
208
                    Taxon taxon = HibernateProxyHelper.deproxy(element, Taxon.class);
209
                    if (taxon.isMisapplication() || taxon.isProparteSynonym() || taxon.isInvalidDesignation()){
210

    
211
                        if(part instanceof ITaxonEditor){
212
                            Taxon accepted = ((ITaxonEditor) part).getTaxon();
213
                            Set<TaxonRelationship> rels =  taxon.getTaxonRelations(accepted);
214

    
215
                            if (rels != null && rels.iterator().hasNext() && !taxon.equals(accepted)){
216
                                TaxonRelationship rel = rels.iterator().next();
217
                                if ((rel.getType().isMisappliedNameOrInvalidDesignation() || rel.getType().isAnySynonym()) && !rel.getFromTaxon().equals(((ITaxonEditor) part).getTaxon())){
218
                                    viewer.setInput(rel);
219
                                    selectionProvidingPart = activePart;
220
                                    return;
221
                                }
222
                            }
223
                        }
224

    
225

    
226
                    }
227
                }
228
                if (element instanceof Distribution && part instanceof IDistributionEditor){
229
                    ((DetailsViewerE4)viewer).setInput(element, part);
230
                }else{
231
                    viewer.setInput(element);
232
                    if (viewer instanceof DetailsViewerE4){
233
                    	((DetailsViewerE4)viewer).setDetailsEnabled(true);
234
                    }
235
                }
236
                selectionProvidingPart = activePart;
237
            }
238
        }
239
    }
240

    
241
    protected Object createPartObject(MPart activePart) {
242
        Object partObject = activePart;
243
        Object wrappedPart = WorkbenchUtility.getE4WrappedPart(activePart);
244
        if(wrappedPart!=null){
245
            partObject = wrappedPart;
246
        }
247
        return partObject;
248
    }
249

    
250
    protected void showEmptyPage() {
251
        if(viewer!=null && viewer.getControl()!=null && !viewer.getControl().isDisposed() ){
252
            viewer.setInput(null);
253
            try{
254
	            if (!viewer.getControl().isDisposed()){
255
	                viewer.getControl().setEnabled(false);
256
	            }
257
            }catch(SWTException e){
258
            	if (PreferencesUtil.isShowUpWidgetIsDisposedMessages() && e.getMessage().equals("Widget is disposed")){
259
                    MessagingUtils.errorDialog("Widget is disposed",
260
                            null,
261
                            MessagingUtils.WIDGET_IS_DISPOSED_MESSAGE,
262
                            null,
263
                            e,
264
                            true);
265

    
266
                }
267
            }
268

    
269
        }
270
        reset();
271
        if(thisPart!=null){
272
            thisPart.setLabel(getViewName());
273
        }
274
    }
275

    
276
    protected IStructuredSelection createSelection(Object selection) {
277
        if(selection==null){
278
            return null;
279
        }
280
        IStructuredSelection structuredSelection;
281
        if(!(selection instanceof IStructuredSelection)){
282
            structuredSelection = new StructuredSelection(selection);
283
        }
284
        else{
285
            structuredSelection = (IStructuredSelection) selection;
286
        }
287
        return structuredSelection;
288
    }
289

    
290
    /**
291
     * {@inheritDoc}
292
     */
293
    @Override
294
    public ConversationHolder getConversationHolder() {
295
        if(selectionProvidingPart != null && selectionProvidingPart instanceof IConversationEnabled) {
296
            return ((IConversationEnabled) selectionProvidingPart).getConversationHolder();
297
        }
298
        return null;
299
    }
300

    
301
    /**
302
     * {@inheritDoc}
303
     */
304
    @Override
305
    public boolean postOperation(CdmBase objectAffectedByOperation) {
306
        changed(objectAffectedByOperation);
307
        return true;
308
    }
309

    
310
    /**
311
     * {@inheritDoc}
312
     */
313
    @Override
314
    public boolean onComplete() {
315
        viewer.refresh();
316
        return true;
317
    }
318

    
319
    /**
320
     * {@inheritDoc}
321
     */
322
    @Override
323
    public MPart getSelectionProvidingPart() {
324
        return selectionProvidingPart;
325
    }
326

    
327
    @PreDestroy
328
    private void dispose() {
329
    }
330

    
331
    private void reset(){
332
        previousSelection = null;
333
        selectionProvidingPart = null;
334
        delaySelection = null;
335
        context.deactivate();
336
    }
337

    
338
    @PersistState
339
    private void persistState(){
340

    
341
    }
342

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

    
350
    /**
351
     * {@inheritDoc}
352
     */
353
    @Override
354
    public void forceDirty() {
355
    }
356

    
357
    protected abstract String getViewName();
358

    
359
}
(2-2/2)