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