Project

General

Profile

Download (8.06 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
        Object savablePart = WorkbenchUtility.findSavablePart(this);
107
        if(savablePart==null){
108
            showEmptyPage();
109
        }
110
        if(delaySelection==null){
111
            delaySelection = new DelaySelection(selection, activePart, thisPart);
112
        }
113
        delaySelection.setSelection(selection);
114
        delaySelection.setActivePart(activePart);
115
        delaySelection.setThisPart(thisPart);
116
        if(!isInDelay){
117
            isInDelay = true;
118
            Display.getCurrent().asyncExec(delaySelection);
119
        }
120
    }
121

    
122
    protected Viewer viewer;
123

    
124
    protected MPart selectionProvidingPart;
125

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

    
143
    public Viewer getViewer() {
144
        return viewer;
145
    }
146

    
147
    protected void showViewer(IStructuredSelection selection, MPart activePart, Viewer viewer){
148
        if(viewer!=null && !viewer.getControl().isDisposed()){
149
            Object element = selection.getFirstElement();
150
            if(selection.getFirstElement()!=null){
151
            	if (element instanceof Taxon){
152
                	Taxon taxon = HibernateProxyHelper.deproxy(element, Taxon.class);
153
                	if (taxon.isMisapplication()){
154
                		Object part = createPartObject(activePart);
155
                		 if(part instanceof ITaxonEditor){
156
                			 Taxon accepted = ((ITaxonEditor) part).getTaxon();
157

    
158
//                			Taxon accepted= ((ITaxonEditor)activePart).getTaxon();
159
                			Set<TaxonRelationship> rels =  taxon.getTaxonRelations(accepted);
160

    
161
                			 if (rels.iterator().hasNext()){
162
                				 TaxonRelationship rel = rels.iterator().next();
163
                				 if (rel.getType().equals(TaxonRelationshipType.MISAPPLIED_NAME_FOR())){
164
        	        				 viewer.setInput(rel);
165

    
166
        	        				 return;
167
                				 }
168
                			 }
169
                		}
170

    
171

    
172
                	}
173
                }
174
            	viewer.setInput(element);
175

    
176

    
177
            }
178
            selectionProvidingPart = activePart;
179
        }
180
    }
181

    
182
    protected Object createPartObject(MPart activePart) {
183
        Object partObject = activePart;
184
        Object wrappedPart = WorkbenchUtility.getE4WrappedPart(activePart);
185
        if(wrappedPart!=null){
186
            partObject = wrappedPart;
187
        }
188
        return partObject;
189
    }
190

    
191
    protected void showEmptyPage() {
192
        if(viewer!=null && !viewer.getControl().isDisposed()){
193
            viewer.setInput(null);
194
        }
195
        selectionProvidingPart = null;
196
    }
197

    
198
    protected IStructuredSelection createSelection(Object selection) {
199
        if(selection==null){
200
            return null;
201
        }
202
        IStructuredSelection structuredSelection;
203
        if(!(selection instanceof IStructuredSelection)){
204
            structuredSelection = new StructuredSelection(selection);
205
        }
206
        else{
207
            structuredSelection = (IStructuredSelection) selection;
208
        }
209
        return structuredSelection;
210
    }
211

    
212
    /**
213
     * {@inheritDoc}
214
     */
215
    @Override
216
    public ConversationHolder getConversationHolder() {
217
        if(selectionProvidingPart != null && selectionProvidingPart instanceof IConversationEnabled) {
218
            return ((IConversationEnabled) selectionProvidingPart).getConversationHolder();
219
        }
220
        return null;
221
    }
222

    
223
    /**
224
     * {@inheritDoc}
225
     */
226
    @Override
227
    public boolean postOperation(CdmBase objectAffectedByOperation) {
228
        changed(objectAffectedByOperation);
229
        return true;
230
    }
231

    
232
    /**
233
     * {@inheritDoc}
234
     */
235
    @Override
236
    public boolean onComplete() {
237
        viewer.refresh();
238
        return true;
239
    }
240

    
241
    /**
242
     * {@inheritDoc}
243
     */
244
    @Override
245
    public Object getSelectionProvidingPart() {
246
        return selectionProvidingPart;
247
    }
248

    
249
    @PreDestroy
250
    private void dispose() {
251
    }
252

    
253
    @PersistState
254
    private void persistState(){
255

    
256
    }
257

    
258
    /**
259
     * {@inheritDoc}
260
     */
261
    @Override
262
    public void update(CdmDataChangeMap arg0) {
263
    }
264

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

    
272
    protected abstract String getViewName();
273

    
274
}
(2-2/2)