2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.taxeditor
.editor
.key
.polytomous
.e4
;
12 import java
.util
.List
;
14 import javax
.annotation
.PostConstruct
;
15 import javax
.annotation
.PreDestroy
;
16 import javax
.inject
.Inject
;
18 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
19 import org
.eclipse
.e4
.ui
.di
.Focus
;
20 import org
.eclipse
.e4
.ui
.di
.Persist
;
21 import org
.eclipse
.e4
.ui
.model
.application
.MApplication
;
22 import org
.eclipse
.e4
.ui
.model
.application
.ui
.MDirtyable
;
23 import org
.eclipse
.e4
.ui
.model
.application
.ui
.basic
.MPart
;
24 import org
.eclipse
.e4
.ui
.services
.EMenuService
;
25 import org
.eclipse
.e4
.ui
.workbench
.modeling
.EModelService
;
26 import org
.eclipse
.e4
.ui
.workbench
.modeling
.EPartService
;
27 import org
.eclipse
.e4
.ui
.workbench
.modeling
.ESelectionService
;
28 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
29 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
30 import org
.eclipse
.jface
.viewers
.TableViewer
;
31 import org
.eclipse
.jface
.viewers
.TableViewerColumn
;
32 import org
.eclipse
.swt
.SWT
;
33 import org
.eclipse
.swt
.events
.MouseAdapter
;
34 import org
.eclipse
.swt
.events
.MouseEvent
;
35 import org
.eclipse
.swt
.graphics
.Point
;
36 import org
.eclipse
.swt
.graphics
.Rectangle
;
37 import org
.eclipse
.swt
.layout
.FillLayout
;
38 import org
.eclipse
.swt
.widgets
.Composite
;
39 import org
.eclipse
.swt
.widgets
.Table
;
40 import org
.eclipse
.swt
.widgets
.TableItem
;
42 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
43 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
44 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
45 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
46 import eu
.etaxonomy
.cdm
.model
.description
.PolytomousKey
;
47 import eu
.etaxonomy
.cdm
.model
.description
.PolytomousKeyNode
;
48 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
49 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
50 import eu
.etaxonomy
.taxeditor
.editor
.EditorUtil
;
51 import eu
.etaxonomy
.taxeditor
.editor
.key
.e4
.KeyEditorDataChangeBehaviourE4
;
52 import eu
.etaxonomy
.taxeditor
.editor
.key
.polytomous
.IPolytomousKeyEditorPage
;
53 import eu
.etaxonomy
.taxeditor
.editor
.key
.polytomous
.PolytomousKeyEditorInput
;
54 import eu
.etaxonomy
.taxeditor
.editor
.key
.polytomous
.PolytomousKeyListContentProvider
;
55 import eu
.etaxonomy
.taxeditor
.editor
.key
.polytomous
.PolytomousKeyListLabelProvider
;
56 import eu
.etaxonomy
.taxeditor
.editor
.l10n
.Messages
;
57 import eu
.etaxonomy
.taxeditor
.model
.DataChangeBridge
;
58 import eu
.etaxonomy
.taxeditor
.model
.IDirtyMarkable
;
59 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasDetails
;
60 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasSupplementalData
;
61 import eu
.etaxonomy
.taxeditor
.workbench
.part
.IE4SavablePart
;
69 public class PolytomousKeyListEditorE4
implements
70 IConversationEnabled
, IDirtyMarkable
, IPartContentHasDetails
, IPartContentHasSupplementalData
,
71 IPolytomousKeyEditorPage
, IE4SavablePart
{
73 private class LinkListener
extends MouseAdapter
{
76 public void mouseUp(MouseEvent event
) {
78 if(event
.button
== 1 && event
.count
== 2) {
79 Table table
= (Table
) event
.widget
;
80 // Determine where the mouse was clicked
81 Point point
= new Point(event
.x
, event
.y
);
83 int selectedColumn
= getSelectedColumn(table
, point
);
85 if (table
== null || point
== null ){
89 TableItem item
= getTableItem(
91 PolytomousKeyNode node
;
93 node
=(PolytomousKeyNode
) item
.getData();
97 if (selectedColumn
== 4) {
98 PolytomousKeyNode linkData
= getItemLinkData(node
);
99 if (linkData
!= null) {
100 viewer
.setSelection(new StructuredSelection(linkData
), true);
103 if (selectedColumn
== 5) {
104 Taxon taxon
= getItemTaxon(node
);
106 EditorUtil
.openTaxonBaseE4((taxon
).getUuid(), modelService
, partService
, application
);
112 private int getSelectedColumn(Table table
, Point point
) {
113 TableItem item
= getTableItem(table
, point
);
115 for (int i
= 0, n
= table
.getColumnCount(); i
< n
; i
++) {
116 Rectangle rect
= item
.getBounds(i
);
117 if (rect
.contains(point
)) {
118 // This is the selected column
126 private TableItem
getTableItem(Table table
, Point point
) {
127 return table
.getItem(point
);
130 private PolytomousKeyNode
getItemLinkData(PolytomousKeyNode node
) {
131 return node
.getChildren().isEmpty() ?
null : node
135 private Taxon
getItemTaxon(PolytomousKeyNode node
) {
136 return node
.getTaxon();
140 public static final String ID
= "eu.etaxonomy.taxeditor.editor.key.polytomous.list"; //$NON-NLS-1$
142 private TableViewer viewer
;
144 private KeyEditorDataChangeBehaviourE4 dataChangeBehavior
;
146 private PolytomousKeyEditorInput input
;
149 private ESelectionService selService
;
152 private MDirtyable dirty
;
155 private MApplication application
;
158 private EModelService modelService
;
161 private EPartService partService
;
163 private ISelectionChangedListener selectionChangedListener
;
166 private MPart thisPart
;
169 public PolytomousKeyListEditorE4() {
173 public void update(CdmDataChangeMap map
) {
174 if(dataChangeBehavior
== null){
175 dataChangeBehavior
= new KeyEditorDataChangeBehaviourE4(this);
177 DataChangeBridge
.handleDataChange(map
, dataChangeBehavior
);
181 public ConversationHolder
getConversationHolder() {
182 return input
.getConversationHolder();
187 public void save(IProgressMonitor monitor
) {
189 monitor
.beginTask(Messages
.KeyEditor_SAVING
, 1);
190 getConversationHolder().bind();
191 getConversationHolder().commit(true);
193 dirty
.setDirty(false);
200 public void init(PolytomousKeyEditorInput input
) {
203 PolytomousKey key
= input
.getKey();
204 key
= HibernateProxyHelper
.deproxy(key
, PolytomousKey
.class);
205 key
.setRoot(HibernateProxyHelper
.deproxy(key
.getRoot(), PolytomousKeyNode
.class));
206 thisPart
.setLabel(key
.getTitleCache());
208 viewer
.setInput(input
);
211 public PolytomousKeyEditorInput
getEditorInput() {
215 public boolean isDirty() {
216 return dirty
.isDirty();
220 public void createPartControl(Composite parent
, EMenuService menuService
) {
222 FillLayout fillLayout
= new FillLayout();
223 fillLayout
.marginWidth
= 0;
224 fillLayout
.marginHeight
= 0;
225 fillLayout
.type
= SWT
.VERTICAL
;
226 parent
.setLayout(fillLayout
);
228 viewer
= new TableViewer(parent
, SWT
.MULTI
| SWT
.H_SCROLL
229 | SWT
.V_SCROLL
| SWT
.FULL_SELECTION
);
231 createColumns(viewer
);
232 viewer
.getControl().addMouseListener(new LinkListener());
233 viewer
.setContentProvider(new PolytomousKeyListContentProvider());
234 viewer
.setLabelProvider(new PolytomousKeyListLabelProvider());
236 //propagate selection
237 selectionChangedListener
= (event
-> selService
.setSelection(event
.getSelection()));
238 viewer
.addSelectionChangedListener(selectionChangedListener
);
240 //create context menu
241 menuService
.registerContextMenu(viewer
.getControl(), "eu.etaxonomy.taxeditor.editor.popupmenu.polytomouskeylisteditor");
246 public void dispose() {
249 input
.getConversationHolder().close();
253 public int getTableItemCount() {
254 if (viewer
!= null && viewer
.getTable() != null) {
255 return viewer
.getTable().getItemCount();
260 public PolytomousKey
getViewerInputKey() {
261 return ((PolytomousKeyEditorInput
) viewer
.getInput()).getKey();
264 // This will create the columns for the table
265 private void createColumns(TableViewer viewer
) {
266 Table table
= viewer
.getTable();
267 String
[] titles
= { Messages
.PolytomousKeyListEditor_NODE
, Messages
.PolytomousKeyListEditor_QUESTION
, Messages
.PolytomousKeyListEditor_EDGE
, Messages
.PolytomousKeyListEditor_STATEMENT
, Messages
.PolytomousKeyListEditor_LINK
, Messages
.PolytomousKeyListEditor_TAXON
};
268 int[] bounds
= { 50, 200, 50, 200, 100, 200 };
270 for (int i
= 0; i
< titles
.length
; i
++) {
271 TableViewerColumn column
= new TableViewerColumn(viewer
, SWT
.NONE
);
272 column
.getColumn().setText(titles
[i
]);
273 column
.getColumn().setWidth(bounds
[i
]);
274 column
.getColumn().setResizable(true);
275 column
.getColumn().setMoveable(true);
277 table
.setHeaderVisible(true);
278 table
.setLinesVisible(false);
283 public void setFocus() {
284 if(viewer
!=null && viewer
.getControl()!=null && !viewer
.getControl().isDisposed()) {
285 viewer
.getControl().setFocus();
293 public void changed(Object element
) {
294 if(element
!= null) {
295 viewer
.update(element
, null);
299 if (element
instanceof PolytomousKeyNode
) {
300 List
<PolytomousKeyNode
> children
= ((PolytomousKeyNode
) element
)
301 .getParent().getChildren();
302 for (PolytomousKeyNode child
: children
) {
303 viewer
.update(child
, null);
306 dirty
.setDirty(true);
312 public void forceDirty() {
317 public boolean postOperation(CdmBase objectAffectedByOperation
) {
320 if (objectAffectedByOperation
!= null) {
321 viewer
.setSelection(new StructuredSelection(
322 objectAffectedByOperation
), true);
328 public boolean onComplete() {
332 public void setPartName() {
333 PolytomousKey key
= input
.getKey();
334 thisPart
.setLabel(key
.getTitleCache());