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