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.model.IDirtyMarkable;
42
import eu.etaxonomy.taxeditor.model.MessagingUtils;
43
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
44
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
45
import eu.etaxonomy.taxeditor.view.e4.details.DetailsViewerE4;
46
import eu.etaxonomy.taxeditor.workbench.WorkbenchUtility;
47
import eu.etaxonomy.taxeditor.workbench.part.ISelectionElementEditingPart;
48

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

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

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

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

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

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

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

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

    
103
    }
104

    
105
    protected Viewer viewer;
106

    
107
    protected MPart thisPart;
108

    
109
    protected MPart selectionProvidingPart;
110

    
111
    protected Object previousSelection;
112

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

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

    
121
    protected ISelectionChangedListener selectionChangedListener;
122

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

    
127
    @Inject
128
    protected ESelectionService selService;
129

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

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

    
148
        }
149

    
150

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

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

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

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

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

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

    
201
                                    return;
202
                                }
203
                            }
204
                        }
205

    
206

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

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

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

    
247
                }
248
            }
249

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

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

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

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

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

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

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

    
312
    @PersistState
313
    private void persistState(){
314

    
315
    }
316

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

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

    
331
    protected abstract String getViewName();
332

    
333
}
(2-2/2)