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 java
.util
.List
;
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
;
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
;
53 * @created Mar 31, 2011
56 public class PolytomousKeyListEditor
extends EditorPart
implements
57 IConversationEnabled
, IDirtyMarkable
,
58 IPolytomousKeyEditorPage
{
60 private class LinkListener
extends MouseAdapter
{
65 * org.eclipse.swt.events.MouseAdapter#mouseUp(org.eclipse.swt.events
69 public void mouseUp(MouseEvent event
) {
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
);
76 int selectedColumn
= getSelectedColumn(table
, point
);
77 PolytomousKeyNode node
= (PolytomousKeyNode
) getTableItem(
78 table
, point
).getData();
80 if (selectedColumn
== 4) {
81 PolytomousKeyNode linkData
= getItemLinkData(node
);
82 if (linkData
!= null) {
83 viewer
.setSelection(new StructuredSelection(linkData
), true);
86 if (selectedColumn
== 5) {
87 Taxon taxon
= getItemTaxon(node
);
90 EditorUtil
.openTaxonBase((taxon
).getUuid());
91 } catch (PartInitException e
) {
92 MessagingUtils
.error(getClass(), e
);
99 private int getSelectedColumn(Table table
, Point point
) {
100 TableItem item
= getTableItem(table
, point
);
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
113 private TableItem
getTableItem(Table table
, Point point
) {
114 return table
.getItem(point
);
120 private PolytomousKeyNode
getItemLinkData(PolytomousKeyNode node
) {
121 return node
.getChildren().isEmpty() ?
null : node
128 private Taxon
getItemTaxon(PolytomousKeyNode node
) {
129 return node
.getTaxon();
133 public static final String ID
= "eu.etaxonomy.taxeditor.editor.key.polytomous.list";
135 private TableViewer viewer
;
137 private final KeyEditor editor
;
139 public PolytomousKeyListEditor(KeyEditor editor
) {
140 this.editor
= editor
;
147 * eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update
148 * (eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
151 public void update(CdmDataChangeMap arg0
) {
159 * eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder
163 public ConversationHolder
getConversationHolder() {
164 return ((PolytomousKeyEditorInput
) getEditorInput())
165 .getConversationHolder();
171 * @see org.eclipse.ui.part.EditorPart#doSave(org.eclipse.core.runtime.
175 public void doSave(IProgressMonitor monitor
) {
181 * @see org.eclipse.ui.part.EditorPart#doSaveAs()
184 public void doSaveAs() {
190 * @see org.eclipse.ui.part.EditorPart#init(org.eclipse.ui.IEditorSite,
191 * org.eclipse.ui.IEditorInput)
194 public void init(IEditorSite site
, IEditorInput input
)
195 throws PartInitException
{
203 * @see org.eclipse.ui.part.EditorPart#isDirty()
206 public boolean isDirty() {
213 * @see org.eclipse.ui.part.EditorPart#isSaveAsAllowed()
216 public boolean isSaveAsAllowed() {
224 * org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets
228 public void createPartControl(Composite parent
) {
230 FillLayout fillLayout
= new FillLayout();
231 fillLayout
.marginWidth
= 0;
232 fillLayout
.marginHeight
= 0;
233 fillLayout
.type
= SWT
.VERTICAL
;
234 parent
.setLayout(fillLayout
);
236 viewer
= new TableViewer(parent
, SWT
.MULTI
| SWT
.H_SCROLL
237 | SWT
.V_SCROLL
| SWT
.FULL_SELECTION
);
238 getSite().setSelectionProvider(viewer
);
240 createColumns(viewer
);
241 viewer
.getControl().addMouseListener(new LinkListener());
242 viewer
.setContentProvider(new PolytomousKeyListContentProvider());
243 viewer
.setLabelProvider(new PolytomousKeyListLabelProvider());
247 PolytomousKey key
= ((PolytomousKeyEditorInput
) getEditorInput())
250 setPartName(key
.getTitleCache());
252 viewer
.setInput(getEditorInput());
255 public int getTableItemCount() {
256 if (viewer
!= null && viewer
.getTable() != null) {
257 return viewer
.getTable().getItemCount();
263 public PolytomousKey
getViewerInputKey() {
264 return ((PolytomousKeyEditorInput
) viewer
.getInput()).getKey();
266 private void createMenu() {
267 // register context menu
268 MenuManager menuManager
= new MenuManager();
270 .add(new GroupMarker(IWorkbenchActionConstants
.MB_ADDITIONS
));
271 getSite().registerContextMenu(ID
, menuManager
, viewer
);
273 Control control
= viewer
.getControl();
274 Menu menu
= menuManager
.createContextMenu(control
);
275 control
.setMenu(menu
);
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 };
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);
291 table
.setHeaderVisible(true);
292 table
.setLinesVisible(false);
299 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
302 public void setFocus() {
303 PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell().setFocus();
310 * eu.etaxonomy.taxeditor.model.IDirtyMarkableSelectionProvider#changed(
314 public void changed(Object element
) {
315 if(element
!= null) {
316 viewer
.update(element
, null);
319 if (element
instanceof PolytomousKeyNode
) {
320 List
<PolytomousKeyNode
> children
= ((PolytomousKeyNode
) element
)
321 .getParent().getChildren();
322 for (PolytomousKeyNode child
: children
) {
323 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() {