Project

General

Profile

Download (9.16 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
package eu.etaxonomy.taxeditor.editor.key.polytomous.e4;
10

    
11
import java.util.List;
12

    
13
import javax.annotation.PostConstruct;
14
import javax.annotation.PreDestroy;
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.hibernate.HibernateProxyHelper;
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.taxeditor.editor.EditorUtil;
46
import eu.etaxonomy.taxeditor.editor.key.polytomous.IPolytomousKeyEditorPage;
47
import eu.etaxonomy.taxeditor.editor.key.polytomous.PolytomousKeyEditorInput;
48
import eu.etaxonomy.taxeditor.editor.key.polytomous.PolytomousKeyListContentProvider;
49
import eu.etaxonomy.taxeditor.editor.key.polytomous.PolytomousKeyListLabelProvider;
50
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
51
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
52
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
53
import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
54
import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
55

    
56
/**
57
 * @author pplitzner
58
 * @since Sep 28, 2017
59
 */
60
public class PolytomousKeyListEditorE4 implements
61
		IDirtyMarkable, IPartContentHasDetails, IPartContentHasSupplementalData,
62
		IPolytomousKeyEditorPage, IE4SavablePart{
63

    
64
	private class LinkListener extends MouseAdapter {
65

    
66
		@Override
67
		public void mouseUp(MouseEvent event) {
68

    
69
	    		if(event.button == 1 && event.count == 2) {
70
		        Table table = (Table) event.widget;
71
		        // Determine where the mouse was clicked
72
		        Point point = new Point(event.x, event.y);
73

    
74
		        int selectedColumn = getSelectedColumn(table, point);
75

    
76
		        if (table == null){
77
		            return;
78
		        }
79

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

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

    
117
		private TableItem getTableItem(Table table, Point point) {
118
			return table.getItem(point);
119
		}
120

    
121
		private PolytomousKeyNode getItemLinkData(PolytomousKeyNode node) {
122
			return node.getChildren().isEmpty() ? null : node
123
					.getChildAt(0);
124
		}
125

    
126
        private Taxon getItemTaxon(PolytomousKeyNode node) {
127
            return node.getTaxon();
128
        }
129
	}
130

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

    
133
	private TableViewer viewer;
134

    
135
	private PolytomousKeyEditorInput input;
136

    
137
    @Inject
138
    private ESelectionService selService;
139

    
140
    @Inject
141
    private MDirtyable dirty;
142

    
143
    @Inject
144
    private MApplication application;
145

    
146
    @Inject
147
    private EModelService modelService;
148

    
149
    @Inject
150
    private EPartService partService;
151

    
152
    private ISelectionChangedListener selectionChangedListener;
153

    
154
    @Inject
155
    private MPart thisPart;
156

    
157
    @Inject
158
	public PolytomousKeyListEditorE4() {
159
	}
160
	
161
	@Override
162
    @Persist
163
	public void save(IProgressMonitor monitor) {
164
        try {
165
            monitor.beginTask(Messages.KeyEditor_SAVING, 1);
166
            input.merge();
167
            dirty.setDirty(false);
168
            monitor.worked(1);
169
        } finally {
170
            monitor.done();
171
        }
172
    }
173

    
174
	public void init(PolytomousKeyEditorInput input) {
175
		this.input = input;
176

    
177
        PolytomousKey key = input.getKey();
178
        key = HibernateProxyHelper.deproxy(key);
179
        key.setRoot(HibernateProxyHelper.deproxy(key.getRoot()));
180
        thisPart.setLabel(key.getTitleCache());
181

    
182
        viewer.setInput(input);
183
	}
184

    
185
    public PolytomousKeyEditorInput getEditorInput() {
186
        return input;
187
    }
188

    
189
	@Override
190
    public boolean isDirty() {
191
		return dirty.isDirty();
192
	}
193

    
194
	@PostConstruct
195
	public void createPartControl(Composite parent, EMenuService menuService) {
196

    
197
		FillLayout fillLayout = new FillLayout();
198
		fillLayout.marginWidth = 0;
199
		fillLayout.marginHeight = 0;
200
		fillLayout.type = SWT.VERTICAL;
201
		parent.setLayout(fillLayout);
202

    
203
		viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
204
				| SWT.V_SCROLL | SWT.FULL_SELECTION);
205

    
206
		createColumns(viewer);
207
		viewer.getControl().addMouseListener(new LinkListener());
208
		viewer.setContentProvider(new PolytomousKeyListContentProvider());
209
		viewer.setLabelProvider(new PolytomousKeyListLabelProvider());
210

    
211
		//propagate selection
212
        selectionChangedListener = (event -> selService.setSelection(event.getSelection()));
213
        viewer.addSelectionChangedListener(selectionChangedListener);
214

    
215
        //create context menu
216
        menuService.registerContextMenu(viewer.getControl(), "eu.etaxonomy.taxeditor.editor.popupmenu.polytomouskeylisteditor");
217
	}
218

    
219

    
220
    @PreDestroy
221
    public void dispose() {
222
        if(input!=null){
223
            input.dispose();
224
        }
225
    }
226

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

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

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

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

    
254
	}
255

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

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

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

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

    
290
	@Override
291
	public boolean postOperation(Object objectAffectedByOperation) {
292
		viewer.refresh();
293

    
294
		if (objectAffectedByOperation != null) {
295
			viewer.setSelection(new StructuredSelection(
296
					objectAffectedByOperation), true);
297
		}
298
		return true;
299
	}
300

    
301
	@Override
302
	public boolean onComplete() {
303
		return true;
304
	}
305

    
306
    public void setPartName() {
307
        PolytomousKey key = input.getKey();
308
        thisPart.setLabel(key.getTitleCache());
309
    }
310

    
311
}
(2-2/2)