Project

General

Profile

Download (9.29 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.EPartService;
23
import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
24
import org.eclipse.jface.viewers.ISelectionChangedListener;
25
import org.eclipse.jface.viewers.IStructuredSelection;
26
import org.eclipse.jface.viewers.StructuredSelection;
27
import org.eclipse.jface.viewers.Viewer;
28

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

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

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

    
62

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

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

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

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

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

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

    
101
    }
102

    
103
    protected Viewer viewer;
104

    
105
    protected MPart thisPart;
106

    
107
    protected MPart selectionProvidingPart;
108

    
109
    protected Object previousSelection;
110

    
111
    protected ISelectionChangedListener selectionChangedListener;
112

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

    
117
    @Inject
118
    protected ESelectionService selService;
119

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

    
122
    @Inject
123
    public void selectionChanged(
124
            @Optional@Named(IServiceConstants.ACTIVE_SELECTION)Object selection,
125
            @Optional@Named(IServiceConstants.ACTIVE_PART)MPart activePart,
126
            MPart thisPart, UISynchronize sync, EPartService partService){
127
        if(activePart==thisPart && EventUtility.getActiveEditorPart(partService)==null){
128
            showEmptyPage();
129
            return;
130
        }
131

    
132
        if(previousSelection==null ||
133
                previousSelection!=selection){//skip redundant rendering of details view
134
            if(delaySelection==null){
135
                delaySelection = new DelaySelection(selection, activePart, thisPart);
136
            }
137
            delaySelection.setSelection(selection);
138
            delaySelection.setActivePart(activePart);
139
            delaySelection.setThisPart(thisPart);
140
            if(!isInDelay){
141
                isInDelay = true;
142
                sync.asyncExec(delaySelection);
143
                previousSelection = selection;
144
            }
145
        }
146
    }
147

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

    
159
    public Viewer getViewer() {
160
        return viewer;
161
    }
162

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

    
172
                        if(part instanceof ITaxonEditor){
173
                            Taxon accepted = ((ITaxonEditor) part).getTaxon();
174

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

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

    
183
                                    return;
184
                                }
185
                            }
186
                        }
187

    
188

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

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

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

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

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

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

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

    
264
    /**
265
     * {@inheritDoc}
266
     */
267
    @Override
268
    public Object getSelectionProvidingPart() {
269
        return selectionProvidingPart;
270
    }
271

    
272
    @PreDestroy
273
    private void dispose() {
274
    }
275

    
276
    @PersistState
277
    private void persistState(){
278

    
279
    }
280

    
281
    /**
282
     * {@inheritDoc}
283
     */
284
    @Override
285
    public void update(CdmDataChangeMap arg0) {
286
    }
287

    
288
    /**
289
     * {@inheritDoc}
290
     */
291
    @Override
292
    public void forceDirty() {
293
    }
294

    
295
    protected abstract String getViewName();
296

    
297
}
(2-2/2)