Project

General

Profile

Download (9.29 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.KeyEditorDataChangeBehaviour;
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.IDirtyMarkable;
55
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
56
import eu.etaxonomy.taxeditor.model.MessagingUtils;
57

    
58
/**
59
 *
60
 * @author pplitzner
61
 * @since Sep 28, 2017
62
 *
63
 */
64
public class PolytomousKeyListEditorE4 implements
65
		IConversationEnabled, IDirtyMarkable, IPartContentHasDetails,
66
		IPolytomousKeyEditorPage {
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
		                try {
102
		                    EditorUtil.openTaxonBaseE4((taxon).getUuid());
103
		                } catch (PartInitException e) {
104
		                    MessagingUtils.error(getClass(), e);
105
		                }
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 ConversationHolder conversation;
144

    
145
    private KeyEditorDataChangeBehaviour 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 arg0) {
166

    
167
	}
168

    
169
	@Override
170
	public ConversationHolder getConversationHolder() {
171
		return input.getConversationHolder();
172
	}
173

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

    
188
	public void init(PolytomousKeyEditorInput input) {
189
		this.input = input;
190

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

    
196
        viewer.setInput(input);
197
	}
198

    
199
	public boolean isDirty() {
200
		return dirty.isDirty();
201
	}
202

    
203
	@PostConstruct
204
	public void createPartControl(Composite parent, EMenuService menuService) {
205

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

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

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

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

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

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

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

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

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

    
255
	}
256

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

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

    
272
        }
273

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

    
284
	}
285

    
286
    @Override
287
    public void forceDirty() {
288
        changed(null);
289
    }
290

    
291
	@Override
292
	public boolean postOperation(CdmBase objectAffectedByOperation) {
293
		viewer.refresh();
294
		getConversationHolder().bind();
295
		getConversationHolder().commit(true);
296
		//FIXME E4 migrate/delete
297
//		editor.changed(objectAffectedByOperation);
298

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

    
306
	@Override
307
	public boolean onComplete() {
308
		return true;
309
	}
310

    
311
}
(2-2/2)