Project

General

Profile

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

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

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

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

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

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

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

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

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

    
106
    }
107

    
108
    protected Viewer viewer;
109

    
110
    protected MPart thisPart;
111

    
112
    protected MPart selectionProvidingPart;
113

    
114
    protected Object previousSelection;
115

    
116
    protected ISelectionChangedListener selectionChangedListener;
117

    
118
    public ISelectionChangedListener getSelectionChangedListener() {
119
        return selectionChangedListener;
120
    }
121

    
122
    @Inject
123
    protected ESelectionService selService;
124

    
125
    @Inject
126
    protected IEclipseContext context;
127

    
128
    protected abstract void selectionChanged_internal(Object selection, MPart activePart, MPart thisPart);
129

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

    
156
        }
157

    
158
        if((previousSelection!=null && selection!=null) &&
159
                (previousSelection==selection
160
                || previousSelection.equals(selection)
161
                || new StructuredSelection(selection).equals(previousSelection))
162
                ){
163
            return;
164
        }
165
        if(delaySelection==null){
166
            delaySelection = new DelaySelection(selection, activePart, thisPart);
167
        }
168
        delaySelection.setSelection(selection);
169
        delaySelection.setActivePart(activePart);
170
        delaySelection.setThisPart(thisPart);
171
        if(!isInDelay){
172
            isInDelay = true;
173
            sync.asyncExec(delaySelection);
174
            previousSelection = selection;
175
        }
176
    }
177

    
178
    /** {@inheritDoc} */
179
    @Override
180
    public void changed(Object object) {
181
        if(selectionProvidingPart!=null){
182
            Object part = selectionProvidingPart.getObject();
183
            if(part instanceof IDirtyMarkable){
184
                ((IDirtyMarkable) part).changed(object);
185
            }
186
        }
187
    }
188

    
189
    public Viewer getViewer() {
190
        return viewer;
191
    }
192

    
193
    protected void showViewer(IStructuredSelection selection, MPart activePart, Viewer viewer){
194
        if(viewer!=null && viewer.getControl()!=null && !viewer.getControl().isDisposed()){
195
            Object element = selection.getFirstElement();
196
            Object part = createPartObject(activePart);
197
            viewer.getControl().setEnabled(true);
198
            if(selection.getFirstElement()!=null){
199
                if (element instanceof Taxon){
200
                    Taxon taxon = HibernateProxyHelper.deproxy(element, Taxon.class);
201
                    if (taxon.isMisapplication() || taxon.isProparteSynonym() || taxon.isInvalidDesignation()){
202

    
203
                        if(part instanceof ITaxonEditor){
204
                            Taxon accepted = ((ITaxonEditor) part).getTaxon();
205
                            Set<TaxonRelationship> rels =  taxon.getTaxonRelations(accepted);
206

    
207
                            if (rels != null && rels.iterator().hasNext() && !taxon.equals(accepted)){
208
                                TaxonRelationship rel = rels.iterator().next();
209
                                if ((rel.getType().isMisappliedNameOrInvalidDesignation() || rel.getType().isAnySynonym()) && !rel.getFromTaxon().equals(((ITaxonEditor) part).getTaxon())){
210
                                    viewer.setInput(rel);
211
                                    selectionProvidingPart = activePart;
212
                                    return;
213
                                }
214
                            }
215
                        }
216

    
217

    
218
                    }
219
                }
220
                //unwrap term DTOs
221
                if(element instanceof TermDto){
222
                    element = CdmStore.getService(ITermService.class).load(((TermDto) element).getUuid());
223
                }
224
                else if(element instanceof TermVocabularyDto){
225
                    element = CdmStore.getService(IVocabularyService.class).load(((TermVocabularyDto) element).getUuid());
226
                }
227
                if (viewer instanceof DetailsViewerE4){
228
                    ((DetailsViewerE4)viewer).setInput(element, part);
229
                    ((DetailsViewerE4)viewer).setDetailsEnabled(true);
230
                }
231
                else{
232
                    viewer.setInput(element);
233
                }
234
                selectionProvidingPart = activePart;
235
            }
236
        }
237
    }
238

    
239
    protected Object createPartObject(MPart activePart) {
240
        Object partObject = activePart;
241
        Object wrappedPart = WorkbenchUtility.getE4WrappedPart(activePart);
242
        if(wrappedPart!=null){
243
            partObject = wrappedPart;
244
        }
245
        return partObject;
246
    }
247

    
248
    protected void showEmptyPage() {
249
        if(viewer!=null && viewer.getControl()!=null && !viewer.getControl().isDisposed() ){
250
            viewer.setInput(null);
251
            try{
252
	            if (!viewer.getControl().isDisposed()){
253
	                viewer.getControl().setEnabled(false);
254
	            }
255
            }catch(SWTException e){
256
            	if (PreferencesUtil.isShowUpWidgetIsDisposedMessages() && e.getMessage().equals("Widget is disposed")){
257
                    MessagingUtils.errorDialog("Widget is disposed",
258
                            null,
259
                            MessagingUtils.WIDGET_IS_DISPOSED_MESSAGE,
260
                            null,
261
                            e,
262
                            true);
263

    
264
                }
265
            }
266

    
267
        }
268
        reset();
269
        if(thisPart!=null){
270
            thisPart.setLabel(getViewName());
271
        }
272
    }
273

    
274
    protected IStructuredSelection createSelection(Object selection) {
275
        if(selection==null){
276
            return null;
277
        }
278
        IStructuredSelection structuredSelection;
279
        if(!(selection instanceof IStructuredSelection)){
280
            structuredSelection = new StructuredSelection(selection);
281
        }
282
        else{
283
            structuredSelection = (IStructuredSelection) selection;
284
        }
285
        return structuredSelection;
286
    }
287

    
288
    /**
289
     * {@inheritDoc}
290
     */
291
    @Override
292
    public ConversationHolder getConversationHolder() {
293
        if(selectionProvidingPart != null && selectionProvidingPart instanceof IConversationEnabled) {
294
            return ((IConversationEnabled) selectionProvidingPart).getConversationHolder();
295
        }
296
        return null;
297
    }
298

    
299
    /**
300
     * {@inheritDoc}
301
     */
302
    @Override
303
    public boolean postOperation(Object objectAffectedByOperation) {
304
        changed(objectAffectedByOperation);
305
        return true;
306
    }
307

    
308
    /**
309
     * {@inheritDoc}
310
     */
311
    @Override
312
    public boolean onComplete() {
313
        viewer.refresh();
314
        return true;
315
    }
316

    
317
    /**
318
     * {@inheritDoc}
319
     */
320
    @Override
321
    public MPart getSelectionProvidingPart() {
322
        return selectionProvidingPart;
323
    }
324

    
325
    @PreDestroy
326
    private void dispose() {
327
    }
328

    
329
    private void reset(){
330
        previousSelection = null;
331
        selectionProvidingPart = null;
332
        delaySelection = null;
333
        context.deactivate();
334
    }
335

    
336
    @PersistState
337
    private void persistState(){
338

    
339
    }
340

    
341
    /**
342
     * {@inheritDoc}
343
     */
344
    @Override
345
    public void update(CdmDataChangeMap arg0) {
346
    }
347

    
348
    /**
349
     * {@inheritDoc}
350
     */
351
    @Override
352
    public void forceDirty() {
353
    }
354

    
355
    protected abstract String getViewName();
356

    
357
}
(2-2/2)