- enabled DescriptiveView (Factual Data) to show descriptions of SpecimenOrObservati...
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / descriptive / DescriptiveViewPart.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.descriptive;
12
13 import java.util.HashMap;
14 import java.util.Map;
15
16 import org.eclipse.jface.action.Action;
17 import org.eclipse.jface.action.GroupMarker;
18 import org.eclipse.jface.action.IAction;
19 import org.eclipse.jface.action.IToolBarManager;
20 import org.eclipse.jface.action.MenuManager;
21 import org.eclipse.jface.resource.ImageDescriptor;
22 import org.eclipse.jface.viewers.ISelection;
23 import org.eclipse.jface.viewers.IStructuredSelection;
24 import org.eclipse.jface.viewers.StructuredSelection;
25 import org.eclipse.jface.viewers.TreeViewer;
26 import org.eclipse.jface.viewers.Viewer;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.dnd.DND;
29 import org.eclipse.swt.dnd.Transfer;
30 import org.eclipse.swt.graphics.ImageData;
31 import org.eclipse.swt.widgets.Composite;
32 import org.eclipse.swt.widgets.Control;
33 import org.eclipse.swt.widgets.Menu;
34 import org.eclipse.swt.widgets.Tree;
35 import org.eclipse.ui.IWorkbenchActionConstants;
36 import org.eclipse.ui.IWorkbenchPart;
37
38 import eu.etaxonomy.cdm.model.common.CdmBase;
39 import eu.etaxonomy.cdm.model.description.DescriptionBase;
40 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
41 import eu.etaxonomy.cdm.model.description.IDescribable;
42 import eu.etaxonomy.taxeditor.model.AbstractUtility;
43 import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
44 import eu.etaxonomy.taxeditor.model.FeatureNodeContainerTree;
45 import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
46 import eu.etaxonomy.taxeditor.model.IPartContentHasFactualData;
47 import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
48 import eu.etaxonomy.taxeditor.model.ImageResources;
49 import eu.etaxonomy.taxeditor.view.AbstractCdmEditorViewPart;
50 import eu.etaxonomy.taxeditor.view.detail.DetailsViewPart;
51 import eu.etaxonomy.taxeditor.view.supplementaldata.SupplementalDataViewPart;
52
53 /**
54 * <p>DescriptiveViewPart class.</p>
55 *
56 * @author n.hoffmann
57 * @created Jun 9, 2010
58 * @version 1.0
59 */
60 public class DescriptiveViewPart extends AbstractCdmEditorViewPart implements IPartContentHasDetails, IPartContentHasSupplementalData {
61
62 /** Constant <code>ID="eu.etaxonomy.taxeditor.editor.view.desc"{trunked}</code> */
63 public static final String ID = "eu.etaxonomy.taxeditor.editor.view.descriptive";
64
65 protected TreeViewer viewer;
66
67 protected Map<DescriptionBase<?>, FeatureNodeContainerTree> featureNodeContainerCache = new HashMap<DescriptionBase<?>, FeatureNodeContainerTree>();
68
69 protected ToggleDescriptionAction showAllElementsAction;
70
71 protected ToggleDescriptionAction hideAllElementsAction;
72
73 protected int dndOperations = DND.DROP_COPY | DND.DROP_MOVE;
74
75 /** {@inheritDoc} */
76 @Override
77 public void createViewer(Composite parent) {
78 viewer = new TreeViewer(new Tree(parent, SWT.MULTI | SWT.H_SCROLL
79 | SWT.V_SCROLL | SWT.FULL_SELECTION));
80 viewer.setContentProvider(new DescriptiveContentProvider(featureNodeContainerCache));
81 viewer.setLabelProvider(new DescriptiveLabelProvider());
82 viewer.setSorter(new DescriptiveViewerSorter());
83 viewer.setAutoExpandLevel(2);
84 Transfer[] transfers = new Transfer[] { DescriptionElementTransfer.getInstance() };
85 viewer.addDragSupport(dndOperations, transfers, new DescriptionElementDragListener(
86 this));
87 viewer.addDropSupport(dndOperations, transfers,
88 new DescriptionElementDropAdapter(viewer));
89
90 // Propagate selection from viewer
91 getSite().setSelectionProvider(viewer);
92
93 showAllElementsAction = new ToggleDescriptionAction(false);
94 hideAllElementsAction = new ToggleDescriptionAction(true);
95
96 // Add context menu to tree
97 createMenu();
98
99 createToolbar();
100
101 // set initial input
102 // if(getEditor() != null){
103 // viewer.setInput(getEditor().getEditorInput());
104 // }
105 }
106
107 /* (non-Javadoc)
108 * @see eu.etaxonomy.taxeditor.model.AbstractCdmViewPart#getInitialSelection()
109 */
110 /** {@inheritDoc} */
111 @Override
112 protected ISelection getInitialSelection() {
113 if(getEditor() != null){
114 return new StructuredSelection(getEditor().getEditorInput());
115 }
116
117 return super.getInitialSelection();
118 }
119
120 protected void createToolbar() {
121 IToolBarManager toolBarManager = getViewSite().getActionBars().getToolBarManager();
122 toolBarManager.add(showAllElementsAction);
123 toolBarManager.add(hideAllElementsAction);
124 }
125
126 /*
127 * TODO add to the views menu
128 */
129 protected void createMenu(){
130 MenuManager menuManager = new MenuManager();
131 menuManager.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
132
133 getSite().registerContextMenu(menuManager, viewer);
134
135 // FIXME for some reason this is not working
136 menuManager.add(showAllElementsAction);
137 menuManager.add(hideAllElementsAction);
138
139 Control control = viewer.getControl();
140 Menu menu = menuManager.createContextMenu(control);
141
142 control.setMenu(menu);
143 }
144
145 /** {@inheritDoc} */
146 @Override
147 public void selectionChanged(IWorkbenchPart part, ISelection selection) {
148 if(AbstractUtility.getActiveEditor() == null){
149 showEmptyPage();
150 return;
151 }
152
153 if(part == this){
154 return;
155 }
156 if(part instanceof DetailsViewPart || part instanceof SupplementalDataViewPart){
157 // do not show empty page as these views are also used to edit the description selected in this view
158 return;
159 }
160
161 if(selection instanceof IStructuredSelection
162 && ((IStructuredSelection) selection).getFirstElement() instanceof IDescribable<?>
163 && part instanceof IPartContentHasFactualData){
164 featureNodeContainerCache.clear();
165 showViewer(part, (IStructuredSelection) selection);
166 return;
167 }
168 else{
169 showEmptyPage();
170 }
171 }
172
173 /**
174 *
175 * @author n.hoffmann
176 * @created May 28, 2010
177 * @version 1.0
178 */
179 protected class ToggleDescriptionAction extends Action{
180 private final boolean expanded;
181
182 public ToggleDescriptionAction(boolean expand){
183 super(null, IAction.AS_PUSH_BUTTON);
184 expanded = expand;
185 setImageAndTooltip();
186 }
187
188 private void setImageAndTooltip(){
189 setImageDescriptor(new ImageDescriptor() {
190 @Override
191 public ImageData getImageData() {
192 setText(expanded ? "Collapse All" : "Expand All");
193 String resource = expanded ? ImageResources.COLLAPSE_ALL : ImageResources.EXPAND_ALL;
194 return ImageResources.getImage(resource).getImageData();
195 }
196 });
197
198 String toolTipText = expanded ? "Collapse all" : "Show all descriptive data";
199 setToolTipText(toolTipText);
200 }
201
202 @Override
203 public void run() {
204 if(expanded){
205 viewer.collapseAll();
206 }else{
207 viewer.expandAll();
208 }
209 setImageAndTooltip();
210 }
211 }
212
213 /** {@inheritDoc} */
214 @Override
215 public boolean postOperation(CdmBase objectAffectedByOperation) {
216 return super.postOperation(objectAffectedByOperation);
217 }
218
219 /** {@inheritDoc} */
220 @Override
221 public void changed(Object object) {
222 for(FeatureNodeContainerTree containerTree : featureNodeContainerCache.values()){
223 containerTree.buildTree();
224 }
225 viewer.refresh();
226
227 if(object instanceof DescriptionElementBase){
228 DescriptionElementBase descriptionElement = (DescriptionElementBase) object;
229 DescriptionBase description = descriptionElement.getInDescription();
230 FeatureNodeContainerTree containerTree = featureNodeContainerCache.get(description);
231
232 FeatureNodeContainer featureNodeContainer = containerTree.getFeatureNodeContainerForDescriptionElement(descriptionElement);
233 viewer.expandToLevel(featureNodeContainer, 1);
234
235 }
236
237 if(object != null){
238 StructuredSelection selection = new StructuredSelection(object);
239 viewer.setSelection(selection, true);
240 }
241
242 super.changed(object);
243 }
244
245 /** {@inheritDoc} */
246 @Override
247 public Viewer getViewer() {
248 return viewer;
249 }
250
251 /**
252 * <p>onComplete</p>
253 *
254 * @return a boolean.
255 */
256 @Override
257 public boolean onComplete() {
258 return false;
259 }
260 }