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.IToolBarManager;
|
19
|
import org.eclipse.jface.action.MenuManager;
|
20
|
import org.eclipse.jface.resource.ImageDescriptor;
|
21
|
import org.eclipse.jface.viewers.ISelection;
|
22
|
import org.eclipse.jface.viewers.StructuredSelection;
|
23
|
import org.eclipse.jface.viewers.TreeViewer;
|
24
|
import org.eclipse.jface.viewers.Viewer;
|
25
|
import org.eclipse.swt.SWT;
|
26
|
import org.eclipse.swt.dnd.DND;
|
27
|
import org.eclipse.swt.dnd.Transfer;
|
28
|
import org.eclipse.swt.graphics.ImageData;
|
29
|
import org.eclipse.swt.widgets.Composite;
|
30
|
import org.eclipse.swt.widgets.Control;
|
31
|
import org.eclipse.swt.widgets.Menu;
|
32
|
import org.eclipse.swt.widgets.Tree;
|
33
|
import org.eclipse.ui.IEditorInput;
|
34
|
import org.eclipse.ui.IEditorPart;
|
35
|
import org.eclipse.ui.IWorkbenchActionConstants;
|
36
|
import org.eclipse.ui.IWorkbenchPart;
|
37
|
|
38
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
39
|
import eu.etaxonomy.cdm.model.description.DescriptionBase;
|
40
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
41
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
42
|
import eu.etaxonomy.taxeditor.bulkeditor.BulkEditor;
|
43
|
import eu.etaxonomy.taxeditor.editor.EditorUtil;
|
44
|
import eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor;
|
45
|
import eu.etaxonomy.taxeditor.editor.key.AbstractKeyEditor;
|
46
|
import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
|
47
|
import eu.etaxonomy.taxeditor.model.FeatureNodeContainerTree;
|
48
|
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
|
49
|
import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
|
50
|
import eu.etaxonomy.taxeditor.model.ImageResources;
|
51
|
import eu.etaxonomy.taxeditor.view.AbstractCdmEditorViewPart;
|
52
|
|
53
|
/**
|
54
|
* <p>DescriptiveViewPart class.</p>
|
55
|
*
|
56
|
* @author n.hoffmann
|
57
|
* @created Jun 9, 2010
|
58
|
* @version 1.0
|
59
|
*/
|
60
|
public class DescriptiveViewPart extends AbstractCdmEditorViewPart implements IPartContentHasDetails, IPartContentHasSupplementalData {
|
61
|
|
62
|
/** Constant <code>ID="eu.etaxonomy.taxeditor.editor.view.desc"{trunked}</code> */
|
63
|
public static final String ID = "eu.etaxonomy.taxeditor.editor.view.descriptive";
|
64
|
|
65
|
private TreeViewer viewer;
|
66
|
|
67
|
private Map<TaxonDescription, FeatureNodeContainerTree> featureNodeContainerCache = new HashMap<TaxonDescription, FeatureNodeContainerTree>();
|
68
|
|
69
|
private ToggleDescriptionAction showAllElementsAction;
|
70
|
|
71
|
private ToggleDescriptionAction hideAllElementsAction;
|
72
|
|
73
|
private int dndOperations = DND.DROP_COPY | DND.DROP_MOVE;
|
74
|
|
75
|
/** {@inheritDoc} */
|
76
|
@Override
|
77
|
public void createViewer(Composite parent) {
|
78
|
|
79
|
viewer = new TreeViewer(new Tree(parent, SWT.MULTI | SWT.H_SCROLL
|
80
|
| SWT.V_SCROLL | SWT.FULL_SELECTION));
|
81
|
|
82
|
viewer.setContentProvider(new DescriptiveContentProvider(featureNodeContainerCache));
|
83
|
viewer.setLabelProvider(new DescriptiveLabelProvider());
|
84
|
|
85
|
viewer.setSorter(new DescriptiveViewerSorter());
|
86
|
|
87
|
viewer.setAutoExpandLevel(2);
|
88
|
|
89
|
|
90
|
Transfer[] transfers = new Transfer[] { DescriptionElementTransfer.getInstance() };
|
91
|
viewer.addDragSupport(dndOperations, transfers, new DescriptionElementDragListener(
|
92
|
this));
|
93
|
viewer.addDropSupport(dndOperations, transfers,
|
94
|
new DescriptionElementDropAdapter(viewer));
|
95
|
|
96
|
// Propagate selection from viewer
|
97
|
getSite().setSelectionProvider(viewer);
|
98
|
|
99
|
showAllElementsAction = new ToggleDescriptionAction(false);
|
100
|
hideAllElementsAction = new ToggleDescriptionAction(true);
|
101
|
|
102
|
// Add context menu to tree
|
103
|
createMenu();
|
104
|
|
105
|
createToolbar();
|
106
|
|
107
|
// set initial input
|
108
|
// if(getEditor() != null){
|
109
|
// viewer.setInput(getEditor().getEditorInput());
|
110
|
// }
|
111
|
}
|
112
|
|
113
|
/* (non-Javadoc)
|
114
|
* @see eu.etaxonomy.taxeditor.model.AbstractCdmViewPart#getInitialSelection()
|
115
|
*/
|
116
|
/** {@inheritDoc} */
|
117
|
@Override
|
118
|
protected ISelection getInitialSelection() {
|
119
|
if(getEditor() != null){
|
120
|
return new StructuredSelection(getEditor().getEditorInput());
|
121
|
}
|
122
|
|
123
|
return super.getInitialSelection();
|
124
|
}
|
125
|
|
126
|
private void createToolbar() {
|
127
|
IToolBarManager toolBarManager = getViewSite().getActionBars().getToolBarManager();
|
128
|
toolBarManager.add(showAllElementsAction);
|
129
|
toolBarManager.add(hideAllElementsAction);
|
130
|
}
|
131
|
|
132
|
/*
|
133
|
* TODO add to the views menu
|
134
|
*/
|
135
|
private void createMenu(){
|
136
|
MenuManager menuManager = new MenuManager();
|
137
|
menuManager.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
|
138
|
|
139
|
getSite().registerContextMenu(menuManager, viewer);
|
140
|
|
141
|
// FIXME for some reason this is not working
|
142
|
menuManager.add(showAllElementsAction);
|
143
|
menuManager.add(hideAllElementsAction);
|
144
|
|
145
|
Control control = viewer.getControl();
|
146
|
Menu menu = menuManager.createContextMenu(control);
|
147
|
|
148
|
control.setMenu(menu);
|
149
|
}
|
150
|
|
151
|
/** {@inheritDoc} */
|
152
|
public void selectionChanged(IWorkbenchPart part, ISelection selection) {
|
153
|
if(EditorUtil.getActiveEditor() == null){
|
154
|
showEmptyPage();
|
155
|
return;
|
156
|
}
|
157
|
|
158
|
if(part instanceof BulkEditor){
|
159
|
showEmptyPage();
|
160
|
return;
|
161
|
}
|
162
|
|
163
|
if(part instanceof AbstractKeyEditor){
|
164
|
showEmptyPage();
|
165
|
return;
|
166
|
}
|
167
|
|
168
|
if(part instanceof MultiPageTaxonEditor){
|
169
|
if(! part.equals(this.part)){
|
170
|
IEditorInput input = ((IEditorPart) part).getEditorInput();
|
171
|
featureNodeContainerCache.clear();
|
172
|
showViewer(part, new StructuredSelection(input));
|
173
|
}
|
174
|
showViewer();
|
175
|
}
|
176
|
}
|
177
|
|
178
|
/**
|
179
|
*
|
180
|
* @author n.hoffmann
|
181
|
* @created May 28, 2010
|
182
|
* @version 1.0
|
183
|
*/
|
184
|
private class ToggleDescriptionAction extends Action{
|
185
|
private boolean expanded;
|
186
|
|
187
|
public ToggleDescriptionAction(boolean expand){
|
188
|
super(null, Action.AS_PUSH_BUTTON);
|
189
|
expanded = expand;
|
190
|
setImageAndTooltip();
|
191
|
}
|
192
|
|
193
|
private void setImageAndTooltip(){
|
194
|
setImageDescriptor(new ImageDescriptor() {
|
195
|
@Override
|
196
|
public ImageData getImageData() {
|
197
|
setText(expanded ? "Collapse All" : "Expand All");
|
198
|
String resource = expanded ? ImageResources.COLLAPSE_ALL : ImageResources.EXPAND_ALL;
|
199
|
return ImageResources.getImage(resource).getImageData();
|
200
|
}
|
201
|
});
|
202
|
|
203
|
String toolTipText = expanded ? "Collapse all" : "Show all descriptive data";
|
204
|
setToolTipText(toolTipText);
|
205
|
}
|
206
|
|
207
|
@Override
|
208
|
public void run() {
|
209
|
if(expanded){
|
210
|
viewer.collapseAll();
|
211
|
}else{
|
212
|
viewer.expandAll();
|
213
|
}
|
214
|
setImageAndTooltip();
|
215
|
}
|
216
|
}
|
217
|
|
218
|
/** {@inheritDoc} */
|
219
|
@Override
|
220
|
public boolean postOperation(CdmBase objectAffectedByOperation) {
|
221
|
return super.postOperation(objectAffectedByOperation);
|
222
|
}
|
223
|
|
224
|
/** {@inheritDoc} */
|
225
|
@Override
|
226
|
public void changed(Object object) {
|
227
|
for(FeatureNodeContainerTree containerTree : featureNodeContainerCache.values()){
|
228
|
containerTree.buildTree();
|
229
|
}
|
230
|
viewer.refresh();
|
231
|
|
232
|
if(object instanceof DescriptionElementBase){
|
233
|
DescriptionElementBase descriptionElement = (DescriptionElementBase) object;
|
234
|
DescriptionBase description = descriptionElement.getInDescription();
|
235
|
FeatureNodeContainerTree containerTree = featureNodeContainerCache.get(description);
|
236
|
|
237
|
FeatureNodeContainer featureNodeContainer = containerTree.getFeatureNodeContainerForDescriptionElement(descriptionElement);
|
238
|
viewer.expandToLevel(featureNodeContainer, 1);
|
239
|
|
240
|
}
|
241
|
|
242
|
if(object != null){
|
243
|
StructuredSelection selection = new StructuredSelection(object);
|
244
|
viewer.setSelection(selection, true);
|
245
|
}
|
246
|
|
247
|
super.changed(object);
|
248
|
}
|
249
|
|
250
|
/** {@inheritDoc} */
|
251
|
@Override
|
252
|
public Viewer getViewer() {
|
253
|
return viewer;
|
254
|
}
|
255
|
|
256
|
/**
|
257
|
* <p>onComplete</p>
|
258
|
*
|
259
|
* @return a boolean.
|
260
|
*/
|
261
|
public boolean onComplete() {
|
262
|
return false;
|
263
|
}
|
264
|
}
|