Project

General

Profile

Download (8.33 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 selectionProvidingPart;
127

    
128
    protected ISelectionChangedListener selectionChangedListener;
129

    
130
    @Inject
131
    protected ESelectionService selService;
132

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

    
150
    public Viewer getViewer() {
151
        return viewer;
152
    }
153

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

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

    
168
                			 if (rels.iterator().hasNext()){
169
                				 TaxonRelationship rel = rels.iterator().next();
170
                				 if (rel.getType().equals(TaxonRelationshipType.MISAPPLIED_NAME_FOR())){
171
        	        				 viewer.setInput(rel);
172

    
173
        	        				 return;
174
                				 }
175
                			 }
176
                		}
177

    
178

    
179
                	}
180
                }
181
            	viewer.setInput(element);
182

    
183

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

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

    
198
    protected void showEmptyPage() {
199
        if(viewer!=null && !viewer.getControl().isDisposed()){
200
            viewer.setInput(null);
201
        }
202
        selectionProvidingPart = null;
203
    }
204

    
205
    protected IStructuredSelection createSelection(Object selection) {
206
        if(selection==null){
207
            return null;
208
        }
209
        IStructuredSelection structuredSelection;
210
        if(!(selection instanceof IStructuredSelection)){
211
            structuredSelection = new StructuredSelection(selection);
212
        }
213
        else{
214
            structuredSelection = (IStructuredSelection) selection;
215
        }
216
        return structuredSelection;
217
    }
218

    
219
    /**
220
     * {@inheritDoc}
221
     */
222
    @Override
223
    public ConversationHolder getConversationHolder() {
224
        if(selectionProvidingPart != null && selectionProvidingPart instanceof IConversationEnabled) {
225
            return ((IConversationEnabled) selectionProvidingPart).getConversationHolder();
226
        }
227
        return null;
228
    }
229

    
230
    /**
231
     * {@inheritDoc}
232
     */
233
    @Override
234
    public boolean postOperation(CdmBase objectAffectedByOperation) {
235
        changed(objectAffectedByOperation);
236
        return true;
237
    }
238

    
239
    /**
240
     * {@inheritDoc}
241
     */
242
    @Override
243
    public boolean onComplete() {
244
        viewer.refresh();
245
        return true;
246
    }
247

    
248
    /**
249
     * {@inheritDoc}
250
     */
251
    @Override
252
    public Object getSelectionProvidingPart() {
253
        return selectionProvidingPart;
254
    }
255

    
256
    @PreDestroy
257
    private void dispose() {
258
    }
259

    
260
    @PersistState
261
    private void persistState(){
262

    
263
    }
264

    
265
    /**
266
     * {@inheritDoc}
267
     */
268
    @Override
269
    public void update(CdmDataChangeMap arg0) {
270
    }
271

    
272
    /**
273
     * {@inheritDoc}
274
     */
275
    @Override
276
    public void forceDirty() {
277
    }
278

    
279
    protected abstract String getViewName();
280

    
281
}
(2-2/2)