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