Project

General

Profile

Download (11.6 KB) Statistics
| Branch: | Tag: | Revision:
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.Collections;
14
import java.util.HashMap;
15
import java.util.Map;
16

    
17
import org.eclipse.jface.action.Action;
18
import org.eclipse.jface.action.GroupMarker;
19
import org.eclipse.jface.action.IAction;
20
import org.eclipse.jface.action.IToolBarManager;
21
import org.eclipse.jface.action.MenuManager;
22
import org.eclipse.jface.resource.ImageDescriptor;
23
import org.eclipse.jface.viewers.DoubleClickEvent;
24
import org.eclipse.jface.viewers.IDoubleClickListener;
25
import org.eclipse.jface.viewers.ISelection;
26
import org.eclipse.jface.viewers.IStructuredSelection;
27
import org.eclipse.jface.viewers.ITreeContentProvider;
28
import org.eclipse.jface.viewers.StructuredSelection;
29
import org.eclipse.jface.viewers.TreeNode;
30
import org.eclipse.jface.viewers.TreeSelection;
31
import org.eclipse.jface.viewers.TreeViewer;
32
import org.eclipse.jface.viewers.Viewer;
33
import org.eclipse.swt.SWT;
34
import org.eclipse.swt.dnd.DND;
35
import org.eclipse.swt.dnd.Transfer;
36
import org.eclipse.swt.graphics.ImageData;
37
import org.eclipse.swt.widgets.Composite;
38
import org.eclipse.swt.widgets.Control;
39
import org.eclipse.swt.widgets.Menu;
40
import org.eclipse.swt.widgets.Tree;
41
import org.eclipse.ui.IWorkbenchActionConstants;
42
import org.eclipse.ui.IWorkbenchPart;
43
import org.eclipse.ui.PartInitException;
44

    
45
import eu.etaxonomy.cdm.model.common.CdmBase;
46
import eu.etaxonomy.cdm.model.description.DescriptionBase;
47
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
48
import eu.etaxonomy.cdm.model.description.IDescribable;
49
import eu.etaxonomy.cdm.model.description.IndividualsAssociation;
50
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
51
import eu.etaxonomy.cdm.model.taxon.Taxon;
52
import eu.etaxonomy.taxeditor.editor.EditorUtil;
53
import eu.etaxonomy.taxeditor.editor.view.checklist.ChecklistEditor;
54
import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateView;
55
import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateViewEditorInput;
56
import eu.etaxonomy.taxeditor.model.AbstractUtility;
57
import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
58
import eu.etaxonomy.taxeditor.model.FeatureNodeContainerTree;
59
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
60
import eu.etaxonomy.taxeditor.model.IPartContentHasFactualData;
61
import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
62
import eu.etaxonomy.taxeditor.model.ImageResources;
63
import eu.etaxonomy.taxeditor.model.MessagingUtils;
64
import eu.etaxonomy.taxeditor.view.AbstractCdmEditorViewPart;
65
import eu.etaxonomy.taxeditor.view.detail.DetailsViewPart;
66
import eu.etaxonomy.taxeditor.view.supplementaldata.SupplementalDataViewPart;
67

    
68
/**
69
 * <p>DescriptiveViewPart class.</p>
70
 *
71
 * @author n.hoffmann
72
 * @created Jun 9, 2010
73
 * @version 1.0
74
 */
75
public class DescriptiveViewPart extends AbstractCdmEditorViewPart implements IPartContentHasDetails, IPartContentHasSupplementalData {
76

    
77
	/** Constant <code>ID="eu.etaxonomy.taxeditor.editor.view.desc"{trunked}</code> */
78
	public static final String ID = "eu.etaxonomy.taxeditor.editor.view.descriptive";
79

    
80
	protected TreeViewer viewer;
81

    
82
	/**
83
	 * Maps {@link FeatureNodeContainerTree} to their corresponding {@link TaxonDescritpion}.<br>
84
	 * This serves as input for the {@link ITreeContentProvider} of the {@link TreeViewer}
85
	 */
86
	protected Map<DescriptionBase<?>, FeatureNodeContainerTree> featureNodeContainerCache = new HashMap<DescriptionBase<?>, FeatureNodeContainerTree>();
87

    
88
	protected ToggleDescriptionAction showAllElementsAction;
89

    
90
	protected ToggleDescriptionAction hideAllElementsAction;
91

    
92
	protected int dndOperations = DND.DROP_COPY | DND.DROP_MOVE;
93

    
94
    private DescriptiveContentProvider provider;
95

    
96
	/** {@inheritDoc} */
97
	@Override
98
	public void createViewer(Composite parent) {
99
		viewer = new TreeViewer(new Tree(parent, SWT.MULTI | SWT.H_SCROLL
100
				| SWT.V_SCROLL | SWT.FULL_SELECTION));
101
		provider = new DescriptiveContentProvider(featureNodeContainerCache);
102
        viewer.setContentProvider(provider);
103
		viewer.setLabelProvider(new DescriptiveLabelProvider());
104
		viewer.setSorter(new DescriptiveViewerSorter());
105
		viewer.setAutoExpandLevel(2);
106
		Transfer[] transfers = new Transfer[] { DescriptionElementTransfer.getInstance() };
107
		viewer.addDragSupport(dndOperations, transfers, new DescriptionElementDragListener(
108
				this));
109
		viewer.addDropSupport(dndOperations, transfers,
110
				new DescriptionElementDropAdapter(viewer));
111

    
112
		// Propagate selection from viewer
113
		getSite().setSelectionProvider(viewer);
114

    
115
		showAllElementsAction = new ToggleDescriptionAction(false);
116
		hideAllElementsAction = new ToggleDescriptionAction(true);
117

    
118
		// Add context menu to tree
119
		createMenu();
120

    
121
		createToolbar();
122

    
123
		// set initial input
124
//		if(getEditor() != null){
125
//			viewer.setInput(getEditor().getEditorInput());
126
//		}
127

    
128
		viewer.addDoubleClickListener(new IDoubleClickListener() {
129
            @Override
130
            public void doubleClick(DoubleClickEvent event) {
131
                //Open derivate editor when specimen description element is double clicked
132
                TreeSelection selection = (TreeSelection) viewer.getSelection();
133
                if(selection.getFirstElement() instanceof IndividualsAssociation){
134
                    SpecimenOrObservationBase specimen = ((IndividualsAssociation)selection.getFirstElement()).getAssociatedSpecimenOrObservation();
135
                    if(specimen!=null){
136
                        try {
137
                            EditorUtil.open(new DerivateViewEditorInput(Collections.singleton(specimen.getUuid())));
138
                        } catch (PartInitException e) {
139
                            MessagingUtils.error(DescriptiveViewPart.class, "Could not open Derivate Editor", e);
140
                        }
141
                    }
142

    
143

    
144
                    //TODO: extend command to accept parameter to open editor
145
//                    String commandId = "eu.etaxonomy.taxeditor.editor.handler.openDerivateView";
146
//                    IHandlerService handlerService = (IHandlerService) AbstractUtility.getService(IHandlerService.class);
147
//                    try {
148
//                        handlerService.executeCommand(commandId, null);
149
//                    } catch (ExecutionException e) {
150
//                        MessagingUtils.error(DerivateSearchCompositeController.class, e);
151
//                    } catch (NotDefinedException e) {
152
//                        MessagingUtils.error(DerivateSearchCompositeController.class, e);
153
//                    } catch (NotEnabledException e) {
154
//                        MessagingUtils.error(DerivateSearchCompositeController.class, e);
155
//                    } catch (NotHandledException e) {
156
//                        MessagingUtils.error(DerivateSearchCompositeController.class, e);
157
//                    }
158
                }
159

    
160
            }
161
        });
162
	}
163

    
164
	/* (non-Javadoc)
165
	 * @see eu.etaxonomy.taxeditor.model.AbstractCdmViewPart#getInitialSelection()
166
	 */
167
	/** {@inheritDoc} */
168
	@Override
169
	protected ISelection getInitialSelection() {
170
		if(getEditor() != null){
171
			return new StructuredSelection(getEditor().getEditorInput());
172
		}
173

    
174
		return super.getInitialSelection();
175
	}
176

    
177
	protected void createToolbar() {
178
		IToolBarManager toolBarManager = getViewSite().getActionBars().getToolBarManager();
179
		toolBarManager.add(showAllElementsAction);
180
		toolBarManager.add(hideAllElementsAction);
181
	}
182

    
183
	/*
184
	 * TODO add to the views menu
185
	 */
186
	protected void createMenu(){
187
		MenuManager menuManager = new MenuManager();
188
		menuManager.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
189

    
190
		getSite().registerContextMenu(menuManager, viewer);
191

    
192
		// FIXME for some reason this is not working
193
		menuManager.add(showAllElementsAction);
194
		menuManager.add(hideAllElementsAction);
195

    
196
		Control control = viewer.getControl();
197
		Menu menu = menuManager.createContextMenu(control);
198

    
199
		control.setMenu(menu);
200
	}
201

    
202
	/** {@inheritDoc} */
203
	@Override
204
    protected void selectionChanged_internal(IWorkbenchPart part, ISelection selection) {
205
		if(AbstractUtility.getActiveEditor() == null){
206
			showEmptyPage();
207
			return;
208
		}
209

    
210
		if(part == this){
211
		    return;
212
		}
213
		if(part instanceof DetailsViewPart || part instanceof SupplementalDataViewPart){
214
		    // do not show empty page as these views are also used to edit the description selected in this view
215
		    return;
216
		}
217
		// unpackage TreeNode of DerivateView
218
		else if(part instanceof DerivateView){
219
		    TreeNode treeNodeOfSelection = EditorUtil.getTreeNodeOfSelection(selection);
220
		    if(treeNodeOfSelection!=null){
221
		        selection = new StructuredSelection(treeNodeOfSelection.getValue());
222
		    }
223
		}
224
        else if(part instanceof ChecklistEditor){
225
            if(selection instanceof StructuredSelection  && ((IStructuredSelection) selection).getFirstElement() instanceof Taxon){
226
                Taxon taxon = (Taxon)((IStructuredSelection) selection).getFirstElement();
227
                selection = new StructuredSelection(taxon);
228
            }
229
        }
230
		if(selection instanceof IStructuredSelection
231
		        && ((IStructuredSelection) selection).getFirstElement() instanceof IDescribable<?>
232
		        && part instanceof IPartContentHasFactualData){
233
		    featureNodeContainerCache.clear();
234
		    showViewer(part, (IStructuredSelection) selection);
235
		    return;
236
		}
237
		else{
238
		    showEmptyPage();
239
		}
240
	}
241

    
242
	/**
243
	 *
244
	 * @author n.hoffmann
245
	 * @created May 28, 2010
246
	 * @version 1.0
247
	 */
248
	protected class ToggleDescriptionAction extends Action{
249
		private final boolean expanded;
250

    
251
		public ToggleDescriptionAction(boolean expand){
252
			super(null, IAction.AS_PUSH_BUTTON);
253
			expanded = expand;
254
			setImageAndTooltip();
255
		}
256

    
257
		private void setImageAndTooltip(){
258
			setImageDescriptor(new ImageDescriptor() {
259
				@Override
260
				public ImageData getImageData() {
261
					setText(expanded ? "Collapse All" : "Expand All");
262
					String resource = expanded ? ImageResources.COLLAPSE_ALL : ImageResources.EXPAND_ALL;
263
					return ImageResources.getImage(resource).getImageData();
264
				}
265
			});
266

    
267
			String toolTipText = expanded ? "Collapse all" : "Show all factual data";
268
			setToolTipText(toolTipText);
269
		}
270

    
271
		@Override
272
		public void run() {
273
			if(expanded){
274
				viewer.collapseAll();
275
			}else{
276
				viewer.expandAll();
277
			}
278
			setImageAndTooltip();
279
		}
280
	}
281

    
282
	/** {@inheritDoc} */
283
	@Override
284
	public boolean postOperation(CdmBase objectAffectedByOperation) {
285
		return super.postOperation(objectAffectedByOperation);
286
	}
287

    
288
	/** {@inheritDoc} */
289
	@Override
290
	public void changed(Object object) {
291
		for(FeatureNodeContainerTree containerTree : featureNodeContainerCache.values()){
292
			containerTree.buildTree();
293
		}
294
		viewer.refresh();
295

    
296
		if(object instanceof DescriptionElementBase){
297
			DescriptionElementBase descriptionElement = (DescriptionElementBase) object;
298
			DescriptionBase description = descriptionElement.getInDescription();
299
			FeatureNodeContainerTree containerTree = featureNodeContainerCache.get(description);
300

    
301
			FeatureNodeContainer featureNodeContainer = containerTree.getFeatureNodeContainerForDescriptionElement(descriptionElement);
302
			viewer.expandToLevel(featureNodeContainer, 1);
303

    
304
		}
305

    
306
		if(object != null){
307
			StructuredSelection selection = new StructuredSelection(object);
308
			viewer.setSelection(selection, true);
309
		}
310

    
311
		super.changed(object);
312
	}
313

    
314
	/** {@inheritDoc} */
315
	@Override
316
	public Viewer getViewer() {
317
		return viewer;
318
	}
319

    
320
	/**
321
	 * <p>onComplete</p>
322
	 *
323
	 * @return a boolean.
324
	 */
325
	@Override
326
    public boolean onComplete() {
327
		return false;
328
	}
329

    
330
	public void toggleShowOnlyIndividualAssociations(){
331
	    provider.toggleShowOnlyIndividualAssociations();
332
	    viewer.refresh();
333
	}
334
}
(6-6/7)