Project

General

Profile

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

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

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

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

    
82
        public DelaySelection(Object selection, MPart activePart, MPart thisPart) {
83
            super();
84
            this.selection = selection;
85
            this.activePart= activePart;
86
            this.thisPart = thisPart;
87
        }
88

    
89
        @Override
90
        public void run() {
91
            try{
92
                selectionChanged_internal(selection, activePart, thisPart);
93
            }
94
            finally{
95
                isInDelay = false;
96
            }
97
        }
98

    
99
        public synchronized void setSelection(Object selection) {
100
            this.selection = selection;
101
        }
102

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

    
107
        public synchronized void setThisPart(MPart thisPart) {
108
            this.thisPart = thisPart;
109
        }
110

    
111
    }
112

    
113
    protected Viewer viewer;
114

    
115
    protected MPart thisPart;
116

    
117
    protected MPart selectionProvidingPart;
118

    
119
    protected Object previousSelection;
120

    
121
    protected ISelectionChangedListener selectionChangedListener;
122

    
123
    public ISelectionChangedListener getSelectionChangedListener() {
124
        return selectionChangedListener;
125
    }
126

    
127
    @Inject
128
    protected ESelectionService selService;
129

    
130
    @Inject
131
    protected IEclipseContext context;
132

    
133
    protected abstract void selectionChanged_internal(Object selection, MPart activePart, MPart thisPart);
134

    
135
    @Inject
136
    public void selectionChanged(
137
            @Optional@Named(IServiceConstants.ACTIVE_SELECTION)Object selection,
138
            @Optional@Named(IServiceConstants.ACTIVE_PART)MPart activePart,
139
            MPart thisPart, UISynchronize sync, EPartService partService){
140
        //multiple selections are not supported
141
        if(activePart!=null
142
                && thisPart!=null
143
                && !activePart.equals(thisPart)
144
                && selection instanceof IStructuredSelection
145
                && ((IStructuredSelection) selection).size()>1){
146
            showEmptyPage();
147
            return;
148
        }
149
        // no active editor found
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 && selection!=null) && (activePart != null &&  selectionProvidingPart != null && activePart.equals(selectionProvidingPart)) &&
164
                (previousSelection==selection
165
                || previousSelection.equals(selection)
166
                || new StructuredSelection(selection).equals(previousSelection))
167
                ) {
168
            return;
169
        }
170
        if(delaySelection==null){
171
            delaySelection = new DelaySelection(selection, activePart, thisPart);
172
        }
173
        delaySelection.setSelection(selection);
174
        delaySelection.setActivePart(activePart);
175
        delaySelection.setThisPart(thisPart);
176
        if(!isInDelay){
177
            isInDelay = true;
178
            sync.asyncExec(delaySelection);
179
            previousSelection = selection;
180
        }
181
    }
182

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

    
194
    public Viewer getViewer() {
195
        return viewer;
196
    }
197

    
198
    public boolean isEnabled() {
199
        return isEnabled;
200
    }
201

    
202
    public void setEnabled(boolean isEnabled) {
203
        this.isEnabled = isEnabled;
204
    }
205

    
206
    protected void showViewer(IStructuredSelection selection, MPart activePart, Viewer viewer){
207

    
208
        if(viewer!=null && viewer.getControl()!=null && !viewer.getControl().isDisposed()){
209
            Object element = selection.getFirstElement();
210
            Object part = createPartObject(activePart);
211
            viewer.getControl().setEnabled(true);
212
            if(selection.getFirstElement()!=null){
213
                if (element instanceof Taxon){
214

    
215
                    Taxon taxon = HibernateProxyHelper.deproxy(element, Taxon.class);
216
                    if (part instanceof ITaxonEditor){
217
                        TaxonNode node = ((ITaxonEditor) part).getTaxonNode();
218
                        if (node != null){
219
                            boolean doEnable = CdmStore.currentAuthentiationHasPermission(node,
220
                                    RequiredPermissions.TAXON_EDIT);
221
                            //TODO: differ between the views
222
                            //this.isEnabled = doEnable;
223
                        }
224

    
225

    
226
                    }
227
                    if (taxon.isMisapplication() || taxon.isProparteSynonym() || taxon.isInvalidDesignation()){
228

    
229
                        if(part instanceof ITaxonEditor){
230
                            Taxon accepted = ((ITaxonEditor) part).getTaxon();
231

    
232
                            Set<TaxonRelationship> rels =  taxon.getTaxonRelations(accepted);
233

    
234
                            if (rels != null && rels.iterator().hasNext() && !taxon.equals(accepted)){
235
                                TaxonRelationship rel = rels.iterator().next();
236
                                if ((rel.getType().isMisappliedNameOrInvalidDesignation() || rel.getType().isAnySynonym()) && !rel.getFromTaxon().equals(((ITaxonEditor) part).getTaxon())){
237
                                    viewer.setInput(rel);
238
                                    selectionProvidingPart = activePart;
239
                                    return;
240
                                }
241
                            }
242
                        }
243

    
244

    
245
                    }
246
                }
247
                //unwrap term DTOs
248
                if(element instanceof TermDto){
249
                    element = CdmStore.getService(ITermService.class).load(((TermDto) element).getUuid());
250
                }
251
                else if(element instanceof TermVocabularyDto){
252
                    element = CdmStore.getService(IVocabularyService.class).load(((TermVocabularyDto) element).getUuid());
253
                }
254

    
255
                selectionProvidingPart = activePart;
256
                if (viewer instanceof DetailsViewerE4){
257

    
258
                    if (!selectionProvidingPart.getElementId().equals("eu.etaxonomy.taxeditor.editor.view.checklist.e4.DistributionEditorPart")){
259
                        ((DetailsViewerE4)viewer).setDetailsEnabled(true);
260
                    }
261
                    ((DetailsViewerE4)viewer).setDetailsEnabled(isEnabled);
262
                    ((DetailsViewerE4)viewer).setInput(element, part);
263
                }
264

    
265
                else{
266
                    if (activePart.getObject() instanceof DetailsPartE4 && element instanceof TaxonName){
267
                        selectionProvidingPart = ((DetailsPartE4)activePart.getObject()).getSelectionProvidingPart();
268
                    }
269
                    viewer.setInput(element);
270
                }
271
            }
272
        }
273
    }
274

    
275
    protected Object createPartObject(MPart activePart) {
276
        Object partObject = activePart;
277
        Object wrappedPart = WorkbenchUtility.getE4WrappedPart(activePart);
278
        if(wrappedPart!=null){
279
            partObject = wrappedPart;
280
        }
281
        return partObject;
282
    }
283

    
284
    protected void showEmptyPage() {
285
        if(viewer!=null && viewer.getControl()!=null && !viewer.getControl().isDisposed() ){
286
            viewer.setInput(null);
287
            try{
288
	            if (!viewer.getControl().isDisposed()){
289
	                viewer.getControl().setEnabled(false);
290
	            }
291
            }catch(SWTException e){
292
            	if (PreferencesUtil.isShowUpWidgetIsDisposedMessages() && e.getMessage().equals("Widget is disposed")){
293
                    MessagingUtils.errorDialog("Widget is disposed",
294
                            null,
295
                            MessagingUtils.WIDGET_IS_DISPOSED_MESSAGE,
296
                            null,
297
                            e,
298
                            true);
299

    
300
                }
301
            }
302

    
303
        }
304
        reset();
305
        if(thisPart!=null){
306
            thisPart.setLabel(getViewName());
307
        }
308
    }
309

    
310
    protected IStructuredSelection createSelection(Object selection) {
311
        if(selection==null){
312
            return null;
313
        }
314
        IStructuredSelection structuredSelection;
315
        if(!(selection instanceof IStructuredSelection)){
316
            structuredSelection = new StructuredSelection(selection);
317
        }
318
        else{
319
            structuredSelection = (IStructuredSelection) selection;
320
        }
321
        return structuredSelection;
322
    }
323

    
324
    /**
325
     * {@inheritDoc}
326
     */
327
    @Override
328
    public ConversationHolder getConversationHolder() {
329
        if(selectionProvidingPart != null && selectionProvidingPart instanceof IConversationEnabled) {
330
            return ((IConversationEnabled) selectionProvidingPart).getConversationHolder();
331
        }
332
        return null;
333
    }
334

    
335
    /**
336
     * {@inheritDoc}
337
     */
338
    @Override
339
    public boolean postOperation(Object objectAffectedByOperation) {
340
        changed(objectAffectedByOperation);
341
        return true;
342
    }
343

    
344
    /**
345
     * {@inheritDoc}
346
     */
347
    @Override
348
    public boolean onComplete() {
349
        viewer.refresh();
350
        return true;
351
    }
352

    
353
    /**
354
     * {@inheritDoc}
355
     */
356
    @Override
357
    public MPart getSelectionProvidingPart() {
358
        return selectionProvidingPart;
359
    }
360

    
361
    @PreDestroy
362
    private void dispose() {
363
    }
364

    
365
    private void reset(){
366
        previousSelection = null;
367
        selectionProvidingPart = null;
368
        delaySelection = null;
369
        context.deactivate();
370
    }
371

    
372
    @PersistState
373
    private void persistState(){
374

    
375
    }
376

    
377
    /**
378
     * {@inheritDoc}
379
     */
380
    @Override
381
    public void update(CdmDataChangeMap arg0) {
382
    }
383

    
384
    /**
385
     * {@inheritDoc}
386
     */
387
    @Override
388
    public void forceDirty() {
389
    }
390

    
391
    protected abstract String getViewName();
392

    
393
}
(2-2/2)