Project

General

Profile

Download (9.87 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.operation.IPostOperationEnabled;
45
import eu.etaxonomy.taxeditor.view.e4.details.DetailsViewerE4;
46
import eu.etaxonomy.taxeditor.workbench.WorkbenchUtility;
47
import eu.etaxonomy.taxeditor.workbench.part.ISelectionElementEditingPart;
48

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

    
57
    private DelaySelection delaySelection = null;
58
    /**
59
     * This is the monitor for the DelaySelection runnable.
60
     * If it is <code>true</code> then it is currently delaying a selection.
61
     */
62
    private boolean isInDelay;
63
    private static final Logger logger = Logger.getLogger(AbstractCdmEditorPartE4.class);
64

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

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

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

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

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

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

    
103
    }
104

    
105
    protected Viewer viewer;
106

    
107
    protected MPart thisPart;
108

    
109
    protected MPart selectionProvidingPart;
110

    
111
    protected Object previousSelection;
112

    
113
    protected ISelectionChangedListener selectionChangedListener;
114

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

    
119
    @Inject
120
    protected ESelectionService selService;
121

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

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

    
141

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

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

    
169
    public Viewer getViewer() {
170
        return viewer;
171
    }
172

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

    
183
                        if(part instanceof ITaxonEditor){
184
                            Taxon accepted = ((ITaxonEditor) part).getTaxon();
185

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

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

    
194
                                    return;
195
                                }
196
                            }
197
                        }
198

    
199

    
200
                    }
201
                }
202
                if (element instanceof Distribution && part instanceof IDistributionEditor){
203
                    ((DetailsViewerE4)viewer).setInput(element, part);
204
                }else{
205
                    viewer.setInput(element);
206
                }
207
                selectionProvidingPart = activePart;
208
            }
209
        }
210
    }
211

    
212
    protected Object createPartObject(MPart activePart) {
213
        Object partObject = activePart;
214
        Object wrappedPart = WorkbenchUtility.getE4WrappedPart(activePart);
215
        if(wrappedPart!=null){
216
            partObject = wrappedPart;
217
        }
218
        return partObject;
219
    }
220

    
221
    protected void showEmptyPage() {
222
        if(viewer!=null && viewer.getControl()!=null && !viewer.getControl().isDisposed()){
223
            viewer.setInput(null);
224
            viewer.getControl().setEnabled(false);
225
        }
226
        selectionProvidingPart = null;
227
        if(thisPart!=null){
228
            thisPart.setLabel(getViewName());
229
        }
230
    }
231

    
232
    protected IStructuredSelection createSelection(Object selection) {
233
        if(selection==null){
234
            return null;
235
        }
236
        IStructuredSelection structuredSelection;
237
        if(!(selection instanceof IStructuredSelection)){
238
            structuredSelection = new StructuredSelection(selection);
239
        }
240
        else{
241
            structuredSelection = (IStructuredSelection) selection;
242
        }
243
        return structuredSelection;
244
    }
245

    
246
    /**
247
     * {@inheritDoc}
248
     */
249
    @Override
250
    public ConversationHolder getConversationHolder() {
251
        if(selectionProvidingPart != null && selectionProvidingPart instanceof IConversationEnabled) {
252
            return ((IConversationEnabled) selectionProvidingPart).getConversationHolder();
253
        }
254
        return null;
255
    }
256

    
257
    /**
258
     * {@inheritDoc}
259
     */
260
    @Override
261
    public boolean postOperation(CdmBase objectAffectedByOperation) {
262
        changed(objectAffectedByOperation);
263
        return true;
264
    }
265

    
266
    /**
267
     * {@inheritDoc}
268
     */
269
    @Override
270
    public boolean onComplete() {
271
        viewer.refresh();
272
        return true;
273
    }
274

    
275
    /**
276
     * {@inheritDoc}
277
     */
278
    @Override
279
    public Object getSelectionProvidingPart() {
280
        return selectionProvidingPart;
281
    }
282

    
283
    @PreDestroy
284
    private void dispose() {
285
    }
286

    
287
    @PersistState
288
    private void persistState(){
289

    
290
    }
291

    
292
    /**
293
     * {@inheritDoc}
294
     */
295
    @Override
296
    public void update(CdmDataChangeMap arg0) {
297
    }
298

    
299
    /**
300
     * {@inheritDoc}
301
     */
302
    @Override
303
    public void forceDirty() {
304
    }
305

    
306
    protected abstract String getViewName();
307

    
308
}
(2-2/2)