Project

General

Profile

Download (9.38 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.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.KeyEditor;
48
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
49
import eu.etaxonomy.taxeditor.model.MessagingUtils;
50
import eu.etaxonomy.taxeditor.store.CdmStore;
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
		 * (non-Javadoc)
64
		 *
65
		 * @see
66
		 * org.eclipse.swt.events.MouseAdapter#mouseUp(org.eclipse.swt.events
67
		 * .MouseEvent)
68
		 */
69
		@Override
70
		public void mouseUp(MouseEvent event) {
71

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

    
77
		        int selectedColumn = getSelectedColumn(table, point);
78
		        PolytomousKeyNode node = (PolytomousKeyNode) getTableItem(
79
                        table, point).getData();
80

    
81
		        if (selectedColumn == 4) {
82
		            PolytomousKeyNode linkData = getItemLinkData(node);
83
		            if (linkData != null) {
84
		                viewer.setSelection(new StructuredSelection(linkData), true);
85
		            }
86
		        }
87
		        if (selectedColumn == 5) {
88
		            Taxon taxon = getItemTaxon(node);
89
		            if (taxon != null) {
90
		                try {
91
		                    EditorUtil.openTaxonBase((taxon).getUuid());
92
		                } catch (PartInitException e) {
93
		                    MessagingUtils.error(getClass(), e);
94
		                }
95
		            }
96
		        }
97
		    }
98
		}
99

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

    
114
		private TableItem getTableItem(Table table, Point point) {
115
			return table.getItem(point);
116
		}
117

    
118
		/**
119
		 * @return
120
		 */
121
		private PolytomousKeyNode getItemLinkData(PolytomousKeyNode node) {
122
			return node.getChildren().isEmpty() ? null : node
123
					.getChildAt(0);
124
		}
125

    
126
	      /**
127
         * @return
128
         */
129
        private Taxon getItemTaxon(PolytomousKeyNode node) {
130
            return node.getTaxon();
131
        }
132
	}
133

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

    
136
	private TableViewer viewer;
137

    
138
	private final KeyEditor editor;
139

    
140
	public PolytomousKeyListEditor(KeyEditor editor) {
141
		this.editor = editor;
142
	}
143

    
144
	/*
145
	 * (non-Javadoc)
146
	 *
147
	 * @see
148
	 * eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update
149
	 * (eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
150
	 */
151
	@Override
152
	public void update(CdmDataChangeMap arg0) {
153

    
154
	}
155

    
156
	/*
157
	 * (non-Javadoc)
158
	 *
159
	 * @see
160
	 * eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder
161
	 * ()
162
	 */
163
	@Override
164
	public ConversationHolder getConversationHolder() {
165
		return ((PolytomousKeyEditorInput) getEditorInput())
166
				.getConversationHolder();
167
	}
168

    
169
	/*
170
	 * (non-Javadoc)
171
	 *
172
	 * @see org.eclipse.ui.part.EditorPart#doSave(org.eclipse.core.runtime.
173
	 * IProgressMonitor)
174
	 */
175
	@Override
176
	public void doSave(IProgressMonitor monitor) {
177

    
178
	}
179

    
180
	/*
181
	 * (non-Javadoc)
182
	 *
183
	 * @see org.eclipse.ui.part.EditorPart#doSaveAs()
184
	 */
185
	@Override
186
	public void doSaveAs() {
187
	}
188

    
189
	/*
190
	 * (non-Javadoc)
191
	 *
192
	 * @see org.eclipse.ui.part.EditorPart#init(org.eclipse.ui.IEditorSite,
193
	 * org.eclipse.ui.IEditorInput)
194
	 */
195
	@Override
196
	public void init(IEditorSite site, IEditorInput input)
197
			throws PartInitException {
198
		setSite(site);
199
		setInput(input);
200
	}
201

    
202
	/*
203
	 * (non-Javadoc)
204
	 *
205
	 * @see org.eclipse.ui.part.EditorPart#isDirty()
206
	 */
207
	@Override
208
	public boolean isDirty() {
209
		return false;
210
	}
211

    
212
	/*
213
	 * (non-Javadoc)
214
	 *
215
	 * @see org.eclipse.ui.part.EditorPart#isSaveAsAllowed()
216
	 */
217
	@Override
218
	public boolean isSaveAsAllowed() {
219
		return false;
220
	}
221

    
222
	/*
223
	 * (non-Javadoc)
224
	 *
225
	 * @see
226
	 * org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets
227
	 * .Composite)
228
	 */
229
	@Override
230
	public void createPartControl(Composite parent) {
231

    
232
		FillLayout fillLayout = new FillLayout();
233
		fillLayout.marginWidth = 0;
234
		fillLayout.marginHeight = 0;
235
		fillLayout.type = SWT.VERTICAL;
236
		parent.setLayout(fillLayout);
237

    
238
		viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
239
				| SWT.V_SCROLL | SWT.FULL_SELECTION);
240
		getSite().setSelectionProvider(viewer);
241

    
242
		createColumns(viewer);
243
		viewer.getControl().addMouseListener(new LinkListener());
244
		viewer.setContentProvider(new PolytomousKeyListContentProvider());
245
		viewer.setLabelProvider(new PolytomousKeyListLabelProvider());
246

    
247
		createMenu();
248

    
249
		PolytomousKey key = ((PolytomousKeyEditorInput) getEditorInput())
250
				.getKey();
251

    
252
		setPartName(key.getTitleCache());
253

    
254
		viewer.setInput(key);
255
	}
256

    
257
	public int getTableItemCount() {
258
	    if (viewer != null && viewer.getTable() != null) {
259
	        return viewer.getTable().getItemCount();
260
	    }
261

    
262
	    return 0;
263
	}
264

    
265
	public PolytomousKey getViewerInputKey() {
266
	    return (PolytomousKey) viewer.getInput();
267
	}
268
	private void createMenu() {
269
		// register context menu
270
		MenuManager menuManager = new MenuManager();
271
		menuManager
272
				.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
273
		getSite().registerContextMenu(ID, menuManager, viewer);
274

    
275
		Control control = viewer.getControl();
276
		Menu menu = menuManager.createContextMenu(control);
277
		control.setMenu(menu);
278
	}
279

    
280
	// This will create the columns for the table
281
	private void createColumns(TableViewer viewer) {
282
		Table table = viewer.getTable();
283
		String[] titles = { "Node", "Question", "Edge",  "Statement", "Link", "Taxon" };
284
		int[] bounds = { 50, 200, 50, 200, 100, 200 };
285

    
286
		for (int i = 0; i < titles.length; i++) {
287
			TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
288
			column.getColumn().setText(titles[i]);
289
			column.getColumn().setWidth(bounds[i]);
290
			column.getColumn().setResizable(true);
291
			column.getColumn().setMoveable(true);
292
		}
293
		table.setHeaderVisible(true);
294
		table.setLinesVisible(false);
295

    
296
	}
297

    
298
	/*
299
	 * (non-Javadoc)
300
	 *
301
	 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
302
	 */
303
	@Override
304
	public void setFocus() {
305
		CdmStore.getCurrentSessionManager().bind((PolytomousKeyEditorInput)getEditorInput());
306
		PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell().setFocus();
307
	}
308

    
309
	/*
310
	 * (non-Javadoc)
311
	 *
312
	 * @see
313
	 * eu.etaxonomy.taxeditor.model.IDirtyMarkableSelectionProvider#changed(
314
	 * java.lang.Object)
315
	 */
316
	@Override
317
	public void changed(Object element) {
318
	    if(element != null) {
319
	        viewer.update(element, null);
320
	    }
321

    
322
		if (element instanceof PolytomousKeyNode) {
323
			List<PolytomousKeyNode> children = ((PolytomousKeyNode) element)
324
					.getParent().getChildren();
325
			for (PolytomousKeyNode child : children) {
326
				viewer.update(child, null);
327
			}
328
		}
329

    
330
	}
331

    
332
    /* (non-Javadoc)
333
     * @see eu.etaxonomy.taxeditor.model.IDirtyMarkableSelectionProvider#forceDirty()
334
     */
335
    @Override
336
    public void forceDirty() {
337
        changed(null);
338
    }
339

    
340
	/*
341
	 * (non-Javadoc)
342
	 *
343
	 * @see
344
	 * eu.etaxonomy.taxeditor.operation.IPostOperationEnabled#postOperation(
345
	 * eu.etaxonomy.cdm.model.common.CdmBase)
346
	 */
347
	@Override
348
	public boolean postOperation(CdmBase objectAffectedByOperation) {
349
		viewer.refresh();
350

    
351
		editor.changed(objectAffectedByOperation);
352

    
353
		if (objectAffectedByOperation != null) {
354
			viewer.setSelection(new StructuredSelection(
355
					objectAffectedByOperation), true);
356
		}
357
		return true;
358
	}
359

    
360
	/*
361
	 * (non-Javadoc)
362
	 *
363
	 * @see eu.etaxonomy.taxeditor.operation.IPostOperationEnabled#onComplete()
364
	 */
365
	@Override
366
	public boolean onComplete() {
367
		return true;
368
	}
369

    
370
}
(7-7/9)