Project

General

Profile

Download (10.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.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.model.taxon.TaxonRelationshipType;
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.EventUtility;
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 boolean isEnabled;
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
    public Object getPreviousSelection() {
117
        return previousSelection;
118
    }
119

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

    
124
    protected ISelectionChangedListener selectionChangedListener;
125

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

    
130
    @Inject
131
    protected ESelectionService selService;
132

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

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

    
151
        }
152

    
153

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

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

    
181
    public Viewer getViewer() {
182
        return viewer;
183
    }
184

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

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

    
199
                            if (rels != null && rels.iterator().hasNext()){
200
                                TaxonRelationship rel = rels.iterator().next();
201
                                if (rel.getType().equals(TaxonRelationshipType.MISAPPLIED_NAME_FOR()) && !rel.getFromTaxon().equals(((ITaxonEditor) part).getTaxon())){
202
                                    viewer.setInput(rel);
203

    
204
                                    return;
205
                                }
206
                            }
207
                        }
208

    
209

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

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

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

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

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

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

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

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

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

    
311
    @PreDestroy
312
    private void dispose() {
313
    }
314

    
315
    @PersistState
316
    private void persistState(){
317

    
318
    }
319

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

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

    
334
    protected abstract String getViewName();
335

    
336
}
(2-2/2)