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