Completed type module.
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / 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.Button;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.swt.widgets.Dialog;
27 import org.eclipse.swt.widgets.Label;
28 import org.eclipse.swt.widgets.Text;
29
30 import eu.etaxonomy.cdm.model.common.CdmBase;
31 import eu.etaxonomy.cdm.model.common.TermVocabulary;
32 import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
33 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
34 import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
35 import eu.etaxonomy.cdm.model.name.TypeDesignationStatus;
36 import eu.etaxonomy.cdm.model.occurrence.DerivedUnitBase;
37 import eu.etaxonomy.cdm.model.occurrence.Specimen;
38 import eu.etaxonomy.cdm.model.reference.Generic;
39 import eu.etaxonomy.cdm.model.reference.ReferenceBase;
40 import eu.etaxonomy.taxeditor.TaxEditorPlugin;
41 import eu.etaxonomy.taxeditor.model.CdmSessionDataRepository;
42 import eu.etaxonomy.taxeditor.propertysheet.reference.IReferenceSearch;
43 import eu.etaxonomy.taxeditor.propertysheet.reference.ReferenceSearchDialog;
44
45 /**
46 * @author p.ciardelli
47 * @created 13.02.2009
48 * @version 1.0
49 */
50 public class ChooseSpecimenTypeWizardPage extends WizardPage {
51 private static Logger logger = Logger
52 .getLogger(ChooseSpecimenTypeWizardPage.class);
53
54 private TaxonNameBase name;
55 private SpecimenTypeDesignation typeDesignation;
56 private WritableList typeDesignationsList;
57
58 private ReferenceBase savedReference;
59
60 private CCombo statusCombo;
61 private Text txtDesignationType;
62 private TypeDesignationStatus[] typeStatusArray;
63 private Text txtReference;
64 private Button btnClearReference;
65
66
67 /**
68 * @param typeDesignation
69 * @param typeDesignationsList
70 */
71 public ChooseSpecimenTypeWizardPage(
72 TypeDesignationBase typeDesignation, TaxonNameBase name, WritableList typeDesignationsList) {
73 super("");
74
75 Assert.isTrue(typeDesignation == null || typeDesignation instanceof SpecimenTypeDesignation,"");
76
77 this.typeDesignation = (SpecimenTypeDesignation) typeDesignation;
78 this.name = name;
79 this.typeDesignationsList = typeDesignationsList;
80
81 setTitle("Create or edit type designation");
82 setDescription("Create or edit type designation for '" + name.getTitleCache() + "\".");
83 }
84
85 /* (non-Javadoc)
86 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
87 */
88 @Override
89 public void createControl(Composite parent) {
90
91 // Create composite for popup dialog
92 Composite container = new Composite(parent, SWT.NULL);
93 container.setLayout(new GridLayout());
94 setControl(container);
95
96 // Create text
97 final Label lblChooseStatus = new Label(container, SWT.NONE);
98 lblChooseStatus.setText("Choose designation type status:");
99
100 // Create designation type status dropdown
101 statusCombo = new CCombo(container, SWT.BORDER);
102 statusCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
103 TermVocabulary<TypeDesignationStatus> typeStatusSet = CdmSessionDataRepository.getDefault().getTypeDesignationStatus();
104 typeStatusArray = new TypeDesignationStatus[CdmSessionDataRepository.getDefault().getTypeDesignationStatus().size()];
105 int i = 0;
106 int selectedIndex = -1;
107 for (TypeDesignationStatus typeStatus :
108 CdmSessionDataRepository.getDefault().getTypeDesignationStatus()) {
109 String label = typeStatus.getLabel();
110 typeStatusArray[i] = typeStatus;
111
112 if (typeDesignation != null && typeDesignation.getTypeStatus() != null) {
113 if (typeStatus.equals(typeDesignation.getTypeStatus())) {
114 selectedIndex = i;
115 }
116 }
117
118 i++;
119 statusCombo.add(label);
120 }
121
122 // Set menu to type designation status if exists
123 statusCombo.select(selectedIndex);
124
125 statusCombo.addSelectionListener(new SelectionAdapter() {
126 @Override
127 public void widgetSelected(SelectionEvent e) {
128 updatePage();
129 }
130 });
131
132 // Create text
133 final Label lblEnterText = new Label(container, SWT.NONE);
134 lblEnterText.setText("Enter designation type text:");
135
136 txtDesignationType = new Text(container, SWT.BORDER);
137 txtDesignationType.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
138 txtDesignationType.addKeyListener(new KeyAdapter() {
139 @Override
140 public void keyReleased(KeyEvent e) {
141 updatePage();
142 }
143 });
144
145 // Set text to specimen text if exists
146 if (typeDesignation != null && typeDesignation.getTypeSpecimen() != null) {
147 txtDesignationType.setText(typeDesignation.getTypeSpecimen().getTitleCache());
148 }
149
150 // Create reference text
151 final Label lblReference = new Label(container, SWT.NONE);
152 lblReference.setText("Choose a reference either by searching or entering it as free text:");
153
154 // Create 3-columned composite for reference
155 Composite referenceComposite = new Composite(container, SWT.NULL);
156 referenceComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
157 GridLayout gridLayout = new GridLayout();
158 gridLayout.numColumns = 3;
159 gridLayout.marginHeight = 0;
160 gridLayout.marginWidth = 0;
161 referenceComposite.setLayout(gridLayout);
162
163 // Create reference input
164 txtReference = new Text(referenceComposite, SWT.BORDER);
165 txtReference.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
166
167 // Create reference search button
168 Button btnSearchReference = new Button(referenceComposite, SWT.NONE);
169 btnSearchReference.setEnabled(true);
170 btnSearchReference.setLayoutData(new GridData());
171 btnSearchReference.setText("Search ...");
172 btnSearchReference.addSelectionListener(new SelectionAdapter() {
173
174 // Popup reference search
175 public void widgetSelected(SelectionEvent e) {
176 popupSearch();
177 }
178 });
179
180 // Create clear reference button
181 btnClearReference = new Button(referenceComposite, SWT.NONE);
182 btnClearReference.setEnabled(false);
183 btnClearReference.setText("Clear");
184 btnClearReference.addSelectionListener(new SelectionAdapter() {
185
186 // Clear selected reference
187 public void widgetSelected(SelectionEvent e) {
188 clearReference();
189 }
190 });
191
192 // Set text to reference text if exists
193 if (typeDesignation != null && typeDesignation.getCitation() != null) {
194
195 savedReference = typeDesignation.getCitation();
196 txtReference.setText(savedReference.getTitleCache());
197 txtReference.setEditable(false);
198
199 btnClearReference.setEnabled(true);
200 }
201
202 // Tell user when he is entering a new reference
203 Label lblNewRefFeedback = new Label(container, SWT.NONE);
204 lblNewRefFeedback.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, false));
205 lblNewRefFeedback.setFont(
206 TaxEditorPlugin.getDefault().italicizeFont(lblNewRefFeedback.getFont()));
207 lblNewRefFeedback.setText("Existing references can only be edited in property sheet.");
208 }
209
210 /**
211 *
212 */
213 protected void popupSearch() {
214 Dialog dialog = new ReferenceSearchDialog(getShell(),
215 IReferenceSearch.BIBREF);
216 Object value = ((ReferenceSearchDialog) dialog).open();
217
218 if (value instanceof ReferenceBase) {
219 setSavedReference((ReferenceBase) value);
220 }
221 }
222
223 /**
224 * @param value
225 */
226 private void setSavedReference(ReferenceBase reference) {
227
228 savedReference = reference;
229
230 txtReference.setText(reference.getTitleCache());
231 txtReference.setEditable(false);
232
233 btnClearReference.setEnabled(true);
234 }
235
236 /**
237 *
238 */
239 protected void clearReference() {
240 savedReference = null;
241
242 txtReference.setText("");
243 txtReference.setEditable(true);
244
245 btnClearReference.setEnabled(false);
246 }
247
248 @Override
249 public boolean canFlipToNextPage() {
250 return isPageComplete();
251 }
252
253 public boolean isPageComplete() {
254 return (statusCombo.getSelectionIndex() > -1
255 && txtDesignationType.getText().length() > 0);
256 }
257
258 private void updatePage() {
259
260 getWizard().getContainer().updateButtons();
261 }
262
263 public void setPageComplete(boolean complete) {
264 super.setPageComplete(complete);
265
266 if (complete) {
267
268 ReferenceBase citation = null;
269 if (savedReference != null) {
270 citation = savedReference;
271 } else {
272 if (!txtReference.getText().equals("")) {
273 citation = Generic.NewInstance();
274 citation.setTitleCache(txtReference.getText());
275 }
276 }
277
278 TypeDesignationStatus status = typeStatusArray[statusCombo.getSelectionIndex()];
279
280 Specimen specimen;
281 if (typeDesignation == null || typeDesignation.getTypeSpecimen() == null) {
282 specimen = Specimen.NewInstance();
283 } else {
284 specimen = CdmBase.deproxy(typeDesignation.getTypeSpecimen(), Specimen.class);
285 }
286 specimen.setTitleCache(txtDesignationType.getText());
287
288 if (typeDesignation == null) {
289 typeDesignation = new TemporarySpecimenTypeDesignation(specimen, status,
290 citation, null, null, true);
291 } else {
292 typeDesignation.setTypeSpecimen(specimen);
293 typeDesignation.setTypeStatus(status);
294 typeDesignation.setCitation(citation);
295 }
296
297 typeDesignationsList.remove(typeDesignation);
298 typeDesignationsList.add(typeDesignation);
299 }
300 }
301
302 public class TemporarySpecimenTypeDesignation extends
303 SpecimenTypeDesignation {
304
305 TemporarySpecimenTypeDesignation(DerivedUnitBase specimen, TypeDesignationStatus status, ReferenceBase citation, String citationMicroReference,
306 String originalNameString, boolean isNotDesignated) {
307 super(specimen, status, citation, citationMicroReference,
308 originalNameString, isNotDesignated);
309 }
310 }
311 }