7bfee153d75129a063401743701729797e56e6e7
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / media / MediaViewPart.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 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
11 package eu.etaxonomy.taxeditor.editor.view.media;
12
13 import org.eclipse.jface.action.GroupMarker;
14 import org.eclipse.jface.action.MenuManager;
15 import org.eclipse.jface.viewers.ISelection;
16 import org.eclipse.jface.viewers.IStructuredSelection;
17 import org.eclipse.jface.viewers.StructuredSelection;
18 import org.eclipse.jface.viewers.TreeNode;
19 import org.eclipse.jface.viewers.TreeViewer;
20 import org.eclipse.jface.viewers.Viewer;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.swt.widgets.Control;
24 import org.eclipse.swt.widgets.Menu;
25 import org.eclipse.swt.widgets.Tree;
26 import org.eclipse.ui.IEditorInput;
27 import org.eclipse.ui.IEditorPart;
28 import org.eclipse.ui.IWorkbenchActionConstants;
29 import org.eclipse.ui.IWorkbenchPart;
30
31 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
32 import eu.etaxonomy.taxeditor.bulkeditor.BulkEditor;
33 import eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor;
34 import eu.etaxonomy.taxeditor.editor.view.descriptive.DescriptiveViewPart;
35 import eu.etaxonomy.taxeditor.model.AbstractUtility;
36 import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
37 import eu.etaxonomy.taxeditor.model.IPartContentHasMedia;
38 import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
39 import eu.etaxonomy.taxeditor.view.AbstractCdmEditorViewPart;
40 import eu.etaxonomy.taxeditor.view.detail.DetailsViewPart;
41 import eu.etaxonomy.taxeditor.view.supplementaldata.SupplementalDataViewPart;
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";
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 control.setMenu(menu);
77
78 }
79
80 /** {@inheritDoc} */
81 @Override
82 protected ISelection getInitialSelection() {
83 if(getEditor() != null){
84 return new StructuredSelection(getEditor().getEditorInput());
85 }
86
87 return super.getInitialSelection();
88 }
89
90 /** {@inheritDoc} */
91 @Override
92 protected void selectionChanged_internal(IWorkbenchPart part, ISelection selection) {
93 if(part==this){
94 return;
95 }
96 if(AbstractUtility.getActiveEditor() == null){
97 showEmptyPage();
98 return;
99 }
100
101 if (part instanceof DetailsViewPart || part instanceof SupplementalDataViewPart
102 || part instanceof DescriptiveViewPart) {
103 // do not show empty page as these views are also used to edit the
104 // description selected in this view
105 return;
106 }
107
108 if(part instanceof IPartContentHasMedia && ((IPartContentHasMedia) part).canAttachMedia()){
109 if(part instanceof MultiPageTaxonEditor){
110 IEditorInput input = ((IEditorPart) part).getEditorInput();
111 showViewer(part, new StructuredSelection(input));
112 return;
113 }
114 else if(selection instanceof IStructuredSelection){
115 Object firstElement = ((IStructuredSelection) selection).getFirstElement();
116 if(firstElement instanceof TreeNode){
117 showViewer(part, new StructuredSelection(((TreeNode) firstElement).getValue()));
118 return;
119 }
120 else if(firstElement!=null){
121 showViewer(part, (IStructuredSelection) selection);
122 return;
123 }
124 }
125 }
126 showEmptyPage();
127 }
128
129 /** {@inheritDoc} */
130 @Override
131 public void changed(Object object) {
132 viewer.expandToLevel(2);
133 viewer.refresh();
134 if(object != null){
135 StructuredSelection selection = new StructuredSelection(object);
136 viewer.setSelection(selection, true);
137 }
138 //TODO: should be replaced with the possibility to set views dirty
139 // when we move to Eclipse 4
140 // take a look at ISaveblePart
141 if(part instanceof BulkEditor && !(object instanceof SpecimenOrObservationBase<?>)){
142 ((BulkEditor) part).forceDirty();
143 }
144 super.changed(object);
145 }
146
147 /** {@inheritDoc} */
148 @Override
149 public Viewer getViewer() {
150 return viewer;
151 }
152
153 @Override
154 public boolean onComplete() {
155 return true;
156 }
157 }