Project

General

Profile

Download (14.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.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
    protected boolean showEmptyIfNoActiveEditor(){
137
        return true;
138
    }
139

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

    
167
        if((previousSelection!=null && selection!=null)
168
               && (activePart != null &&  selectionProvidingPart != null && activePart.equals(selectionProvidingPart))
169
               && (previousSelection==selection
170
                    || previousSelection.equals(selection)
171
                    || new StructuredSelection(selection).equals(previousSelection))
172
                ) {
173
            return;
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
    @Override
189
    public void changed(Object object) {
190
        if(selectionProvidingPart!=null){
191
            Object part = selectionProvidingPart.getObject();
192
            if(part instanceof IDirtyMarkable){
193
                ((IDirtyMarkable) part).changed(object);
194
            }
195
        }
196
    }
197

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

    
202
    public boolean isEnabled() {
203
        return isEnabled;
204
    }
205

    
206
    public void setEnabled(boolean isEnabled) {
207
        this.isEnabled = isEnabled;
208
    }
209

    
210
    protected void showViewer(IStructuredSelection selection, MPart activePart, Viewer viewer){
211
        if(CdmStore.getCurrentSessionManager().getActiveSession()==null){
212
            return;
213
        }
214
        if(viewer!=null && viewer.getControl()!=null && !viewer.getControl().isDisposed()){
215
            Object element = selection.getFirstElement();
216
            Object part = createPartObject(activePart);
217
            if (viewer.getControl().isDisposed()){
218
                return;
219
            }
220
            viewer.getControl().setEnabled(true);
221
            if(element != null){
222
                if (element instanceof Taxon){
223

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

    
244
                            //TODO: differ between the views
245
                            this.isEnabled = doEnable;
246
                        }
247
                    }
248
                    if (taxon.isMisapplication() || taxon.isProparteSynonym() ){
249

    
250
                        if(part instanceof ITaxonEditor){
251
                            Taxon accepted = ((ITaxonEditor) part).getTaxon();
252

    
253
                            Set<TaxonRelationship> rels =  taxon.getTaxonRelations(accepted);
254

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

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

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

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

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

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

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

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