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