Merged refactoring from development branch.
[taxeditor.git] / taxeditor-store / src / main / java / eu / etaxonomy / taxeditor / newWizard / NewTaxonNodeWizardPage.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.newWizard;
12
13 import org.eclipse.jface.util.IPropertyChangeListener;
14 import org.eclipse.jface.util.PropertyChangeEvent;
15 import org.eclipse.jface.wizard.WizardPage;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.graphics.Color;
18 import org.eclipse.swt.widgets.Composite;
19 import org.eclipse.swt.widgets.Display;
20
21 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
22 import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
23 import eu.etaxonomy.cdm.common.CdmUtils;
24 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
25 import eu.etaxonomy.cdm.model.reference.Reference;
26 import eu.etaxonomy.cdm.model.taxon.Classification;
27 import eu.etaxonomy.cdm.model.taxon.ITreeNode;
28 import eu.etaxonomy.cdm.model.taxon.Taxon;
29 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
30 import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
31 import eu.etaxonomy.taxeditor.parser.ParseHandler;
32 import eu.etaxonomy.taxeditor.ui.forms.CdmFormFactory;
33 import eu.etaxonomy.taxeditor.ui.forms.CheckboxElement;
34 import eu.etaxonomy.taxeditor.ui.forms.RootElement;
35 import eu.etaxonomy.taxeditor.ui.forms.TextWithLabelElement;
36 import eu.etaxonomy.taxeditor.ui.forms.CdmFormFactory.SelectionType;
37 import eu.etaxonomy.taxeditor.ui.selection.ClassificationSelectionElement;
38 import eu.etaxonomy.taxeditor.ui.selection.NameSelectionElement;
39 import eu.etaxonomy.taxeditor.ui.selection.TaxonNodeSelectionElement;
40 import eu.etaxonomy.taxeditor.ui.selection.TaxonSelectionElement;
41
42 /**
43 * <p>NewTaxonNodeWizardPage class.</p>
44 *
45 * @author n.hoffmann
46 * @created Sep 29, 2010
47 * @version 1.0
48 */
49 @Deprecated //delete this file when refactoring is complete
50 public class NewTaxonNodeWizardPage extends WizardPage implements IPropertyChangeListener, IConversationEnabled{
51 private ClassificationSelectionElement selection_classification;
52 private TaxonNodeSelectionElement selection_parentTaxonNode;
53 private TaxonSelectionElement selection_reuseExistingTaxon;
54 private TextWithLabelElement text_newTaxonName;
55 private CheckboxElement checkbox_openInEditor;
56 private CdmFormFactory formFactory;
57 private NameSelectionElement selection_reuseExistingName;
58 private ITreeNode parentTreeNode;
59 private Classification classification;
60 private Taxon taxon;
61 private boolean openInEditor = true;
62 private ConversationHolder conversation;
63 private ITreeNode initiallySelectedNode;
64
65 /**
66 * <p>Constructor for NewTaxonNodeWizardPage.</p>
67 *
68 * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
69 * @param selectedNode a {@link eu.etaxonomy.cdm.model.taxon.ITreeNode} object.
70 */
71 protected NewTaxonNodeWizardPage(ConversationHolder conversation, ITreeNode selectedNode) {
72 super("Taxon");
73
74 setTitle("Taxon");
75
76 this.conversation = conversation;
77 this.formFactory = new CdmFormFactory(Display.getCurrent());
78
79 formFactory.addPropertyChangeListener(this);
80
81 initiallySelectedNode = selectedNode;
82
83 }
84
85 /**
86 * <p>openInEditor</p>
87 *
88 * @return a boolean.
89 */
90 public boolean openInEditor(){
91 return openInEditor;
92 }
93
94 /* (non-Javadoc)
95 * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
96 */
97 /** {@inheritDoc} */
98 @Override
99 public void propertyChange(PropertyChangeEvent event) {
100
101 Object eventSource = event.getSource();
102 if(eventSource == selection_classification){
103 setClassification(selection_classification.getEntity());
104 }
105 else if(eventSource == selection_parentTaxonNode){
106 setParentTreeNode(selection_parentTaxonNode.getEntity());
107 }
108 else if(eventSource == selection_reuseExistingTaxon){
109 boolean enabled = selection_reuseExistingTaxon.getEntity() == null;
110 selection_reuseExistingName.setEnabled(enabled);
111 text_newTaxonName.setEnabled(enabled);
112
113 setTaxon(selection_reuseExistingTaxon.getEntity());
114 }
115 else if(eventSource == selection_reuseExistingName){
116 boolean enabled = selection_reuseExistingName.getEntity() == null;
117 selection_reuseExistingTaxon.setEnabled(enabled);
118 text_newTaxonName.setEnabled(enabled);
119
120 setTaxon(selection_reuseExistingName.getEntity());
121 }
122 else if(eventSource == text_newTaxonName){
123 boolean enabled = CdmUtils.isEmpty(text_newTaxonName.getText());
124 selection_reuseExistingTaxon.setEnabled(enabled);
125 selection_reuseExistingName.setEnabled(enabled);
126
127 setTaxon(text_newTaxonName.getText());
128 }
129 else if(eventSource == checkbox_openInEditor){
130 setOpenInEditor(checkbox_openInEditor.getSelection());
131 }
132
133 checkComplete();
134 }
135
136
137 /* (non-Javadoc)
138 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
139 */
140 /** {@inheritDoc} */
141 @Override
142 public void createControl(Composite parent) {
143 Composite control = formFactory.createComposite(parent);
144
145 control.setLayout(CdmFormFactory.LAYOUT(2, false));
146 RootElement rootElement = new RootElement(formFactory, control);
147
148 selection_classification = (ClassificationSelectionElement) formFactory.createSelectionElement(SelectionType.CLASSIFICATION, getConversationHolder(), rootElement, "Classification", null, SWT.NULL);
149 selection_parentTaxonNode = (TaxonNodeSelectionElement) formFactory.createSelectionElement(SelectionType.TAXON_NODE, getConversationHolder(), rootElement, "Parent", null, SWT.NULL);
150 selection_reuseExistingTaxon = (TaxonSelectionElement) formFactory.createSelectionElement(SelectionType.TAXON, getConversationHolder(), rootElement, "Reuse existing Taxon", null, SWT.NULL);
151
152 selection_reuseExistingName = (NameSelectionElement) formFactory.createSelectionElement(SelectionType.NAME, getConversationHolder(), rootElement, "Reuse existing name", null, SWT.NULL);
153
154 text_newTaxonName = formFactory.createTextWithLabelElement(rootElement, "New Taxon", "", SWT.NULL);
155 text_newTaxonName.setFocus();
156
157 checkbox_openInEditor = formFactory.createCheckbox(rootElement, "Open in Editor", openInEditor, SWT.NULL);
158
159 setParentTreeNode(initiallySelectedNode);
160
161 Color bgColor = getShell().getBackground();
162
163 rootElement.setBackground(bgColor);
164 control.setBackground(bgColor);
165
166 setControl(control);
167
168 }
169
170 /**
171 * @param classification the classification to set
172 */
173 private void setClassification(Classification classification) {
174 this.classification = classification;
175 setParentTreeNode(classification);
176 }
177
178 /**
179 * @param parentTreeNode the parentTreeNode to set
180 */
181 private void setParentTreeNode(ITreeNode parentTreeNode) {
182 if(parentTreeNode == null){
183 setPageComplete(false);
184 return;
185 }
186
187 this.parentTreeNode = parentTreeNode;
188
189 if(parentTreeNode instanceof Classification){
190 classification = (Classification) parentTreeNode;
191 selection_classification.setEntity(classification);
192 selection_parentTaxonNode.setEntity(null);
193 }
194 else if(parentTreeNode instanceof TaxonNode){
195 classification = ((TaxonNode) parentTreeNode).getClassification();
196 selection_classification.setEntity(classification);
197 selection_parentTaxonNode.setEntity((TaxonNode) parentTreeNode);
198 }
199 }
200
201 /**
202 *
203 * @param taxon
204 */
205 private void setTaxon(Taxon taxon) {
206 this.taxon = taxon;
207 }
208
209 private void setTaxon(String taxonNameString){
210 TaxonNameBase taxonName = ParseHandler.parseReferencedName(taxonNameString, null);
211 setTaxon(taxonName);
212 }
213
214 private void setTaxon(TaxonNameBase taxonName){
215 Reference secundum = null;
216 if(getParentTreeNode() != null){
217 if(getParentTreeNode() instanceof Classification){
218 secundum = ((Classification) getParentTreeNode()).getReference();
219 }
220 else if(getParentTreeNode() instanceof TaxonNode){
221 secundum = ((TaxonNode) getParentTreeNode()).getTaxon().getSec();
222 }
223 }
224 taxon = Taxon.NewInstance(taxonName, secundum);
225 }
226
227 /**
228 * @param openInEditor the openInEditor to set
229 */
230 private void setOpenInEditor(boolean openInEditor) {
231 this.openInEditor = openInEditor;
232 }
233
234 /**
235 * <p>Getter for the field <code>parentTreeNode</code>.</p>
236 *
237 * @return a {@link eu.etaxonomy.cdm.model.taxon.ITreeNode} object.
238 */
239 public ITreeNode getParentTreeNode() {
240 return parentTreeNode;
241 }
242
243 /**
244 * <p>Getter for the field <code>taxon</code>.</p>
245 *
246 * @return the taxon
247 */
248 public Taxon getTaxon() {
249 return taxon;
250 }
251
252 /**
253 *
254 */
255 private void checkComplete() {
256 if(classification == null){
257 setMessage("No classification", WARNING);
258 setPageComplete(false);
259 }else{
260 setMessage(null);
261 setPageComplete(true);
262 }
263
264
265 }
266
267 /* (non-Javadoc)
268 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
269 */
270 /** {@inheritDoc} */
271 @Override
272 public void update(CdmDataChangeMap changeEvents) {
273
274 }
275
276
277 /* (non-Javadoc)
278 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
279 */
280 /** {@inheritDoc} */
281 @Override
282 public ConversationHolder getConversationHolder() {
283 return conversation;
284 }
285
286 /* (non-Javadoc)
287 * @see org.eclipse.jface.dialogs.DialogPage#dispose()
288 */
289 /** {@inheritDoc} */
290 @Override
291 public void dispose() {
292 formFactory.removePropertyChangeListener(this);
293 super.dispose();
294 }
295 }