Project

General

Profile

Download (6.27 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 org.eclipse.core.runtime.IProgressMonitor;
14
import org.eclipse.jface.viewers.StructuredSelection;
15
import org.eclipse.jface.viewers.TableViewer;
16
import org.eclipse.jface.viewers.TableViewerColumn;
17
import org.eclipse.swt.SWT;
18
import org.eclipse.swt.events.MouseAdapter;
19
import org.eclipse.swt.events.MouseEvent;
20
import org.eclipse.swt.graphics.Point;
21
import org.eclipse.swt.graphics.Rectangle;
22
import org.eclipse.swt.layout.FillLayout;
23
import org.eclipse.swt.widgets.Composite;
24
import org.eclipse.swt.widgets.Table;
25
import org.eclipse.swt.widgets.TableItem;
26
import org.eclipse.ui.IEditorInput;
27
import org.eclipse.ui.IEditorSite;
28
import org.eclipse.ui.PartInitException;
29
import org.eclipse.ui.part.EditorPart;
30

    
31
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
32
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
33
import eu.etaxonomy.cdm.model.description.PolytomousKey;
34
import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
35
import eu.etaxonomy.cdm.model.taxon.Taxon;
36
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
37
import eu.etaxonomy.taxeditor.editor.EditorUtil;
38
import eu.etaxonomy.taxeditor.ui.forms.CdmFormFactory;
39

    
40
/**
41
 * @author n.hoffmann
42
 * @created Mar 31, 2011
43
 * @version 1.0
44
 */
45
public class PolytomousKeyListEditor extends EditorPart implements
46
		IConversationEnabled {
47

    
48
	private class LinkListener extends MouseAdapter {
49
		/*
50
		 * (non-Javadoc)
51
		 * 
52
		 * @see
53
		 * org.eclipse.swt.events.MouseAdapter#mouseUp(org.eclipse.swt.events
54
		 * .MouseEvent)
55
		 */
56
		@Override
57
		public void mouseUp(MouseEvent event) {
58

    
59
			Table table = (Table) event.widget;
60
			// Determine where the mouse was clicked
61
			Point pt = new Point(event.x, event.y);
62

    
63
			final TableItem item = table.getItem(pt);
64
			if (item != null) {
65
				// Determine which column was selected
66
				int column = -1;
67
				for (int i = 0, n = table.getColumnCount(); i < n; i++) {
68
					Rectangle rect = item.getBounds(i);
69
					if (rect.contains(pt)) {
70
						// This is the selected column
71
						column = i;
72
						break;
73
					}
74
				}
75
				if (column == 3) {
76
					PolytomousKeyNode node = (PolytomousKeyNode) item.getData();
77
					Object linkData = getItemLinkData(node);
78
					if (linkData instanceof PolytomousKeyNode) {
79
						viewer.setSelection(new StructuredSelection(linkData),
80
								true);
81
					} else if (linkData instanceof Taxon) {
82
						try {
83
							EditorUtil.openTaxonBase(((Taxon) linkData)
84
									.getUuid());
85
						} catch (PartInitException e) {
86
							EditorUtil.error(getClass(), e);
87
						}
88
					}
89
				}
90
			}
91
		}
92

    
93
		/**
94
		 * @return
95
		 */
96
		private Object getItemLinkData(PolytomousKeyNode node) {
97
			return node.getChildren().isEmpty() ? node.getTaxon() : node
98
					.getChildAt(0);
99
		}
100
	}
101

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

    
104
	private CdmFormFactory formFactory;
105
	private Composite container;
106

    
107
	private TableViewer viewer;
108

    
109
	/*
110
	 * (non-Javadoc)
111
	 * 
112
	 * @see
113
	 * eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update
114
	 * (eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
115
	 */
116
	@Override
117
	public void update(CdmDataChangeMap arg0) {
118

    
119
	}
120

    
121
	/*
122
	 * (non-Javadoc)
123
	 * 
124
	 * @see
125
	 * eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder
126
	 * ()
127
	 */
128
	@Override
129
	public ConversationHolder getConversationHolder() {
130
		return ((PolytomousKeyEditorInput) getEditorInput())
131
				.getConversationHolder();
132
	}
133

    
134
	/*
135
	 * (non-Javadoc)
136
	 * 
137
	 * @see org.eclipse.ui.part.EditorPart#doSave(org.eclipse.core.runtime.
138
	 * IProgressMonitor)
139
	 */
140
	@Override
141
	public void doSave(IProgressMonitor monitor) {
142

    
143
	}
144

    
145
	/*
146
	 * (non-Javadoc)
147
	 * 
148
	 * @see org.eclipse.ui.part.EditorPart#doSaveAs()
149
	 */
150
	@Override
151
	public void doSaveAs() {
152
	}
153

    
154
	/*
155
	 * (non-Javadoc)
156
	 * 
157
	 * @see org.eclipse.ui.part.EditorPart#init(org.eclipse.ui.IEditorSite,
158
	 * org.eclipse.ui.IEditorInput)
159
	 */
160
	@Override
161
	public void init(IEditorSite site, IEditorInput input)
162
			throws PartInitException {
163
		setSite(site);
164
		setInput(input);
165
		formFactory = new CdmFormFactory(site.getShell().getDisplay());
166
	}
167

    
168
	/*
169
	 * (non-Javadoc)
170
	 * 
171
	 * @see org.eclipse.ui.part.EditorPart#isDirty()
172
	 */
173
	@Override
174
	public boolean isDirty() {
175
		// TODO Auto-generated method stub
176
		return false;
177
	}
178

    
179
	/*
180
	 * (non-Javadoc)
181
	 * 
182
	 * @see org.eclipse.ui.part.EditorPart#isSaveAsAllowed()
183
	 */
184
	@Override
185
	public boolean isSaveAsAllowed() {
186
		return false;
187
	}
188

    
189
	/*
190
	 * (non-Javadoc)
191
	 * 
192
	 * @see
193
	 * org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets
194
	 * .Composite)
195
	 */
196
	@Override
197
	public void createPartControl(Composite parent) {
198

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

    
205
		viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
206
				| SWT.V_SCROLL | SWT.FULL_SELECTION);
207
		getSite().setSelectionProvider(viewer);
208

    
209
		createColumns(viewer);
210
		viewer.getControl().addMouseListener(new LinkListener());
211
		viewer.setContentProvider(new PolytomousKeyListContentProvider());
212
		viewer.setLabelProvider(new PolytomousKeyListLabelProvider());
213

    
214
		// viewer.getControl().setLayoutData(CdmFormFactory.FILL());
215

    
216
		PolytomousKey key = ((PolytomousKeyEditorInput) getEditorInput())
217
				.getKey();
218

    
219
		viewer.setInput(key);
220
	}
221

    
222
	// This will create the columns for the table
223
	private void createColumns(TableViewer viewer) {
224
		Table table = viewer.getTable();
225
		String[] titles = { "Node Number", "Question", "Statement", "Link" };
226
		int[] bounds = { 50, 200, 200, 100 };
227

    
228
		for (int i = 0; i < titles.length; i++) {
229
			TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
230
			column.getColumn().setText(titles[i]);
231
			column.getColumn().setWidth(bounds[i]);
232
			column.getColumn().setResizable(true);
233
			column.getColumn().setMoveable(true);
234
		}
235
		table.setHeaderVisible(true);
236
		table.setLinesVisible(false);
237

    
238
	}
239

    
240
	/*
241
	 * (non-Javadoc)
242
	 * 
243
	 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
244
	 */
245
	@Override
246
	public void setFocus() {
247
		setPartName("Test");
248
	}
249

    
250
}
(6-6/8)