ref #6925 Set conversation and cdmEntitySession to null for dispose
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / key / polytomous / e4 / PolytomousKeyListEditorE4.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9
10 package eu.etaxonomy.taxeditor.editor.key.polytomous.e4;
11
12 import java.util.List;
13
14 import javax.annotation.PostConstruct;
15 import javax.annotation.PreDestroy;
16 import javax.inject.Inject;
17
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;
41
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;
62
63 /**
64 *
65 * @author pplitzner
66 * @since Sep 28, 2017
67 *
68 */
69 public class PolytomousKeyListEditorE4 implements
70 IConversationEnabled, IDirtyMarkable, IPartContentHasDetails, IPartContentHasSupplementalData,
71 IPolytomousKeyEditorPage, IE4SavablePart{
72
73 private class LinkListener extends MouseAdapter {
74
75 @Override
76 public void mouseUp(MouseEvent event) {
77
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);
82
83 int selectedColumn = getSelectedColumn(table, point);
84
85 if (table == null || point == null ){
86 return;
87 }
88
89 TableItem item = getTableItem(
90 table, point);
91 PolytomousKeyNode node ;
92 if (item != null){
93 node =(PolytomousKeyNode) item.getData();
94 } else{
95 return;
96 }
97 if (selectedColumn == 4) {
98 PolytomousKeyNode linkData = getItemLinkData(node);
99 if (linkData != null) {
100 viewer.setSelection(new StructuredSelection(linkData), true);
101 }
102 }
103 if (selectedColumn == 5) {
104 Taxon taxon = getItemTaxon(node);
105 if (taxon != null) {
106 EditorUtil.openTaxonBaseE4((taxon).getUuid(), modelService, partService, application);
107 }
108 }
109 }
110 }
111
112 private int getSelectedColumn(Table table, Point point) {
113 TableItem item = getTableItem(table, point);
114 if (item != null) {
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
119 return i;
120 }
121 }
122 }
123 return -1;
124 }
125
126 private TableItem getTableItem(Table table, Point point) {
127 return table.getItem(point);
128 }
129
130 private PolytomousKeyNode getItemLinkData(PolytomousKeyNode node) {
131 return node.getChildren().isEmpty() ? null : node
132 .getChildAt(0);
133 }
134
135 private Taxon getItemTaxon(PolytomousKeyNode node) {
136 return node.getTaxon();
137 }
138 }
139
140 public static final String ID = "eu.etaxonomy.taxeditor.editor.key.polytomous.list"; //$NON-NLS-1$
141
142 private TableViewer viewer;
143
144 private KeyEditorDataChangeBehaviourE4 dataChangeBehavior;
145
146 private PolytomousKeyEditorInput input;
147
148 @Inject
149 private ESelectionService selService;
150
151 @Inject
152 private MDirtyable dirty;
153
154 @Inject
155 private MApplication application;
156
157 @Inject
158 private EModelService modelService;
159
160 @Inject
161 private EPartService partService;
162
163 private ISelectionChangedListener selectionChangedListener;
164
165 @Inject
166 private MPart thisPart;
167
168 @Inject
169 public PolytomousKeyListEditorE4() {
170 }
171
172 @Override
173 public void update(CdmDataChangeMap map) {
174 if(dataChangeBehavior == null){
175 dataChangeBehavior = new KeyEditorDataChangeBehaviourE4(this);
176 }
177 DataChangeBridge.handleDataChange(map, dataChangeBehavior);
178 }
179
180 @Override
181 public ConversationHolder getConversationHolder() {
182 return input.getConversationHolder();
183 }
184
185 @Override
186 @Persist
187 public void save(IProgressMonitor monitor) {
188 try {
189 monitor.beginTask(Messages.KeyEditor_SAVING, 1);
190 getConversationHolder().bind();
191 getConversationHolder().commit(true);
192 input.merge();
193 dirty.setDirty(false);
194 monitor.worked(1);
195 } finally {
196 monitor.done();
197 }
198 }
199
200 public void init(PolytomousKeyEditorInput input) {
201 this.input = input;
202
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());
207
208 viewer.setInput(input);
209 }
210
211 public PolytomousKeyEditorInput getEditorInput() {
212 return input;
213 }
214
215 public boolean isDirty() {
216 return dirty.isDirty();
217 }
218
219 @PostConstruct
220 public void createPartControl(Composite parent, EMenuService menuService) {
221
222 FillLayout fillLayout = new FillLayout();
223 fillLayout.marginWidth = 0;
224 fillLayout.marginHeight = 0;
225 fillLayout.type = SWT.VERTICAL;
226 parent.setLayout(fillLayout);
227
228 viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
229 | SWT.V_SCROLL | SWT.FULL_SELECTION);
230
231 createColumns(viewer);
232 viewer.getControl().addMouseListener(new LinkListener());
233 viewer.setContentProvider(new PolytomousKeyListContentProvider());
234 viewer.setLabelProvider(new PolytomousKeyListLabelProvider());
235
236 //propagate selection
237 selectionChangedListener = (event -> selService.setSelection(event.getSelection()));
238 viewer.addSelectionChangedListener(selectionChangedListener);
239
240 //create context menu
241 menuService.registerContextMenu(viewer.getControl(), "eu.etaxonomy.taxeditor.editor.popupmenu.polytomouskeylisteditor");
242 }
243
244
245 @PreDestroy
246 public void dispose() {
247 if(input!=null){
248 input.dispose();
249 input.getConversationHolder().close();
250 }
251 }
252
253 public int getTableItemCount() {
254 if (viewer != null && viewer.getTable() != null) {
255 return viewer.getTable().getItemCount();
256 }
257 return 0;
258 }
259
260 public PolytomousKey getViewerInputKey() {
261 return ((PolytomousKeyEditorInput) viewer.getInput()).getKey();
262 }
263
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 };
269
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);
276 }
277 table.setHeaderVisible(true);
278 table.setLinesVisible(false);
279
280 }
281
282 @Focus
283 public void setFocus() {
284 if(viewer!=null && viewer.getControl()!=null && !viewer.getControl().isDisposed()) {
285 viewer.getControl().setFocus();
286 }
287 if(input!=null){
288 input.bind();
289 }
290 }
291
292 @Override
293 public void changed(Object element) {
294 if(element != null) {
295 viewer.update(element, null);
296
297 }
298
299 if (element instanceof PolytomousKeyNode) {
300 List<PolytomousKeyNode> children = ((PolytomousKeyNode) element)
301 .getParent().getChildren();
302 for (PolytomousKeyNode child : children) {
303 viewer.update(child, null);
304 }
305 }
306 dirty.setDirty(true);
307 viewer.refresh();
308
309 }
310
311 @Override
312 public void forceDirty() {
313 changed(null);
314 }
315
316 @Override
317 public boolean postOperation(CdmBase objectAffectedByOperation) {
318 viewer.refresh();
319
320 if (objectAffectedByOperation != null) {
321 viewer.setSelection(new StructuredSelection(
322 objectAffectedByOperation), true);
323 }
324 return true;
325 }
326
327 @Override
328 public boolean onComplete() {
329 return true;
330 }
331
332 public void setPartName() {
333 PolytomousKey key = input.getKey();
334 thisPart.setLabel(key.getTitleCache());
335 }
336
337 }