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.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
    protected ISelectionChangedListener selectionChangedListener;
115

    
116
    public ISelectionChangedListener getSelectionChangedListener() {
117
        return selectionChangedListener;
118
    }
119

    
120
    @Inject
121
    protected ESelectionService selService;
122

    
123
    protected abstract void selectionChanged_internal(Object selection, MPart activePart, MPart thisPart);
124

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

    
141
        }
142

    
143

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

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

    
171
    public Viewer getViewer() {
172
        return viewer;
173
    }
174

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

    
185
                        if(part instanceof ITaxonEditor){
186
                            Taxon accepted = ((ITaxonEditor) part).getTaxon();
187

    
188
                            //                			Taxon accepted= ((ITaxonEditor)activePart).getTaxon();
189
                            Set<TaxonRelationship> rels =  taxon.getTaxonRelations(accepted);
190

    
191
                            if (rels.iterator().hasNext()){
192
                                TaxonRelationship rel = rels.iterator().next();
193
                                if (rel.getType().isAnyMisappliedName() && !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)