Project

General

Profile

Download (9.14 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.eclipse.e4.core.di.annotations.Optional;
18
import org.eclipse.e4.ui.di.PersistState;
19
import org.eclipse.e4.ui.di.UISynchronize;
20
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
21
import org.eclipse.e4.ui.services.IServiceConstants;
22
import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
23
import org.eclipse.jface.viewers.ISelectionChangedListener;
24
import org.eclipse.jface.viewers.IStructuredSelection;
25
import org.eclipse.jface.viewers.StructuredSelection;
26
import org.eclipse.jface.viewers.Viewer;
27

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

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

    
54
    private DelaySelection delaySelection = null;
55
    /**
56
     * This is the monitor for the DelaySelection runnable.
57
     * If it is <code>true</code> then it is currently delaying a selection.
58
     */
59
    private boolean isInDelay;
60

    
61

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

    
71
        public DelaySelection(Object selection, MPart activePart, MPart thisPart) {
72
            super();
73
            this.selection = selection;
74
            this.activePart= activePart;
75
            this.thisPart = thisPart;
76
        }
77

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

    
88
        public synchronized void setSelection(Object selection) {
89
            this.selection = selection;
90
        }
91

    
92
        public synchronized void setActivePart(MPart activePart) {
93
            this.activePart = activePart;
94
        }
95

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

    
100
    }
101

    
102
    protected Viewer viewer;
103

    
104
    protected MPart thisPart;
105

    
106
    protected MPart selectionProvidingPart;
107

    
108
    protected Object previousSelection;
109

    
110
    protected ISelectionChangedListener selectionChangedListener;
111

    
112
    public ISelectionChangedListener getSelectionChangedListener() {
113
        return selectionChangedListener;
114
    }
115

    
116
    @Inject
117
    protected ESelectionService selService;
118

    
119
    protected abstract void selectionChanged_internal(Object selection, MPart activePart, MPart thisPart);
120

    
121
    @Inject
122
    public void selectionChanged(
123
            @Optional@Named(IServiceConstants.ACTIVE_SELECTION)Object selection,
124
            @Optional@Named(IServiceConstants.ACTIVE_PART)MPart activePart,
125
            MPart thisPart, UISynchronize sync){
126
        if(activePart==thisPart && EventUtility.getTaxonEditor()==null){
127
            showEmptyPage();
128
            return;
129
        }
130
        if(previousSelection==null ||
131
                previousSelection!=selection){//skip redundant rendering of details view
132
            if(delaySelection==null){
133
                delaySelection = new DelaySelection(selection, activePart, thisPart);
134
            }
135
            delaySelection.setSelection(selection);
136
            delaySelection.setActivePart(activePart);
137
            delaySelection.setThisPart(thisPart);
138
            if(!isInDelay){
139
                isInDelay = true;
140
                sync.asyncExec(delaySelection);
141
                previousSelection = selection;
142
            }
143
        }
144
    }
145

    
146
    /** {@inheritDoc} */
147
    @Override
148
    public void changed(Object object) {
149
        if(selectionProvidingPart!=null){
150
            Object part = selectionProvidingPart.getObject();
151
            if(part instanceof IDirtyMarkable){
152
                ((IDirtyMarkable) part).changed(object);
153
            }
154
        }
155
    }
156

    
157
    public Viewer getViewer() {
158
        return viewer;
159
    }
160

    
161
    protected void showViewer(IStructuredSelection selection, MPart activePart, Viewer viewer){
162
        if(viewer!=null && viewer.getControl()!=null && !viewer.getControl().isDisposed()){
163
            Object element = selection.getFirstElement();
164
            Object part = createPartObject(activePart);
165
            if(selection.getFirstElement()!=null){
166
                if (element instanceof Taxon){
167
                    Taxon taxon = HibernateProxyHelper.deproxy(element, Taxon.class);
168
                    if (taxon.isMisapplication()){
169

    
170
                        if(part instanceof ITaxonEditor){
171
                            Taxon accepted = ((ITaxonEditor) part).getTaxon();
172

    
173
                            //                			Taxon accepted= ((ITaxonEditor)activePart).getTaxon();
174
                            Set<TaxonRelationship> rels =  taxon.getTaxonRelations(accepted);
175

    
176
                            if (rels.iterator().hasNext()){
177
                                TaxonRelationship rel = rels.iterator().next();
178
                                if (rel.getType().equals(TaxonRelationshipType.MISAPPLIED_NAME_FOR()) && !rel.getFromTaxon().equals(((ITaxonEditor) part).getTaxon())){
179
                                    viewer.setInput(rel);
180

    
181
                                    return;
182
                                }
183
                            }
184
                        }
185

    
186

    
187
                    }
188
                }
189
                if (element instanceof Distribution && part instanceof IDistributionEditor){
190
                    ((DetailsViewerE4)viewer).setInput(element, part);
191
                }else{
192
                    viewer.setInput(element);
193
                }
194
                selectionProvidingPart = activePart;
195
            }
196
        }
197
    }
198

    
199
    protected Object createPartObject(MPart activePart) {
200
        Object partObject = activePart;
201
        Object wrappedPart = WorkbenchUtility.getE4WrappedPart(activePart);
202
        if(wrappedPart!=null){
203
            partObject = wrappedPart;
204
        }
205
        return partObject;
206
    }
207

    
208
    protected void showEmptyPage() {
209
        if(viewer!=null && viewer.getControl()!=null && !viewer.getControl().isDisposed()){
210
            viewer.setInput(null);
211
        }
212
        selectionProvidingPart = null;
213
        if(thisPart!=null){
214
            thisPart.setLabel(getViewName());
215
        }
216
    }
217

    
218
    protected IStructuredSelection createSelection(Object selection) {
219
        if(selection==null){
220
            return null;
221
        }
222
        IStructuredSelection structuredSelection;
223
        if(!(selection instanceof IStructuredSelection)){
224
            structuredSelection = new StructuredSelection(selection);
225
        }
226
        else{
227
            structuredSelection = (IStructuredSelection) selection;
228
        }
229
        return structuredSelection;
230
    }
231

    
232
    /**
233
     * {@inheritDoc}
234
     */
235
    @Override
236
    public ConversationHolder getConversationHolder() {
237
        if(selectionProvidingPart != null && selectionProvidingPart instanceof IConversationEnabled) {
238
            return ((IConversationEnabled) selectionProvidingPart).getConversationHolder();
239
        }
240
        return null;
241
    }
242

    
243
    /**
244
     * {@inheritDoc}
245
     */
246
    @Override
247
    public boolean postOperation(CdmBase objectAffectedByOperation) {
248
        changed(objectAffectedByOperation);
249
        return true;
250
    }
251

    
252
    /**
253
     * {@inheritDoc}
254
     */
255
    @Override
256
    public boolean onComplete() {
257
        viewer.refresh();
258
        return true;
259
    }
260

    
261
    /**
262
     * {@inheritDoc}
263
     */
264
    @Override
265
    public Object getSelectionProvidingPart() {
266
        return selectionProvidingPart;
267
    }
268

    
269
    @PreDestroy
270
    private void dispose() {
271
    }
272

    
273
    @PersistState
274
    private void persistState(){
275

    
276
    }
277

    
278
    /**
279
     * {@inheritDoc}
280
     */
281
    @Override
282
    public void update(CdmDataChangeMap arg0) {
283
    }
284

    
285
    /**
286
     * {@inheritDoc}
287
     */
288
    @Override
289
    public void forceDirty() {
290
    }
291

    
292
    protected abstract String getViewName();
293

    
294
}
(2-2/2)