Project

General

Profile

Download (10.3 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.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.Messages;
49
import eu.etaxonomy.taxeditor.editor.view.checklist.ChecklistEditor;
50
import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateView;
51
import eu.etaxonomy.taxeditor.editor.view.media.MediaViewPart;
52
import eu.etaxonomy.taxeditor.model.AbstractUtility;
53
import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
54
import eu.etaxonomy.taxeditor.model.FeatureNodeContainerTree;
55
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
56
import eu.etaxonomy.taxeditor.model.IPartContentHasFactualData;
57
import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
58
import eu.etaxonomy.taxeditor.model.ImageResources;
59
import eu.etaxonomy.taxeditor.store.CdmStore;
60
import eu.etaxonomy.taxeditor.view.AbstractCdmEditorViewPart;
61
import eu.etaxonomy.taxeditor.view.detail.DetailsViewPart;
62
import eu.etaxonomy.taxeditor.view.supplementaldata.SupplementalDataViewPart;
63

    
64
/**
65
 * @author n.hoffmann
66
 * @created Jun 9, 2010
67
 * @version 1.0
68
 */
69
public class DescriptiveViewPart extends AbstractCdmEditorViewPart implements IPartContentHasDetails, IPartContentHasSupplementalData {
70

    
71
	/** Constant <code>ID="eu.etaxonomy.taxeditor.editor.view.desc"{trunked}</code> */
72
	public static final String ID = "eu.etaxonomy.taxeditor.editor.view.descriptive"; //$NON-NLS-1$
73

    
74
	protected TreeViewer viewer;
75

    
76
	/**
77
	 * Maps {@link FeatureNodeContainerTree} to their corresponding {@link TaxonDescritpion}.<br>
78
	 * This serves as input for the {@link ITreeContentProvider} of the {@link TreeViewer}
79
	 */
80
	protected Map<DescriptionBase<?>, FeatureNodeContainerTree> featureNodeContainerCache = new HashMap<DescriptionBase<?>, FeatureNodeContainerTree>();
81

    
82
	protected ToggleDescriptionAction showAllElementsAction;
83

    
84
	protected ToggleDescriptionAction hideAllElementsAction;
85

    
86
	protected int dndOperations = DND.DROP_COPY | DND.DROP_MOVE;
87

    
88
    private DescriptiveContentProvider provider;
89

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

    
106
		// Propagate selection from viewer
107
		getSite().setSelectionProvider(viewer);
108

    
109
		showAllElementsAction = new ToggleDescriptionAction(false);
110
		hideAllElementsAction = new ToggleDescriptionAction(true);
111

    
112
		// Add context menu to tree
113
		createMenu();
114

    
115
		createToolbar();
116

    
117
	}
118

    
119
	/** {@inheritDoc} */
120
	@Override
121
	protected ISelection getInitialSelection() {
122
		if(getEditor() != null){
123
			return new StructuredSelection(getEditor().getEditorInput());
124
		}
125

    
126
		return super.getInitialSelection();
127
	}
128

    
129
	protected void createToolbar() {
130
		IToolBarManager toolBarManager = getViewSite().getActionBars().getToolBarManager();
131
		toolBarManager.add(showAllElementsAction);
132
		toolBarManager.add(hideAllElementsAction);
133
	}
134

    
135
	/*
136
	 * TODO add to the views menu
137
	 */
138
	protected void createMenu(){
139
		MenuManager menuManager = new MenuManager();
140
		menuManager.setRemoveAllWhenShown(true);
141
		menuManager.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
142

    
143
		getSite().registerContextMenu(menuManager, viewer);
144

    
145
		// FIXME for some reason this is not working
146
		menuManager.add(showAllElementsAction);
147
		menuManager.add(hideAllElementsAction);
148

    
149
		Control control = viewer.getControl();
150
		Menu menu = menuManager.createContextMenu(control);
151

    
152
		control.setMenu(menu);
153
	}
154

    
155
	/** {@inheritDoc} */
156
	@Override
157
    protected void selectionChanged_internal(IWorkbenchPart part, ISelection selection) {
158
		if(AbstractUtility.getActiveEditor() == null){
159
		    setPartName(createPartTitle(null));
160
			showEmptyPage();
161
			return;
162
		}
163

    
164
		if(part == this){
165
		    return;
166
		}
167
        if (part instanceof DetailsViewPart || part instanceof SupplementalDataViewPart
168
                || part instanceof MediaViewPart) {
169
            // do not show empty page as these views are also used to edit the
170
            // description selected in this view
171
            return;
172
        }
173
		// unpackage TreeNode of DerivateView
174
		else if(part instanceof DerivateView){
175
		    TreeNode treeNodeOfSelection = EditorUtil.getTreeNodeOfSelection(selection);
176
		    if(treeNodeOfSelection!=null){
177
		        selection = new StructuredSelection(treeNodeOfSelection.getValue());
178
		    }
179
		}
180
        else if(part instanceof ChecklistEditor){
181
            if(selection instanceof StructuredSelection  && ((IStructuredSelection) selection).getFirstElement() instanceof Taxon){
182
                Taxon taxon = (Taxon)((IStructuredSelection) selection).getFirstElement();
183
                selection = new StructuredSelection(taxon);
184
            }
185
        }
186
		if(selection instanceof IStructuredSelection
187
		        && ((IStructuredSelection) selection).getFirstElement() instanceof IDescribable<?>
188
		        && part instanceof IPartContentHasFactualData  && !(((IStructuredSelection) selection).getFirstElement() instanceof SpecimenOrObservationBase && part instanceof BulkEditor)){
189
		    featureNodeContainerCache.clear();
190
	        setPartName(createPartTitle(((IStructuredSelection) selection).getFirstElement()));
191
		    showViewer(part, (IStructuredSelection) selection);
192
		    return;
193
		}
194
		else{
195
            setPartName(createPartTitle(null));
196
		    showEmptyPage();
197
		}
198
	}
199

    
200
	protected String getViewName(){
201
	    return Messages.DescriptiveViewPart_FACTUAL_DATA;
202
	}
203

    
204
	private String createPartTitle(Object selectedObject){
205
	    if(selectedObject!=null){
206
	        return getViewName()+": "+selectedObject; //$NON-NLS-1$
207
	    }
208
        return getViewName();
209
	}
210

    
211
	/**
212
	 *
213
	 * @author n.hoffmann
214
	 * @created May 28, 2010
215
	 * @version 1.0
216
	 */
217
	protected class ToggleDescriptionAction extends Action{
218
		private final boolean expanded;
219

    
220
		public ToggleDescriptionAction(boolean expand){
221
			super(null, IAction.AS_PUSH_BUTTON);
222
			expanded = expand;
223
			setImageAndTooltip();
224
		}
225

    
226
		private void setImageAndTooltip(){
227
			setImageDescriptor(new ImageDescriptor() {
228
				@Override
229
				public ImageData getImageData() {
230
					setText(expanded ? Messages.DescriptiveViewPart_COLLAPSE_ALL : Messages.DescriptiveViewPart_EXPAND_ALL);
231
					String resource = expanded ? ImageResources.COLLAPSE_ALL : ImageResources.EXPAND_ALL;
232
					return ImageResources.getImage(resource).getImageData();
233
				}
234
			});
235

    
236
			String toolTipText = expanded ? Messages.DescriptiveViewPart_COLLAPSE_ALL : Messages.DescriptiveViewPart_SHOW_ALL_DATA;
237
			setToolTipText(toolTipText);
238
		}
239

    
240
		@Override
241
		public void run() {
242
			if(expanded){
243
				viewer.collapseAll();
244
			}else{
245
				viewer.expandAll();
246
			}
247
			setImageAndTooltip();
248
		}
249
	}
250

    
251
	/** {@inheritDoc} */
252
	@Override
253
	public boolean postOperation(CdmBase objectAffectedByOperation) {
254
		return super.postOperation(objectAffectedByOperation);
255
	}
256

    
257
	/** {@inheritDoc} */
258
	@Override
259
	public void changed(Object object) {
260
		for(FeatureNodeContainerTree containerTree : featureNodeContainerCache.values()){
261
			containerTree.buildTree();
262
		}
263
		viewer.expandToLevel(2);
264
		viewer.refresh();
265

    
266
		if(object instanceof DescriptionElementBase){
267
			DescriptionElementBase descriptionElement = (DescriptionElementBase) object;
268
			DescriptionBase description = descriptionElement.getInDescription();
269
			FeatureNodeContainerTree containerTree = featureNodeContainerCache.get(description);
270

    
271
			FeatureNodeContainer featureNodeContainer = containerTree.getFeatureNodeContainerForDescriptionElement(descriptionElement);
272
			viewer.expandToLevel(featureNodeContainer, 1);
273

    
274
		}
275

    
276
		if(object != null){
277
			StructuredSelection selection = new StructuredSelection(object);
278
			viewer.setSelection(selection, true);
279
		}
280
		if(part instanceof BulkEditor && !(object instanceof SpecimenOrObservationBase<?>)){
281
		    ((BulkEditor) part).forceDirty();
282
		    if(CdmStore.getCurrentSessionManager().isRemoting()) {
283
		        IStructuredSelection selection = (IStructuredSelection) ((BulkEditor) part).getSelectionProvider().getSelection();
284
		        ((BulkEditor) part).changed(selection.getFirstElement());
285
		    }
286
		}
287

    
288

    
289
		super.changed(object);
290
	}
291

    
292
	/** {@inheritDoc} */
293
	@Override
294
	public Viewer getViewer() {
295
		return viewer;
296
	}
297

    
298
	@Override
299
    public boolean onComplete() {
300
		return false;
301
	}
302

    
303
	public void toggleShowOnlyIndividualAssociations(){
304
	    provider.toggleShowOnlyIndividualAssociations();
305
	    viewer.refresh();
306
	}
307
}
(6-6/7)