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
) {
182 * @see org.eclipse.ui.part.EditorPart#doSaveAs()
185 public void doSaveAs() {
191 * @see org.eclipse.ui.part.EditorPart#init(org.eclipse.ui.IEditorSite,
192 * org.eclipse.ui.IEditorInput)
195 public void init(IEditorSite site
, IEditorInput input
)
196 throws PartInitException
{
204 * @see org.eclipse.ui.part.EditorPart#isDirty()
207 public boolean isDirty() {
214 * @see org.eclipse.ui.part.EditorPart#isSaveAsAllowed()
217 public boolean isSaveAsAllowed() {
225 * org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets
229 public void createPartControl(Composite parent
) {
231 FillLayout fillLayout
= new FillLayout();
232 fillLayout
.marginWidth
= 0;
233 fillLayout
.marginHeight
= 0;
234 fillLayout
.type
= SWT
.VERTICAL
;
235 parent
.setLayout(fillLayout
);
237 viewer
= new TableViewer(parent
, SWT
.MULTI
| SWT
.H_SCROLL
238 | SWT
.V_SCROLL
| SWT
.FULL_SELECTION
);
239 getSite().setSelectionProvider(viewer
);
241 createColumns(viewer
);
242 viewer
.getControl().addMouseListener(new LinkListener());
243 viewer
.setContentProvider(new PolytomousKeyListContentProvider());
244 viewer
.setLabelProvider(new PolytomousKeyListLabelProvider());
248 PolytomousKey key
= ((PolytomousKeyEditorInput
) getEditorInput())
251 setPartName(key
.getTitleCache());
253 viewer
.setInput(getEditorInput());
256 public int getTableItemCount() {
257 if (viewer
!= null && viewer
.getTable() != null) {
258 return viewer
.getTable().getItemCount();
264 public PolytomousKey
getViewerInputKey() {
265 return ((PolytomousKeyEditorInput
) viewer
.getInput()).getKey();
267 private void createMenu() {
268 // register context menu
269 MenuManager menuManager
= new MenuManager();
271 .add(new GroupMarker(IWorkbenchActionConstants
.MB_ADDITIONS
));
272 getSite().registerContextMenu(ID
, menuManager
, viewer
);
274 Control control
= viewer
.getControl();
275 Menu menu
= menuManager
.createContextMenu(control
);
276 control
.setMenu(menu
);
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 };
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);
292 table
.setHeaderVisible(true);
293 table
.setLinesVisible(false);
300 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
303 public void setFocus() {
304 PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell().setFocus();
311 * eu.etaxonomy.taxeditor.model.IDirtyMarkableSelectionProvider#changed(
315 public void changed(Object element
) {
316 if(element
!= null) {
317 viewer
.update(element
, null);
320 if (element
instanceof PolytomousKeyNode
) {
321 List
<PolytomousKeyNode
> children
= ((PolytomousKeyNode
) element
)
322 .getParent().getChildren();
323 viewer
.update(((PolytomousKeyNode
) element
)
325 for (PolytomousKeyNode child
: children
) {
326 viewer
.update(child
, null);
334 * @see eu.etaxonomy.taxeditor.model.IDirtyMarkableSelectionProvider#forceDirty()
337 public void forceDirty() {
345 * eu.etaxonomy.taxeditor.operation.IPostOperationEnabled#postOperation(
346 * eu.etaxonomy.cdm.model.common.CdmBase)
349 public boolean postOperation(CdmBase objectAffectedByOperation
) {
351 getConversationHolder().bind();
352 getConversationHolder().commit(true);
353 editor
.changed(objectAffectedByOperation
);
355 if (objectAffectedByOperation
!= null) {
356 viewer
.setSelection(new StructuredSelection(
357 objectAffectedByOperation
), true);
365 * @see eu.etaxonomy.taxeditor.operation.IPostOperationEnabled#onComplete()
368 public boolean onComplete() {