AT: commiting latest changes to the Palm Use data extension
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / key / polytomous / PolytomousKeyListItem.java
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.swt.SWT;
14 import org.eclipse.swt.events.SelectionAdapter;
15 import org.eclipse.swt.events.SelectionEvent;
16 import org.eclipse.swt.events.SelectionListener;
17 import org.eclipse.swt.widgets.Composite;
18 import org.eclipse.swt.widgets.Label;
19 import org.eclipse.swt.widgets.Link;
20 import org.eclipse.ui.PartInitException;
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.store.CdmStore;
31 import eu.etaxonomy.taxeditor.ui.forms.AbstractCdmFormElement;
32 import eu.etaxonomy.taxeditor.ui.forms.CdmFormFactory;
33 import eu.etaxonomy.taxeditor.ui.forms.ISelectable;
34
35 /**
36 * @author n.hoffmann
37 * @created Apr 13, 2011
38 * @version 1.0
39 */
40 public class PolytomousKeyListItem extends AbstractCdmFormElement implements
41 IFormPart, ISelectable {
42
43 private boolean dirty;
44 private final PolytomousKeyNode entity;
45
46 private SelectionListener linkSelectionListener;
47
48 /**
49 * @param formFactory
50 * @param layoutComposite
51 */
52 protected PolytomousKeyListItem(CdmFormFactory formFactory,
53 Composite layoutComposite, PolytomousKeyNode entity) {
54 super(formFactory, layoutComposite);
55
56 // layoutComposite.setBackground(Display.getDefault().getSystemColor(
57 // SWT.COLOR_CYAN));
58
59 this.entity = (PolytomousKeyNode) HibernateProxyHelper.deproxy(entity);
60
61 Label label_nodeNumber = new Label(getLayoutComposite(), SWT.NULL);
62 label_nodeNumber.setText(getItemNumber());
63
64 Label label_question = new Label(getLayoutComposite(), SWT.NULL);
65 label_question.setText(getItemQuestion());
66
67 Label label_statement = new Label(getLayoutComposite(), SWT.NULL);
68 label_statement.setText(getItemStatement());
69 label_statement.setLayoutData(CdmFormFactory.FILL_HORIZONTALLY());
70
71 Link link = new Link(getLayoutComposite(), SWT.NONE);
72 link.setText("<a>" + getItemLink() + "</a>");
73 link.setData(getItemLinkData());
74 link.addSelectionListener(getLinkSelectionListener());
75
76 // Label label_link = new Label(getLayoutComposite(), SWT.NULL);
77 // label_link.setText(link);
78
79 }
80
81 /**
82 * @return
83 */
84 private SelectionListener getLinkSelectionListener() {
85 if (linkSelectionListener == null) {
86 linkSelectionListener = new SelectionAdapter() {
87 /*
88 * (non-Javadoc)
89 *
90 * @see
91 * org.eclipse.swt.events.SelectionAdapter#widgetSelected(org
92 * .eclipse.swt.events.SelectionEvent)
93 */
94 @Override
95 public void widgetSelected(SelectionEvent e) {
96 Object data = e.widget.getData();
97
98 if (data instanceof Taxon) {
99 try {
100 EditorUtil.openTaxonBase(((Taxon) data).getUuid());
101 } catch (PartInitException e1) {
102 // TODO Auto-generated catch block
103 e1.printStackTrace();
104 }
105 }
106 }
107 };
108 }
109 return linkSelectionListener;
110 }
111
112 private String getItemNumber() {
113 if (isParentRoot()) {
114 return "root";
115 } else {
116 PolytomousKeyNode parent = getParent();
117 String itemNumber = parent.getNodeNumber() != null ? parent
118 .getNodeNumber().toString() : "NaN";
119
120 int index = parent.getChildren().indexOf(entity);
121
122 for (int i = 0; i < index; i++) {
123 itemNumber += "'";
124 }
125
126 return itemNumber;
127 }
128 }
129
130 private String getItemQuestion() {
131 if (isParentRoot()) {
132 return "";
133 } else {
134 KeyStatement question = getParent().getQuestion();
135 return question != null ? question.getLabelText(CdmStore
136 .getDefaultLanguage()) : "";
137 }
138
139 }
140
141 private String getItemStatement() {
142 KeyStatement statement = entity.getStatement();
143 return statement != null ? CdmUtils.Nz(statement.getLabelText(CdmStore
144 .getDefaultLanguage())) : "No statement";
145 }
146
147 private String getItemLink() {
148 String taxonString = entity.getTaxon() != null ? entity.getTaxon()
149 .getName().getTitleCache() : "Taxon empty";
150
151 return entity.getChildren().isEmpty() ? taxonString : entity
152 .getNodeNumber().toString();
153 }
154
155 /**
156 * @return
157 */
158 private Object getItemLinkData() {
159 return entity.getChildren().isEmpty() ? entity.getTaxon() : entity
160 .getChildAt(0);
161 }
162
163 private PolytomousKeyNode getParent() {
164 return entity.getParent();
165 }
166
167 private boolean isParentRoot() {
168 return getParent() == null;
169 }
170
171 /*
172 * (non-Javadoc)
173 *
174 * @see
175 * org.eclipse.ui.forms.IFormPart#initialize(org.eclipse.ui.forms.IManagedForm
176 * )
177 */
178 @Override
179 public void initialize(IManagedForm form) {
180 // TODO Auto-generated method stub
181
182 }
183
184 /*
185 * (non-Javadoc)
186 *
187 * @see org.eclipse.ui.forms.IFormPart#isDirty()
188 */
189 @Override
190 public boolean isDirty() {
191 return dirty;
192 }
193
194 /*
195 * (non-Javadoc)
196 *
197 * @see org.eclipse.ui.forms.IFormPart#commit(boolean)
198 */
199 @Override
200 public void commit(boolean onSave) {
201 // TODO Auto-generated method stub
202
203 }
204
205 /*
206 * (non-Javadoc)
207 *
208 * @see org.eclipse.ui.forms.IFormPart#setFormInput(java.lang.Object)
209 */
210 @Override
211 public boolean setFormInput(Object input) {
212 // TODO Auto-generated method stub
213 return false;
214 }
215
216 /*
217 * (non-Javadoc)
218 *
219 * @see org.eclipse.ui.forms.IFormPart#setFocus()
220 */
221 @Override
222 public void setFocus() {
223 // TODO Auto-generated method stub
224
225 }
226
227 /*
228 * (non-Javadoc)
229 *
230 * @see org.eclipse.ui.forms.IFormPart#isStale()
231 */
232 @Override
233 public boolean isStale() {
234 // TODO Auto-generated method stub
235 return false;
236 }
237
238 /*
239 * (non-Javadoc)
240 *
241 * @see org.eclipse.ui.forms.IFormPart#refresh()
242 */
243 @Override
244 public void refresh() {
245 // TODO Auto-generated method stub
246
247 }
248
249 /*
250 * (non-Javadoc)
251 *
252 * @see org.eclipse.ui.forms.IFormPart#dispose()
253 */
254 @Override
255 public void dispose() {
256 // TODO Auto-generated method stub
257
258 }
259
260 /**
261 * @return the entity
262 */
263 public PolytomousKeyNode getEntity() {
264 return entity;
265 }
266
267 @Override
268 public void setSelected(boolean selected) {
269 setBackground(selected ? SELECTED : getPersistentBackground());
270 }
271
272 }