5ac40f536071de654a8df5356bf562f4269081a4
[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.Text;
15
16 import eu.etaxonomy.cdm.common.CdmUtils;
17 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
18 import eu.etaxonomy.cdm.model.name.NonViralName;
19 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
20 import eu.etaxonomy.cdm.model.reference.Reference;
21 import eu.etaxonomy.cdm.model.taxon.Classification;
22 import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
23 import eu.etaxonomy.cdm.model.taxon.Taxon;
24 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
25 import eu.etaxonomy.taxeditor.parser.ParseHandler;
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 Reference secReference;
59
60 private TextWithLabelElement textNewTaxonName;
61
62 private TaxonNodeAgentRelationCollectionSection selectionNodeAgentRelation;
63
64
65 private CheckboxElement checkbox_openInEditor;
66
67 private boolean openInEditor;
68
69 private boolean complete;
70
71 private EntitySelectionElement<TaxonNameBase> selection_reuseExistingName;
72
73 public TaxonNodeDetailElement(CdmFormFactory formFactory,
74 ICdmFormElement formElement, boolean isCreateNew) {
75 super(formFactory, formElement);
76 createNew = isCreateNew;
77 }
78
79 /** {@inheritDoc} */
80 @Override
81 protected void createControls(ICdmFormElement formElement,
82 ITaxonTreeNode entity, int style) {
83 selection_classification = formFactory
84 .createSelectionElement(Classification.class,
85 getConversationHolder(), formElement, "Classification",
86 null, EntitySelectionElement.DELETABLE, style);
87 selection_parentTaxonNode = formFactory
88 .createTaxonNodeSelectionElement(getConversationHolder(), formElement, "Parent", null,
89 EntitySelectionElement.DELETABLE, style);
90
91 selection_reuseExistingTaxon = formFactory
92 .createSelectionElement(Taxon.class,
93 getConversationHolder(), formElement,
94 "Reuse existing taxon", null,
95 EntitySelectionElement.DELETABLE, style);
96
97 selection_reuseExistingName = formFactory
98 .createSelectionElement(TaxonNameBase.class,
99 getConversationHolder(), formElement,
100 "Reuse existing name", null,
101 EntitySelectionElement.DELETABLE, style);
102 selection_SecRef = formFactory
103 .createSelectionElement(Reference.class,getConversationHolder(), formElement, "Secundum Reference", null,
104 EntitySelectionElement.DELETABLE, style);
105 if (isCreateNew()){
106 textNewTaxonName = formFactory.createTextWithLabelElement(formElement,"New Taxon", "", style);
107 textNewTaxonName.setFocus();
108 } else{
109 textNewTaxonName = formFactory.createTextWithLabelElement(formElement,"Taxon", "", style);
110
111 }
112
113
114 if (isCreateNew()){
115 setParentTreeNode(entity);
116
117 } else{
118 setTreeNode(entity);
119 complete = true;
120 }
121 if (!isCreateNew()){
122 selectionNodeAgentRelation = formFactory.createTaxonNodeAgentRelationCollectionSection(formElement, getConversationHolder(), style);
123 selectionNodeAgentRelation.setEntity((TaxonNode)entity);
124 selectionNodeAgentRelation.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
125 }
126 preFillTaxonName();
127 checkbox_openInEditor = formFactory.createCheckbox(formElement,
128 "Open in Editor", true, style);
129 setOpenInEditor(true);
130
131 }
132
133 private void preFillTaxonName() {
134 if(getEntity() instanceof TaxonNode ){
135 TaxonNode node = (TaxonNode)getEntity();
136 if(node.getTaxon()!=null){
137 Taxon taxon = HibernateProxyHelper.deproxy(node.getTaxon(), Taxon.class);
138 if(taxon.getName()!=null && taxon.getName().isInstanceOf(NonViralName.class)){
139 NonViralName<?> name = HibernateProxyHelper.deproxy(node.getTaxon().getName(), NonViralName.class);
140 if (!isCreateNew()){
141 textNewTaxonName.setText(name.getNameCache());
142 } else if( ! name.isSupraGeneric() && name.getRank() != null){
143 String taxonName = "";
144 if(name.isGenus() || name.isInfraGeneric()|| name.isSpeciesAggregate() ){
145 taxonName = name.getGenusOrUninomial();
146 }
147 else if(name.isSpecies() || name.isInfraSpecific() ){
148 taxonName = CdmUtils.concat(" ", name.getGenusOrUninomial(),name.getSpecificEpithet());
149 }
150 if (StringUtils.isNotBlank(taxonName)){
151 textNewTaxonName.setText(taxonName + " ");
152 if(textNewTaxonName.getMainControl() instanceof Text){
153 Text text = (Text)textNewTaxonName.getMainControl();
154 text.setSelection(textNewTaxonName.getText().length());
155 }
156 }
157 }
158
159 }
160
161 }
162
163 }
164 }
165
166 /** {@inheritDoc} */
167 @Override
168 public void handleEvent(Object eventSource) {
169 if (eventSource == selection_classification) {
170 setClassification(selection_classification.getEntity());
171 } else if (eventSource == selection_parentTaxonNode) {
172 setParentTreeNode(selection_parentTaxonNode.getEntity());
173 } else if (eventSource == selection_SecRef) {
174 setSecReference(selection_SecRef.getEntity());
175 } else if (eventSource == selection_reuseExistingTaxon) {
176 boolean enabled = selection_reuseExistingTaxon.getEntity() == null;
177 selection_reuseExistingName.setEnabled(enabled);
178 if (!enabled){
179 setTaxon(selection_reuseExistingTaxon.getEntity());
180 }
181 if (isCreateNew()){
182 textNewTaxonName.setEnabled(enabled);
183 if (!enabled){
184 setTaxon(selection_reuseExistingTaxon.getEntity());
185 } else{
186 textNewTaxonName.setText(null);
187 }
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 setTaxon(textNewTaxonName.getText());
207 complete = !textNewTaxonName.getText().isEmpty();
208 } else if (eventSource == checkbox_openInEditor) {
209 setOpenInEditor(checkbox_openInEditor.getSelection());
210 } else if (eventSource == selectionNodeAgentRelation){
211 boolean allComplete = true;
212 for (ICdmFormElement element : selectionNodeAgentRelation.getElements()){
213 allComplete &=((TaxonNodeAgentRelationCollectionElement)element).isComplete();
214 }
215
216 complete = !isCreateNew() && allComplete;
217 }
218 }
219
220 public Classification getClassification() {
221 return classification;
222 }
223
224 public boolean isOpenInEditor() {
225 return openInEditor;
226 }
227
228 public ITaxonTreeNode getParentTreeNode() {
229 return parentTreeNode;
230 }
231
232 public Taxon getTaxon() {
233 return taxon;
234 }
235
236 private void setClassification(Classification classification) {
237 this.classification = classification;
238 setParentTreeNode(classification);
239 }
240
241 private void setParentTreeNode(ITaxonTreeNode parentTreeNode) {
242 this.parentTreeNode = parentTreeNode;
243
244 if (parentTreeNode instanceof Classification) {
245 classification = (Classification) parentTreeNode;
246 selection_classification.setEntity(classification);
247 selection_parentTaxonNode.setEntity(null);
248 selection_parentTaxonNode.setClassification(classification);
249 selection_SecRef.setEntity(classification.getReference());
250 } else if (parentTreeNode instanceof TaxonNode) {
251 classification = HibernateProxyHelper
252 .deproxy(((TaxonNode) parentTreeNode).getClassification());
253 selection_classification.setEntity(classification);
254 selection_parentTaxonNode.setEntity((TaxonNode) parentTreeNode);
255 selection_parentTaxonNode.setClassification(classification);
256 selection_SecRef.setEntity(((TaxonNode)parentTreeNode).getTaxon().getSec());
257 } else if(parentTreeNode == null){
258 this.parentTreeNode = selection_classification.getEntity();
259 }
260 }
261
262 private void setTreeNode(ITaxonTreeNode treeNode) {
263 classification = HibernateProxyHelper
264 .deproxy(((TaxonNode) treeNode).getClassification());
265 selection_classification.setEntity(classification);
266 selection_parentTaxonNode.setEntity(((TaxonNode) treeNode).getParent());
267 selection_parentTaxonNode.setClassification(((TaxonNode) treeNode).getParent().getClassification());
268 selection_SecRef.setEntity(((TaxonNode)treeNode).getTaxon().getSec());
269 }
270
271 private void setSecReference(Reference secRef){
272 this.secReference = secRef;
273 setTaxon(textNewTaxonName.getText());
274 }
275
276 private void setTaxon(Taxon taxon) {
277 this.taxon = taxon;
278 if (isCreateNew()){
279 textNewTaxonName.setText(taxon.getName().getTitleCache());
280 }
281 selection_SecRef.setEntity(taxon.getSec());
282 }
283
284 private void setTaxon(String taxonNameString) {
285 TaxonNameBase taxonName = ParseHandler.parseReferencedName(
286 taxonNameString, null);
287 setTaxon(taxonName);
288 }
289
290 private void setTaxon(TaxonNameBase taxonName) {
291 Reference secundum = null;
292 if (getParentTreeNode() != null) {
293 if (this.selection_SecRef.getEntity() != null){
294 secundum = this.selection_SecRef.getEntity();
295 } /*else if (getParentTreeNode() instanceof Classification) {
296 secundum = ((Classification) getParentTreeNode())
297 .getReference();
298 } else if (getParentTreeNode() instanceof TaxonNode) {
299 secundum = ((TaxonNode) getParentTreeNode()).getTaxon()
300 .getSec();
301 }*/
302 }
303 if (taxonName != null){
304 taxon = Taxon.NewInstance(taxonName, secundum);
305 if (!taxonName.getTitleCache().trim().equals(textNewTaxonName.getText().trim())){
306 textNewTaxonName.setText(taxonName.getTitleCache());
307 }
308 } else {
309 textNewTaxonName.setText(null);
310 }
311 }
312
313 private void setOpenInEditor(boolean openInEditor) {
314 this.openInEditor = openInEditor;
315 }
316
317 public TextWithLabelElement getTextNewTaxonName() {
318 return textNewTaxonName;
319 }
320
321 public boolean isComplete() {
322 boolean allComplete = true;
323 if (!isCreateNew()){
324 for (ICdmFormElement element : selectionNodeAgentRelation.getElements()){
325 allComplete &=((TaxonNodeAgentRelationCollectionElement)element).isComplete();
326 }
327 complete = allComplete;
328 }
329
330
331 return complete;
332 }
333
334 /**
335 * @return the createNew
336 */
337 public boolean isCreateNew() {
338 return createNew;
339 }
340
341 }