Project

General

Profile

Download (10.9 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.di.annotations.Optional;
19
import org.eclipse.e4.ui.di.PersistState;
20
import org.eclipse.e4.ui.di.UIEventTopic;
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.hibernate.HibernateProxyHelper;
35
import eu.etaxonomy.cdm.model.common.CdmBase;
36
import eu.etaxonomy.cdm.model.description.Distribution;
37
import eu.etaxonomy.cdm.model.taxon.Taxon;
38
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
39
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
40
import eu.etaxonomy.taxeditor.editor.IDistributionEditor;
41
import eu.etaxonomy.taxeditor.editor.ITaxonEditor;
42
import eu.etaxonomy.taxeditor.event.WorkbenchEventConstants;
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.view.e4.details.DetailsViewerE4;
48
import eu.etaxonomy.taxeditor.workbench.WorkbenchUtility;
49
import eu.etaxonomy.taxeditor.workbench.part.ISelectionElementEditingPart;
50

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

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

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

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

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

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

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

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

    
105
    }
106

    
107
    protected Viewer viewer;
108

    
109
    protected MPart thisPart;
110

    
111
    protected MPart selectionProvidingPart;
112

    
113
    protected Object previousSelection;
114

    
115
    public Object getPreviousSelection() {
116
        return previousSelection;
117
    }
118

    
119
    public void setPreviousSelection(Object previousSelection) {
120
        this.previousSelection = previousSelection;
121
    }
122

    
123
    protected ISelectionChangedListener selectionChangedListener;
124

    
125
    public ISelectionChangedListener getSelectionChangedListener() {
126
        return selectionChangedListener;
127
    }
128

    
129
    @Inject
130
    protected ESelectionService selService;
131

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

    
134
    @Inject
135
    public void selectionChanged(
136
            @Optional@Named(IServiceConstants.ACTIVE_SELECTION)Object selection,
137
            @Optional@Named(IServiceConstants.ACTIVE_PART)MPart activePart,
138
            MPart thisPart, UISynchronize sync, EPartService partService){
139
        if(activePart==thisPart && WorkbenchUtility.getActiveEditorPart(partService)==null){
140
            showEmptyPage();
141
            return;
142
        }
143
        if (viewer != null && viewer.getControl()!= null && viewer.getInput() != null && !viewer.getControl().isDisposed()){
144
           try{
145
               viewer.getControl().setEnabled(true);
146
           }catch(SWTException e){
147
              logger.debug("Something went wrong for viewer.getControl().setEnabled(true) in " + this.getClass().getSimpleName(), e);
148
           }
149

    
150
        }
151

    
152

    
153
        if(previousSelection==null ||
154
                previousSelection!=selection){//skip redundant rendering of details view
155
            if(delaySelection==null){
156
                delaySelection = new DelaySelection(selection, activePart, thisPart);
157
            }
158
            delaySelection.setSelection(selection);
159
            delaySelection.setActivePart(activePart);
160
            delaySelection.setThisPart(thisPart);
161
            if(!isInDelay){
162
                isInDelay = true;
163
                sync.asyncExec(delaySelection);
164
                previousSelection = selection;
165
            }
166
        }
167
    }
168

    
169
    /** {@inheritDoc} */
170
    @Override
171
    public void changed(Object object) {
172
        if(selectionProvidingPart!=null){
173
            Object part = selectionProvidingPart.getObject();
174
            if(part instanceof IDirtyMarkable){
175
                ((IDirtyMarkable) part).changed(object);
176
            }
177
        }
178
    }
179

    
180
    @Inject
181
    @Optional
182
    private void clearOnReconnect(@UIEventTopic(WorkbenchEventConstants.RECONNECT)Object o){
183
        showEmptyPage();
184
    }
185

    
186
    public Viewer getViewer() {
187
        return viewer;
188
    }
189

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

    
200
                        if(part instanceof ITaxonEditor){
201
                            Taxon accepted = ((ITaxonEditor) part).getTaxon();
202
                            Set<TaxonRelationship> rels =  taxon.getTaxonRelations(accepted);
203

    
204
                            if (rels != null && rels.iterator().hasNext()){
205
                                TaxonRelationship rel = rels.iterator().next();
206
                                if (rel.getType().isAnyMisappliedName() && !rel.getFromTaxon().equals(((ITaxonEditor) part).getTaxon())){
207
                                    viewer.setInput(rel);
208

    
209
                                    return;
210
                                }
211
                            }
212
                        }
213

    
214

    
215
                    }
216
                }
217
                if (element instanceof Distribution && part instanceof IDistributionEditor){
218
                    ((DetailsViewerE4)viewer).setInput(element, part);
219
                }else{
220
                    viewer.setInput(element);
221
                    if (viewer instanceof DetailsViewerE4){
222
                    	((DetailsViewerE4)viewer).setDetailsEnabled(true);
223
                    }
224
                }
225
                selectionProvidingPart = activePart;
226
            }
227
        }
228
    }
229

    
230
    protected Object createPartObject(MPart activePart) {
231
        Object partObject = activePart;
232
        Object wrappedPart = WorkbenchUtility.getE4WrappedPart(activePart);
233
        if(wrappedPart!=null){
234
            partObject = wrappedPart;
235
        }
236
        return partObject;
237
    }
238

    
239
    protected void showEmptyPage() {
240
        if(viewer!=null && viewer.getControl()!=null && !viewer.getControl().isDisposed() ){
241
            viewer.setInput(null);
242
            try{
243
	            if (!viewer.getControl().isDisposed()){
244
	                viewer.getControl().setEnabled(false);
245
	            }
246
            }catch(SWTException e){
247
            	if (PreferencesUtil.isShowUpWidgetIsDisposedMessages() && e.getMessage().equals("Widget is disposed")){
248
                    MessagingUtils.errorDialog("Widget is disposed",
249
                            null,
250
                            MessagingUtils.WIDGET_IS_DISPOSED_MESSAGE,
251
                            null,
252
                            e,
253
                            true);
254

    
255
                }
256
            }
257

    
258
        }
259
        selectionProvidingPart = null;
260
        if(thisPart!=null){
261
            thisPart.setLabel(getViewName());
262
        }
263
    }
264

    
265
    protected IStructuredSelection createSelection(Object selection) {
266
        if(selection==null){
267
            return null;
268
        }
269
        IStructuredSelection structuredSelection;
270
        if(!(selection instanceof IStructuredSelection)){
271
            structuredSelection = new StructuredSelection(selection);
272
        }
273
        else{
274
            structuredSelection = (IStructuredSelection) selection;
275
        }
276
        return structuredSelection;
277
    }
278

    
279
    /**
280
     * {@inheritDoc}
281
     */
282
    @Override
283
    public ConversationHolder getConversationHolder() {
284
        if(selectionProvidingPart != null && selectionProvidingPart instanceof IConversationEnabled) {
285
            return ((IConversationEnabled) selectionProvidingPart).getConversationHolder();
286
        }
287
        return null;
288
    }
289

    
290
    /**
291
     * {@inheritDoc}
292
     */
293
    @Override
294
    public boolean postOperation(CdmBase objectAffectedByOperation) {
295
        changed(objectAffectedByOperation);
296
        return true;
297
    }
298

    
299
    /**
300
     * {@inheritDoc}
301
     */
302
    @Override
303
    public boolean onComplete() {
304
        viewer.refresh();
305
        return true;
306
    }
307

    
308
    /**
309
     * {@inheritDoc}
310
     */
311
    @Override
312
    public MPart getSelectionProvidingPart() {
313
        return selectionProvidingPart;
314
    }
315

    
316
    @PreDestroy
317
    private void dispose() {
318
    }
319

    
320
    @PersistState
321
    private void persistState(){
322

    
323
    }
324

    
325
    /**
326
     * {@inheritDoc}
327
     */
328
    @Override
329
    public void update(CdmDataChangeMap arg0) {
330
    }
331

    
332
    /**
333
     * {@inheritDoc}
334
     */
335
    @Override
336
    public void forceDirty() {
337
    }
338

    
339
    protected abstract String getViewName();
340

    
341
}
(2-2/2)