Project

General

Profile

Download (11.9 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.bulkeditor.BulkEditor;
53
import eu.etaxonomy.taxeditor.editor.EditorUtil;
54
import eu.etaxonomy.taxeditor.editor.view.checklist.ChecklistEditor;
55
import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateView;
56
import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateViewEditorInput;
57
import eu.etaxonomy.taxeditor.model.AbstractUtility;
58
import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
59
import eu.etaxonomy.taxeditor.model.FeatureNodeContainerTree;
60
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
61
import eu.etaxonomy.taxeditor.model.IPartContentHasFactualData;
62
import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
63
import eu.etaxonomy.taxeditor.model.ImageResources;
64
import eu.etaxonomy.taxeditor.model.MessagingUtils;
65
import eu.etaxonomy.taxeditor.view.AbstractCdmEditorViewPart;
66
import eu.etaxonomy.taxeditor.view.detail.DetailsViewPart;
67
import eu.etaxonomy.taxeditor.view.supplementaldata.SupplementalDataViewPart;
68

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

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

    
81
	protected TreeViewer viewer;
82

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

    
89
	protected ToggleDescriptionAction showAllElementsAction;
90

    
91
	protected ToggleDescriptionAction hideAllElementsAction;
92

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

    
95
    private DescriptiveContentProvider provider;
96

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

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

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

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

    
122
		createToolbar();
123

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

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

    
144

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

    
161
            }
162
        });
163
	}
164

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

    
175
		return super.getInitialSelection();
176
	}
177

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

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

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

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

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

    
200
		control.setMenu(menu);
201
	}
202

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

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

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

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

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

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

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

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

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

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

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

    
305
		}
306

    
307
		if(object != null){
308
			StructuredSelection selection = new StructuredSelection(object);
309
			viewer.setSelection(selection, true);
310
		}
311
		if(part instanceof BulkEditor && !(object instanceof SpecimenOrObservationBase<?>)){
312
		    ((BulkEditor) part).forceDirty();
313
		}
314

    
315
		super.changed(object);
316
	}
317

    
318
	/** {@inheritDoc} */
319
	@Override
320
	public Viewer getViewer() {
321
		return viewer;
322
	}
323

    
324
	/**
325
	 * <p>onComplete</p>
326
	 *
327
	 * @return a boolean.
328
	 */
329
	@Override
330
    public boolean onComplete() {
331
		return false;
332
	}
333

    
334
	public void toggleShowOnlyIndividualAssociations(){
335
	    provider.toggleShowOnlyIndividualAssociations();
336
	    viewer.refresh();
337
	}
338
}
(6-6/7)