Project

General

Profile

Download (7.46 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.IMenuManager;
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.StructuredSelection;
24
import org.eclipse.jface.viewers.TreeViewer;
25
import org.eclipse.jface.viewers.Viewer;
26
import org.eclipse.swt.SWT;
27
import org.eclipse.swt.graphics.ImageData;
28
import org.eclipse.swt.widgets.Composite;
29
import org.eclipse.swt.widgets.Control;
30
import org.eclipse.swt.widgets.Menu;
31
import org.eclipse.swt.widgets.Tree;
32
import org.eclipse.ui.IEditorInput;
33
import org.eclipse.ui.IEditorPart;
34
import org.eclipse.ui.IWorkbenchActionConstants;
35
import org.eclipse.ui.IWorkbenchPart;
36

    
37
import eu.etaxonomy.cdm.model.common.CdmBase;
38
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
39
import eu.etaxonomy.cdm.model.description.TaxonDescription;
40
import eu.etaxonomy.taxeditor.bulkeditor.BulkEditor;
41
import eu.etaxonomy.taxeditor.editor.EditorUtil;
42
import eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor;
43
import eu.etaxonomy.taxeditor.editor.view.AbstractCdmEditorViewPart;
44
import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
45
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
46
import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
47
import eu.etaxonomy.taxeditor.model.ImageResources;
48

    
49
/**
50
 * <p>DescriptiveViewPart class.</p>
51
 *
52
 * @author n.hoffmann
53
 * @created Jun 9, 2010
54
 * @version 1.0
55
 */
56
public class DescriptiveViewPart extends AbstractCdmEditorViewPart implements IPartContentHasDetails, IPartContentHasSupplementalData {
57

    
58
	/** Constant <code>ID="eu.etaxonomy.taxeditor.editor.view.desc"{trunked}</code> */
59
	public static final String ID = "eu.etaxonomy.taxeditor.editor.view.description";
60
	
61
	private TreeViewer viewer;
62
	
63
	private Map<TaxonDescription, FeatureNodeContainer> featureNodeContainerCache = new HashMap<TaxonDescription, FeatureNodeContainer>();
64

    
65
	private ToggleDescriptionAction showAllElementsAction;
66

    
67
	private ToggleDescriptionAction hideAllElementsAction;
68
		
69
	/** {@inheritDoc} */
70
	@Override
71
	public void createViewer(Composite parent) {
72
		
73
		viewer = new TreeViewer(new Tree(parent, SWT.NULL));
74
				
75
		viewer.setContentProvider(new DescriptiveContentProvider(featureNodeContainerCache));		
76
		viewer.setLabelProvider(new DescriptiveLabelProvider());
77
		
78
		viewer.setAutoExpandLevel(2);
79
						
80
		// Propagate selection from viewer
81
		getSite().setSelectionProvider(viewer);
82
		
83
		// Add context menu to tree
84
		MenuManager menuMgr = new MenuManager();
85
		menuMgr.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
86
		getSite().registerContextMenu(menuMgr, viewer);
87
		
88
		Control control = viewer.getControl();
89
		Menu menu = menuMgr.createContextMenu(control);
90
		control.setMenu(menu);			
91
		
92
		showAllElementsAction = new ToggleDescriptionAction(false);		
93
		hideAllElementsAction = new ToggleDescriptionAction(true);
94
		
95
		createToolbar();
96
		
97
		// set initial input
98
//		if(getEditor() != null){
99
//			viewer.setInput(getEditor().getEditorInput());
100
//		}
101
	}
102
	
103
	/* (non-Javadoc)
104
	 * @see eu.etaxonomy.taxeditor.model.AbstractCdmViewPart#getInitialSelection()
105
	 */
106
	/** {@inheritDoc} */
107
	@Override
108
	protected ISelection getInitialSelection() {
109
		if(getEditor() != null){
110
			return new StructuredSelection(getEditor().getEditorInput());
111
		}	
112
		
113
		return super.getInitialSelection();
114
	}
115

    
116
	private void createToolbar() {
117
		IToolBarManager toolBarManager = getViewSite().getActionBars().getToolBarManager();
118
		toolBarManager.add(showAllElementsAction);
119
		toolBarManager.add(hideAllElementsAction);
120
	}
121
	
122
	/*
123
	 * TODO add to the views menu
124
	 */
125
	private void createMenu(){
126
		IMenuManager menuManager = getViewSite().getActionBars().getMenuManager();
127
		menuManager.add(showAllElementsAction);
128
		menuManager.add(hideAllElementsAction);
129
	}
130

    
131
	/** {@inheritDoc} */
132
	public void selectionChanged(IWorkbenchPart part, ISelection selection) {
133
		if(EditorUtil.getActiveEditor() == null){
134
			showEmptyPage();
135
			return;
136
		}
137
		
138
		if(part instanceof BulkEditor){
139
			showEmptyPage();
140
			return;
141
		}
142
		
143
		if(part instanceof MultiPageTaxonEditor){
144
			if(! part.equals(this.part)){
145
				IEditorInput input = ((IEditorPart) part).getEditorInput();
146
				featureNodeContainerCache.clear();
147
				showViewer(part, new StructuredSelection(input));
148
			}
149
			showViewer();
150
		}
151
	}
152
	
153
	/**
154
	 * 
155
	 * @author n.hoffmann
156
	 * @created May 28, 2010
157
	 * @version 1.0
158
	 */
159
	private class ToggleDescriptionAction extends Action{
160
		private boolean expanded;
161
		
162
		public ToggleDescriptionAction(boolean expand){
163
			super(null, Action.AS_PUSH_BUTTON);
164
			expanded = expand;
165
			setImageAndTooltip();
166
		}
167
		
168
		private void setImageAndTooltip(){
169
			setImageDescriptor(new ImageDescriptor() {
170
				@Override
171
				public ImageData getImageData() {
172
					String resource = expanded ? ImageResources.COLLAPSE_ALL : ImageResources.EXPAND_ALL;
173
					return ImageResources.getImage(resource).getImageData();
174
				}
175
			});
176
			
177
			String toolTipText = expanded ? "Collapse all" : "Show all descriptive data";
178
			setToolTipText(toolTipText);
179
		}
180
		
181
		@Override
182
		public void run() {
183
			if(expanded){
184
				viewer.collapseAll();
185
			}else{
186
				viewer.expandAll();
187
			}
188
			setImageAndTooltip();
189
		}
190
	}
191

    
192
	/** {@inheritDoc} */
193
	@Override
194
	public boolean postOperation(CdmBase objectAffectedByOperation) {
195
		boolean result = super.postOperation(objectAffectedByOperation);
196
		
197
//		if(objectAffectedByOperation != null){
198
//			
199
//			if(objectAffectedByOperation instanceof DescriptionElementBase){
200
//				DescriptionElementBase descriptionElement = (DescriptionElementBase) objectAffectedByOperation;
201
//				FeatureNodeContainer featureNodeContainerBranch = featureNodeContainerCache.get(descriptionElement.getInDescription());
202
//				FeatureNodeContainer featureNodeContainer = featureNodeContainerBranch.processDescriptionElement(descriptionElement);
203
////				viewer.refresh(featureNodeContainer, true);
204
//				viewer.expandToLevel(featureNodeContainer, 1);
205
//			}
206
//			
207
//			StructuredSelection selection = new StructuredSelection(objectAffectedByOperation);
208
//			viewer.setSelection(selection, true);
209
//		}else{
210
////			viewer.refresh();
211
//		}
212

    
213
		return result;
214
	}
215

    
216
	/** {@inheritDoc} */
217
	@Override
218
	public void changed(Object object) {
219
		if(object instanceof DescriptionElementBase){
220
			DescriptionElementBase descriptionElement = (DescriptionElementBase) object;
221
			FeatureNodeContainer featureNodeContainerBranch = featureNodeContainerCache.get(descriptionElement.getInDescription());
222
			FeatureNodeContainer featureNodeContainer = featureNodeContainerBranch.processDescriptionElement(descriptionElement);
223
			viewer.refresh(featureNodeContainer, true);
224
			viewer.expandToLevel(featureNodeContainer, 1);
225
			
226
		}else{
227
			viewer.refresh(true);
228
		}
229
		
230
		if(object != null){
231
			StructuredSelection selection = new StructuredSelection(object);
232
			viewer.setSelection(selection, true);
233
		}
234

    
235
		super.changed(object);
236
	}
237

    
238
	/** {@inheritDoc} */
239
	@Override
240
	public Viewer getViewer() {
241
		return viewer;
242
	}
243

    
244
	/**
245
	 * <p>onComplete</p>
246
	 *
247
	 * @return a boolean.
248
	 */
249
	public boolean onComplete() {
250
		return false;
251
	}
252
}
(3-3/3)