Project

General

Profile

Download (10.7 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.UISynchronize;
21
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
22
import org.eclipse.e4.ui.services.IServiceConstants;
23
import org.eclipse.e4.ui.workbench.modeling.EPartService;
24
import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
25
import org.eclipse.jface.viewers.ISelectionChangedListener;
26
import org.eclipse.jface.viewers.IStructuredSelection;
27
import org.eclipse.jface.viewers.StructuredSelection;
28
import org.eclipse.jface.viewers.Viewer;
29
import org.eclipse.swt.SWTException;
30

    
31
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
32
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
33
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
34
import eu.etaxonomy.cdm.model.common.CdmBase;
35
import eu.etaxonomy.cdm.model.description.Distribution;
36
import eu.etaxonomy.cdm.model.taxon.Taxon;
37
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
38
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
39
import eu.etaxonomy.taxeditor.editor.IDistributionEditor;
40
import eu.etaxonomy.taxeditor.editor.ITaxonEditor;
41
import eu.etaxonomy.taxeditor.event.EventUtility;
42
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
43
import eu.etaxonomy.taxeditor.model.MessagingUtils;
44
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
45
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
46
import eu.etaxonomy.taxeditor.view.e4.details.DetailsViewerE4;
47
import eu.etaxonomy.taxeditor.workbench.WorkbenchUtility;
48
import eu.etaxonomy.taxeditor.workbench.part.ISelectionElementEditingPart;
49

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

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

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

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

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

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

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

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

    
104
    }
105

    
106
    protected Viewer viewer;
107

    
108
    protected MPart thisPart;
109

    
110
    protected MPart selectionProvidingPart;
111

    
112
    protected Object previousSelection;
113

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

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

    
122
    protected ISelectionChangedListener selectionChangedListener;
123

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

    
128
    @Inject
129
    protected ESelectionService selService;
130

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

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

    
149
        }
150

    
151

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

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

    
179
    public Viewer getViewer() {
180
        return viewer;
181
    }
182

    
183
    protected void showViewer(IStructuredSelection selection, MPart activePart, Viewer viewer){
184
        if(viewer!=null && viewer.getControl()!=null && !viewer.getControl().isDisposed()){
185
            Object element = selection.getFirstElement();
186
            Object part = createPartObject(activePart);
187
            viewer.getControl().setEnabled(true);
188
            if(selection.getFirstElement()!=null){
189
                if (element instanceof Taxon){
190
                    Taxon taxon = HibernateProxyHelper.deproxy(element, Taxon.class);
191
                    if (taxon.isMisapplication()){
192

    
193
                        if(part instanceof ITaxonEditor){
194
                            Taxon accepted = ((ITaxonEditor) part).getTaxon();
195
                            Set<TaxonRelationship> rels =  taxon.getTaxonRelations(accepted);
196

    
197
                            if (rels != null && rels.iterator().hasNext()){
198
                                TaxonRelationship rel = rels.iterator().next();
199
                                if (rel.getType().isAnyMisappliedName() && !rel.getFromTaxon().equals(((ITaxonEditor) part).getTaxon())){
200
                                    viewer.setInput(rel);
201

    
202
                                    return;
203
                                }
204
                            }
205
                        }
206

    
207

    
208
                    }
209
                }
210
                if (element instanceof Distribution && part instanceof IDistributionEditor){
211
                    ((DetailsViewerE4)viewer).setInput(element, part);
212
                }else{
213
                    viewer.setInput(element);
214
                    if (viewer instanceof DetailsViewerE4){
215
                    	((DetailsViewerE4)viewer).setDetailsEnabled(true);
216
                    }
217
                }
218
                selectionProvidingPart = activePart;
219
            }
220
        }
221
    }
222

    
223
    protected Object createPartObject(MPart activePart) {
224
        Object partObject = activePart;
225
        Object wrappedPart = WorkbenchUtility.getE4WrappedPart(activePart);
226
        if(wrappedPart!=null){
227
            partObject = wrappedPart;
228
        }
229
        return partObject;
230
    }
231

    
232
    protected void showEmptyPage() {
233
        if(viewer!=null && viewer.getControl()!=null && !viewer.getControl().isDisposed() ){
234
            viewer.setInput(null);
235
            try{
236
	            if (!viewer.getControl().isDisposed()){
237
	                viewer.getControl().setEnabled(false);
238
	            }
239
            }catch(SWTException e){
240
            	if (PreferencesUtil.isShowUpWidgetIsDisposedMessages() && e.getMessage().equals("Widget is disposed")){
241
                    MessagingUtils.errorDialog("Widget is disposed",
242
                            null,
243
                            MessagingUtils.WIDGET_IS_DISPOSED_MESSAGE,
244
                            null,
245
                            e,
246
                            true);
247

    
248
                }
249
            }
250

    
251
        }
252
        selectionProvidingPart = null;
253
        if(thisPart!=null){
254
            thisPart.setLabel(getViewName());
255
        }
256
    }
257

    
258
    protected IStructuredSelection createSelection(Object selection) {
259
        if(selection==null){
260
            return null;
261
        }
262
        IStructuredSelection structuredSelection;
263
        if(!(selection instanceof IStructuredSelection)){
264
            structuredSelection = new StructuredSelection(selection);
265
        }
266
        else{
267
            structuredSelection = (IStructuredSelection) selection;
268
        }
269
        return structuredSelection;
270
    }
271

    
272
    /**
273
     * {@inheritDoc}
274
     */
275
    @Override
276
    public ConversationHolder getConversationHolder() {
277
        if(selectionProvidingPart != null && selectionProvidingPart instanceof IConversationEnabled) {
278
            return ((IConversationEnabled) selectionProvidingPart).getConversationHolder();
279
        }
280
        return null;
281
    }
282

    
283
    /**
284
     * {@inheritDoc}
285
     */
286
    @Override
287
    public boolean postOperation(CdmBase objectAffectedByOperation) {
288
        changed(objectAffectedByOperation);
289
        return true;
290
    }
291

    
292
    /**
293
     * {@inheritDoc}
294
     */
295
    @Override
296
    public boolean onComplete() {
297
        viewer.refresh();
298
        return true;
299
    }
300

    
301
    /**
302
     * {@inheritDoc}
303
     */
304
    @Override
305
    public Object getSelectionProvidingPart() {
306
        return selectionProvidingPart;
307
    }
308

    
309
    @PreDestroy
310
    private void dispose() {
311
    }
312

    
313
    @PersistState
314
    private void persistState(){
315

    
316
    }
317

    
318
    /**
319
     * {@inheritDoc}
320
     */
321
    @Override
322
    public void update(CdmDataChangeMap arg0) {
323
    }
324

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

    
332
    protected abstract String getViewName();
333

    
334
}
(2-2/2)