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
.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
.Table
;
27 import org
.eclipse
.swt
.widgets
.TableItem
;
28 import org
.eclipse
.ui
.IEditorInput
;
29 import org
.eclipse
.ui
.IEditorSite
;
30 import org
.eclipse
.ui
.PartInitException
;
31 import org
.eclipse
.ui
.part
.EditorPart
;
33 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
34 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
35 import eu
.etaxonomy
.cdm
.model
.description
.PolytomousKey
;
36 import eu
.etaxonomy
.cdm
.model
.description
.PolytomousKeyNode
;
37 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
38 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
39 import eu
.etaxonomy
.taxeditor
.editor
.EditorUtil
;
40 import eu
.etaxonomy
.taxeditor
.model
.IDirtyMarkableSelectionProvider
;
44 * @created Mar 31, 2011
47 public class PolytomousKeyListEditor
extends EditorPart
implements
48 IConversationEnabled
, IDirtyMarkableSelectionProvider
{
50 private class LinkListener
extends MouseAdapter
{
55 * org.eclipse.swt.events.MouseAdapter#mouseUp(org.eclipse.swt.events
59 public void mouseUp(MouseEvent event
) {
61 Table table
= (Table
) event
.widget
;
62 // Determine where the mouse was clicked
63 Point pt
= new Point(event
.x
, event
.y
);
65 final TableItem item
= table
.getItem(pt
);
67 // Determine which column was selected
69 for (int i
= 0, n
= table
.getColumnCount(); i
< n
; i
++) {
70 Rectangle rect
= item
.getBounds(i
);
71 if (rect
.contains(pt
)) {
72 // This is the selected column
78 PolytomousKeyNode node
= (PolytomousKeyNode
) item
.getData();
79 Object linkData
= getItemLinkData(node
);
80 if (linkData
instanceof PolytomousKeyNode
) {
81 viewer
.setSelection(new StructuredSelection(linkData
),
83 } else if (linkData
instanceof Taxon
) {
85 EditorUtil
.openTaxonBase(((Taxon
) linkData
)
87 } catch (PartInitException e
) {
88 EditorUtil
.error(getClass(), e
);
98 private Object
getItemLinkData(PolytomousKeyNode node
) {
99 return node
.getChildren().isEmpty() ? node
.getTaxon() : node
104 public static final String ID
= "eu.etaxonomy.taxeditor.editor.key.polytomous.list";
106 private TableViewer viewer
;
112 * eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update
113 * (eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
116 public void update(CdmDataChangeMap arg0
) {
124 * eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder
128 public ConversationHolder
getConversationHolder() {
129 return ((PolytomousKeyEditorInput
) getEditorInput())
130 .getConversationHolder();
136 * @see org.eclipse.ui.part.EditorPart#doSave(org.eclipse.core.runtime.
140 public void doSave(IProgressMonitor monitor
) {
147 * @see org.eclipse.ui.part.EditorPart#doSaveAs()
150 public void doSaveAs() {
156 * @see org.eclipse.ui.part.EditorPart#init(org.eclipse.ui.IEditorSite,
157 * org.eclipse.ui.IEditorInput)
160 public void init(IEditorSite site
, IEditorInput input
)
161 throws PartInitException
{
169 * @see org.eclipse.ui.part.EditorPart#isDirty()
172 public boolean isDirty() {
173 // TODO Auto-generated method stub
180 * @see org.eclipse.ui.part.EditorPart#isSaveAsAllowed()
183 public boolean isSaveAsAllowed() {
191 * org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets
195 public void createPartControl(Composite parent
) {
197 FillLayout fillLayout
= new FillLayout();
198 fillLayout
.marginWidth
= 0;
199 fillLayout
.marginHeight
= 0;
200 fillLayout
.type
= SWT
.VERTICAL
;
201 parent
.setLayout(fillLayout
);
203 viewer
= new TableViewer(parent
, SWT
.MULTI
| SWT
.H_SCROLL
204 | SWT
.V_SCROLL
| SWT
.FULL_SELECTION
);
205 getSite().setSelectionProvider(viewer
);
207 createColumns(viewer
);
208 viewer
.getControl().addMouseListener(new LinkListener());
209 viewer
.setContentProvider(new PolytomousKeyListContentProvider());
210 viewer
.setLabelProvider(new PolytomousKeyListLabelProvider());
212 // viewer.getControl().setLayoutData(CdmFormFactory.FILL());
214 PolytomousKey key
= ((PolytomousKeyEditorInput
) getEditorInput())
217 viewer
.setInput(key
);
220 // This will create the columns for the table
221 private void createColumns(TableViewer viewer
) {
222 Table table
= viewer
.getTable();
223 String
[] titles
= { "Node Number", "Question", "Statement", "Link" };
224 int[] bounds
= { 50, 200, 200, 100 };
226 for (int i
= 0; i
< titles
.length
; i
++) {
227 TableViewerColumn column
= new TableViewerColumn(viewer
, SWT
.NONE
);
228 column
.getColumn().setText(titles
[i
]);
229 column
.getColumn().setWidth(bounds
[i
]);
230 column
.getColumn().setResizable(true);
231 column
.getColumn().setMoveable(true);
233 table
.setHeaderVisible(true);
234 table
.setLinesVisible(false);
241 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
244 public void setFocus() {
252 * eu.etaxonomy.taxeditor.model.IDirtyMarkableSelectionProvider#changed(
256 public void changed(Object element
) {
257 viewer
.update(element
, null);
259 if (element
instanceof PolytomousKeyNode
) {
260 List
<PolytomousKeyNode
> children
= ((PolytomousKeyNode
) element
)
261 .getParent().getChildren();
262 for (PolytomousKeyNode child
: children
) {
263 viewer
.update(child
, null);