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