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