Project

General

Profile

Download (6.63 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;
11

    
12
import org.eclipse.swt.SWT;
13
import org.eclipse.swt.events.SelectionAdapter;
14
import org.eclipse.swt.events.SelectionEvent;
15
import org.eclipse.swt.events.SelectionListener;
16
import org.eclipse.swt.widgets.Composite;
17
import org.eclipse.swt.widgets.Label;
18
import org.eclipse.swt.widgets.Link;
19
import org.eclipse.ui.PartInitException;
20
import org.eclipse.ui.PlatformUI;
21
import org.eclipse.ui.forms.IFormPart;
22
import org.eclipse.ui.forms.IManagedForm;
23

    
24
import eu.etaxonomy.cdm.common.CdmUtils;
25
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
26
import eu.etaxonomy.cdm.model.description.KeyStatement;
27
import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
28
import eu.etaxonomy.cdm.model.taxon.Taxon;
29
import eu.etaxonomy.taxeditor.editor.EditorUtil;
30
import eu.etaxonomy.taxeditor.editor.Messages;
31
import eu.etaxonomy.taxeditor.store.CdmStore;
32
import eu.etaxonomy.taxeditor.ui.element.AbstractCdmFormElement;
33
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
34
import eu.etaxonomy.taxeditor.ui.element.ISelectable;
35
import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
36

    
37
/**
38
 * @author n.hoffmann
39
 * @created Apr 13, 2011
40
 * @version 1.0
41
 */
42
public class PolytomousKeyListItem extends AbstractCdmFormElement implements
43
		IFormPart, ISelectable {
44

    
45
	private boolean dirty;
46
	private final PolytomousKeyNode entity;
47

    
48
	private SelectionListener linkSelectionListener;
49

    
50
	/**
51
	 * @param formFactory
52
	 * @param layoutComposite
53
	 */
54
	protected PolytomousKeyListItem(CdmFormFactory formFactory,
55
			Composite layoutComposite, PolytomousKeyNode entity) {
56
		super(formFactory, layoutComposite);
57

    
58
		// layoutComposite.setBackground(Display.getDefault().getSystemColor(
59
		// SWT.COLOR_CYAN));
60

    
61
		this.entity = (PolytomousKeyNode) HibernateProxyHelper.deproxy(entity);
62

    
63
		Label label_nodeNumber = new Label(getLayoutComposite(), SWT.NULL);
64
		label_nodeNumber.setText(getItemNumber());
65

    
66
		Label label_question = new Label(getLayoutComposite(), SWT.NULL);
67
		label_question.setText(getItemQuestion());
68

    
69
		Label label_statement = new Label(getLayoutComposite(), SWT.NULL);
70
		label_statement.setText(getItemStatement());
71
		label_statement.setLayoutData(LayoutConstants.FILL_HORIZONTALLY());
72

    
73
		Link link = new Link(getLayoutComposite(), SWT.NONE);
74
		link.setText("<a>" + getItemLink() + "</a>"); //$NON-NLS-1$ //$NON-NLS-2$
75
		link.setData(getItemLinkData());
76
		link.addSelectionListener(getLinkSelectionListener());
77

    
78
		// Label label_link = new Label(getLayoutComposite(), SWT.NULL);
79
		// label_link.setText(link);
80

    
81
	}
82

    
83
	/**
84
	 * @return
85
	 */
86
	private SelectionListener getLinkSelectionListener() {
87
		if (linkSelectionListener == null) {
88
			linkSelectionListener = new SelectionAdapter() {
89
				/*
90
				 * (non-Javadoc)
91
				 * 
92
				 * @see
93
				 * org.eclipse.swt.events.SelectionAdapter#widgetSelected(org
94
				 * .eclipse.swt.events.SelectionEvent)
95
				 */
96
				@Override
97
				public void widgetSelected(SelectionEvent e) {
98
					Object data = e.widget.getData();
99

    
100
					if (data instanceof Taxon) {
101
						try {
102
							EditorUtil.openTaxonBase(((Taxon) data).getUuid());
103
						} catch (PartInitException e1) {
104
							// TODO Auto-generated catch block
105
							e1.printStackTrace();
106
						}
107
					}
108
				}
109
			};
110
		}
111
		return linkSelectionListener;
112
	}
113

    
114
	private String getItemNumber() {
115
		if (isParentRoot()) {
116
			return "root"; //$NON-NLS-1$
117
		} else {
118
			PolytomousKeyNode parent = getParent();
119
			String itemNumber = parent.getNodeNumber() != null ? parent
120
					.getNodeNumber().toString() : "NaN"; //$NON-NLS-1$
121

    
122
			int index = parent.getChildren().indexOf(entity);
123

    
124
			for (int i = 0; i < index; i++) {
125
				itemNumber += "'"; //$NON-NLS-1$
126
			}
127

    
128
			return itemNumber;
129
		}
130
	}
131

    
132
	private String getItemQuestion() {
133
		if (isParentRoot()) {
134
			return ""; //$NON-NLS-1$
135
		} else {
136
			KeyStatement question = getParent().getQuestion();
137
			return question != null ? question.getLabelText(CdmStore
138
					.getDefaultLanguage()) : ""; //$NON-NLS-1$
139
		}
140

    
141
	}
142

    
143
	private String getItemStatement() {
144
		KeyStatement statement = entity.getStatement();
145
		return statement != null ? CdmUtils.Nz(statement.getLabelText(CdmStore
146
				.getDefaultLanguage())) : Messages.PolytomousKeyListItem_NO_STATEMENT;
147
	}
148

    
149
	private String getItemLink() {
150
		String taxonString = entity.getTaxon() != null ? entity.getTaxon()
151
				.getName().getTitleCache() : Messages.PolytomousKeyListItem_TAXON_EMPTY;
152

    
153
		return entity.getChildren().isEmpty() ? taxonString : entity
154
				.getNodeNumber().toString();
155
	}
156

    
157
	/**
158
	 * @return
159
	 */
160
	private Object getItemLinkData() {
161
		return entity.getChildren().isEmpty() ? entity.getTaxon() : entity
162
				.getChildAt(0);
163
	}
164

    
165
	private PolytomousKeyNode getParent() {
166
		return entity.getParent();
167
	}
168

    
169
	private boolean isParentRoot() {
170
		return getParent() == null;
171
	}
172

    
173
	/*
174
	 * (non-Javadoc)
175
	 * 
176
	 * @see
177
	 * org.eclipse.ui.forms.IFormPart#initialize(org.eclipse.ui.forms.IManagedForm
178
	 * )
179
	 */
180
	@Override
181
	public void initialize(IManagedForm form) {
182
		// TODO Auto-generated method stub
183

    
184
	}
185

    
186
	/*
187
	 * (non-Javadoc)
188
	 * 
189
	 * @see org.eclipse.ui.forms.IFormPart#isDirty()
190
	 */
191
	@Override
192
	public boolean isDirty() {
193
		return dirty;
194
	}
195

    
196
	/*
197
	 * (non-Javadoc)
198
	 * 
199
	 * @see org.eclipse.ui.forms.IFormPart#commit(boolean)
200
	 */
201
	@Override
202
	public void commit(boolean onSave) {
203
		// TODO Auto-generated method stub
204

    
205
	}
206

    
207
	/*
208
	 * (non-Javadoc)
209
	 * 
210
	 * @see org.eclipse.ui.forms.IFormPart#setFormInput(java.lang.Object)
211
	 */
212
	@Override
213
	public boolean setFormInput(Object input) {
214
		// TODO Auto-generated method stub
215
		return false;
216
	}
217

    
218
	/*
219
	 * (non-Javadoc)
220
	 * 
221
	 * @see org.eclipse.ui.forms.IFormPart#setFocus()
222
	 */
223
	@Override
224
	public void setFocus() {
225
		// TODO Auto-generated method stub
226
		PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell().setFocus();
227
	}
228

    
229
	/*
230
	 * (non-Javadoc)
231
	 * 
232
	 * @see org.eclipse.ui.forms.IFormPart#isStale()
233
	 */
234
	@Override
235
	public boolean isStale() {
236
		// TODO Auto-generated method stub
237
		return false;
238
	}
239

    
240
	/*
241
	 * (non-Javadoc)
242
	 * 
243
	 * @see org.eclipse.ui.forms.IFormPart#refresh()
244
	 */
245
	@Override
246
	public void refresh() {
247
		// TODO Auto-generated method stub
248

    
249
	}
250

    
251
	/*
252
	 * (non-Javadoc)
253
	 * 
254
	 * @see org.eclipse.ui.forms.IFormPart#dispose()
255
	 */
256
	@Override
257
	public void dispose() {
258
		// TODO Auto-generated method stub
259

    
260
	}
261

    
262
	/**
263
	 * @return the entity
264
	 */
265
	public PolytomousKeyNode getEntity() {
266
		return entity;
267
	}
268
	
269
	@Override
270
	public void setSelected(boolean selected) {
271
		setBackground(selected ? SELECTED : getPersistentBackground());
272
	}
273

    
274
}
(9-9/11)