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

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

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

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

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

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

    
224

    
225
                    }
226
                }
227
                if (element instanceof Distribution && part instanceof IDistributionEditor && viewer instanceof DetailsViewerE4){
228

    
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(Object 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)