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.key.polytomous;
|
12
|
|
13
|
import java.util.List;
|
14
|
|
15
|
import org.eclipse.core.runtime.IProgressMonitor;
|
16
|
import org.eclipse.jface.action.GroupMarker;
|
17
|
import org.eclipse.jface.action.MenuManager;
|
18
|
import org.eclipse.jface.viewers.StructuredSelection;
|
19
|
import org.eclipse.jface.viewers.TableViewer;
|
20
|
import org.eclipse.jface.viewers.TableViewerColumn;
|
21
|
import org.eclipse.swt.SWT;
|
22
|
import org.eclipse.swt.events.MouseAdapter;
|
23
|
import org.eclipse.swt.events.MouseEvent;
|
24
|
import org.eclipse.swt.graphics.Point;
|
25
|
import org.eclipse.swt.graphics.Rectangle;
|
26
|
import org.eclipse.swt.layout.FillLayout;
|
27
|
import org.eclipse.swt.widgets.Composite;
|
28
|
import org.eclipse.swt.widgets.Control;
|
29
|
import org.eclipse.swt.widgets.Menu;
|
30
|
import org.eclipse.swt.widgets.Table;
|
31
|
import org.eclipse.swt.widgets.TableItem;
|
32
|
import org.eclipse.ui.IEditorInput;
|
33
|
import org.eclipse.ui.IEditorSite;
|
34
|
import org.eclipse.ui.IWorkbenchActionConstants;
|
35
|
import org.eclipse.ui.PartInitException;
|
36
|
import org.eclipse.ui.part.EditorPart;
|
37
|
|
38
|
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
|
39
|
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
|
40
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
41
|
import eu.etaxonomy.cdm.model.description.PolytomousKey;
|
42
|
import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
|
43
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
44
|
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
|
45
|
import eu.etaxonomy.taxeditor.editor.EditorUtil;
|
46
|
import eu.etaxonomy.taxeditor.editor.key.KeyEditor;
|
47
|
import eu.etaxonomy.taxeditor.model.IDirtyMarkableSelectionProvider;
|
48
|
|
49
|
/**
|
50
|
* @author n.hoffmann
|
51
|
* @created Mar 31, 2011
|
52
|
* @version 1.0
|
53
|
*/
|
54
|
public class PolytomousKeyListEditor extends EditorPart implements
|
55
|
IConversationEnabled, IDirtyMarkableSelectionProvider,
|
56
|
IPolytomousKeyEditorPage {
|
57
|
|
58
|
private class LinkListener extends MouseAdapter {
|
59
|
/*
|
60
|
* (non-Javadoc)
|
61
|
*
|
62
|
* @see
|
63
|
* org.eclipse.swt.events.MouseAdapter#mouseUp(org.eclipse.swt.events
|
64
|
* .MouseEvent)
|
65
|
*/
|
66
|
@Override
|
67
|
public void mouseUp(MouseEvent event) {
|
68
|
|
69
|
Table table = (Table) event.widget;
|
70
|
// Determine where the mouse was clicked
|
71
|
Point point = new Point(event.x, event.y);
|
72
|
|
73
|
int selectedColumn = getSelectedColumn(table, point);
|
74
|
|
75
|
if (selectedColumn == 3) {
|
76
|
PolytomousKeyNode node = (PolytomousKeyNode) getTableItem(
|
77
|
table, point).getData();
|
78
|
Object linkData = getItemLinkData(node);
|
79
|
if (linkData instanceof PolytomousKeyNode) {
|
80
|
viewer.setSelection(new StructuredSelection(linkData), true);
|
81
|
} else if (linkData instanceof Taxon) {
|
82
|
try {
|
83
|
EditorUtil.openTaxonBase(((Taxon) linkData).getUuid());
|
84
|
} catch (PartInitException e) {
|
85
|
EditorUtil.error(getClass(), e);
|
86
|
}
|
87
|
}
|
88
|
}
|
89
|
|
90
|
}
|
91
|
|
92
|
private int getSelectedColumn(Table table, Point point) {
|
93
|
TableItem item = getTableItem(table, point);
|
94
|
if (item != null) {
|
95
|
for (int i = 0, n = table.getColumnCount(); i < n; i++) {
|
96
|
Rectangle rect = item.getBounds(i);
|
97
|
if (rect.contains(point)) {
|
98
|
// This is the selected column
|
99
|
return i;
|
100
|
}
|
101
|
}
|
102
|
}
|
103
|
return -1;
|
104
|
}
|
105
|
|
106
|
private TableItem getTableItem(Table table, Point point) {
|
107
|
return table.getItem(point);
|
108
|
}
|
109
|
|
110
|
/**
|
111
|
* @return
|
112
|
*/
|
113
|
private Object getItemLinkData(PolytomousKeyNode node) {
|
114
|
return node.getChildren().isEmpty() ? node.getTaxon() : node
|
115
|
.getChildAt(0);
|
116
|
}
|
117
|
}
|
118
|
|
119
|
public static final String ID = "eu.etaxonomy.taxeditor.editor.key.polytomous.list";
|
120
|
|
121
|
private TableViewer viewer;
|
122
|
|
123
|
private final KeyEditor editor;
|
124
|
|
125
|
public PolytomousKeyListEditor(KeyEditor editor) {
|
126
|
this.editor = editor;
|
127
|
}
|
128
|
|
129
|
/*
|
130
|
* (non-Javadoc)
|
131
|
*
|
132
|
* @see
|
133
|
* eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update
|
134
|
* (eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
|
135
|
*/
|
136
|
@Override
|
137
|
public void update(CdmDataChangeMap arg0) {
|
138
|
|
139
|
}
|
140
|
|
141
|
/*
|
142
|
* (non-Javadoc)
|
143
|
*
|
144
|
* @see
|
145
|
* eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder
|
146
|
* ()
|
147
|
*/
|
148
|
@Override
|
149
|
public ConversationHolder getConversationHolder() {
|
150
|
return ((PolytomousKeyEditorInput) getEditorInput())
|
151
|
.getConversationHolder();
|
152
|
}
|
153
|
|
154
|
/*
|
155
|
* (non-Javadoc)
|
156
|
*
|
157
|
* @see org.eclipse.ui.part.EditorPart#doSave(org.eclipse.core.runtime.
|
158
|
* IProgressMonitor)
|
159
|
*/
|
160
|
@Override
|
161
|
public void doSave(IProgressMonitor monitor) {
|
162
|
|
163
|
}
|
164
|
|
165
|
/*
|
166
|
* (non-Javadoc)
|
167
|
*
|
168
|
* @see org.eclipse.ui.part.EditorPart#doSaveAs()
|
169
|
*/
|
170
|
@Override
|
171
|
public void doSaveAs() {
|
172
|
}
|
173
|
|
174
|
/*
|
175
|
* (non-Javadoc)
|
176
|
*
|
177
|
* @see org.eclipse.ui.part.EditorPart#init(org.eclipse.ui.IEditorSite,
|
178
|
* org.eclipse.ui.IEditorInput)
|
179
|
*/
|
180
|
@Override
|
181
|
public void init(IEditorSite site, IEditorInput input)
|
182
|
throws PartInitException {
|
183
|
setSite(site);
|
184
|
setInput(input);
|
185
|
}
|
186
|
|
187
|
/*
|
188
|
* (non-Javadoc)
|
189
|
*
|
190
|
* @see org.eclipse.ui.part.EditorPart#isDirty()
|
191
|
*/
|
192
|
@Override
|
193
|
public boolean isDirty() {
|
194
|
return false;
|
195
|
}
|
196
|
|
197
|
/*
|
198
|
* (non-Javadoc)
|
199
|
*
|
200
|
* @see org.eclipse.ui.part.EditorPart#isSaveAsAllowed()
|
201
|
*/
|
202
|
@Override
|
203
|
public boolean isSaveAsAllowed() {
|
204
|
return false;
|
205
|
}
|
206
|
|
207
|
/*
|
208
|
* (non-Javadoc)
|
209
|
*
|
210
|
* @see
|
211
|
* org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets
|
212
|
* .Composite)
|
213
|
*/
|
214
|
@Override
|
215
|
public void createPartControl(Composite parent) {
|
216
|
|
217
|
FillLayout fillLayout = new FillLayout();
|
218
|
fillLayout.marginWidth = 0;
|
219
|
fillLayout.marginHeight = 0;
|
220
|
fillLayout.type = SWT.VERTICAL;
|
221
|
parent.setLayout(fillLayout);
|
222
|
|
223
|
viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
|
224
|
| SWT.V_SCROLL | SWT.FULL_SELECTION);
|
225
|
getSite().setSelectionProvider(viewer);
|
226
|
|
227
|
createColumns(viewer);
|
228
|
viewer.getControl().addMouseListener(new LinkListener());
|
229
|
viewer.setContentProvider(new PolytomousKeyListContentProvider());
|
230
|
viewer.setLabelProvider(new PolytomousKeyListLabelProvider());
|
231
|
|
232
|
createMenu();
|
233
|
|
234
|
PolytomousKey key = ((PolytomousKeyEditorInput) getEditorInput())
|
235
|
.getKey();
|
236
|
|
237
|
setPartName(key.getTitleCache());
|
238
|
|
239
|
viewer.setInput(key);
|
240
|
}
|
241
|
|
242
|
private void createMenu() {
|
243
|
// register context menu
|
244
|
MenuManager menuManager = new MenuManager();
|
245
|
menuManager
|
246
|
.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
|
247
|
getSite().registerContextMenu(ID, menuManager, viewer);
|
248
|
|
249
|
Control control = viewer.getControl();
|
250
|
Menu menu = menuManager.createContextMenu(control);
|
251
|
control.setMenu(menu);
|
252
|
}
|
253
|
|
254
|
// This will create the columns for the table
|
255
|
private void createColumns(TableViewer viewer) {
|
256
|
Table table = viewer.getTable();
|
257
|
String[] titles = { "Node Number", "Question", "Statement", "Link" };
|
258
|
int[] bounds = { 50, 200, 200, 100 };
|
259
|
|
260
|
for (int i = 0; i < titles.length; i++) {
|
261
|
TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
|
262
|
column.getColumn().setText(titles[i]);
|
263
|
column.getColumn().setWidth(bounds[i]);
|
264
|
column.getColumn().setResizable(true);
|
265
|
column.getColumn().setMoveable(true);
|
266
|
}
|
267
|
table.setHeaderVisible(true);
|
268
|
table.setLinesVisible(false);
|
269
|
|
270
|
}
|
271
|
|
272
|
/*
|
273
|
* (non-Javadoc)
|
274
|
*
|
275
|
* @see org.eclipse.ui.part.WorkbenchPart#setFocus()
|
276
|
*/
|
277
|
@Override
|
278
|
public void setFocus() {
|
279
|
|
280
|
}
|
281
|
|
282
|
/*
|
283
|
* (non-Javadoc)
|
284
|
*
|
285
|
* @see
|
286
|
* eu.etaxonomy.taxeditor.model.IDirtyMarkableSelectionProvider#changed(
|
287
|
* java.lang.Object)
|
288
|
*/
|
289
|
@Override
|
290
|
public void changed(Object element) {
|
291
|
viewer.update(element, null);
|
292
|
|
293
|
if (element instanceof PolytomousKeyNode) {
|
294
|
List<PolytomousKeyNode> children = ((PolytomousKeyNode) element)
|
295
|
.getParent().getChildren();
|
296
|
for (PolytomousKeyNode child : children) {
|
297
|
viewer.update(child, null);
|
298
|
}
|
299
|
}
|
300
|
|
301
|
}
|
302
|
|
303
|
/*
|
304
|
* (non-Javadoc)
|
305
|
*
|
306
|
* @see
|
307
|
* eu.etaxonomy.taxeditor.operation.IPostOperationEnabled#postOperation(
|
308
|
* eu.etaxonomy.cdm.model.common.CdmBase)
|
309
|
*/
|
310
|
@Override
|
311
|
public boolean postOperation(CdmBase objectAffectedByOperation) {
|
312
|
viewer.refresh();
|
313
|
|
314
|
editor.changed(objectAffectedByOperation);
|
315
|
|
316
|
if (objectAffectedByOperation != null) {
|
317
|
viewer.setSelection(new StructuredSelection(
|
318
|
objectAffectedByOperation), true);
|
319
|
}
|
320
|
return true;
|
321
|
}
|
322
|
|
323
|
/*
|
324
|
* (non-Javadoc)
|
325
|
*
|
326
|
* @see eu.etaxonomy.taxeditor.operation.IPostOperationEnabled#onComplete()
|
327
|
*/
|
328
|
@Override
|
329
|
public boolean onComplete() {
|
330
|
return true;
|
331
|
}
|
332
|
|
333
|
}
|