Project

General

Profile

Download (9.76 KB) Statistics
| Branch: | Tag: | Revision:
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.inject.Inject;
16

    
17
import org.eclipse.core.runtime.IProgressMonitor;
18
import org.eclipse.e4.ui.di.Focus;
19
import org.eclipse.e4.ui.di.Persist;
20
import org.eclipse.e4.ui.model.application.ui.MDirtyable;
21
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
22
import org.eclipse.e4.ui.services.EMenuService;
23
import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
24
import org.eclipse.jface.viewers.ISelectionChangedListener;
25
import org.eclipse.jface.viewers.StructuredSelection;
26
import org.eclipse.jface.viewers.TableViewer;
27
import org.eclipse.jface.viewers.TableViewerColumn;
28
import org.eclipse.swt.SWT;
29
import org.eclipse.swt.events.MouseAdapter;
30
import org.eclipse.swt.events.MouseEvent;
31
import org.eclipse.swt.graphics.Point;
32
import org.eclipse.swt.graphics.Rectangle;
33
import org.eclipse.swt.layout.FillLayout;
34
import org.eclipse.swt.widgets.Composite;
35
import org.eclipse.swt.widgets.Table;
36
import org.eclipse.swt.widgets.TableItem;
37
import org.eclipse.ui.PartInitException;
38

    
39
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
40
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
41
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
42
import eu.etaxonomy.cdm.model.common.CdmBase;
43
import eu.etaxonomy.cdm.model.description.PolytomousKey;
44
import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
45
import eu.etaxonomy.cdm.model.taxon.Taxon;
46
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
47
import eu.etaxonomy.taxeditor.editor.EditorUtil;
48
import eu.etaxonomy.taxeditor.editor.key.e4.KeyEditorDataChangeBehaviourE4;
49
import eu.etaxonomy.taxeditor.editor.key.polytomous.IPolytomousKeyEditorPage;
50
import eu.etaxonomy.taxeditor.editor.key.polytomous.PolytomousKeyEditorInput;
51
import eu.etaxonomy.taxeditor.editor.key.polytomous.PolytomousKeyListContentProvider;
52
import eu.etaxonomy.taxeditor.editor.key.polytomous.PolytomousKeyListLabelProvider;
53
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
54
import eu.etaxonomy.taxeditor.model.DataChangeBridge;
55
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
56
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
57
import eu.etaxonomy.taxeditor.model.MessagingUtils;
58
import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
59

    
60
/**
61
 *
62
 * @author pplitzner
63
 * @since Sep 28, 2017
64
 *
65
 */
66
public class PolytomousKeyListEditorE4 implements
67
		IConversationEnabled, IDirtyMarkable, IPartContentHasDetails,
68
		IPolytomousKeyEditorPage, IE4SavablePart{
69

    
70
	private class LinkListener extends MouseAdapter {
71

    
72
		@Override
73
		public void mouseUp(MouseEvent event) {
74

    
75
	    		if(event.button == 1 && event.count == 2) {
76
		        Table table = (Table) event.widget;
77
		        // Determine where the mouse was clicked
78
		        Point point = new Point(event.x, event.y);
79

    
80
		        int selectedColumn = getSelectedColumn(table, point);
81

    
82
		        if (table == null || point == null ){
83
		            return;
84
		        }
85

    
86
		       TableItem item = getTableItem(
87
                        table, point);
88
		       PolytomousKeyNode node ;
89
		       if (item != null){
90
		         node =(PolytomousKeyNode) item.getData();
91
		       } else{
92
		           return;
93
		       }
94
		        if (selectedColumn == 4) {
95
		            PolytomousKeyNode linkData = getItemLinkData(node);
96
		            if (linkData != null) {
97
		                viewer.setSelection(new StructuredSelection(linkData), true);
98
		            }
99
		        }
100
		        if (selectedColumn == 5) {
101
		            Taxon taxon = getItemTaxon(node);
102
		            if (taxon != null) {
103
		                try {
104
		                    EditorUtil.openTaxonBaseE4((taxon).getUuid());
105
		                } catch (PartInitException e) {
106
		                    MessagingUtils.error(getClass(), e);
107
		                }
108
		            }
109
		        }
110
		    }
111
		}
112

    
113
		private int getSelectedColumn(Table table, Point point) {
114
			TableItem item = getTableItem(table, point);
115
			if (item != null) {
116
				for (int i = 0, n = table.getColumnCount(); i < n; i++) {
117
					Rectangle rect = item.getBounds(i);
118
					if (rect.contains(point)) {
119
						// This is the selected column
120
						return i;
121
					}
122
				}
123
			}
124
			return -1;
125
		}
126

    
127
		private TableItem getTableItem(Table table, Point point) {
128
			return table.getItem(point);
129
		}
130

    
131
		private PolytomousKeyNode getItemLinkData(PolytomousKeyNode node) {
132
			return node.getChildren().isEmpty() ? null : node
133
					.getChildAt(0);
134
		}
135

    
136
        private Taxon getItemTaxon(PolytomousKeyNode node) {
137
            return node.getTaxon();
138
        }
139
	}
140

    
141
	public static final String ID = "eu.etaxonomy.taxeditor.editor.key.polytomous.list"; //$NON-NLS-1$
142

    
143
	private TableViewer viewer;
144

    
145
    private KeyEditorDataChangeBehaviourE4 dataChangeBehavior;
146

    
147
    private PolytomousKeyEditorInput input;
148

    
149
    @Inject
150
    private ESelectionService selService;
151

    
152
    @Inject
153
    private MDirtyable dirty;
154

    
155
    private ISelectionChangedListener selectionChangedListener;
156

    
157
    @Inject
158
    private MPart thisPart;
159

    
160
    @Inject
161
	public PolytomousKeyListEditorE4() {
162
	}
163

    
164
	@Override
165
	public void update(CdmDataChangeMap map) {
166
	    if(dataChangeBehavior == null){
167
            dataChangeBehavior = new KeyEditorDataChangeBehaviourE4(this);
168
        }
169
        DataChangeBridge.handleDataChange(map, dataChangeBehavior);
170
	}
171

    
172
	@Override
173
	public ConversationHolder getConversationHolder() {
174
		return input.getConversationHolder();
175
	}
176

    
177
	@Override
178
    @Persist
179
	public void save(IProgressMonitor monitor) {
180
        try {
181
            monitor.beginTask(Messages.KeyEditor_SAVING, 1);
182
            getConversationHolder().bind();
183
            getConversationHolder().commit(true);
184
            input.merge();
185
            dirty.setDirty(false);
186
            monitor.worked(1);
187
        } finally {
188
            monitor.done();
189
        }
190
    }
191

    
192
	public void init(PolytomousKeyEditorInput input) {
193
		this.input = input;
194

    
195
        PolytomousKey key = input.getKey();
196
        key = HibernateProxyHelper.deproxy(key, PolytomousKey.class);
197
        key.setRoot(HibernateProxyHelper.deproxy(key.getRoot(), PolytomousKeyNode.class));
198
        thisPart.setLabel(key.getTitleCache());
199

    
200
        viewer.setInput(input);
201
	}
202

    
203
    public PolytomousKeyEditorInput getEditorInput() {
204
        return input;
205
    }
206

    
207
	public boolean isDirty() {
208
		return dirty.isDirty();
209
	}
210

    
211
	@PostConstruct
212
	public void createPartControl(Composite parent, EMenuService menuService) {
213

    
214
		FillLayout fillLayout = new FillLayout();
215
		fillLayout.marginWidth = 0;
216
		fillLayout.marginHeight = 0;
217
		fillLayout.type = SWT.VERTICAL;
218
		parent.setLayout(fillLayout);
219

    
220
		viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
221
				| SWT.V_SCROLL | SWT.FULL_SELECTION);
222

    
223
		createColumns(viewer);
224
		viewer.getControl().addMouseListener(new LinkListener());
225
		viewer.setContentProvider(new PolytomousKeyListContentProvider());
226
		viewer.setLabelProvider(new PolytomousKeyListLabelProvider());
227

    
228
		//propagate selection
229
        selectionChangedListener = (event -> selService.setSelection(event.getSelection()));
230
        viewer.addSelectionChangedListener(selectionChangedListener);
231

    
232
        //create context menu
233
        menuService.registerContextMenu(viewer.getControl(), "eu.etaxonomy.taxeditor.editor.popupmenu.polytomouskeylisteditor");
234
	}
235

    
236
	public int getTableItemCount() {
237
	    if (viewer != null && viewer.getTable() != null) {
238
	        return viewer.getTable().getItemCount();
239
	    }
240
	    return 0;
241
	}
242

    
243
	public PolytomousKey getViewerInputKey() {
244
	    return ((PolytomousKeyEditorInput) viewer.getInput()).getKey();
245
	}
246

    
247
	// This will create the columns for the table
248
	private void createColumns(TableViewer viewer) {
249
		Table table = viewer.getTable();
250
		String[] titles = { Messages.PolytomousKeyListEditor_NODE, Messages.PolytomousKeyListEditor_QUESTION, Messages.PolytomousKeyListEditor_EDGE,  Messages.PolytomousKeyListEditor_STATEMENT, Messages.PolytomousKeyListEditor_LINK, Messages.PolytomousKeyListEditor_TAXON };
251
		int[] bounds = { 50, 200, 50, 200, 100, 200 };
252

    
253
		for (int i = 0; i < titles.length; i++) {
254
			TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
255
			column.getColumn().setText(titles[i]);
256
			column.getColumn().setWidth(bounds[i]);
257
			column.getColumn().setResizable(true);
258
			column.getColumn().setMoveable(true);
259
		}
260
		table.setHeaderVisible(true);
261
		table.setLinesVisible(false);
262

    
263
	}
264

    
265
	@Focus
266
	public void setFocus() {
267
	    if(viewer!=null && viewer.getControl()!=null){
268
	        viewer.getControl().setFocus();
269
	    }
270
	    if(input!=null){
271
	        input.bind();
272
	    }
273
	}
274

    
275
	@Override
276
	public void changed(Object element) {
277
        if(element != null) {
278
            viewer.update(element, null);
279

    
280
        }
281

    
282
        if (element instanceof PolytomousKeyNode) {
283
            List<PolytomousKeyNode> children = ((PolytomousKeyNode) element)
284
                    .getParent().getChildren();
285
            for (PolytomousKeyNode child : children) {
286
                viewer.update(child, null);
287
            }
288
        }
289
        dirty.setDirty(true);
290
        viewer.refresh();
291

    
292
	}
293

    
294
    @Override
295
    public void forceDirty() {
296
        changed(null);
297
    }
298

    
299
	@Override
300
	public boolean postOperation(CdmBase objectAffectedByOperation) {
301
		viewer.refresh();
302
//		getConversationHolder().bind();
303
//		getConversationHolder().commit(true);
304
		changed(objectAffectedByOperation);
305

    
306
		if (objectAffectedByOperation != null) {
307
			viewer.setSelection(new StructuredSelection(
308
					objectAffectedByOperation), true);
309
		}
310
		return true;
311
	}
312

    
313
	@Override
314
	public boolean onComplete() {
315
		return true;
316
	}
317

    
318
    public void setPartName() {
319
        PolytomousKey key = input.getKey();
320
        thisPart.setLabel(key.getTitleCache());
321
    }
322

    
323
}
(2-2/2)