Project

General

Profile

Download (8.53 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.model.application.ui.basic.MPart;
20
import org.eclipse.e4.ui.services.IServiceConstants;
21
import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
22
import org.eclipse.jface.viewers.ISelectionChangedListener;
23
import org.eclipse.jface.viewers.IStructuredSelection;
24
import org.eclipse.jface.viewers.StructuredSelection;
25
import org.eclipse.jface.viewers.Viewer;
26
import org.eclipse.swt.widgets.Display;
27
import org.eclipse.ui.IEditorPart;
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.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.ITaxonEditor;
38
import eu.etaxonomy.taxeditor.model.AbstractUtility;
39
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
40
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
41
import eu.etaxonomy.taxeditor.workbench.WorkbenchUtility;
42
import eu.etaxonomy.taxeditor.workbench.part.ISelectionElementEditingPart;
43

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

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

    
59

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

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

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

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

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

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

    
98
    }
99

    
100
    protected abstract void selectionChanged_internal(Object selection, MPart activePart, MPart thisPart);
101

    
102
    @Inject
103
    @Optional
104
    public void selectionChanged(
105
            @Named(IServiceConstants.ACTIVE_SELECTION)Object selection,
106
            @Named(IServiceConstants.ACTIVE_PART)MPart activePart,
107
            MPart thisPart){
108
        Object savablePart = WorkbenchUtility.findSavablePart(this);
109
        if(savablePart==null){
110
            showEmptyPage();
111
        }
112
        if(delaySelection==null){
113
            delaySelection = new DelaySelection(selection, activePart, thisPart);
114
        }
115
        delaySelection.setSelection(selection);
116
        delaySelection.setActivePart(activePart);
117
        delaySelection.setThisPart(thisPart);
118
        if(!isInDelay){
119
            isInDelay = true;
120
            Display.getCurrent().asyncExec(delaySelection);
121
        }
122
    }
123

    
124
    protected Viewer viewer;
125

    
126
    protected MPart thisPart;
127

    
128
    protected MPart selectionProvidingPart;
129

    
130
    protected ISelectionChangedListener selectionChangedListener;
131

    
132
    @Inject
133
    protected ESelectionService selService;
134

    
135
    /** {@inheritDoc} */
136
    @Override
137
    public void changed(Object object) {
138
        if(selectionProvidingPart!=null){
139
            Object part = selectionProvidingPart.getObject();
140
            if(part instanceof IDirtyMarkable){
141
                ((IDirtyMarkable) part).changed(object);
142
            }
143
            else {
144
                IEditorPart editor = AbstractUtility.getActiveEditor();
145
                if (editor != null && editor instanceof IDirtyMarkable) {
146
                    ((IDirtyMarkable) editor).changed(object);
147
                }
148
            }
149
        }
150
    }
151

    
152
    public Viewer getViewer() {
153
        return viewer;
154
    }
155

    
156
    protected void showViewer(IStructuredSelection selection, MPart activePart, Viewer viewer){
157
        if(viewer!=null && viewer.getControl()!=null && !viewer.getControl().isDisposed()){
158
            Object element = selection.getFirstElement();
159
            if(selection.getFirstElement()!=null){
160
            	if (element instanceof Taxon){
161
                	Taxon taxon = HibernateProxyHelper.deproxy(element, Taxon.class);
162
                	if (taxon.isMisapplication()){
163
                		Object part = createPartObject(activePart);
164
                		 if(part instanceof ITaxonEditor){
165
                			 Taxon accepted = ((ITaxonEditor) part).getTaxon();
166

    
167
//                			Taxon accepted= ((ITaxonEditor)activePart).getTaxon();
168
                			Set<TaxonRelationship> rels =  taxon.getTaxonRelations(accepted);
169

    
170
                			 if (rels.iterator().hasNext()){
171
                				 TaxonRelationship rel = rels.iterator().next();
172
                				 if (rel.getType().equals(TaxonRelationshipType.MISAPPLIED_NAME_FOR()) && !rel.getFromTaxon().equals(((ITaxonEditor) part).getTaxon())){
173
        	        				 viewer.setInput(rel);
174

    
175
        	        				 return;
176
                				 }
177
                			 }
178
                		}
179

    
180

    
181
                	}
182
                }
183
            	viewer.setInput(element);
184

    
185
            }
186
            selectionProvidingPart = activePart;
187
        }
188
    }
189

    
190
    protected Object createPartObject(MPart activePart) {
191
        Object partObject = activePart;
192
        Object wrappedPart = WorkbenchUtility.getE4WrappedPart(activePart);
193
        if(wrappedPart!=null){
194
            partObject = wrappedPart;
195
        }
196
        return partObject;
197
    }
198

    
199
    protected void showEmptyPage() {
200
        if(viewer!=null && viewer.getControl()!=null && viewer.getControl().isDisposed()){
201
            viewer.setInput(null);
202
        }
203
        selectionProvidingPart = null;
204
        if(thisPart!=null){
205
            thisPart.setLabel(getViewName());
206
        }
207
    }
208

    
209
    protected IStructuredSelection createSelection(Object selection) {
210
        if(selection==null){
211
            return null;
212
        }
213
        IStructuredSelection structuredSelection;
214
        if(!(selection instanceof IStructuredSelection)){
215
            structuredSelection = new StructuredSelection(selection);
216
        }
217
        else{
218
            structuredSelection = (IStructuredSelection) selection;
219
        }
220
        return structuredSelection;
221
    }
222

    
223
    /**
224
     * {@inheritDoc}
225
     */
226
    @Override
227
    public ConversationHolder getConversationHolder() {
228
        if(selectionProvidingPart != null && selectionProvidingPart instanceof IConversationEnabled) {
229
            return ((IConversationEnabled) selectionProvidingPart).getConversationHolder();
230
        }
231
        return null;
232
    }
233

    
234
    /**
235
     * {@inheritDoc}
236
     */
237
    @Override
238
    public boolean postOperation(CdmBase objectAffectedByOperation) {
239
        changed(objectAffectedByOperation);
240
        return true;
241
    }
242

    
243
    /**
244
     * {@inheritDoc}
245
     */
246
    @Override
247
    public boolean onComplete() {
248
        viewer.refresh();
249
        return true;
250
    }
251

    
252
    /**
253
     * {@inheritDoc}
254
     */
255
    @Override
256
    public Object getSelectionProvidingPart() {
257
        return selectionProvidingPart;
258
    }
259

    
260
    @PreDestroy
261
    private void dispose() {
262
    }
263

    
264
    @PersistState
265
    private void persistState(){
266

    
267
    }
268

    
269
    /**
270
     * {@inheritDoc}
271
     */
272
    @Override
273
    public void update(CdmDataChangeMap arg0) {
274
    }
275

    
276
    /**
277
     * {@inheritDoc}
278
     */
279
    @Override
280
    public void forceDirty() {
281
    }
282

    
283
    protected abstract String getViewName();
284

    
285
}
(2-2/2)