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
        if((previousSelection!=null && selection!=null)
164
               && (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(element != 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
                    if (taxon.isMisapplication() || taxon.isProparteSynonym() ){
245

    
246
                        if(part instanceof ITaxonEditor){
247
                            Taxon accepted = ((ITaxonEditor) part).getTaxon();
248

    
249
                            Set<TaxonRelationship> rels =  taxon.getTaxonRelations(accepted);
250

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

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

    
271
                selectionProvidingPart = activePart;
272
                if (viewer instanceof DetailsViewerE4){
273

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

    
282
                else{
283
                    if (activePart.getObject() instanceof DetailsPartE4 && element instanceof TaxonName){
284
                        selectionProvidingPart = ((DetailsPartE4)activePart.getObject()).getSelectionProvidingPart();
285
                    }
286

    
287
                    viewer.setInput(element);
288
                    viewer.getControl().setEnabled(isEnabled);
289
                }
290
            }
291
        }
292
    }
293

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

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

    
322
        reset();
323
        if(thisPart!=null){
324
            thisPart.setLabel(getViewName());
325
        }
326
    }
327

    
328
    protected IStructuredSelection createSelection(Object selection) {
329
        if(selection==null){
330
            return null;
331
        }
332
        IStructuredSelection structuredSelection;
333
        if(!(selection instanceof IStructuredSelection)){
334
            structuredSelection = new StructuredSelection(selection);
335
        }
336
        else{
337
            structuredSelection = (IStructuredSelection) selection;
338
        }
339
        return structuredSelection;
340
    }
341

    
342
    @Override
343
    public ConversationHolder getConversationHolder() {
344
        if(selectionProvidingPart != null && selectionProvidingPart instanceof IConversationEnabled) {
345
            return ((IConversationEnabled) selectionProvidingPart).getConversationHolder();
346
        }
347
        return null;
348
    }
349

    
350
    @Override
351
    public boolean postOperation(Object objectAffectedByOperation) {
352
        changed(objectAffectedByOperation);
353
        return true;
354
    }
355

    
356
    @Override
357
    public boolean onComplete() {
358
        viewer.refresh();
359
        return true;
360
    }
361

    
362
    @Override
363
    public MPart getSelectionProvidingPart() {
364
        return selectionProvidingPart;
365
    }
366

    
367
    @PreDestroy
368
    private void dispose() {
369
    }
370

    
371
    private void reset(){
372
        previousSelection = null;
373
        selectionProvidingPart = null;
374
        delaySelection = null;
375
        context.deactivate();
376
    }
377

    
378
    @PersistState
379
    private void persistState(){
380

    
381
    }
382

    
383
    @Override
384
    public void update(CdmDataChangeMap arg0) {
385
    }
386

    
387
    @Override
388
    public void forceDirty() {
389
    }
390

    
391
    protected abstract String getViewName();
392

    
393
}
(2-2/2)