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