Project

General

Profile

Download (6.3 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;
11

    
12
import org.eclipse.jface.action.GroupMarker;
13
import org.eclipse.jface.action.MenuManager;
14
import org.eclipse.jface.viewers.ISelection;
15
import org.eclipse.jface.viewers.IStructuredSelection;
16
import org.eclipse.jface.viewers.StructuredSelection;
17
import org.eclipse.jface.viewers.TreeNode;
18
import org.eclipse.jface.viewers.TreeViewer;
19
import org.eclipse.jface.viewers.Viewer;
20
import org.eclipse.swt.SWT;
21
import org.eclipse.swt.widgets.Composite;
22
import org.eclipse.swt.widgets.Control;
23
import org.eclipse.swt.widgets.Menu;
24
import org.eclipse.swt.widgets.Tree;
25
import org.eclipse.ui.IWorkbenchActionConstants;
26
import org.eclipse.ui.IWorkbenchPart;
27
import org.eclipse.ui.internal.E4PartWrapper;
28

    
29
import eu.etaxonomy.cdm.model.description.IDescribable;
30
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
31
import eu.etaxonomy.taxeditor.bulkeditor.BulkEditor;
32
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
33
import eu.etaxonomy.taxeditor.editor.view.descriptive.DescriptiveViewPart;
34
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
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.AbstractCdmEditorViewPart;
39
import eu.etaxonomy.taxeditor.view.detail.DetailsViewPart;
40
import eu.etaxonomy.taxeditor.view.supplementaldata.SupplementalDataViewPart;
41
import eu.etaxonomy.taxeditor.workbench.WorkbenchUtility;
42

    
43
/**
44
 * @author n.hoffmann
45
 * @created Jun 15, 2010
46
 * @version 1.0
47
 */
48
public class MediaViewPart extends AbstractCdmEditorViewPart implements IPartContentHasDetails, IPartContentHasSupplementalData {
49

    
50
	public static final String ID = "eu.etaxonomy.taxeditor.editor.view.media"; //$NON-NLS-1$
51

    
52

    
53
	private TreeViewer viewer;
54

    
55
	/** {@inheritDoc} */
56
	@Override
57
	public void createViewer(Composite parent) {
58

    
59
		viewer = new TreeViewer(new Tree(parent, SWT.MULTI | SWT.H_SCROLL
60
				| SWT.V_SCROLL | SWT.FULL_SELECTION));
61

    
62
		viewer.setContentProvider(new MediaContentProvider());
63
		viewer.setLabelProvider(new MediaLabelProvider());
64
		viewer.setAutoExpandLevel(2);
65

    
66
		// Propagate selection from viewer
67
		getSite().setSelectionProvider(viewer);
68

    
69
		// Add context menu to tree
70
		MenuManager menuMgr = new MenuManager();
71
		menuMgr.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
72
		menuMgr.setRemoveAllWhenShown(true);
73
		getSite().registerContextMenu(menuMgr, viewer);
74

    
75
		Control control = viewer.getControl();
76
		Menu menu = menuMgr.createContextMenu(control);
77
		menuMgr.setRemoveAllWhenShown(true);
78
		control.setMenu(menu);
79

    
80
	}
81

    
82
	/** {@inheritDoc} */
83
	@Override
84
	protected ISelection getInitialSelection() {
85
		if(getEditor() != null){
86
			return new StructuredSelection(getEditor().getEditorInput());
87
		}
88

    
89
		return super.getInitialSelection();
90
	}
91

    
92
	/** {@inheritDoc} */
93
	@Override
94
    protected void selectionChanged_internal(IWorkbenchPart workbenchPart, ISelection selection) {
95
	    Object partObject = workbenchPart;
96

    
97
	    if(partObject==this){
98
	        return;
99
	    }
100

    
101
        Object wrappedPart = WorkbenchUtility.getE4WrappedPart(partObject);
102
        if(wrappedPart!=null){
103
            partObject = wrappedPart;
104
        }
105
//		if(AbstractUtility.getActiveE4Editor() == null){
106
//			showEmptyPage();
107
//			return;
108
//		}
109

    
110

    
111
        if (partObject instanceof DetailsViewPart || partObject instanceof SupplementalDataViewPart
112
                || partObject instanceof DescriptiveViewPart) {
113
            // do not show empty page as these views are also used to edit the
114
            // description selected in this view
115
            return;
116
        }
117

    
118
		if(partObject instanceof IPartContentHasMedia && ((IPartContentHasMedia) partObject).canAttachMedia() ){
119
		    if(selection instanceof IStructuredSelection){
120
		    	Object firstElement = ((IStructuredSelection) selection).getFirstElement();
121
		        if(firstElement instanceof TreeNode){
122
		            showViewer(partObject, new StructuredSelection(((TreeNode) firstElement).getValue()));
123
		            return;
124
		        }
125
		        else if(firstElement!=null
126
	                    && firstElement instanceof IDescribable<?>){
127
		            showViewer(partObject, (IStructuredSelection) selection);
128
		            return;
129
		        }
130
		    }
131
		}
132
		if (selection instanceof IStructuredSelection){
133
			IStructuredSelection structuredSelection = (IStructuredSelection) selection;
134
			if(structuredSelection.getFirstElement() instanceof TreeNode){
135
					if (((TreeNode)structuredSelection.getFirstElement()).getValue() instanceof SpecimenOrObservationBase){
136
				    structuredSelection = new StructuredSelection(((TreeNode)structuredSelection.getFirstElement()).getValue());
137
				    showViewer(partObject, structuredSelection);
138
				    return;
139
				}
140
			}
141
		}
142

    
143
		showEmptyPage();
144
	}
145

    
146
	/** {@inheritDoc} */
147
	@Override
148
	public void changed(Object object) {
149
	    viewer.expandToLevel(2);
150
	    viewer.refresh();
151
	    if(object != null){
152
	        StructuredSelection selection = new StructuredSelection(object);
153
	        viewer.setSelection(selection, true);
154
	    }
155
	    //TODO: should be replaced with the possibility to set views dirty
156
	    // when we move to Eclipse 4
157
	    // take a look at ISaveblePart
158
	    if(part instanceof BulkEditor && !(object instanceof SpecimenOrObservationBase<?>)){
159
	        ((BulkEditor) part).forceDirty();
160
	        IStructuredSelection selection = (IStructuredSelection) ((BulkEditor) part).getSelectionProvider().getSelection();
161
            ((BulkEditor) part).changed(selection.getFirstElement());
162

    
163
	    }
164
	    if (part instanceof E4PartWrapper){
165
	    	part = WorkbenchUtility.getE4WrappedPart(part);
166
	    	if (part instanceof IDirtyMarkable){
167
	    		 StructuredSelection selection = new StructuredSelection(object);
168
	    		((IDirtyMarkable)part).changed(selection.getFirstElement());
169
	    	}
170
	    }
171

    
172

    
173
	    super.changed(object);
174
	}
175

    
176
	/** {@inheritDoc} */
177
	@Override
178
	public Viewer getViewer() {
179
		return viewer;
180
	}
181

    
182
	@Override
183
    public boolean onComplete() {
184
		return true;
185
	}
186

    
187
	@Override
188
	protected String getViewName() {
189
		return Messages.MediaViewPart_MEDIA;
190
	}
191
}
(3-3/3)