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