Project

General

Profile

Download (6.01 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
		getSite().registerContextMenu(menuMgr, viewer);
73

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

    
79
	}
80

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

    
88
		return super.getInitialSelection();
89
	}
90

    
91
	/** {@inheritDoc} */
92
	@Override
93
    protected void selectionChanged_internal(IWorkbenchPart part, ISelection selection) {
94
	    if(part==this){
95
	        return;
96
	    }
97
//		if(AbstractUtility.getActiveE4Editor() == null){
98
//			showEmptyPage();
99
//			return;
100
//		}
101

    
102

    
103
        if (part instanceof DetailsViewPart || part instanceof SupplementalDataViewPart
104
                || part instanceof DescriptiveViewPart) {
105
            // do not show empty page as these views are also used to edit the
106
            // description selected in this view
107
            return;
108
        }
109

    
110
		if(part instanceof IPartContentHasMedia && ((IPartContentHasMedia) part).canAttachMedia() ){
111
		    if(selection instanceof IStructuredSelection){
112
		    	Object firstElement = ((IStructuredSelection) selection).getFirstElement();
113
		        if(firstElement instanceof TreeNode){
114
		            showViewer(part, new StructuredSelection(((TreeNode) firstElement).getValue()));
115
		            return;
116
		        }
117
		        else if(firstElement!=null
118
	                    && firstElement instanceof IDescribable<?>){
119
		            showViewer(part, (IStructuredSelection) selection);
120
		            return;
121
		        }
122
		    }
123
		}
124
		if (selection instanceof IStructuredSelection){
125
			IStructuredSelection structuredSelection = (IStructuredSelection) selection;
126
			if(structuredSelection.getFirstElement() instanceof TreeNode){
127
					if (((TreeNode)structuredSelection.getFirstElement()).getValue() instanceof SpecimenOrObservationBase){
128
				    structuredSelection = new StructuredSelection(((TreeNode)structuredSelection.getFirstElement()).getValue());
129
				    showViewer(part, structuredSelection);
130
				    return;
131
				}
132
			}
133
		}
134

    
135
		showEmptyPage();
136
	}
137

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

    
155
	    }
156
	    if (part instanceof E4PartWrapper){
157
	    	part = WorkbenchUtility.getE4WrappedPart(part);
158
	    	if (part instanceof IDirtyMarkable){
159
	    		 StructuredSelection selection = new StructuredSelection(object);
160
	    		((IDirtyMarkable)part).changed(selection.getFirstElement());
161
	    	}
162
	    }
163

    
164

    
165
	    super.changed(object);
166
	}
167

    
168
	/** {@inheritDoc} */
169
	@Override
170
	public Viewer getViewer() {
171
		return viewer;
172
	}
173

    
174
	@Override
175
    public boolean onComplete() {
176
		return true;
177
	}
178

    
179
	@Override
180
	protected String getViewName() {
181
		return Messages.MediaViewPart_MEDIA;
182
	}
183
}
(3-3/3)