Project

General

Profile

Download (4.8 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 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

    
10
package eu.etaxonomy.taxeditor.editor.view.media.e4;
11

    
12
import javax.annotation.PostConstruct;
13
import javax.inject.Inject;
14

    
15
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
16
import org.eclipse.e4.ui.services.EMenuService;
17
import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
18
import org.eclipse.jface.viewers.AbstractTreeViewer;
19
import org.eclipse.jface.viewers.ISelectionChangedListener;
20
import org.eclipse.jface.viewers.IStructuredSelection;
21
import org.eclipse.jface.viewers.StructuredSelection;
22
import org.eclipse.jface.viewers.TreeNode;
23
import org.eclipse.jface.viewers.TreeViewer;
24
import org.eclipse.swt.SWT;
25
import org.eclipse.swt.widgets.Composite;
26
import org.eclipse.swt.widgets.Tree;
27

    
28
import eu.etaxonomy.cdm.model.description.IDescribable;
29
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
30
import eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4;
31
import eu.etaxonomy.taxeditor.editor.view.descriptive.e4.FactualDataPartE4;
32
import eu.etaxonomy.taxeditor.editor.view.media.MediaContentProvider;
33
import eu.etaxonomy.taxeditor.editor.view.media.MediaLabelProvider;
34
import eu.etaxonomy.taxeditor.model.AbstractUtility;
35
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
36
import eu.etaxonomy.taxeditor.model.IPartContentHasMedia;
37
import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
38
import eu.etaxonomy.taxeditor.view.e4.AbstractCdmEditorPartE4;
39
import eu.etaxonomy.taxeditor.view.e4.details.DetailsPartE4;
40
import eu.etaxonomy.taxeditor.view.e4.supplementaldata.SupplementalDataPartE4;
41

    
42
/**
43
 *
44
 * @author pplitzner
45
 * @since Aug 14, 2017
46
 *
47
 */
48
public class MediaViewPartE4 extends AbstractCdmEditorPartE4
49
        implements IPartContentHasDetails, IPartContentHasSupplementalData {
50

    
51
    private ISelectionChangedListener selectionChangedListener;
52

    
53
	@Inject
54
	private ESelectionService selService;
55

    
56
	@PostConstruct
57
	public void create(Composite parent, EMenuService menuService, MPart thisPart) {
58

    
59
	    this.thisPart = thisPart;
60
		TreeViewer treeViewer = new TreeViewer(new Tree(parent, SWT.H_SCROLL
61
				| SWT.V_SCROLL | SWT.FULL_SELECTION));
62

    
63
		treeViewer.setContentProvider(new MediaContentProvider());
64
		treeViewer.setLabelProvider(new MediaLabelProvider());
65
		treeViewer.setAutoExpandLevel(2);
66
		viewer = treeViewer;
67

    
68
		// Propagate selection from viewer
69
        selectionChangedListener = (event -> selService.setSelection(AbstractUtility.getElementsFromSelectionChangedEvent(event)));
70
        viewer.addSelectionChangedListener(selectionChangedListener);
71

    
72
        //create context menu
73
        menuService.registerContextMenu(viewer.getControl(), "eu.etaxonomy.taxeditor.editor.popupmenu.mediaview");
74

    
75
	}
76

    
77
    @Override
78
    public void selectionChanged_internal(Object selection, MPart activePart, MPart thisPart) {
79
	    if(activePart==thisPart){
80
	        return;
81
	    }
82

    
83
	    Object partObject = createPartObject(activePart);
84

    
85
	    if (partObject instanceof DetailsPartE4 || partObject instanceof SupplementalDataPartE4
86
	            || partObject instanceof FactualDataPartE4) {
87
	        // do not show empty page as these views are also used to edit the
88
	        // description selected in this view
89
	        return;
90
	    }
91

    
92
	    if(partObject instanceof IPartContentHasMedia && ((IPartContentHasMedia) partObject).canAttachMedia()){
93
	        IStructuredSelection structuredSelection = createSelection(selection);
94
	        if(structuredSelection==null || structuredSelection.isEmpty()){
95
	            showEmptyPage();
96
	            return;
97
	        }
98

    
99
	        Object firstElement = structuredSelection.getFirstElement();
100
	        if (partObject instanceof TaxonNameEditorE4){
101
	            if (firstElement != ((TaxonNameEditorE4)partObject).getTaxon()){
102
	                showEmptyPage();
103
	                return;
104
	            }
105
	        }
106
	        if(firstElement instanceof TreeNode){
107
	            showViewer(new StructuredSelection(((TreeNode) firstElement).getValue()), activePart, viewer);
108
	            return;
109
	        }
110
	        else if(firstElement!=null
111
	                && firstElement instanceof IDescribable<?>){
112
	            showViewer(structuredSelection, activePart, viewer);
113
	            return;
114
	        }
115
	    }
116
	    showEmptyPage();
117
	    return;
118
	}
119

    
120
	/** {@inheritDoc} */
121
	@Override
122
	public void changed(Object object) {
123
	    ((AbstractTreeViewer) viewer).expandToLevel(2);
124
	    viewer.refresh();
125
	    if(object != null){
126
	        StructuredSelection selection = new StructuredSelection(object);
127
	        viewer.setSelection(selection, true);
128
	    }
129
	    super.changed(object);
130
	}
131

    
132
	@Override
133
	protected String getViewName() {
134
		return Messages.MediaViewPart_MEDIA;
135
	}
136

    
137
}
    (1-1/1)