Project

General

Profile

Download (14.2 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.Collection;
12
import java.util.Set;
13

    
14
import javax.annotation.PreDestroy;
15
import javax.inject.Inject;
16
import javax.inject.Named;
17

    
18
import org.apache.log4j.Logger;
19
import org.eclipse.e4.core.contexts.IEclipseContext;
20
import org.eclipse.e4.core.di.annotations.Optional;
21
import org.eclipse.e4.ui.di.PersistState;
22
import org.eclipse.e4.ui.di.UISynchronize;
23
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
24
import org.eclipse.e4.ui.services.IServiceConstants;
25
import org.eclipse.e4.ui.workbench.modeling.EPartService;
26
import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
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
import org.springframework.security.core.GrantedAuthority;
33

    
34
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
35
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
36
import eu.etaxonomy.cdm.api.service.ITermService;
37
import eu.etaxonomy.cdm.api.service.IVocabularyService;
38
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
39
import eu.etaxonomy.cdm.model.name.TaxonName;
40
import eu.etaxonomy.cdm.model.taxon.Taxon;
41
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
42
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
43
import eu.etaxonomy.cdm.persistence.dto.TermDto;
44
import eu.etaxonomy.cdm.persistence.dto.TermVocabularyDto;
45
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
46
import eu.etaxonomy.taxeditor.editor.ITaxonEditor;
47
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
48
import eu.etaxonomy.taxeditor.model.MessagingUtils;
49
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
50
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
51
import eu.etaxonomy.taxeditor.security.RequiredPermissions;
52
import eu.etaxonomy.taxeditor.store.CdmStore;
53
import eu.etaxonomy.taxeditor.view.e4.details.DetailsPartE4;
54
import eu.etaxonomy.taxeditor.view.e4.details.DetailsViewerE4;
55
import eu.etaxonomy.taxeditor.workbench.WorkbenchUtility;
56
import eu.etaxonomy.taxeditor.workbench.part.ISelectionElementEditingPart;
57

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

    
65
	private static final Logger logger = Logger.getLogger(AbstractCdmEditorPartE4.class);
66

    
67
	private DelaySelection delaySelection = null;
68
    /**
69
     * This is the monitor for the DelaySelection runnable.
70
     * If it is <code>true</code> then it is currently delaying a selection.
71
     */
72
    private boolean isInDelay;
73
    private boolean isEnabled = true;
74

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

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

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

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

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

    
109
        public synchronized void setThisPart(MPart thisPart) {
110
            this.thisPart = thisPart;
111
        }
112
    }
113

    
114
    protected Viewer viewer;
115

    
116
    protected MPart thisPart;
117

    
118
    protected MPart selectionProvidingPart;
119

    
120
    protected Object previousSelection;
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
        // no active editor found
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(isEnabled);
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 && selection!=null) && (activePart != null &&  selectionProvidingPart != null && activePart.equals(selectionProvidingPart)) &&
165
                (previousSelection==selection
166
                || previousSelection.equals(selection)
167
                || new StructuredSelection(selection).equals(previousSelection))
168
                ) {
169
            return;
170
        }
171
        if(delaySelection==null){
172
            delaySelection = new DelaySelection(selection, activePart, thisPart);
173
        }
174
        delaySelection.setSelection(selection);
175
        delaySelection.setActivePart(activePart);
176
        delaySelection.setThisPart(thisPart);
177
        if(!isInDelay){
178
            isInDelay = true;
179
            sync.asyncExec(delaySelection);
180
            previousSelection = selection;
181
        }
182
    }
183

    
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
        if(CdmStore.getCurrentSessionManager().getActiveSession()==null){
208
            return;
209
        }
210
        if(viewer!=null && viewer.getControl()!=null && !viewer.getControl().isDisposed()){
211
            Object element = selection.getFirstElement();
212
            Object part = createPartObject(activePart);
213
            if (viewer.getControl().isDisposed()){
214
                return;
215
            }
216
            viewer.getControl().setEnabled(true);
217
            if(selection.getFirstElement()!=null){
218
                if (element instanceof Taxon){
219

    
220
                    Taxon taxon = HibernateProxyHelper.deproxy(element, Taxon.class);
221
                    if (part instanceof ITaxonEditor){
222
                        TaxonNode node = ((ITaxonEditor) part).getTaxonNode();
223
                        if (node != null){
224
                            boolean doEnable = CdmStore.currentAuthentiationHasPermission(node,
225
                                    RequiredPermissions.TAXON_EDIT);
226
                             if (!doEnable){
227
                                 //check whether there are explicit TaxonNode rights
228
                                 boolean taxonnodePermissionExists = false;
229
                                 Collection<? extends GrantedAuthority> authorities = CdmStore.getCurrentAuthentiation().getAuthorities();
230
                                 for (GrantedAuthority grantedAuthority: authorities){
231
                                     if (grantedAuthority.getAuthority().startsWith("TAXONNODE")){
232
                                         taxonnodePermissionExists = true;
233
                                     }
234
                                 }
235
                                 if (!taxonnodePermissionExists){
236
                                     doEnable = true;
237
                                 }
238
                             }
239

    
240
                            //TODO: differ between the views
241
                            this.isEnabled = doEnable;
242
                        }
243

    
244

    
245
                    }
246
                    if (taxon.isMisapplication() || taxon.isProparteSynonym() ){
247

    
248
                        if(part instanceof ITaxonEditor){
249
                            Taxon accepted = ((ITaxonEditor) part).getTaxon();
250

    
251
                            Set<TaxonRelationship> rels =  taxon.getTaxonRelations(accepted);
252

    
253
                            if (rels != null && rels.iterator().hasNext() && !taxon.equals(accepted)){
254
                                TaxonRelationship rel = rels.iterator().next();
255
                                if ((rel.getType().isMisappliedName() || rel.getType().isAnySynonym()) && !rel.getFromTaxon().equals(((ITaxonEditor) part).getTaxon())){
256
                                    viewer.setInput(rel);
257
                                    selectionProvidingPart = activePart;
258
                                    return;
259
                                }
260
                            }
261
                        }
262

    
263

    
264
                    }
265
                }
266
                //unwrap term DTOs
267
                if(element instanceof TermDto){
268
                    element = CdmStore.getService(ITermService.class).load(((TermDto) element).getUuid());
269
                }
270
                else if(element instanceof TermVocabularyDto){
271
                    element = CdmStore.getService(IVocabularyService.class).load(((TermVocabularyDto) element).getUuid());
272
                }
273

    
274
                selectionProvidingPart = activePart;
275
                if (viewer instanceof DetailsViewerE4){
276

    
277
                    if (selectionProvidingPart.getElementId().equals("eu.etaxonomy.taxeditor.editor.view.checklist.e4.DistributionEditorPart")){
278
                        ((DetailsViewerE4)viewer).setDetailsEnabled(false);
279
                    }else{
280
                        ((DetailsViewerE4)viewer).setDetailsEnabled(isEnabled);
281
                    }
282
                    ((DetailsViewerE4)viewer).setInput(element, part);
283
                }
284

    
285
                else{
286
                    if (activePart.getObject() instanceof DetailsPartE4 && element instanceof TaxonName){
287
                        selectionProvidingPart = ((DetailsPartE4)activePart.getObject()).getSelectionProvidingPart();
288
                    }
289

    
290
                    viewer.setInput(element);
291
                    viewer.getControl().setEnabled(isEnabled);
292
                }
293
            }
294
        }
295
    }
296

    
297
    protected Object createPartObject(MPart activePart) {
298
        Object partObject = activePart;
299
        Object wrappedPart = WorkbenchUtility.getE4WrappedPart(activePart);
300
        if(wrappedPart!=null){
301
            partObject = wrappedPart;
302
        }
303
        return partObject;
304
    }
305

    
306
    protected void showEmptyPage() {
307
        if(viewer!=null && viewer.getControl()!=null && !viewer.getControl().isDisposed() ){
308
            viewer.setInput(null);
309
            try{
310
	            if (!viewer.getControl().isDisposed()){
311
	                viewer.getControl().setEnabled(false);
312
	            }
313
            }catch(SWTException e){
314
            	if (PreferencesUtil.isShowUpWidgetIsDisposedMessages() && e.getMessage().equals("Widget is disposed")){
315
                    MessagingUtils.errorDialog("Widget is disposed",
316
                            null,
317
                            MessagingUtils.WIDGET_IS_DISPOSED_MESSAGE,
318
                            null,
319
                            e,
320
                            true);
321

    
322
                }
323
            }
324

    
325
        }
326
        reset();
327
        if(thisPart!=null){
328
            thisPart.setLabel(getViewName());
329
        }
330
    }
331

    
332
    protected IStructuredSelection createSelection(Object selection) {
333
        if(selection==null){
334
            return null;
335
        }
336
        IStructuredSelection structuredSelection;
337
        if(!(selection instanceof IStructuredSelection)){
338
            structuredSelection = new StructuredSelection(selection);
339
        }
340
        else{
341
            structuredSelection = (IStructuredSelection) selection;
342
        }
343
        return structuredSelection;
344
    }
345

    
346
    @Override
347
    public ConversationHolder getConversationHolder() {
348
        if(selectionProvidingPart != null && selectionProvidingPart instanceof IConversationEnabled) {
349
            return ((IConversationEnabled) selectionProvidingPart).getConversationHolder();
350
        }
351
        return null;
352
    }
353

    
354
    @Override
355
    public boolean postOperation(Object objectAffectedByOperation) {
356
        changed(objectAffectedByOperation);
357
        return true;
358
    }
359

    
360
    @Override
361
    public boolean onComplete() {
362
        viewer.refresh();
363
        return true;
364
    }
365

    
366
    @Override
367
    public MPart getSelectionProvidingPart() {
368
        return selectionProvidingPart;
369
    }
370

    
371
    @PreDestroy
372
    private void dispose() {
373
    }
374

    
375
    private void reset(){
376
        previousSelection = null;
377
        selectionProvidingPart = null;
378
        delaySelection = null;
379
        context.deactivate();
380
    }
381

    
382
    @PersistState
383
    private void persistState(){
384

    
385
    }
386

    
387
    @Override
388
    public void update(CdmDataChangeMap arg0) {
389
    }
390

    
391
    @Override
392
    public void forceDirty() {
393
    }
394

    
395
    protected abstract String getViewName();
396

    
397
}
(2-2/2)