switched plugin version to 2.1
[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 public void createControl(Composite parent) {
89
90 // Create composite for popup dialog
91 Composite container = new Composite(parent, SWT.NULL);
92 container.setLayout(new GridLayout());
93 setControl(container);
94
95 // Create text
96 final Label lblChooseStatus = new Label(container, SWT.NONE);
97 lblChooseStatus.setText("Choose designation type status:");
98
99 // Create designation type status dropdown
100 statusCombo = new CCombo(container, SWT.BORDER);
101 statusCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
102 TermVocabulary<TypeDesignationStatus> typeStatusSet = CdmSessionDataRepository.getDefault().getTypeDesignationStatus();
103 typeStatusArray = new TypeDesignationStatus[CdmSessionDataRepository.getDefault().getTypeDesignationStatus().size()];
104 int i = 0;
105 int selectedIndex = -1;
106 for (TypeDesignationStatus typeStatus :
107 CdmSessionDataRepository.getDefault().getTypeDesignationStatus()) {
108 String label = typeStatus.getLabel();
109 typeStatusArray[i] = typeStatus;
110
111 if (typeDesignation != null && typeDesignation.getTypeStatus() != null) {
112 if (typeStatus.equals(typeDesignation.getTypeStatus())) {
113 selectedIndex = i;
114 }
115 }
116
117 i++;
118 statusCombo.add(label);
119 }
120
121 // Set menu to type designation status if exists
122 statusCombo.select(selectedIndex);
123
124 statusCombo.addSelectionListener(new SelectionAdapter() {
125 @Override
126 public void widgetSelected(SelectionEvent e) {
127 updatePage();
128 }
129 });
130
131 // Create text
132 final Label lblEnterText = new Label(container, SWT.NONE);
133 lblEnterText.setText("Enter designation type text:");
134
135 txtDesignationType = new Text(container, SWT.BORDER);
136 txtDesignationType.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
137 txtDesignationType.addKeyListener(new KeyAdapter() {
138 @Override
139 public void keyReleased(KeyEvent e) {
140 updatePage();
141 }
142 });
143
144 // Set text to specimen text if exists
145 if (typeDesignation != null && typeDesignation.getTypeSpecimen() != null) {
146 txtDesignationType.setText(typeDesignation.getTypeSpecimen().getTitleCache());
147 }
148
149 // Create reference text
150 final Label lblReference = new Label(container, SWT.NONE);
151 lblReference.setText("Choose a reference either by searching or entering it as free text:");
152
153 // Create 3-columned composite for reference
154 Composite referenceComposite = new Composite(container, SWT.NULL);
155 referenceComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
156 GridLayout gridLayout = new GridLayout();
157 gridLayout.numColumns = 3;
158 gridLayout.marginHeight = 0;
159 gridLayout.marginWidth = 0;
160 referenceComposite.setLayout(gridLayout);
161
162 // Create reference input
163 txtReference = new Text(referenceComposite, SWT.BORDER);
164 txtReference.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
165
166 // Create reference search button
167 Button btnSearchReference = new Button(referenceComposite, SWT.NONE);
168 btnSearchReference.setEnabled(true);
169 btnSearchReference.setLayoutData(new GridData());
170 btnSearchReference.setText("Search ...");
171 btnSearchReference.addSelectionListener(new SelectionAdapter() {
172
173 // Popup reference search
174 public void widgetSelected(SelectionEvent e) {
175 popupSearch();
176 }
177 });
178
179 // Create clear reference button
180 btnClearReference = new Button(referenceComposite, SWT.NONE);
181 btnClearReference.setEnabled(false);
182 btnClearReference.setText("Clear");
183 btnClearReference.addSelectionListener(new SelectionAdapter() {
184
185 // Clear selected reference
186 public void widgetSelected(SelectionEvent e) {
187 clearReference();
188 }
189 });
190
191 // Set text to reference text if exists
192 if (typeDesignation != null && typeDesignation.getCitation() != null) {
193
194 savedReference = typeDesignation.getCitation();
195 txtReference.setText(savedReference.getTitleCache());
196 txtReference.setEditable(false);
197
198 btnClearReference.setEnabled(true);
199 }
200
201 // Tell user when he is entering a new reference
202 Label lblNewRefFeedback = new Label(container, SWT.NONE);
203 lblNewRefFeedback.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, false));
204 lblNewRefFeedback.setFont(
205 TaxEditorPlugin.getDefault().italicizeFont(lblNewRefFeedback.getFont()));
206 lblNewRefFeedback.setText("Existing references can only be edited in property sheet.");
207 }
208
209 /**
210 *
211 */
212 protected void popupSearch() {
213 Dialog dialog = new ReferenceSearchDialog(getShell(),
214 IReferenceSearch.BIBREF);
215 Object value = ((ReferenceSearchDialog) dialog).open();
216
217 if (value instanceof ReferenceBase) {
218 setSavedReference((ReferenceBase) value);
219 }
220 }
221
222 /**
223 * @param value
224 */
225 private void setSavedReference(ReferenceBase reference) {
226
227 savedReference = reference;
228
229 txtReference.setText(reference.getTitleCache());
230 txtReference.setEditable(false);
231
232 btnClearReference.setEnabled(true);
233 }
234
235 /**
236 *
237 */
238 protected void clearReference() {
239 savedReference = null;
240
241 txtReference.setText("");
242 txtReference.setEditable(true);
243
244 btnClearReference.setEnabled(false);
245 }
246
247 @Override
248 public boolean canFlipToNextPage() {
249 return isPageComplete();
250 }
251
252 public boolean isPageComplete() {
253 return (statusCombo.getSelectionIndex() > -1
254 && txtDesignationType.getText().length() > 0);
255 }
256
257 private void updatePage() {
258
259 getWizard().getContainer().updateButtons();
260 }
261
262 public void setPageComplete(boolean complete) {
263 super.setPageComplete(complete);
264
265 if (complete) {
266
267 ReferenceBase citation = null;
268 if (savedReference != null) {
269 citation = savedReference;
270 } else {
271 if (!txtReference.getText().equals("")) {
272 citation = Generic.NewInstance();
273 citation.setTitleCache(txtReference.getText());
274 }
275 }
276
277 TypeDesignationStatus status = typeStatusArray[statusCombo.getSelectionIndex()];
278
279 Specimen specimen;
280 if (typeDesignation == null || typeDesignation.getTypeSpecimen() == null) {
281 specimen = Specimen.NewInstance();
282 } else {
283 specimen = CdmBase.deproxy(typeDesignation.getTypeSpecimen(), Specimen.class);
284 }
285 specimen.setTitleCache(txtDesignationType.getText());
286
287 if (typeDesignation == null) {
288 typeDesignation = new TemporarySpecimenTypeDesignation(specimen, status,
289 citation, null, null, true);
290 } else {
291 typeDesignation.setTypeSpecimen(specimen);
292 typeDesignation.setTypeStatus(status);
293 typeDesignation.setCitation(citation);
294 }
295
296 typeDesignationsList.remove(typeDesignation);
297 typeDesignationsList.add(typeDesignation);
298 }
299 }
300
301 public class TemporarySpecimenTypeDesignation extends
302 SpecimenTypeDesignation {
303
304 TemporarySpecimenTypeDesignation(DerivedUnitBase specimen, TypeDesignationStatus status, ReferenceBase citation, String citationMicroReference,
305 String originalNameString, boolean isNotDesignated) {
306 super(specimen, status, citation, citationMicroReference,
307 originalNameString, isNotDesignated);
308 }
309 }
310 }