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