Project

General

Profile

Download (10.6 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
    protected ISelectionChangedListener selectionChangedListener;
117

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

    
122
    @Inject
123
    protected ESelectionService selService;
124

    
125
    protected abstract void selectionChanged_internal(Object selection, MPart activePart, MPart thisPart);
126

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

    
143
        }
144

    
145

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

    
162
    /** {@inheritDoc} */
163
    @Override
164
    public void changed(Object object) {
165
        if(selectionProvidingPart!=null){
166
            Object part = selectionProvidingPart.getObject();
167
            if(part instanceof IDirtyMarkable){
168
                ((IDirtyMarkable) part).changed(object);
169
            }
170
        }
171
    }
172

    
173
    public Viewer getViewer() {
174
        return viewer;
175
    }
176

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

    
187
                        if(part instanceof ITaxonEditor){
188
                            Taxon accepted = ((ITaxonEditor) part).getTaxon();
189
                            Set<TaxonRelationship> rels =  taxon.getTaxonRelations(accepted);
190

    
191
                            if (rels.iterator().hasNext()){
192
                                TaxonRelationship rel = rels.iterator().next();
193
                                if (rel.getType().equals(TaxonRelationshipType.MISAPPLIED_NAME_FOR()) && !rel.getFromTaxon().equals(((ITaxonEditor) part).getTaxon())){
194
                                    viewer.setInput(rel);
195

    
196
                                    return;
197
                                }
198
                            }
199
                        }
200

    
201

    
202
                    }
203
                }
204
                if (element instanceof Distribution && part instanceof IDistributionEditor){
205
                    ((DetailsViewerE4)viewer).setInput(element, part);
206
                }else{
207
                    viewer.setInput(element);
208
                    if (viewer instanceof DetailsViewerE4){
209
                    	((DetailsViewerE4)viewer).setDetailsEnabled(true);
210
                    }
211
                }
212
                selectionProvidingPart = activePart;
213
            }
214
        }
215
    }
216

    
217
    protected Object createPartObject(MPart activePart) {
218
        Object partObject = activePart;
219
        Object wrappedPart = WorkbenchUtility.getE4WrappedPart(activePart);
220
        if(wrappedPart!=null){
221
            partObject = wrappedPart;
222
        }
223
        return partObject;
224
    }
225

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

    
242
                }
243
            }
244
            
245
        }
246
        selectionProvidingPart = null;
247
        if(thisPart!=null){
248
            thisPart.setLabel(getViewName());
249
        }
250
    }
251

    
252
    protected IStructuredSelection createSelection(Object selection) {
253
        if(selection==null){
254
            return null;
255
        }
256
        IStructuredSelection structuredSelection;
257
        if(!(selection instanceof IStructuredSelection)){
258
            structuredSelection = new StructuredSelection(selection);
259
        }
260
        else{
261
            structuredSelection = (IStructuredSelection) selection;
262
        }
263
        return structuredSelection;
264
    }
265

    
266
    /**
267
     * {@inheritDoc}
268
     */
269
    @Override
270
    public ConversationHolder getConversationHolder() {
271
        if(selectionProvidingPart != null && selectionProvidingPart instanceof IConversationEnabled) {
272
            return ((IConversationEnabled) selectionProvidingPart).getConversationHolder();
273
        }
274
        return null;
275
    }
276

    
277
    /**
278
     * {@inheritDoc}
279
     */
280
    @Override
281
    public boolean postOperation(CdmBase objectAffectedByOperation) {
282
        changed(objectAffectedByOperation);
283
        return true;
284
    }
285

    
286
    /**
287
     * {@inheritDoc}
288
     */
289
    @Override
290
    public boolean onComplete() {
291
        viewer.refresh();
292
        return true;
293
    }
294

    
295
    /**
296
     * {@inheritDoc}
297
     */
298
    @Override
299
    public Object getSelectionProvidingPart() {
300
        return selectionProvidingPart;
301
    }
302

    
303
    @PreDestroy
304
    private void dispose() {
305
    }
306

    
307
    @PersistState
308
    private void persistState(){
309

    
310
    }
311

    
312
    /**
313
     * {@inheritDoc}
314
     */
315
    @Override
316
    public void update(CdmDataChangeMap arg0) {
317
    }
318

    
319
    /**
320
     * {@inheritDoc}
321
     */
322
    @Override
323
    public void forceDirty() {
324
    }
325

    
326
    protected abstract String getViewName();
327

    
328
}
(2-2/2)