Project

General

Profile

Download (12.1 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.api.service.ITermService;
36
import eu.etaxonomy.cdm.api.service.IVocabularyService;
37
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
38
import eu.etaxonomy.cdm.model.taxon.Taxon;
39
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
40
import eu.etaxonomy.cdm.persistence.dto.TermDto;
41
import eu.etaxonomy.cdm.persistence.dto.TermVocabularyDto;
42
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
43
import eu.etaxonomy.taxeditor.editor.ITaxonEditor;
44
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
45
import eu.etaxonomy.taxeditor.model.MessagingUtils;
46
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
47
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
48
import eu.etaxonomy.taxeditor.store.CdmStore;
49
import eu.etaxonomy.taxeditor.view.e4.details.DetailsViewerE4;
50
import eu.etaxonomy.taxeditor.workbench.WorkbenchUtility;
51
import eu.etaxonomy.taxeditor.workbench.part.ISelectionElementEditingPart;
52

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

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

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

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

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

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

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

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

    
107
    }
108

    
109
    protected Viewer viewer;
110

    
111
    protected MPart thisPart;
112

    
113
    protected MPart selectionProvidingPart;
114

    
115
    protected Object previousSelection;
116

    
117
    public Object getPreviousSelection() {
118
        return previousSelection;
119
    }
120

    
121
    public void setPreviousSelection(Object previousSelection) {
122
        this.previousSelection = previousSelection;
123
    }
124

    
125
    protected ISelectionChangedListener selectionChangedListener;
126

    
127
    public ISelectionChangedListener getSelectionChangedListener() {
128
        return selectionChangedListener;
129
    }
130

    
131
    @Inject
132
    protected ESelectionService selService;
133

    
134
    @Inject
135
    protected IEclipseContext context;
136

    
137
    protected abstract void selectionChanged_internal(Object selection, MPart activePart, MPart thisPart);
138

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

    
164
        }
165

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

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

    
199
    public Viewer getViewer() {
200
        return viewer;
201
    }
202

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

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

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

    
227

    
228
                    }
229
                }
230
                //unwrap term DTOs
231
                if(element instanceof TermDto){
232
                    element = CdmStore.getService(ITermService.class).load(((TermDto) element).getUuid());
233
                }
234
                else if(element instanceof TermVocabularyDto){
235
                    element = CdmStore.getService(IVocabularyService.class).load(((TermVocabularyDto) element).getUuid());
236
                }
237
                if (viewer instanceof DetailsViewerE4){
238
                    ((DetailsViewerE4)viewer).setInput(element, part);
239
                    ((DetailsViewerE4)viewer).setDetailsEnabled(true);
240
                }
241
                else{
242
                    viewer.setInput(element);
243
                }
244
                selectionProvidingPart = activePart;
245
            }
246
        }
247
    }
248

    
249
    protected Object createPartObject(MPart activePart) {
250
        Object partObject = activePart;
251
        Object wrappedPart = WorkbenchUtility.getE4WrappedPart(activePart);
252
        if(wrappedPart!=null){
253
            partObject = wrappedPart;
254
        }
255
        return partObject;
256
    }
257

    
258
    protected void showEmptyPage() {
259
        if(viewer!=null && viewer.getControl()!=null && !viewer.getControl().isDisposed() ){
260
            viewer.setInput(null);
261
            try{
262
	            if (!viewer.getControl().isDisposed()){
263
	                viewer.getControl().setEnabled(false);
264
	            }
265
            }catch(SWTException e){
266
            	if (PreferencesUtil.isShowUpWidgetIsDisposedMessages() && e.getMessage().equals("Widget is disposed")){
267
                    MessagingUtils.errorDialog("Widget is disposed",
268
                            null,
269
                            MessagingUtils.WIDGET_IS_DISPOSED_MESSAGE,
270
                            null,
271
                            e,
272
                            true);
273

    
274
                }
275
            }
276

    
277
        }
278
        reset();
279
        if(thisPart!=null){
280
            thisPart.setLabel(getViewName());
281
        }
282
    }
283

    
284
    protected IStructuredSelection createSelection(Object selection) {
285
        if(selection==null){
286
            return null;
287
        }
288
        IStructuredSelection structuredSelection;
289
        if(!(selection instanceof IStructuredSelection)){
290
            structuredSelection = new StructuredSelection(selection);
291
        }
292
        else{
293
            structuredSelection = (IStructuredSelection) selection;
294
        }
295
        return structuredSelection;
296
    }
297

    
298
    /**
299
     * {@inheritDoc}
300
     */
301
    @Override
302
    public ConversationHolder getConversationHolder() {
303
        if(selectionProvidingPart != null && selectionProvidingPart instanceof IConversationEnabled) {
304
            return ((IConversationEnabled) selectionProvidingPart).getConversationHolder();
305
        }
306
        return null;
307
    }
308

    
309
    /**
310
     * {@inheritDoc}
311
     */
312
    @Override
313
    public boolean postOperation(Object objectAffectedByOperation) {
314
        changed(objectAffectedByOperation);
315
        return true;
316
    }
317

    
318
    /**
319
     * {@inheritDoc}
320
     */
321
    @Override
322
    public boolean onComplete() {
323
        viewer.refresh();
324
        return true;
325
    }
326

    
327
    /**
328
     * {@inheritDoc}
329
     */
330
    @Override
331
    public MPart getSelectionProvidingPart() {
332
        return selectionProvidingPart;
333
    }
334

    
335
    @PreDestroy
336
    private void dispose() {
337
    }
338

    
339
    private void reset(){
340
        previousSelection = null;
341
        selectionProvidingPart = null;
342
        delaySelection = null;
343
        context.deactivate();
344
    }
345

    
346
    @PersistState
347
    private void persistState(){
348

    
349
    }
350

    
351
    /**
352
     * {@inheritDoc}
353
     */
354
    @Override
355
    public void update(CdmDataChangeMap arg0) {
356
    }
357

    
358
    /**
359
     * {@inheritDoc}
360
     */
361
    @Override
362
    public void forceDirty() {
363
    }
364

    
365
    protected abstract String getViewName();
366

    
367
}
(2-2/2)