Project

General

Profile

Download (8.37 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 thisPart;
121

    
122
    protected MPart selectionProvidingPart;
123

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

    
144
    protected void createPartTitle(Object selectedObject){
145
        if(thisPart!=null){
146
            if(selectedObject!=null){
147
                thisPart.setLabel(getViewName()+": "+selectedObject); //$NON-NLS-1$
148
            }
149
            else{
150
                thisPart.setLabel(getViewName());
151
            }
152
        }
153
    }
154

    
155
    public Viewer getViewer() {
156
        return viewer;
157
    }
158

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

    
170
//                			Taxon accepted= ((ITaxonEditor)activePart).getTaxon();
171
                			Set<TaxonRelationship> rels =  taxon.getTaxonRelations(accepted);
172

    
173
                			 if (rels.iterator().hasNext()){
174
                				 TaxonRelationship rel = rels.iterator().next();
175
                				 if (rel.getType().equals(TaxonRelationshipType.MISAPPLIED_NAME_FOR())){
176
        	        				 viewer.setInput(rel);
177

    
178
        	        				 return;
179
                				 }
180
                			 }
181
                		}
182

    
183

    
184
                	}
185
                }
186
            	viewer.setInput(element);
187

    
188
            	createPartTitle(element);
189

    
190
            }
191
            selectionProvidingPart = activePart;
192
        }
193
    }
194

    
195
    protected Object createPartObject(MPart activePart) {
196
        Object partObject = activePart;
197
        Object wrappedPart = WorkbenchUtility.getE4WrappedPart(activePart);
198
        if(wrappedPart!=null){
199
            partObject = wrappedPart;
200
        }
201
        return partObject;
202
    }
203

    
204
    protected void showEmptyPage() {
205
        if(viewer!=null && viewer.getControl()!=null && viewer.getControl().isDisposed()){
206
            viewer.setInput(null);
207
        }
208
        selectionProvidingPart = null;
209
    }
210

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

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

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

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

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

    
262
    @PreDestroy
263
    private void dispose() {
264
    }
265

    
266
    @PersistState
267
    private void persistState(){
268

    
269
    }
270

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

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

    
285
    protected abstract String getViewName();
286

    
287
}
(2-2/2)