Moving editor sources back into trunk
[taxeditor.git] / taxeditor-editor / src / main / java / eu / etaxonomy / taxeditor / propertysheet / type / wizard / ChooseSpecimenTypeWizardPage.java
1 /**
2 * Copyright (C) 2009 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9
10 package eu.etaxonomy.taxeditor.propertysheet.type.wizard;
11
12 import org.apache.log4j.Logger;
13 import org.eclipse.core.databinding.observable.list.WritableList;
14 import org.eclipse.core.runtime.Assert;
15 import org.eclipse.jface.wizard.WizardPage;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.custom.CCombo;
18 import org.eclipse.swt.events.KeyAdapter;
19 import org.eclipse.swt.events.KeyEvent;
20 import org.eclipse.swt.events.SelectionAdapter;
21 import org.eclipse.swt.events.SelectionEvent;
22 import org.eclipse.swt.layout.GridData;
23 import org.eclipse.swt.layout.GridLayout;
24 import org.eclipse.swt.widgets.Combo;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.swt.widgets.Label;
27 import org.eclipse.swt.widgets.Text;
28
29 import eu.etaxonomy.cdm.model.common.CdmBase;
30 import eu.etaxonomy.cdm.model.common.TermVocabulary;
31 import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
32 import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignationStatus;
33 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
34 import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
35 import eu.etaxonomy.cdm.model.occurrence.DerivedUnitBase;
36 import eu.etaxonomy.cdm.model.occurrence.Specimen;
37 import eu.etaxonomy.cdm.model.reference.ReferenceBase;
38 import eu.etaxonomy.taxeditor.editor.reference.ReferenceSelectComposite;
39 import eu.etaxonomy.taxeditor.store.CdmStore;
40
41 /**
42 * @author p.ciardelli
43 * @created 13.02.2009
44 * @version 1.0
45 */
46 public class ChooseSpecimenTypeWizardPage extends WizardPage {
47 private static Logger logger = Logger
48 .getLogger(ChooseSpecimenTypeWizardPage.class);
49
50 private TaxonNameBase name;
51 private SpecimenTypeDesignation typeDesignation;
52 private WritableList typeDesignationsList;
53
54 private Combo statusCombo;
55 private Text txtDesignationType;
56 private SpecimenTypeDesignationStatus[] typeStatusArray;
57
58 private ReferenceSelectComposite referenceComposite;
59
60 /**
61 * @param typeDesignation
62 * @param typeDesignationsList
63 */
64 public ChooseSpecimenTypeWizardPage(
65 TypeDesignationBase typeDesignation, TaxonNameBase name, WritableList typeDesignationsList) {
66 super("");
67
68 Assert.isTrue(typeDesignation == null || typeDesignation instanceof SpecimenTypeDesignation,"");
69
70 this.typeDesignation = (SpecimenTypeDesignation) typeDesignation;
71 this.name = name;
72 this.typeDesignationsList = typeDesignationsList;
73
74 setTitle("Create or edit type designation");
75 setDescription("Create or edit type designation for '" + name.getTitleCache() + "\".");
76 }
77
78 /* (non-Javadoc)
79 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
80 */
81 public void createControl(Composite parent) {
82
83 // Create composite for popup dialog
84 Composite container = new Composite(parent, SWT.NULL);
85 container.setLayout(new GridLayout());
86 setControl(container);
87
88 // Create text
89 final Label lblChooseStatus = new Label(container, SWT.NONE);
90 lblChooseStatus.setText("Choose designation type status:");
91
92 // Create designation type status dropdown
93 statusCombo = new Combo(container, SWT.BORDER);
94 statusCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
95 TermVocabulary<SpecimenTypeDesignationStatus> typeStatusSet = CdmStore.getDefault().getSpecimenTypeDesignationStatus();
96 typeStatusArray = new SpecimenTypeDesignationStatus[CdmStore.getDefault().getSpecimenTypeDesignationStatus().size()];
97 int i = 0;
98 int selectedIndex = -1;
99
100 SpecimenTypeDesignationStatus typeDesignationStatus = null;
101 if (typeDesignation != null && typeDesignation.getTypeStatus() != null) {
102 typeDesignationStatus = CdmBase.deproxy(typeDesignation.getTypeStatus(),
103 SpecimenTypeDesignationStatus.class);
104 }
105
106 for (Object typeStatusObject :
107 CdmStore.getDefault().getSpecimenTypeDesignationStatus()) {
108
109 SpecimenTypeDesignationStatus typeStatus = CdmBase.deproxy(typeStatusObject,
110 SpecimenTypeDesignationStatus.class);
111 String label = typeStatus.getLabel();
112 typeStatusArray[i] = typeStatus;
113
114 if (typeDesignationStatus != null) {
115 if (typeStatus.equals(typeDesignationStatus)) {
116 selectedIndex = i;
117 }
118 }
119
120 i++;
121 statusCombo.add(label);
122 }
123
124 // Set menu to type designation status if exists
125 statusCombo.select(selectedIndex);
126
127 statusCombo.addSelectionListener(new SelectionAdapter() {
128 @Override
129 public void widgetSelected(SelectionEvent e) {
130 updatePage();
131 }
132 });
133
134 // Create text
135 final Label lblEnterText = new Label(container, SWT.NONE);
136 lblEnterText.setText("Enter designation type text:");
137
138 txtDesignationType = new Text(container, SWT.BORDER);
139 txtDesignationType.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
140 txtDesignationType.addKeyListener(new KeyAdapter() {
141 @Override
142 public void keyReleased(KeyEvent e) {
143 updatePage();
144 }
145 });
146
147 // Set text to specimen text if exists
148 if (typeDesignation != null && typeDesignation.getTypeSpecimen() != null) {
149 txtDesignationType.setText(typeDesignation.getTypeSpecimen().getTitleCache());
150 }
151
152 // Create reference input composite
153 referenceComposite = new ReferenceSelectComposite(container);
154
155 // Set text to reference text if exists
156 if (typeDesignation != null) {
157
158 referenceComposite.setMicroReference(typeDesignation.getCitationMicroReference());
159
160 if (typeDesignation.getCitation() != null) {
161 referenceComposite.setReference(typeDesignation.getCitation());
162 }
163 }
164 }
165
166
167
168 @Override
169 public boolean canFlipToNextPage() {
170 return isPageComplete();
171 }
172
173 public boolean isPageComplete() {
174 return (statusCombo.getSelectionIndex() > -1
175 && txtDesignationType.getText().length() > 0);
176 }
177
178 private void updatePage() {
179
180 getWizard().getContainer().updateButtons();
181 }
182
183 public void setPageComplete(boolean complete) {
184 super.setPageComplete(complete);
185
186 if (complete) {
187
188 ReferenceBase citation = referenceComposite.getReference();
189 String citationMicroReference = referenceComposite.getMicroReference();
190
191 SpecimenTypeDesignationStatus status = typeStatusArray[statusCombo.getSelectionIndex()];
192
193 Specimen specimen;
194 if (typeDesignation == null || typeDesignation.getTypeSpecimen() == null) {
195 specimen = Specimen.NewInstance();
196 } else {
197 specimen = CdmBase.deproxy(typeDesignation.getTypeSpecimen(), Specimen.class);
198 }
199 specimen.setTitleCache(txtDesignationType.getText());
200
201 if (typeDesignation == null) {
202 typeDesignation = new TemporarySpecimenTypeDesignation(specimen, status,
203 citation, citationMicroReference, null, true);
204 } else {
205 typeDesignation.setTypeSpecimen(specimen);
206 typeDesignation.setTypeStatus(status);
207 typeDesignation.setCitation(citation);
208 typeDesignation.setCitationMicroReference(citationMicroReference);
209 }
210
211 typeDesignationsList.remove(typeDesignation);
212 typeDesignationsList.add(typeDesignation);
213 }
214 }
215
216 public class TemporarySpecimenTypeDesignation extends
217 SpecimenTypeDesignation {
218
219 TemporarySpecimenTypeDesignation(DerivedUnitBase specimen, SpecimenTypeDesignationStatus status, ReferenceBase citation, String citationMicroReference,
220 String originalNameString, boolean isNotDesignated) {
221 super(specimen, status, citation, citationMicroReference,
222 originalNameString, isNotDesignated);
223 }
224 }
225 }