3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
11 package eu
.etaxonomy
.taxeditor
.editor
.key
.polytomous
;
13 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
14 import org
.eclipse
.jface
.action
.GroupMarker
;
15 import org
.eclipse
.jface
.action
.MenuManager
;
16 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
17 import org
.eclipse
.jface
.viewers
.TableViewer
;
18 import org
.eclipse
.jface
.viewers
.TableViewerColumn
;
19 import org
.eclipse
.swt
.SWT
;
20 import org
.eclipse
.swt
.events
.MouseAdapter
;
21 import org
.eclipse
.swt
.events
.MouseEvent
;
22 import org
.eclipse
.swt
.graphics
.Point
;
23 import org
.eclipse
.swt
.graphics
.Rectangle
;
24 import org
.eclipse
.swt
.layout
.FillLayout
;
25 import org
.eclipse
.swt
.widgets
.Composite
;
26 import org
.eclipse
.swt
.widgets
.Control
;
27 import org
.eclipse
.swt
.widgets
.Menu
;
28 import org
.eclipse
.swt
.widgets
.Table
;
29 import org
.eclipse
.swt
.widgets
.TableItem
;
30 import org
.eclipse
.ui
.IEditorInput
;
31 import org
.eclipse
.ui
.IEditorSite
;
32 import org
.eclipse
.ui
.IWorkbenchActionConstants
;
33 import org
.eclipse
.ui
.PartInitException
;
34 import org
.eclipse
.ui
.PlatformUI
;
35 import org
.eclipse
.ui
.part
.EditorPart
;
37 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
38 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
39 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
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
.editor
.key
.KeyEditor
;
46 import eu
.etaxonomy
.taxeditor
.model
.IDirtyMarkable
;
47 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
51 * @created Mar 31, 2011
54 public class PolytomousKeyListEditor
extends EditorPart
implements
55 IConversationEnabled
, IDirtyMarkable
,
56 IPolytomousKeyEditorPage
{
58 private class LinkListener
extends MouseAdapter
{
63 * org.eclipse.swt.events.MouseAdapter#mouseUp(org.eclipse.swt.events
67 public void mouseUp(MouseEvent event
) {
69 if(event
.button
== 1 && event
.count
== 2) {
70 Table table
= (Table
) event
.widget
;
71 // Determine where the mouse was clicked
72 Point point
= new Point(event
.x
, event
.y
);
74 int selectedColumn
= getSelectedColumn(table
, point
);
75 PolytomousKeyNode node
= (PolytomousKeyNode
) getTableItem(
76 table
, point
).getData();
78 if (selectedColumn
== 4) {
79 PolytomousKeyNode linkData
= getItemLinkData(node
);
80 if (linkData
!= null) {
81 viewer
.setSelection(new StructuredSelection(linkData
), true);
84 if (selectedColumn
== 5) {
85 Taxon taxon
= getItemTaxon(node
);
88 EditorUtil
.openTaxonBase((taxon
).getUuid());
89 } catch (PartInitException e
) {
90 MessagingUtils
.error(getClass(), e
);
97 private int getSelectedColumn(Table table
, Point point
) {
98 TableItem item
= getTableItem(table
, point
);
100 for (int i
= 0, n
= table
.getColumnCount(); i
< n
; i
++) {
101 Rectangle rect
= item
.getBounds(i
);
102 if (rect
.contains(point
)) {
103 // This is the selected column
111 private TableItem
getTableItem(Table table
, Point point
) {
112 return table
.getItem(point
);
118 private PolytomousKeyNode
getItemLinkData(PolytomousKeyNode node
) {
119 return node
.getChildren().isEmpty() ?
null : node
126 private Taxon
getItemTaxon(PolytomousKeyNode node
) {
127 return node
.getTaxon();
131 public static final String ID
= "eu.etaxonomy.taxeditor.editor.key.polytomous.list";
133 private TableViewer viewer
;
135 private final KeyEditor editor
;
137 public PolytomousKeyListEditor(KeyEditor editor
) {
138 this.editor
= editor
;
145 * eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update
146 * (eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
149 public void update(CdmDataChangeMap arg0
) {
157 * eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder
161 public ConversationHolder
getConversationHolder() {
162 return ((PolytomousKeyEditorInput
) getEditorInput())
163 .getConversationHolder();
169 * @see org.eclipse.ui.part.EditorPart#doSave(org.eclipse.core.runtime.
173 public void doSave(IProgressMonitor monitor
) {
180 * @see org.eclipse.ui.part.EditorPart#doSaveAs()
183 public void doSaveAs() {
189 * @see org.eclipse.ui.part.EditorPart#init(org.eclipse.ui.IEditorSite,
190 * org.eclipse.ui.IEditorInput)
193 public void init(IEditorSite site
, IEditorInput input
)
194 throws PartInitException
{
202 * @see org.eclipse.ui.part.EditorPart#isDirty()
205 public boolean isDirty() {
212 * @see org.eclipse.ui.part.EditorPart#isSaveAsAllowed()
215 public boolean isSaveAsAllowed() {
223 * org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets
227 public void createPartControl(Composite parent
) {
229 FillLayout fillLayout
= new FillLayout();
230 fillLayout
.marginWidth
= 0;
231 fillLayout
.marginHeight
= 0;
232 fillLayout
.type
= SWT
.VERTICAL
;
233 parent
.setLayout(fillLayout
);
235 viewer
= new TableViewer(parent
, SWT
.MULTI
| SWT
.H_SCROLL
236 | SWT
.V_SCROLL
| SWT
.FULL_SELECTION
);
237 getSite().setSelectionProvider(viewer
);
239 createColumns(viewer
);
240 viewer
.getControl().addMouseListener(new LinkListener());
241 viewer
.setContentProvider(new PolytomousKeyListContentProvider());
242 viewer
.setLabelProvider(new PolytomousKeyListLabelProvider());
246 PolytomousKey key
= ((PolytomousKeyEditorInput
) getEditorInput())
249 setPartName(key
.getTitleCache());
251 viewer
.setInput(getEditorInput());
254 public int getTableItemCount() {
255 if (viewer
!= null && viewer
.getTable() != null) {
256 return viewer
.getTable().getItemCount();
262 public PolytomousKey
getViewerInputKey() {
263 return ((PolytomousKeyEditorInput
) viewer
.getInput()).getKey();
265 private void createMenu() {
266 // register context menu
267 MenuManager menuManager
= new MenuManager();
269 .add(new GroupMarker(IWorkbenchActionConstants
.MB_ADDITIONS
));
270 getSite().registerContextMenu(ID
, menuManager
, viewer
);
272 Control control
= viewer
.getControl();
273 Menu menu
= menuManager
.createContextMenu(control
);
274 control
.setMenu(menu
);
277 // This will create the columns for the table
278 private void createColumns(TableViewer viewer
) {
279 Table table
= viewer
.getTable();
280 String
[] titles
= { "Node", "Question", "Edge", "Statement", "Link", "Taxon" };
281 int[] bounds
= { 50, 200, 50, 200, 100, 200 };
283 for (int i
= 0; i
< titles
.length
; i
++) {
284 TableViewerColumn column
= new TableViewerColumn(viewer
, SWT
.NONE
);
285 column
.getColumn().setText(titles
[i
]);
286 column
.getColumn().setWidth(bounds
[i
]);
287 column
.getColumn().setResizable(true);
288 column
.getColumn().setMoveable(true);
290 table
.setHeaderVisible(true);
291 table
.setLinesVisible(false);
298 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
301 public void setFocus() {
302 PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell().setFocus();
309 * eu.etaxonomy.taxeditor.model.IDirtyMarkableSelectionProvider#changed(
313 public void changed(Object element
) {
314 if(element
!= null) {
315 viewer
.update(element
, null);
318 if (element
instanceof PolytomousKeyNode
) {
319 PolytomousKeyNode node
= (PolytomousKeyNode
) element
;
320 viewer
.update(node
, null);
321 for (PolytomousKeyNode child
: node
.getChildren()) {
322 viewer
.update(child
, null);
330 * @see eu.etaxonomy.taxeditor.model.IDirtyMarkableSelectionProvider#forceDirty()
333 public void forceDirty() {
341 * eu.etaxonomy.taxeditor.operation.IPostOperationEnabled#postOperation(
342 * eu.etaxonomy.cdm.model.common.CdmBase)
345 public boolean postOperation(CdmBase objectAffectedByOperation
) {
347 getConversationHolder().bind();
348 getConversationHolder().commit(true);
349 editor
.changed(objectAffectedByOperation
);
351 if (objectAffectedByOperation
!= null) {
352 viewer
.setSelection(new StructuredSelection(
353 objectAffectedByOperation
), true);
361 * @see eu.etaxonomy.taxeditor.operation.IPostOperationEnabled#onComplete()
364 public boolean onComplete() {