Project

General

Profile

Download (9.43 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

    
38
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
39
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
40
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
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.e4.KeyEditorDataChangeBehaviourE4;
48
import eu.etaxonomy.taxeditor.editor.key.polytomous.IPolytomousKeyEditorPage;
49
import eu.etaxonomy.taxeditor.editor.key.polytomous.PolytomousKeyEditorInput;
50
import eu.etaxonomy.taxeditor.editor.key.polytomous.PolytomousKeyListContentProvider;
51
import eu.etaxonomy.taxeditor.editor.key.polytomous.PolytomousKeyListLabelProvider;
52
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
53
import eu.etaxonomy.taxeditor.model.DataChangeBridge;
54
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
55
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
56
import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
57

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

    
68
	private class LinkListener extends MouseAdapter {
69

    
70
		@Override
71
		public void mouseUp(MouseEvent event) {
72

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

    
78
		        int selectedColumn = getSelectedColumn(table, point);
79

    
80
		        if (table == null || point == null ){
81
		            return;
82
		        }
83

    
84
		       TableItem item = getTableItem(
85
                        table, point);
86
		       PolytomousKeyNode node ;
87
		       if (item != null){
88
		         node =(PolytomousKeyNode) item.getData();
89
		       } else{
90
		           return;
91
		       }
92
		        if (selectedColumn == 4) {
93
		            PolytomousKeyNode linkData = getItemLinkData(node);
94
		            if (linkData != null) {
95
		                viewer.setSelection(new StructuredSelection(linkData), true);
96
		            }
97
		        }
98
		        if (selectedColumn == 5) {
99
		            Taxon taxon = getItemTaxon(node);
100
		            if (taxon != null) {
101
		                EditorUtil.openTaxonBaseE4((taxon).getUuid());
102
		            }
103
		        }
104
		    }
105
		}
106

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

    
121
		private TableItem getTableItem(Table table, Point point) {
122
			return table.getItem(point);
123
		}
124

    
125
		private PolytomousKeyNode getItemLinkData(PolytomousKeyNode node) {
126
			return node.getChildren().isEmpty() ? null : node
127
					.getChildAt(0);
128
		}
129

    
130
        private Taxon getItemTaxon(PolytomousKeyNode node) {
131
            return node.getTaxon();
132
        }
133
	}
134

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

    
137
	private TableViewer viewer;
138

    
139
    private KeyEditorDataChangeBehaviourE4 dataChangeBehavior;
140

    
141
    private PolytomousKeyEditorInput input;
142

    
143
    @Inject
144
    private ESelectionService selService;
145

    
146
    @Inject
147
    private MDirtyable dirty;
148

    
149
    private ISelectionChangedListener selectionChangedListener;
150

    
151
    @Inject
152
    private MPart thisPart;
153

    
154
    @Inject
155
	public PolytomousKeyListEditorE4() {
156
	}
157

    
158
	@Override
159
	public void update(CdmDataChangeMap map) {
160
	    if(dataChangeBehavior == null){
161
            dataChangeBehavior = new KeyEditorDataChangeBehaviourE4(this);
162
        }
163
        DataChangeBridge.handleDataChange(map, dataChangeBehavior);
164
	}
165

    
166
	@Override
167
	public ConversationHolder getConversationHolder() {
168
		return input.getConversationHolder();
169
	}
170

    
171
	@Override
172
    @Persist
173
	public void save(IProgressMonitor monitor) {
174
        try {
175
            monitor.beginTask(Messages.KeyEditor_SAVING, 1);
176
            getConversationHolder().bind();
177
            getConversationHolder().commit(true);
178
            input.merge();
179
            dirty.setDirty(false);
180
            monitor.worked(1);
181
        } finally {
182
            monitor.done();
183
        }
184
    }
185

    
186
	public void init(PolytomousKeyEditorInput input) {
187
		this.input = input;
188

    
189
        PolytomousKey key = input.getKey();
190
        key = HibernateProxyHelper.deproxy(key, PolytomousKey.class);
191
        key.setRoot(HibernateProxyHelper.deproxy(key.getRoot(), PolytomousKeyNode.class));
192
        thisPart.setLabel(key.getTitleCache());
193

    
194
        viewer.setInput(input);
195
	}
196

    
197
    public PolytomousKeyEditorInput getEditorInput() {
198
        return input;
199
    }
200

    
201
	public boolean isDirty() {
202
		return dirty.isDirty();
203
	}
204

    
205
	@PostConstruct
206
	public void createPartControl(Composite parent, EMenuService menuService) {
207

    
208
		FillLayout fillLayout = new FillLayout();
209
		fillLayout.marginWidth = 0;
210
		fillLayout.marginHeight = 0;
211
		fillLayout.type = SWT.VERTICAL;
212
		parent.setLayout(fillLayout);
213

    
214
		viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
215
				| SWT.V_SCROLL | SWT.FULL_SELECTION);
216

    
217
		createColumns(viewer);
218
		viewer.getControl().addMouseListener(new LinkListener());
219
		viewer.setContentProvider(new PolytomousKeyListContentProvider());
220
		viewer.setLabelProvider(new PolytomousKeyListLabelProvider());
221

    
222
		//propagate selection
223
        selectionChangedListener = (event -> selService.setSelection(event.getSelection()));
224
        viewer.addSelectionChangedListener(selectionChangedListener);
225

    
226
        //create context menu
227
        menuService.registerContextMenu(viewer.getControl(), "eu.etaxonomy.taxeditor.editor.popupmenu.polytomouskeylisteditor");
228
	}
229

    
230
	public int getTableItemCount() {
231
	    if (viewer != null && viewer.getTable() != null) {
232
	        return viewer.getTable().getItemCount();
233
	    }
234
	    return 0;
235
	}
236

    
237
	public PolytomousKey getViewerInputKey() {
238
	    return ((PolytomousKeyEditorInput) viewer.getInput()).getKey();
239
	}
240

    
241
	// This will create the columns for the table
242
	private void createColumns(TableViewer viewer) {
243
		Table table = viewer.getTable();
244
		String[] titles = { Messages.PolytomousKeyListEditor_NODE, Messages.PolytomousKeyListEditor_QUESTION, Messages.PolytomousKeyListEditor_EDGE,  Messages.PolytomousKeyListEditor_STATEMENT, Messages.PolytomousKeyListEditor_LINK, Messages.PolytomousKeyListEditor_TAXON };
245
		int[] bounds = { 50, 200, 50, 200, 100, 200 };
246

    
247
		for (int i = 0; i < titles.length; i++) {
248
			TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
249
			column.getColumn().setText(titles[i]);
250
			column.getColumn().setWidth(bounds[i]);
251
			column.getColumn().setResizable(true);
252
			column.getColumn().setMoveable(true);
253
		}
254
		table.setHeaderVisible(true);
255
		table.setLinesVisible(false);
256

    
257
	}
258

    
259
	@Focus
260
	public void setFocus() {
261
	    if(viewer!=null && viewer.getControl()!=null){
262
	        viewer.getControl().setFocus();
263
	    }
264
	    if(input!=null){
265
	        input.bind();
266
	    }
267
	}
268

    
269
	@Override
270
	public void changed(Object element) {
271
        if(element != null) {
272
            viewer.update(element, null);
273

    
274
        }
275

    
276
        if (element instanceof PolytomousKeyNode) {
277
            List<PolytomousKeyNode> children = ((PolytomousKeyNode) element)
278
                    .getParent().getChildren();
279
            for (PolytomousKeyNode child : children) {
280
                viewer.update(child, null);
281
            }
282
        }
283
        dirty.setDirty(true);
284
        viewer.refresh();
285

    
286
	}
287

    
288
    @Override
289
    public void forceDirty() {
290
        changed(null);
291
    }
292

    
293
	@Override
294
	public boolean postOperation(CdmBase objectAffectedByOperation) {
295
		viewer.refresh();
296

    
297
		if (objectAffectedByOperation != null) {
298
			viewer.setSelection(new StructuredSelection(
299
					objectAffectedByOperation), true);
300
		}
301
		return true;
302
	}
303

    
304
	@Override
305
	public boolean onComplete() {
306
		return true;
307
	}
308

    
309
    public void setPartName() {
310
        PolytomousKey key = input.getKey();
311
        thisPart.setLabel(key.getTitleCache());
312
    }
313

    
314
}
(2-2/2)