Project

General

Profile

Download (7.85 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.jface.viewers.IStructuredSelection;
22
import org.eclipse.jface.viewers.StructuredSelection;
23
import org.eclipse.jface.viewers.Viewer;
24
import org.eclipse.swt.widgets.Display;
25
import org.eclipse.ui.IEditorPart;
26

    
27
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
28
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
29
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
30
import eu.etaxonomy.cdm.model.common.CdmBase;
31
import eu.etaxonomy.cdm.model.taxon.Taxon;
32
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
33
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
34
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
35
import eu.etaxonomy.taxeditor.editor.ITaxonEditor;
36
import eu.etaxonomy.taxeditor.model.AbstractUtility;
37
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
38
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
39
import eu.etaxonomy.taxeditor.workbench.WorkbenchUtility;
40
import eu.etaxonomy.taxeditor.workbench.part.ISelectionElementEditingPart;
41

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

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

    
57

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

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

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

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

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

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

    
96
    }
97

    
98
    protected abstract void selectionChanged_internal(Object selection, MPart activePart, MPart thisPart);
99

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

    
118
    protected Viewer viewer;
119

    
120
    protected MPart selectionProvidingPart;
121

    
122
    /** {@inheritDoc} */
123
    @Override
124
    public void changed(Object object) {
125
        if(selectionProvidingPart!=null){
126
            Object part = selectionProvidingPart.getObject();
127
            if(part instanceof IDirtyMarkable){
128
                ((IDirtyMarkable) part).changed(object);
129
            }
130
            else {
131
                IEditorPart editor = AbstractUtility.getActiveEditor();
132
                if (editor != null && editor instanceof IDirtyMarkable) {
133
                    ((IDirtyMarkable) editor).changed(object);
134
                }
135
            }
136
        }
137
    }
138

    
139
    public Viewer getViewer() {
140
        return viewer;
141
    }
142

    
143
    protected void showViewer(IStructuredSelection selection, MPart activePart, Viewer viewer){
144
        if(viewer!=null){
145
            Object element = selection.getFirstElement();
146
            if(selection.getFirstElement()!=null){
147
            	if (element instanceof Taxon){
148
                	Taxon taxon = HibernateProxyHelper.deproxy(element, Taxon.class);
149
                	if (taxon.isMisapplication()){
150
                		Object part = createPartObject(activePart);
151
                		 if(part instanceof ITaxonEditor){
152
                			 Taxon accepted = ((ITaxonEditor) part).getTaxon();
153

    
154
//                			Taxon accepted= ((ITaxonEditor)activePart).getTaxon();
155
                			Set<TaxonRelationship> rels =  taxon.getTaxonRelations(accepted);
156

    
157
                			 if (rels.iterator().hasNext()){
158
                				 TaxonRelationship rel = rels.iterator().next();
159
                				 if (rel.getType().equals(TaxonRelationshipType.MISAPPLIED_NAME_FOR())){
160
        	        				 viewer.setInput(rel);
161

    
162
        	        				 return;
163
                				 }
164
                			 }
165
                		}
166

    
167

    
168
                	}
169
                }
170
            	viewer.setInput(element);
171

    
172

    
173
            }
174
            selectionProvidingPart = activePart;
175
        }
176
    }
177

    
178
    protected Object createPartObject(MPart activePart) {
179
        Object partObject = activePart;
180
        Object wrappedPart = WorkbenchUtility.getE4WrappedPart(activePart);
181
        if(wrappedPart!=null){
182
            partObject = wrappedPart;
183
        }
184
        return partObject;
185
    }
186

    
187
    protected void showEmptyPage() {
188
        if(viewer!=null){
189
            viewer.setInput(null);
190
        }
191
        selectionProvidingPart = null;
192
    }
193

    
194
    protected IStructuredSelection createSelection(Object selection) {
195
        if(selection==null){
196
            return null;
197
        }
198
        IStructuredSelection structuredSelection;
199
        if(!(selection instanceof IStructuredSelection)){
200
            structuredSelection = new StructuredSelection(selection);
201
        }
202
        else{
203
            structuredSelection = (IStructuredSelection) selection;
204
        }
205
        return structuredSelection;
206
    }
207

    
208
    /**
209
     * {@inheritDoc}
210
     */
211
    @Override
212
    public ConversationHolder getConversationHolder() {
213
        if(selectionProvidingPart != null && selectionProvidingPart instanceof IConversationEnabled) {
214
            return ((IConversationEnabled) selectionProvidingPart).getConversationHolder();
215
        }
216
        return null;
217
    }
218

    
219
    /**
220
     * {@inheritDoc}
221
     */
222
    @Override
223
    public boolean postOperation(CdmBase objectAffectedByOperation) {
224
        changed(objectAffectedByOperation);
225
        return true;
226
    }
227

    
228
    /**
229
     * {@inheritDoc}
230
     */
231
    @Override
232
    public boolean onComplete() {
233
        viewer.refresh();
234
        return true;
235
    }
236

    
237
    /**
238
     * {@inheritDoc}
239
     */
240
    @Override
241
    public Object getSelectionProvidingPart() {
242
        return selectionProvidingPart;
243
    }
244

    
245
    @PreDestroy
246
    private void dispose() {
247
    }
248

    
249
    @PersistState
250
    private void persistState(){
251

    
252
    }
253

    
254
    /**
255
     * {@inheritDoc}
256
     */
257
    @Override
258
    public void update(CdmDataChangeMap arg0) {
259
    }
260

    
261
    /**
262
     * {@inheritDoc}
263
     */
264
    @Override
265
    public void forceDirty() {
266
    }
267

    
268
    protected abstract String getViewName();
269

    
270
}
(2-2/2)