Project

General

Profile

Download (9.9 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.MApplication;
21
import org.eclipse.e4.ui.model.application.ui.MDirtyable;
22
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
23
import org.eclipse.e4.ui.services.EMenuService;
24
import org.eclipse.e4.ui.workbench.modeling.EModelService;
25
import org.eclipse.e4.ui.workbench.modeling.EPartService;
26
import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
27
import org.eclipse.jface.viewers.ISelectionChangedListener;
28
import org.eclipse.jface.viewers.StructuredSelection;
29
import org.eclipse.jface.viewers.TableViewer;
30
import org.eclipse.jface.viewers.TableViewerColumn;
31
import org.eclipse.swt.SWT;
32
import org.eclipse.swt.events.MouseAdapter;
33
import org.eclipse.swt.events.MouseEvent;
34
import org.eclipse.swt.graphics.Point;
35
import org.eclipse.swt.graphics.Rectangle;
36
import org.eclipse.swt.layout.FillLayout;
37
import org.eclipse.swt.widgets.Composite;
38
import org.eclipse.swt.widgets.Table;
39
import org.eclipse.swt.widgets.TableItem;
40

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

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

    
72
	private class LinkListener extends MouseAdapter {
73

    
74
		@Override
75
		public void mouseUp(MouseEvent event) {
76

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

    
82
		        int selectedColumn = getSelectedColumn(table, point);
83

    
84
		        if (table == null || point == null ){
85
		            return;
86
		        }
87

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

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

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

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

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

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

    
141
	private TableViewer viewer;
142

    
143
    private KeyEditorDataChangeBehaviourE4 dataChangeBehavior;
144

    
145
    private PolytomousKeyEditorInput input;
146

    
147
    @Inject
148
    private ESelectionService selService;
149

    
150
    @Inject
151
    private MDirtyable dirty;
152

    
153
    @Inject
154
    private MApplication application;
155

    
156
    @Inject
157
    private EModelService modelService;
158

    
159
    @Inject
160
    private EPartService partService;
161

    
162
    private ISelectionChangedListener selectionChangedListener;
163

    
164
    @Inject
165
    private MPart thisPart;
166

    
167
    @Inject
168
	public PolytomousKeyListEditorE4() {
169
	}
170

    
171
	@Override
172
	public void update(CdmDataChangeMap map) {
173
	    if(dataChangeBehavior == null){
174
            dataChangeBehavior = new KeyEditorDataChangeBehaviourE4(this);
175
        }
176
        DataChangeBridge.handleDataChange(map, dataChangeBehavior);
177
	}
178

    
179
	@Override
180
	public ConversationHolder getConversationHolder() {
181
		return input.getConversationHolder();
182
	}
183

    
184
	@Override
185
    @Persist
186
	public void save(IProgressMonitor monitor) {
187
        try {
188
            monitor.beginTask(Messages.KeyEditor_SAVING, 1);
189
            getConversationHolder().bind();
190
            getConversationHolder().commit(true);
191
            input.merge();
192
            dirty.setDirty(false);
193
            monitor.worked(1);
194
        } finally {
195
            monitor.done();
196
        }
197
    }
198

    
199
	public void init(PolytomousKeyEditorInput input) {
200
		this.input = input;
201

    
202
        PolytomousKey key = input.getKey();
203
        key = HibernateProxyHelper.deproxy(key, PolytomousKey.class);
204
        key.setRoot(HibernateProxyHelper.deproxy(key.getRoot(), PolytomousKeyNode.class));
205
        thisPart.setLabel(key.getTitleCache());
206

    
207
        viewer.setInput(input);
208
	}
209

    
210
    public PolytomousKeyEditorInput getEditorInput() {
211
        return input;
212
    }
213

    
214
	public boolean isDirty() {
215
		return dirty.isDirty();
216
	}
217

    
218
	@PostConstruct
219
	public void createPartControl(Composite parent, EMenuService menuService) {
220

    
221
		FillLayout fillLayout = new FillLayout();
222
		fillLayout.marginWidth = 0;
223
		fillLayout.marginHeight = 0;
224
		fillLayout.type = SWT.VERTICAL;
225
		parent.setLayout(fillLayout);
226

    
227
		viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
228
				| SWT.V_SCROLL | SWT.FULL_SELECTION);
229

    
230
		createColumns(viewer);
231
		viewer.getControl().addMouseListener(new LinkListener());
232
		viewer.setContentProvider(new PolytomousKeyListContentProvider());
233
		viewer.setLabelProvider(new PolytomousKeyListLabelProvider());
234

    
235
		//propagate selection
236
        selectionChangedListener = (event -> selService.setSelection(event.getSelection()));
237
        viewer.addSelectionChangedListener(selectionChangedListener);
238

    
239
        //create context menu
240
        menuService.registerContextMenu(viewer.getControl(), "eu.etaxonomy.taxeditor.editor.popupmenu.polytomouskeylisteditor");
241
	}
242

    
243
	public int getTableItemCount() {
244
	    if (viewer != null && viewer.getTable() != null) {
245
	        return viewer.getTable().getItemCount();
246
	    }
247
	    return 0;
248
	}
249

    
250
	public PolytomousKey getViewerInputKey() {
251
	    return ((PolytomousKeyEditorInput) viewer.getInput()).getKey();
252
	}
253

    
254
	// This will create the columns for the table
255
	private void createColumns(TableViewer viewer) {
256
		Table table = viewer.getTable();
257
		String[] titles = { Messages.PolytomousKeyListEditor_NODE, Messages.PolytomousKeyListEditor_QUESTION, Messages.PolytomousKeyListEditor_EDGE,  Messages.PolytomousKeyListEditor_STATEMENT, Messages.PolytomousKeyListEditor_LINK, Messages.PolytomousKeyListEditor_TAXON };
258
		int[] bounds = { 50, 200, 50, 200, 100, 200 };
259

    
260
		for (int i = 0; i < titles.length; i++) {
261
			TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
262
			column.getColumn().setText(titles[i]);
263
			column.getColumn().setWidth(bounds[i]);
264
			column.getColumn().setResizable(true);
265
			column.getColumn().setMoveable(true);
266
		}
267
		table.setHeaderVisible(true);
268
		table.setLinesVisible(false);
269

    
270
	}
271

    
272
	@Focus
273
	public void setFocus() {
274
	    if(viewer!=null && viewer.getControl()!=null && !viewer.getControl().isDisposed()) {
275
	        viewer.getControl().setFocus();
276
	    }
277
	    if(input!=null){
278
	        input.bind();
279
	    }
280
	}
281

    
282
	@Override
283
	public void changed(Object element) {
284
        if(element != null) {
285
            viewer.update(element, null);
286

    
287
        }
288

    
289
        if (element instanceof PolytomousKeyNode) {
290
            List<PolytomousKeyNode> children = ((PolytomousKeyNode) element)
291
                    .getParent().getChildren();
292
            for (PolytomousKeyNode child : children) {
293
                viewer.update(child, null);
294
            }
295
        }
296
        dirty.setDirty(true);
297
        viewer.refresh();
298

    
299
	}
300

    
301
    @Override
302
    public void forceDirty() {
303
        changed(null);
304
    }
305

    
306
	@Override
307
	public boolean postOperation(CdmBase objectAffectedByOperation) {
308
		viewer.refresh();
309

    
310
		if (objectAffectedByOperation != null) {
311
			viewer.setSelection(new StructuredSelection(
312
					objectAffectedByOperation), true);
313
		}
314
		return true;
315
	}
316

    
317
	@Override
318
	public boolean onComplete() {
319
		return true;
320
	}
321

    
322
    public void setPartName() {
323
        PolytomousKey key = input.getKey();
324
        thisPart.setLabel(key.getTitleCache());
325
    }
326

    
327
}
(2-2/2)