Project

General

Profile

Download (9.14 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.taxon.Taxon;
45
import eu.etaxonomy.taxeditor.editor.EditorUtil;
46
import eu.etaxonomy.taxeditor.editor.view.checklist.ChecklistEditor;
47
import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateView;
48
import eu.etaxonomy.taxeditor.model.AbstractUtility;
49
import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
50
import eu.etaxonomy.taxeditor.model.FeatureNodeContainerTree;
51
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
52
import eu.etaxonomy.taxeditor.model.IPartContentHasFactualData;
53
import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
54
import eu.etaxonomy.taxeditor.model.ImageResources;
55
import eu.etaxonomy.taxeditor.view.AbstractCdmEditorViewPart;
56
import eu.etaxonomy.taxeditor.view.detail.DetailsViewPart;
57
import eu.etaxonomy.taxeditor.view.supplementaldata.SupplementalDataViewPart;
58

    
59
/**
60
 * <p>DescriptiveViewPart class.</p>
61
 *
62
 * @author n.hoffmann
63
 * @created Jun 9, 2010
64
 * @version 1.0
65
 */
66
public class DescriptiveViewPart extends AbstractCdmEditorViewPart implements IPartContentHasDetails, IPartContentHasSupplementalData {
67

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

    
71
	protected TreeViewer viewer;
72

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

    
79
	protected ToggleDescriptionAction showAllElementsAction;
80

    
81
	protected ToggleDescriptionAction hideAllElementsAction;
82

    
83
	protected int dndOperations = DND.DROP_COPY | DND.DROP_MOVE;
84

    
85
    private DescriptiveContentProvider provider;
86

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

    
103
		// Propagate selection from viewer
104
		getSite().setSelectionProvider(viewer);
105

    
106
		showAllElementsAction = new ToggleDescriptionAction(false);
107
		hideAllElementsAction = new ToggleDescriptionAction(true);
108

    
109
		// Add context menu to tree
110
		createMenu();
111

    
112
		createToolbar();
113

    
114
		// set initial input
115
//		if(getEditor() != null){
116
//			viewer.setInput(getEditor().getEditorInput());
117
//		}
118
	}
119

    
120
	/* (non-Javadoc)
121
	 * @see eu.etaxonomy.taxeditor.model.AbstractCdmViewPart#getInitialSelection()
122
	 */
123
	/** {@inheritDoc} */
124
	@Override
125
	protected ISelection getInitialSelection() {
126
		if(getEditor() != null){
127
			return new StructuredSelection(getEditor().getEditorInput());
128
		}
129

    
130
		return super.getInitialSelection();
131
	}
132

    
133
	protected void createToolbar() {
134
		IToolBarManager toolBarManager = getViewSite().getActionBars().getToolBarManager();
135
		toolBarManager.add(showAllElementsAction);
136
		toolBarManager.add(hideAllElementsAction);
137
	}
138

    
139
	/*
140
	 * TODO add to the views menu
141
	 */
142
	protected void createMenu(){
143
		MenuManager menuManager = new MenuManager();
144
		menuManager.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
145

    
146
		getSite().registerContextMenu(menuManager, viewer);
147

    
148
		// FIXME for some reason this is not working
149
		menuManager.add(showAllElementsAction);
150
		menuManager.add(hideAllElementsAction);
151

    
152
		Control control = viewer.getControl();
153
		Menu menu = menuManager.createContextMenu(control);
154

    
155
		control.setMenu(menu);
156
	}
157

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

    
166
		if(part == this){
167
		    return;
168
		}
169
		if(part instanceof DetailsViewPart || part instanceof SupplementalDataViewPart){
170
		    // do not show empty page as these views are also used to edit the 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){
189
		    featureNodeContainerCache.clear();
190
		    showViewer(part, (IStructuredSelection) selection);
191
		    return;
192
		}
193
		else{
194
		    showEmptyPage();
195
		}
196
	}
197

    
198
	/**
199
	 *
200
	 * @author n.hoffmann
201
	 * @created May 28, 2010
202
	 * @version 1.0
203
	 */
204
	protected class ToggleDescriptionAction extends Action{
205
		private final boolean expanded;
206

    
207
		public ToggleDescriptionAction(boolean expand){
208
			super(null, IAction.AS_PUSH_BUTTON);
209
			expanded = expand;
210
			setImageAndTooltip();
211
		}
212

    
213
		private void setImageAndTooltip(){
214
			setImageDescriptor(new ImageDescriptor() {
215
				@Override
216
				public ImageData getImageData() {
217
					setText(expanded ? "Collapse All" : "Expand All");
218
					String resource = expanded ? ImageResources.COLLAPSE_ALL : ImageResources.EXPAND_ALL;
219
					return ImageResources.getImage(resource).getImageData();
220
				}
221
			});
222

    
223
			String toolTipText = expanded ? "Collapse all" : "Show all factual data";
224
			setToolTipText(toolTipText);
225
		}
226

    
227
		@Override
228
		public void run() {
229
			if(expanded){
230
				viewer.collapseAll();
231
			}else{
232
				viewer.expandAll();
233
			}
234
			setImageAndTooltip();
235
		}
236
	}
237

    
238
	/** {@inheritDoc} */
239
	@Override
240
	public boolean postOperation(CdmBase objectAffectedByOperation) {
241
		return super.postOperation(objectAffectedByOperation);
242
	}
243

    
244
	/** {@inheritDoc} */
245
	@Override
246
	public void changed(Object object) {
247
		for(FeatureNodeContainerTree containerTree : featureNodeContainerCache.values()){
248
			containerTree.buildTree();
249
		}
250
		viewer.refresh();
251

    
252
		if(object instanceof DescriptionElementBase){
253
			DescriptionElementBase descriptionElement = (DescriptionElementBase) object;
254
			DescriptionBase description = descriptionElement.getInDescription();
255
			FeatureNodeContainerTree containerTree = featureNodeContainerCache.get(description);
256

    
257
			FeatureNodeContainer featureNodeContainer = containerTree.getFeatureNodeContainerForDescriptionElement(descriptionElement);
258
			viewer.expandToLevel(featureNodeContainer, 1);
259

    
260
		}
261

    
262
		if(object != null){
263
			StructuredSelection selection = new StructuredSelection(object);
264
			viewer.setSelection(selection, true);
265
		}
266

    
267
		super.changed(object);
268
	}
269

    
270
	/** {@inheritDoc} */
271
	@Override
272
	public Viewer getViewer() {
273
		return viewer;
274
	}
275

    
276
	/**
277
	 * <p>onComplete</p>
278
	 *
279
	 * @return a boolean.
280
	 */
281
	@Override
282
    public boolean onComplete() {
283
		return false;
284
	}
285

    
286
	public void toggleShowOnlyIndividualAssociations(){
287
	    provider.toggleShowOnlyIndividualAssociations();
288
	    viewer.refresh();
289
	}
290
}
(6-6/7)