Project

General

Profile

Download (11.9 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2017 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10
package eu.etaxonomy.taxeditor.editor.view.descriptive.e4;
11

    
12
import java.util.Arrays;
13
import java.util.HashMap;
14
import java.util.List;
15
import java.util.Map;
16

    
17
import javax.annotation.PostConstruct;
18

    
19
import org.eclipse.e4.core.contexts.ContextInjectionFactory;
20
import org.eclipse.e4.core.contexts.IEclipseContext;
21
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
22
import org.eclipse.e4.ui.services.EMenuService;
23
import org.eclipse.jface.action.Action;
24
import org.eclipse.jface.action.IAction;
25
import org.eclipse.jface.resource.ImageDescriptor;
26
import org.eclipse.jface.viewers.AbstractTreeViewer;
27
import org.eclipse.jface.viewers.IStructuredSelection;
28
import org.eclipse.jface.viewers.ITreeContentProvider;
29
import org.eclipse.jface.viewers.StructuredSelection;
30
import org.eclipse.jface.viewers.TreeNode;
31
import org.eclipse.jface.viewers.TreeViewer;
32
import org.eclipse.swt.SWT;
33
import org.eclipse.swt.dnd.DND;
34
import org.eclipse.swt.dnd.Transfer;
35
import org.eclipse.swt.graphics.ImageData;
36
import org.eclipse.swt.widgets.Composite;
37
import org.eclipse.swt.widgets.Tree;
38

    
39
import eu.etaxonomy.cdm.model.common.CdmBase;
40
import eu.etaxonomy.cdm.model.description.DescriptionBase;
41
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
42
import eu.etaxonomy.cdm.model.description.IDescribable;
43
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
44
import eu.etaxonomy.cdm.model.taxon.Taxon;
45
import eu.etaxonomy.taxeditor.bulkeditor.BulkEditor;
46
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
47
import eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4;
48
import eu.etaxonomy.taxeditor.editor.view.checklist.ChecklistEditor;
49
import eu.etaxonomy.taxeditor.editor.view.concept.e4.ConceptViewPartE4;
50
import eu.etaxonomy.taxeditor.editor.view.descriptive.DescriptionElementDragListener;
51
import eu.etaxonomy.taxeditor.editor.view.descriptive.DescriptionElementDropAdapter;
52
import eu.etaxonomy.taxeditor.editor.view.descriptive.DescriptionElementTransfer;
53
import eu.etaxonomy.taxeditor.editor.view.descriptive.DescriptiveContentProvider;
54
import eu.etaxonomy.taxeditor.editor.view.descriptive.DescriptiveLabelProvider;
55
import eu.etaxonomy.taxeditor.editor.view.descriptive.DescriptiveViewerSorter;
56
import eu.etaxonomy.taxeditor.editor.view.media.e4.MediaViewPartE4;
57
import eu.etaxonomy.taxeditor.model.AbstractUtility;
58
import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
59
import eu.etaxonomy.taxeditor.model.FeatureNodeContainerTree;
60
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
61
import eu.etaxonomy.taxeditor.model.IPartContentHasFactualData;
62
import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
63
import eu.etaxonomy.taxeditor.model.ImageResources;
64
import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
65
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
66
import eu.etaxonomy.taxeditor.view.e4.AbstractCdmEditorPartE4;
67
import eu.etaxonomy.taxeditor.view.e4.details.DetailsPartE4;
68
import eu.etaxonomy.taxeditor.view.e4.supplementaldata.SupplementalDataPartE4;
69

    
70
/**
71
 * @author pplitzner
72
 * @date 15.08.2017
73
 *
74
 */
75
public class FactualDataPartE4 extends AbstractCdmEditorPartE4
76
        implements IPartContentHasDetails, IPartContentHasSupplementalData, ICdmEntitySessionEnabled {
77

    
78
    /**
79
     * Maps {@link FeatureNodeContainerTree} to their corresponding {@link TaxonDescritpion}.<br>
80
     * This serves as input for the {@link ITreeContentProvider} of the {@link TreeViewer}
81
     */
82
    protected Map<DescriptionBase<?>, FeatureNodeContainerTree> featureNodeContainerCache = new HashMap<DescriptionBase<?>, FeatureNodeContainerTree>();
83

    
84
    protected ToggleDescriptionAction showAllElementsAction;
85

    
86
    protected ToggleDescriptionAction hideAllElementsAction;
87

    
88
    protected int dndOperations = DND.DROP_COPY | DND.DROP_MOVE;
89

    
90
    private DescriptiveContentProvider provider;
91

    
92
    @PostConstruct
93
    public void create(Composite parent, EMenuService menuService, MPart thisPart,
94
            IEclipseContext context) {
95
        TreeViewer treeViewer = new TreeViewer(new Tree(parent, SWT.H_SCROLL
96
                | SWT.V_SCROLL | SWT.FULL_SELECTION));
97

    
98
        this.thisPart = thisPart;
99

    
100
        provider = new DescriptiveContentProvider(featureNodeContainerCache);
101
        treeViewer.setContentProvider(provider);
102
        treeViewer.setLabelProvider(new DescriptiveLabelProvider());
103
        treeViewer.setSorter(new DescriptiveViewerSorter());
104
        treeViewer.setAutoExpandLevel(2);
105
        Transfer[] transfers = new Transfer[] { DescriptionElementTransfer.getInstance() };
106
        treeViewer.addDragSupport(dndOperations, transfers, new DescriptionElementDragListener(
107
                this));
108
        DescriptionElementDropAdapter dropListener = new DescriptionElementDropAdapter(viewer);
109
        ContextInjectionFactory.inject(dropListener, context);
110
        treeViewer.addDropSupport(dndOperations, transfers,
111
                dropListener);
112

    
113
        viewer = treeViewer;
114

    
115
        // Propagate selection from viewer
116
        selectionChangedListener = (event -> selService.setSelection(AbstractUtility.getElementsFromSelectionChangedEvent(event)));
117
        viewer.addSelectionChangedListener(selectionChangedListener);
118

    
119
        //create context menu
120
        menuService.registerContextMenu(viewer.getControl(), "eu.etaxonomy.taxeditor.editor.popupmenu.factualDataView");
121

    
122
        showAllElementsAction = new ToggleDescriptionAction(false);
123
        hideAllElementsAction = new ToggleDescriptionAction(true);
124

    
125
        //FIXME E4 implement toolbar
126
//        createToolbar();
127

    
128
        //FIXME E4 migrate drag and drop
129
    }
130

    
131
//    protected void createToolbar() {
132
//        IToolBarManager toolBarManager = getViewSite().getActionBars().getToolBarManager();
133
//        toolBarManager.add(showAllElementsAction);
134
//        toolBarManager.add(hideAllElementsAction);
135
//    }
136

    
137

    
138
    @Override
139
    public void selectionChanged_internal(Object selection, MPart activePart, MPart thisPart) {
140
        if(activePart==thisPart){
141
            return;
142
        }
143

    
144
        Object partObject = createPartObject(activePart);
145

    
146
        if (partObject instanceof DetailsPartE4 || partObject instanceof SupplementalDataPartE4
147
                || partObject instanceof MediaViewPartE4 || partObject instanceof ConceptViewPartE4) {
148
            // do not show empty page as these views are also used to edit the
149
            // description selected in this view
150
            return;
151
        }
152

    
153
        if(partObject instanceof IPartContentHasFactualData){
154
            IStructuredSelection structuredSelection = createSelection(selection);
155
            if(structuredSelection==null || structuredSelection.isEmpty()){
156
                showEmptyPage();
157
                return;
158
            }
159
            if(partObject instanceof ChecklistEditor){
160
                if(structuredSelection.getFirstElement() instanceof Taxon){
161
                    Taxon taxon = (Taxon)structuredSelection.getFirstElement();
162
                    structuredSelection = new StructuredSelection(taxon);
163
                }
164
            }
165
            Object firstElement = structuredSelection.getFirstElement();
166
            if (partObject instanceof TaxonNameEditorE4){
167
                if (firstElement != ((TaxonNameEditorE4)partObject).getTaxon()){
168
                    showEmptyPage();
169
                    return;
170
                }
171
            }
172
            if(firstElement instanceof TreeNode){
173
                firstElement = ((TreeNode) firstElement).getValue();
174
            }
175
            if(firstElement instanceof IDescribable<?>
176
                && partObject instanceof IPartContentHasFactualData
177
                    && !(firstElement instanceof SpecimenOrObservationBase && partObject instanceof BulkEditor)){
178

    
179
                featureNodeContainerCache.clear();
180
                showViewer(structuredSelection, activePart, viewer);
181
                return;
182
            }
183
        }
184
        showEmptyPage();
185
        return;
186
    }
187

    
188
    @Override
189
    protected String getViewName(){
190
        return Messages.DescriptiveViewPart_FACTUAL_DATA;
191
    }
192

    
193
    /**
194
     *
195
     * @author n.hoffmann
196
     * @created May 28, 2010
197
     * @version 1.0
198
     */
199
    protected class ToggleDescriptionAction extends Action{
200
        private final boolean expanded;
201

    
202
        public ToggleDescriptionAction(boolean expand){
203
            super(null, IAction.AS_PUSH_BUTTON);
204
            expanded = expand;
205
            setImageAndTooltip();
206
        }
207

    
208
        private void setImageAndTooltip(){
209
            setImageDescriptor(new ImageDescriptor() {
210
                @Override
211
                public ImageData getImageData() {
212
                    setText(expanded ? Messages.DescriptiveViewPart_COLLAPSE_ALL : Messages.DescriptiveViewPart_EXPAND_ALL);
213
                    String resource = expanded ? ImageResources.COLLAPSE_ALL : ImageResources.EXPAND_ALL;
214
                    return ImageResources.getImage(resource).getImageData();
215
                }
216
            });
217

    
218
            String toolTipText = expanded ? Messages.DescriptiveViewPart_COLLAPSE_ALL : Messages.DescriptiveViewPart_SHOW_ALL_DATA;
219
            setToolTipText(toolTipText);
220
        }
221

    
222
        @Override
223
        public void run() {
224
            if(expanded){
225
                ((AbstractTreeViewer) viewer).collapseAll();
226
            }else{
227
                ((AbstractTreeViewer) viewer).expandAll();
228
            }
229
            setImageAndTooltip();
230
        }
231
    }
232

    
233
    /** {@inheritDoc} */
234
    @Override
235
    public void changed(Object object) {
236
        for(FeatureNodeContainerTree containerTree : featureNodeContainerCache.values()){
237
            containerTree.buildTree();
238
        }
239
        ((AbstractTreeViewer) viewer).expandToLevel(2);
240
        viewer.refresh();
241

    
242
        if(object instanceof DescriptionElementBase){
243
            DescriptionElementBase descriptionElement = (DescriptionElementBase) object;
244
            DescriptionBase description = descriptionElement.getInDescription();
245
            FeatureNodeContainerTree containerTree = featureNodeContainerCache.get(description);
246

    
247
            FeatureNodeContainer featureNodeContainer = containerTree.getFeatureNodeContainerForDescriptionElement(descriptionElement);
248
            ((AbstractTreeViewer) viewer).expandToLevel(featureNodeContainer, 1);
249

    
250
        }
251

    
252
        if(object != null){
253
            StructuredSelection selection = new StructuredSelection(object);
254
            viewer.setSelection(selection, true);
255
        }
256
//        if(part instanceof BulkEditor && !(object instanceof SpecimenOrObservationBase<?>)){
257
//            ((BulkEditor) part).forceDirty();
258
//            IStructuredSelection selection = (IStructuredSelection) ((BulkEditor) part).getSelectionProvider().getSelection();
259
//            ((BulkEditor) part).changed(selection.getFirstElement());
260
//
261
//        }
262
//
263
//        if (part instanceof E4PartWrapper){
264
//            part = WorkbenchUtility.getE4WrappedPart(part);
265
//            if (part instanceof IDirtyMarkable){
266
//                 StructuredSelection selection = new StructuredSelection(object);
267
//                ((IDirtyMarkable)part).changed(selection.getFirstElement());
268
//            }
269
//        }
270

    
271

    
272
        super.changed(object);
273
    }
274

    
275
    public void toggleShowOnlyIndividualAssociations(){
276
        //FIXME E4 migrate toggle menu icon
277
        provider.toggleShowOnlyIndividualAssociations();
278
        viewer.refresh();
279
    }
280

    
281
    @Override
282
    public ICdmEntitySession getCdmEntitySession() {
283
        if(selectionProvidingPart != null && selectionProvidingPart instanceof ICdmEntitySessionEnabled) {
284
            return ((ICdmEntitySessionEnabled) selectionProvidingPart).getCdmEntitySession();
285
        }
286
        return null;
287
    }
288

    
289
    @Override
290
    public  List<? extends CdmBase> getRootEntities() {
291
        return Arrays.asList((CdmBase)getViewer().getInput());
292
    }
293
    @Override
294
    public Map<Object, List<String>> getPropertyPathsMap() {
295
        return null;
296
    }
297

    
298
}
    (1-1/1)