Project

General

Profile

Download (9.91 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
 * Copyright (C) 2007 EDIT
4
 * European Distributed Institute of Taxonomy
5
 * http://www.e-taxonomy.eu
6
 *
7
 * The contents of this file are subject to the Mozilla Public License Version 1.1
8
 * See LICENSE.TXT at the top of this package for the full license terms.
9
 */
10

    
11
package eu.etaxonomy.taxeditor.editor.key.polytomous;
12

    
13
import java.util.List;
14

    
15
import org.eclipse.core.runtime.IProgressMonitor;
16
import org.eclipse.jface.action.GroupMarker;
17
import org.eclipse.jface.action.MenuManager;
18
import org.eclipse.jface.viewers.StructuredSelection;
19
import org.eclipse.jface.viewers.TableViewer;
20
import org.eclipse.jface.viewers.TableViewerColumn;
21
import org.eclipse.swt.SWT;
22
import org.eclipse.swt.events.MouseAdapter;
23
import org.eclipse.swt.events.MouseEvent;
24
import org.eclipse.swt.graphics.Point;
25
import org.eclipse.swt.graphics.Rectangle;
26
import org.eclipse.swt.layout.FillLayout;
27
import org.eclipse.swt.widgets.Composite;
28
import org.eclipse.swt.widgets.Control;
29
import org.eclipse.swt.widgets.Menu;
30
import org.eclipse.swt.widgets.Table;
31
import org.eclipse.swt.widgets.TableItem;
32
import org.eclipse.ui.IEditorInput;
33
import org.eclipse.ui.IEditorSite;
34
import org.eclipse.ui.IWorkbenchActionConstants;
35
import org.eclipse.ui.PartInitException;
36
import org.eclipse.ui.PlatformUI;
37
import org.eclipse.ui.part.EditorPart;
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.KeyEditor;
49
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
50
import eu.etaxonomy.taxeditor.model.MessagingUtils;
51

    
52
/**
53
 * @author n.hoffmann
54
 * @created Mar 31, 2011
55
 * @version 1.0
56
 */
57
public class PolytomousKeyListEditor extends EditorPart implements
58
		IConversationEnabled, IDirtyMarkable,
59
		IPolytomousKeyEditorPage {
60

    
61
	private class LinkListener extends MouseAdapter {
62

    
63

    
64
		/*
65
		 * (non-Javadoc)
66
		 *
67
		 * @see
68
		 * org.eclipse.swt.events.MouseAdapter#mouseUp(org.eclipse.swt.events
69
		 * .MouseEvent)
70
		 */
71
		@Override
72
		public void mouseUp(MouseEvent event) {
73

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

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

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

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

    
111

    
112
		}
113

    
114

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

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

    
133
		/**
134
		 * @return
135
		 */
136
		private PolytomousKeyNode getItemLinkData(PolytomousKeyNode node) {
137
			return node.getChildren().isEmpty() ? null : node
138
					.getChildAt(0);
139
		}
140

    
141
	      /**
142
         * @return
143
         */
144
        private Taxon getItemTaxon(PolytomousKeyNode node) {
145
            return node.getTaxon();
146
        }
147
	}
148

    
149
	public static final String ID = "eu.etaxonomy.taxeditor.editor.key.polytomous.list";
150

    
151
	private TableViewer viewer;
152

    
153
	private final KeyEditor editor;
154

    
155
	public PolytomousKeyListEditor(KeyEditor editor) {
156
		this.editor = editor;
157
	}
158

    
159
	/*
160
	 * (non-Javadoc)
161
	 *
162
	 * @see
163
	 * eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update
164
	 * (eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
165
	 */
166
	@Override
167
	public void update(CdmDataChangeMap arg0) {
168

    
169
	}
170

    
171
	/*
172
	 * (non-Javadoc)
173
	 *
174
	 * @see
175
	 * eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder
176
	 * ()
177
	 */
178
	@Override
179
	public ConversationHolder getConversationHolder() {
180
		return ((PolytomousKeyEditorInput) getEditorInput())
181
				.getConversationHolder();
182
	}
183

    
184
	/*
185
	 * (non-Javadoc)
186
	 *
187
	 * @see org.eclipse.ui.part.EditorPart#doSave(org.eclipse.core.runtime.
188
	 * IProgressMonitor)
189
	 */
190
	@Override
191
	public void doSave(IProgressMonitor monitor) {
192
	}
193

    
194
	/*
195
	 * (non-Javadoc)
196
	 *
197
	 * @see org.eclipse.ui.part.EditorPart#doSaveAs()
198
	 */
199
	@Override
200
	public void doSaveAs() {
201
	}
202

    
203
	/*
204
	 * (non-Javadoc)
205
	 *
206
	 * @see org.eclipse.ui.part.EditorPart#init(org.eclipse.ui.IEditorSite,
207
	 * org.eclipse.ui.IEditorInput)
208
	 */
209
	@Override
210
	public void init(IEditorSite site, IEditorInput input)
211
			throws PartInitException {
212
		setSite(site);
213
		setInput(input);
214
	}
215

    
216
	/*
217
	 * (non-Javadoc)
218
	 *
219
	 * @see org.eclipse.ui.part.EditorPart#isDirty()
220
	 */
221
	@Override
222
	public boolean isDirty() {
223
		return false;
224
	}
225

    
226
	/*
227
	 * (non-Javadoc)
228
	 *
229
	 * @see org.eclipse.ui.part.EditorPart#isSaveAsAllowed()
230
	 */
231
	@Override
232
	public boolean isSaveAsAllowed() {
233
		return false;
234
	}
235

    
236
	/*
237
	 * (non-Javadoc)
238
	 *
239
	 * @see
240
	 * org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets
241
	 * .Composite)
242
	 */
243
	@Override
244
	public void createPartControl(Composite parent) {
245

    
246
		FillLayout fillLayout = new FillLayout();
247
		fillLayout.marginWidth = 0;
248
		fillLayout.marginHeight = 0;
249
		fillLayout.type = SWT.VERTICAL;
250
		parent.setLayout(fillLayout);
251

    
252
		viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
253
				| SWT.V_SCROLL | SWT.FULL_SELECTION);
254
		getSite().setSelectionProvider(viewer);
255

    
256
		createColumns(viewer);
257
		viewer.getControl().addMouseListener(new LinkListener());
258
		viewer.setContentProvider(new PolytomousKeyListContentProvider());
259
		viewer.setLabelProvider(new PolytomousKeyListLabelProvider());
260

    
261
		createMenu();
262

    
263
		PolytomousKey key = ((PolytomousKeyEditorInput) getEditorInput())
264
				.getKey();
265
		key = HibernateProxyHelper.deproxy(key, PolytomousKey.class);
266
		key.setRoot(HibernateProxyHelper.deproxy(key.getRoot(), PolytomousKeyNode.class));
267
		setPartName(key.getTitleCache());
268

    
269
		viewer.setInput(getEditorInput());
270
	}
271

    
272
	public int getTableItemCount() {
273
	    if (viewer != null && viewer.getTable() != null) {
274
	        return viewer.getTable().getItemCount();
275
	    }
276

    
277
	    return 0;
278
	}
279

    
280
	public PolytomousKey getViewerInputKey() {
281
	    return ((PolytomousKeyEditorInput) viewer.getInput()).getKey();
282
	}
283
	private void createMenu() {
284
		// register context menu
285
		MenuManager menuManager = new MenuManager();
286
		menuManager
287
				.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
288
		getSite().registerContextMenu(ID, menuManager, viewer);
289

    
290
		Control control = viewer.getControl();
291
		Menu menu = menuManager.createContextMenu(control);
292
		menuManager.setRemoveAllWhenShown(true);
293
		control.setMenu(menu);
294
	}
295

    
296
	// This will create the columns for the table
297
	private void createColumns(TableViewer viewer) {
298
		Table table = viewer.getTable();
299
		String[] titles = { "Node", "Question", "Edge",  "Statement", "Link", "Taxon" };
300
		int[] bounds = { 50, 200, 50, 200, 100, 200 };
301

    
302
		for (int i = 0; i < titles.length; i++) {
303
			TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
304
			column.getColumn().setText(titles[i]);
305
			column.getColumn().setWidth(bounds[i]);
306
			column.getColumn().setResizable(true);
307
			column.getColumn().setMoveable(true);
308
		}
309
		table.setHeaderVisible(true);
310
		table.setLinesVisible(false);
311

    
312
	}
313

    
314
	/*
315
	 * (non-Javadoc)
316
	 *
317
	 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
318
	 */
319
	@Override
320
	public void setFocus() {
321
		PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell().setFocus();
322
	}
323

    
324
	/*
325
	 * (non-Javadoc)
326
	 *
327
	 * @see
328
	 * eu.etaxonomy.taxeditor.model.IDirtyMarkableSelectionProvider#changed(
329
	 * java.lang.Object)
330
	 */
331
	@Override
332
	public void changed(Object element) {
333
        if(element != null) {
334
            viewer.update(element, null);
335

    
336
        }
337

    
338
        if (element instanceof PolytomousKeyNode) {
339
            List<PolytomousKeyNode> children = ((PolytomousKeyNode) element)
340
                    .getParent().getChildren();
341
            for (PolytomousKeyNode child : children) {
342
                viewer.update(child, null);
343
            }
344
        }
345

    
346
        viewer.refresh();
347

    
348
	}
349

    
350
    /* (non-Javadoc)
351
     * @see eu.etaxonomy.taxeditor.model.IDirtyMarkableSelectionProvider#forceDirty()
352
     */
353
    @Override
354
    public void forceDirty() {
355
        changed(null);
356
    }
357

    
358
	/*
359
	 * (non-Javadoc)
360
	 *
361
	 * @see
362
	 * eu.etaxonomy.taxeditor.operation.IPostOperationEnabled#postOperation(
363
	 * eu.etaxonomy.cdm.model.common.CdmBase)
364
	 */
365
	@Override
366
	public boolean postOperation(CdmBase objectAffectedByOperation) {
367
		viewer.refresh();
368
		getConversationHolder().bind();
369
		getConversationHolder().commit(true);
370
		editor.changed(objectAffectedByOperation);
371

    
372
		if (objectAffectedByOperation != null) {
373
			viewer.setSelection(new StructuredSelection(
374
					objectAffectedByOperation), true);
375
		}
376
		return true;
377
	}
378

    
379
	/*
380
	 * (non-Javadoc)
381
	 *
382
	 * @see eu.etaxonomy.taxeditor.operation.IPostOperationEnabled#onComplete()
383
	 */
384
	@Override
385
	public boolean onComplete() {
386
		return true;
387
	}
388

    
389
}
(8-8/11)