9087bac7ad5ebfb15ade18d4f39000f3e1ffdb81
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / classification / TaxonNodeDetailElement.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.ui.section.classification;
12
13 import org.apache.commons.lang.StringUtils;
14 import org.eclipse.swt.widgets.Control;
15 import org.eclipse.swt.widgets.Text;
16
17 import eu.etaxonomy.cdm.common.CdmUtils;
18 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
19 import eu.etaxonomy.cdm.model.name.NonViralName;
20 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
21 import eu.etaxonomy.cdm.model.reference.Reference;
22 import eu.etaxonomy.cdm.model.taxon.Classification;
23 import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
24 import eu.etaxonomy.cdm.model.taxon.Taxon;
25 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
26 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
27 import eu.etaxonomy.taxeditor.ui.element.CheckboxElement;
28 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
29 import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
30 import eu.etaxonomy.taxeditor.ui.element.TextWithLabelElement;
31 import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailElement;
32 import eu.etaxonomy.taxeditor.ui.selection.EntitySelectionElement;
33 import eu.etaxonomy.taxeditor.ui.selection.TaxonNodeSelectionElement;
34
35 /**
36 *
37 * @author n.hoffmann
38 * @created Sep 27, 2010
39 * @version 1.0
40 */
41 public class TaxonNodeDetailElement extends AbstractCdmDetailElement<ITaxonTreeNode> {
42
43 private boolean createNew = false;
44
45 private EntitySelectionElement<Classification> selection_classification;
46
47 private Classification classification;
48
49 private TaxonNodeSelectionElement selection_parentTaxonNode;
50
51 private ITaxonTreeNode parentTreeNode;
52
53 private EntitySelectionElement<Taxon> selection_reuseExistingTaxon;
54 private EntitySelectionElement<Reference> selection_SecRef;
55
56 private Taxon taxon;
57
58 private TextWithLabelElement textNewTaxonName;
59
60 private TaxonNodeAgentRelationCollectionSection selectionNodeAgentRelation;
61
62 private CheckboxElement checkbox_openInEditor;
63
64 private CheckboxElement checkbox_excluded;
65
66 private CheckboxElement checkbox_unplaced;
67
68 private boolean openInEditor;
69
70 private boolean complete;
71
72 private EntitySelectionElement<TaxonNameBase> selection_reuseExistingName;
73
74 public TaxonNodeDetailElement(CdmFormFactory formFactory,
75 ICdmFormElement formElement, boolean isCreateNew) {
76 super(formFactory, formElement);
77 createNew = isCreateNew;
78 }
79
80 /** {@inheritDoc} */
81 @Override
82 protected void createControls(ICdmFormElement formElement,
83 ITaxonTreeNode entity, int style) {
84 selection_classification = formFactory
85 .createSelectionElement(Classification.class,
86 getConversationHolder(), formElement, "Classification",
87 null, EntitySelectionElement.DELETABLE, style);
88 selection_parentTaxonNode = formFactory
89 .createTaxonNodeSelectionElement(getConversationHolder(), formElement, "Parent", null,
90 EntitySelectionElement.DELETABLE, style);
91
92 selection_reuseExistingTaxon = formFactory
93 .createSelectionElement(Taxon.class,
94 getConversationHolder(), formElement,
95 "Reuse existing taxon", null,
96 EntitySelectionElement.DELETABLE, style);
97
98 selection_reuseExistingName = formFactory
99 .createSelectionElement(TaxonNameBase.class,
100 getConversationHolder(), formElement,
101 "Reuse existing name", null,
102 EntitySelectionElement.DELETABLE, style);
103 selection_SecRef = formFactory
104 .createSelectionElement(Reference.class,getConversationHolder(), formElement, "Secundum Reference", null,
105 EntitySelectionElement.DELETABLE, style);
106 if (isCreateNew()){
107 textNewTaxonName = formFactory.createTextWithLabelElement(formElement,"New Taxon", "", style);
108 textNewTaxonName.setFocus();
109 } else{
110 textNewTaxonName = formFactory.createTextWithLabelElement(formElement,"Taxon", "", style);
111
112 }
113
114
115 if (isCreateNew()){
116 setParentTreeNode(entity);
117
118 } else{
119 setTreeNode(entity);
120 complete = true;
121 }
122 if (entity instanceof TaxonNode) {
123 checkbox_excluded = formFactory.createCheckbox(formElement,
124 "Taxon is excluded", ((TaxonNode) entity).isExcluded(), style);
125 checkbox_unplaced = formFactory.createCheckbox(formElement,
126 "Taxon is unplaced", ((TaxonNode) entity).isUnplaced(), style);
127 }
128
129 if (!isCreateNew()){
130 selectionNodeAgentRelation = formFactory.createTaxonNodeAgentRelationCollectionSection(formElement, getConversationHolder(), style);
131 selectionNodeAgentRelation.setEntity((TaxonNode)entity);
132 selectionNodeAgentRelation.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
133 }
134 preFillTaxonName();
135 checkbox_openInEditor = formFactory.createCheckbox(formElement,
136 "Open in Editor", true, style);
137 setOpenInEditor(true);
138
139 }
140
141 private void preFillTaxonName() {
142 if(getEntity() instanceof TaxonNode ){
143 TaxonNode node = (TaxonNode)getEntity();
144 if(node.getTaxon()!=null){
145 Taxon taxon = HibernateProxyHelper.deproxy(node.getTaxon(), Taxon.class);
146 if(taxon.getName()!=null && taxon.getName().isInstanceOf(NonViralName.class)){
147 NonViralName<?> name = HibernateProxyHelper.deproxy(node.getTaxon().getName(), NonViralName.class);
148 if (!isCreateNew()){
149 textNewTaxonName.setText(name.getNameCache());
150 } else if( ! name.isSupraGeneric() && name.getRank() != null){
151 String taxonName = "";
152 if(name.isGenus() || name.isInfraGeneric()|| name.isSpeciesAggregate() ){
153 taxonName = name.getGenusOrUninomial();
154 }
155 else if(name.isSpecies() || name.isInfraSpecific() ){
156 taxonName = CdmUtils.concat(" ", name.getGenusOrUninomial(),name.getSpecificEpithet());
157 }
158 if (StringUtils.isNotBlank(taxonName)){
159 textNewTaxonName.setText(taxonName + " ");
160 if(textNewTaxonName.getMainControl() instanceof Text){
161 Text text = (Text)textNewTaxonName.getMainControl();
162 text.setSelection(textNewTaxonName.getText().length());
163 }
164 }
165 }
166
167 }
168
169 }
170
171 }
172 }
173
174 /** {@inheritDoc} */
175 @Override
176 public void handleEvent(Object eventSource) {
177 if (eventSource == selection_classification) {
178 setClassification(selection_classification.getEntity());
179 } else if (eventSource == selection_parentTaxonNode) {
180 setParentTreeNode(selection_parentTaxonNode.getEntity());
181 } else if (eventSource == selection_reuseExistingTaxon) {
182 boolean enabled = selection_reuseExistingTaxon.getEntity() == null;
183 selection_reuseExistingName.setEnabled(enabled);
184 if (!enabled){
185 setTaxon(selection_reuseExistingTaxon.getEntity());
186 }else{
187 textNewTaxonName.setText(null);
188 }
189 complete = !textNewTaxonName.getText().isEmpty();
190
191
192 } else if (eventSource == selection_reuseExistingName) {
193 boolean enabled = selection_reuseExistingName.getEntity() == null;
194 setTaxon(selection_reuseExistingName.getEntity());
195 selection_reuseExistingTaxon.setEnabled(enabled);
196 textNewTaxonName.setEnabled(enabled);
197
198 //setTaxon(selection_reuseExistingName.getEntity());
199
200 complete = !textNewTaxonName.getText().isEmpty();
201 } else if (eventSource == textNewTaxonName) {
202 boolean enabled = CdmUtils.isBlank(textNewTaxonName.getText());
203 selection_reuseExistingTaxon.setEnabled(enabled);
204 selection_reuseExistingName.setEnabled(enabled);
205
206 complete = CdmUtils.isNotBlank(textNewTaxonName.getText());
207 } else if (eventSource == checkbox_excluded) {
208 ((TaxonNode) getEntity()).setExcluded(checkbox_excluded.getSelection());
209 } else if (eventSource == checkbox_unplaced) {
210 ((TaxonNode) getEntity()).setUnplaced(checkbox_unplaced.getSelection());
211 } else if (eventSource == checkbox_openInEditor) {
212 setOpenInEditor(checkbox_openInEditor.getSelection());
213 } else if (eventSource == selectionNodeAgentRelation){
214 boolean allComplete = true;
215 for (ICdmFormElement element : selectionNodeAgentRelation.getElements()){
216 allComplete &=((TaxonNodeAgentRelationCollectionElement)element).isComplete();
217 }
218
219 complete = !isCreateNew() && allComplete;
220 }
221 }
222
223 public Classification getClassification() {
224 return classification;
225 }
226
227 public boolean isOpenInEditor() {
228 return openInEditor;
229 }
230
231 public ITaxonTreeNode getParentTreeNode() {
232 return parentTreeNode;
233 }
234
235 private void setClassification(Classification classification) {
236 this.classification = classification;
237 setParentTreeNode(classification);
238 }
239
240 private void setParentTreeNode(ITaxonTreeNode parentTreeNode) {
241 this.parentTreeNode = parentTreeNode;
242
243 if (parentTreeNode instanceof Classification) {
244 classification = (Classification) parentTreeNode;
245 selection_classification.setEntity(classification);
246 selection_parentTaxonNode.setEntity(null);
247 selection_parentTaxonNode.setClassification(classification);
248 selection_SecRef.setEntity(classification.getReference());
249 } else if (parentTreeNode instanceof TaxonNode) {
250 classification = HibernateProxyHelper
251 .deproxy(((TaxonNode) parentTreeNode).getClassification());
252 selection_classification.setEntity(classification);
253 selection_parentTaxonNode.setEntity((TaxonNode) parentTreeNode);
254 selection_parentTaxonNode.setClassification(classification);
255 selection_SecRef.setEntity(((TaxonNode)parentTreeNode).getTaxon().getSec());
256 } else if(parentTreeNode == null){
257 this.parentTreeNode = selection_classification.getEntity();
258 }
259 }
260
261 private void setTreeNode(ITaxonTreeNode treeNode) {
262 classification = HibernateProxyHelper
263 .deproxy(((TaxonNode) treeNode).getClassification());
264 selection_classification.setEntity(classification);
265 selection_parentTaxonNode.setEntity(((TaxonNode) treeNode).getParent());
266 selection_parentTaxonNode.setClassification(((TaxonNode) treeNode).getParent().getClassification());
267 selection_SecRef.setEntity(((TaxonNode)treeNode).getTaxon().getSec());
268 }
269
270 private void setTaxon(Taxon taxon) {
271 this.taxon = taxon;
272 if (isCreateNew()){
273 textNewTaxonName.setText(taxon.getName().getTitleCache());
274 }
275 selection_SecRef.setEntity(taxon.getSec());
276 }
277
278 private void setTaxon(TaxonNameBase taxonName) {
279 Reference secundum = null;
280 if (getParentTreeNode() != null) {
281 if (this.selection_SecRef.getEntity() != null){
282 secundum = this.selection_SecRef.getEntity();
283 }
284 }
285 if (taxonName != null){
286 taxon = Taxon.NewInstance(taxonName, secundum);
287 if (!taxonName.getTitleCache().trim().equals(textNewTaxonName.getText().trim())){
288 textNewTaxonName.setText(taxonName.getTitleCache());
289 }
290 } else {
291 textNewTaxonName.setText(null);
292 }
293 }
294
295 private void setOpenInEditor(boolean openInEditor) {
296 this.openInEditor = openInEditor;
297 }
298
299 public String getTaxonName() {
300 return textNewTaxonName.getText();
301 }
302 public Taxon getTaxon() {
303 return taxon;
304 }
305
306 public Reference getReference(){
307 return selection_SecRef.getSelection();
308 }
309
310 public Control getMainControl(){
311 return textNewTaxonName.getMainControl();
312 }
313
314 public boolean isComplete() {
315 boolean allComplete = true;
316 if (!isCreateNew()){
317 for (ICdmFormElement element : selectionNodeAgentRelation.getElements()){
318 allComplete &=((TaxonNodeAgentRelationCollectionElement)element).isComplete();
319 }
320 complete = allComplete;
321 }
322
323
324 return complete;
325 }
326
327 /**
328 * @return the createNew
329 */
330 public boolean isCreateNew() {
331 return createNew;
332 }
333
334 }