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