46f55589c559bd63a6d1372434eceb461d41c8b1
[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.IWorkbenchActionConstants;
27 import org.eclipse.ui.IWorkbenchPart;
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.view.descriptive.DescriptiveViewPart;
33 import eu.etaxonomy.taxeditor.model.AbstractUtility;
34 import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
35 import eu.etaxonomy.taxeditor.model.IPartContentHasMedia;
36 import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
37 import eu.etaxonomy.taxeditor.view.AbstractCdmEditorViewPart;
38 import eu.etaxonomy.taxeditor.view.detail.DetailsViewPart;
39 import eu.etaxonomy.taxeditor.view.supplementaldata.SupplementalDataViewPart;
40
41 /**
42 * @author n.hoffmann
43 * @created Jun 15, 2010
44 * @version 1.0
45 */
46 public class MediaViewPart extends AbstractCdmEditorViewPart implements IPartContentHasDetails, IPartContentHasSupplementalData {
47
48 public static final String ID = "eu.etaxonomy.taxeditor.editor.view.media";
49
50
51 private TreeViewer viewer;
52
53 /** {@inheritDoc} */
54 @Override
55 public void createViewer(Composite parent) {
56
57 viewer = new TreeViewer(new Tree(parent, SWT.MULTI | SWT.H_SCROLL
58 | SWT.V_SCROLL | SWT.FULL_SELECTION));
59
60 viewer.setContentProvider(new MediaContentProvider());
61 viewer.setLabelProvider(new MediaLabelProvider());
62 viewer.setAutoExpandLevel(2);
63
64 // Propagate selection from viewer
65 getSite().setSelectionProvider(viewer);
66
67 // Add context menu to tree
68 MenuManager menuMgr = new MenuManager();
69 menuMgr.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
70 getSite().registerContextMenu(menuMgr, viewer);
71
72 Control control = viewer.getControl();
73 Menu menu = menuMgr.createContextMenu(control);
74 control.setMenu(menu);
75
76 }
77
78 /** {@inheritDoc} */
79 @Override
80 protected ISelection getInitialSelection() {
81 if(getEditor() != null){
82 return new StructuredSelection(getEditor().getEditorInput());
83 }
84
85 return super.getInitialSelection();
86 }
87
88 /** {@inheritDoc} */
89 @Override
90 protected void selectionChanged_internal(IWorkbenchPart part, ISelection selection) {
91 if(part==this){
92 return;
93 }
94 if(AbstractUtility.getActiveEditor() == null){
95 showEmptyPage();
96 return;
97 }
98
99 if (part instanceof DetailsViewPart || part instanceof SupplementalDataViewPart
100 || part instanceof DescriptiveViewPart) {
101 // do not show empty page as these views are also used to edit the
102 // description selected in this view
103 return;
104 }
105
106 if(part instanceof IPartContentHasMedia && ((IPartContentHasMedia) part).canAttachMedia()){
107 if(selection instanceof IStructuredSelection
108 && ((IStructuredSelection) selection).getFirstElement() instanceof IDescribable<?>){
109 Object firstElement = ((IStructuredSelection) selection).getFirstElement();
110 if(firstElement instanceof TreeNode){
111 showViewer(part, new StructuredSelection(((TreeNode) firstElement).getValue()));
112 return;
113 }
114 else if(firstElement!=null){
115 showViewer(part, (IStructuredSelection) selection);
116 return;
117 }
118 }
119 }
120 showEmptyPage();
121 }
122
123 /** {@inheritDoc} */
124 @Override
125 public void changed(Object object) {
126 viewer.expandToLevel(2);
127 viewer.refresh();
128 if(object != null){
129 StructuredSelection selection = new StructuredSelection(object);
130 viewer.setSelection(selection, true);
131 }
132 //TODO: should be replaced with the possibility to set views dirty
133 // when we move to Eclipse 4
134 // take a look at ISaveblePart
135 if(part instanceof BulkEditor && !(object instanceof SpecimenOrObservationBase<?>)){
136 ((BulkEditor) part).forceDirty();
137 }
138 super.changed(object);
139 }
140
141 /** {@inheritDoc} */
142 @Override
143 public Viewer getViewer() {
144 return viewer;
145 }
146
147 @Override
148 public boolean onComplete() {
149 return true;
150 }
151 }