Features lists now sorted alphabetically. Added microreference, cache strings to...
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / eu / etaxonomy / taxeditor / propertysheet / type / wizard / ChooseNameTypeWizardPage.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.events.KeyAdapter;
18 import org.eclipse.swt.events.KeyEvent;
19 import org.eclipse.swt.events.SelectionAdapter;
20 import org.eclipse.swt.events.SelectionEvent;
21 import org.eclipse.swt.layout.GridData;
22 import org.eclipse.swt.layout.GridLayout;
23 import org.eclipse.swt.widgets.Button;
24 import org.eclipse.swt.widgets.Composite;
25 import org.eclipse.swt.widgets.Dialog;
26 import org.eclipse.swt.widgets.Label;
27 import org.eclipse.swt.widgets.Text;
28
29 import eu.etaxonomy.cdm.model.name.NameTypeDesignation;
30 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
31 import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
32 import eu.etaxonomy.cdm.model.reference.Generic;
33 import eu.etaxonomy.cdm.model.reference.ReferenceBase;
34 import eu.etaxonomy.taxeditor.TaxEditorPlugin;
35 import eu.etaxonomy.taxeditor.controller.PreferencesController;
36 import eu.etaxonomy.taxeditor.propertysheet.reference.IReferenceSearch;
37 import eu.etaxonomy.taxeditor.propertysheet.reference.ReferenceSearchDialog;
38
39 /**
40 * @author p.ciardelli
41 * @created 09.03.2009
42 * @version 1.0
43 */
44 public class ChooseNameTypeWizardPage extends WizardPage {
45 private static Logger logger = Logger
46 .getLogger(ChooseNameTypeWizardPage.class);
47
48 private TaxonNameBase name;
49 private NameTypeDesignation typeDesignation;
50 private TaxonNameBase savedName;
51
52 private ReferenceBase savedReference;
53
54 private Text txtName;
55 private Button btnClearName;
56 private Text txtReference;
57 private Button btnClearReference;
58 private Button btnRejected;
59 private Button btnConserved;
60 private Button btnLecto;
61 private Button btnNotDesignated;
62
63 private WritableList typeDesignationsList;
64
65 /**
66 * @param typeDesignation
67 * @param name
68 * @param typeDesignationsList
69 */
70 public ChooseNameTypeWizardPage(TypeDesignationBase typeDesignation,
71 TaxonNameBase name, WritableList typeDesignationsList) {
72 super("");
73
74 Assert.isTrue(typeDesignation == null || typeDesignation instanceof NameTypeDesignation);
75
76 this.typeDesignation = (NameTypeDesignation) typeDesignation;
77 this.name = name;
78 this.typeDesignationsList = typeDesignationsList;
79
80 setTitle("Create or edit type designation");
81 setDescription("Create or edit type designation for '" + name.getTitleCache() + "\".");
82 }
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 name text
97 final Label lblName = new Label(container, SWT.NONE);
98 lblName.setText("Choose a name either by searching or entering it as free text:");
99
100 // Create 3-columned composite for name
101 Composite nameComposite = new Composite(container, SWT.NULL);
102 nameComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
103 GridLayout gridLayout = new GridLayout();
104 gridLayout.numColumns = 3;
105 gridLayout.marginHeight = 0;
106 gridLayout.marginWidth = 0;
107 nameComposite.setLayout(gridLayout);
108
109 // Create name input
110 txtName = new Text(nameComposite, SWT.BORDER);
111 txtName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
112 txtName.addKeyListener(new KeyAdapter() {
113 @Override
114 public void keyReleased(KeyEvent e) {
115 updatePage();
116 }
117 });
118
119 // Create name search button
120 Button btnSearchName = new Button(nameComposite, SWT.NONE);
121 btnSearchName.setEnabled(true);
122 btnSearchName.setLayoutData(new GridData());
123 btnSearchName.setText("Search ...");
124 btnSearchName.addSelectionListener(new SelectionAdapter() {
125
126 // Popup reference search
127 public void widgetSelected(SelectionEvent e) {
128 popupNameSearch();
129 }
130 });
131
132 // Create clear name button
133 btnClearName = new Button(nameComposite, SWT.NONE);
134 btnClearName.setEnabled(false);
135 btnClearName.setText("Clear");
136 btnClearName.addSelectionListener(new SelectionAdapter() {
137
138 // Clear selected reference
139 public void widgetSelected(SelectionEvent e) {
140 clearName();
141 }
142 });
143
144 // Set text to name text if exists, disable ability to edit name
145 if (typeDesignation != null && typeDesignation.getTypeName() != null) {
146
147 savedName = typeDesignation.getTypeName();
148 txtName.setText(savedName.getTitleCache());
149 txtName.setEditable(false);
150
151 btnClearName.setEnabled(false);
152 btnSearchName.setEnabled(false);
153 }
154
155 // Tell user when he is entering a new name
156 Label lblNewNameFeedback = new Label(container, SWT.NONE);
157 lblNewNameFeedback.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, false));
158 lblNewNameFeedback.setFont(
159 TaxEditorPlugin.getDefault().italicizeFont(lblNewNameFeedback.getFont()));
160 lblNewNameFeedback.setText("Existing names can not be edited.");
161
162 // Create 2-columned composite for name
163 Composite booleansComposite = new Composite(container, SWT.NULL);
164 booleansComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
165 GridLayout gridLayout2 = new GridLayout();
166 gridLayout2.numColumns = 2;
167 gridLayout2.marginHeight = 15;
168 gridLayout2.marginWidth = 0;
169 booleansComposite.setLayout(gridLayout2);
170
171 btnRejected = new Button(booleansComposite, SWT.CHECK);
172 btnRejected.setText("Is rejected type");
173 btnRejected.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
174
175 btnConserved = new Button(booleansComposite, SWT.CHECK);
176 btnConserved.setText("Is conserved type");
177 btnConserved.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
178
179 btnLecto = new Button(booleansComposite, SWT.CHECK);
180 btnLecto.setText("Is lecto type");
181 btnLecto.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
182
183 btnNotDesignated = new Button(booleansComposite, SWT.CHECK);
184 btnNotDesignated.setText("Is not designated");
185 btnNotDesignated.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
186
187 // Set any boolean values
188 if (typeDesignation != null) {
189 if (typeDesignation.isRejectedType()) {
190 btnRejected.setSelection(true);
191 }
192 if (typeDesignation.isConservedType()) {
193 btnConserved.setSelection(true);
194 }
195 if (typeDesignation.isLectoType()) {
196 btnLecto.setSelection(true);
197 }
198 if (typeDesignation.isNotDesignated()) {
199 btnNotDesignated.setSelection(true);
200 }
201 }
202
203 // Create reference text
204 final Label lblReference = new Label(container, SWT.NONE);
205 lblReference.setText("Choose a reference either by searching or entering it as free text:");
206
207 // Create 3-columned composite for reference
208 Composite referenceComposite = new Composite(container, SWT.NULL);
209 referenceComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
210 GridLayout gridLayout3 = new GridLayout();
211 gridLayout3.numColumns = 3;
212 gridLayout3.marginHeight = 0;
213 gridLayout3.marginWidth = 0;
214 referenceComposite.setLayout(gridLayout3);
215
216 // Create reference input
217 txtReference = new Text(referenceComposite, SWT.BORDER);
218 txtReference.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
219
220 // Create reference search button
221 Button btnSearchReference = new Button(referenceComposite, SWT.NONE);
222 btnSearchReference.setEnabled(true);
223 btnSearchReference.setLayoutData(new GridData());
224 btnSearchReference.setText("Search ...");
225 btnSearchReference.addSelectionListener(new SelectionAdapter() {
226
227 // Popup reference search
228 public void widgetSelected(SelectionEvent e) {
229 popupReferenceSearch();
230 }
231 });
232
233 // Create clear reference button
234 btnClearReference = new Button(referenceComposite, SWT.NONE);
235 btnClearReference.setEnabled(false);
236 btnClearReference.setText("Clear");
237 btnClearReference.addSelectionListener(new SelectionAdapter() {
238
239 // Clear selected reference
240 public void widgetSelected(SelectionEvent e) {
241 clearReference();
242 }
243 });
244
245 // Set text to reference text if exists
246 if (typeDesignation != null && typeDesignation.getCitation() != null) {
247
248 savedReference = typeDesignation.getCitation();
249 txtReference.setText(savedReference.getTitleCache());
250 txtReference.setEditable(false);
251
252 btnClearReference.setEnabled(true);
253 }
254
255 // Tell user when he is entering a new reference
256 Label lblNewRefFeedback = new Label(container, SWT.NONE);
257 lblNewRefFeedback.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, false));
258 lblNewRefFeedback.setFont(
259 TaxEditorPlugin.getDefault().italicizeFont(lblNewRefFeedback.getFont()));
260 lblNewRefFeedback.setText("Existing references can only be edited in property sheet.");
261 }
262
263 /**
264 *
265 */
266 protected void popupNameSearch() {
267 Dialog dialog = new NameSearchDialog(getShell());
268 Object value = ((NameSearchDialog) dialog).open();
269
270 if (value instanceof TaxonNameBase) {
271 setSavedName((TaxonNameBase) value);
272 updatePage();
273 }
274 }
275
276 protected void popupReferenceSearch() {
277 Dialog dialog = new ReferenceSearchDialog(getShell(),
278 IReferenceSearch.BIBREF);
279 Object value = ((ReferenceSearchDialog) dialog).open();
280
281 if (value instanceof ReferenceBase) {
282 setSavedReference((ReferenceBase) value);
283 }
284 }
285
286 /**
287 * @param value
288 */
289 private void setSavedName(TaxonNameBase savedName) {
290
291 this.savedName = savedName;
292
293 txtName.setText(savedName.getTitleCache());
294 txtName.setEditable(false);
295
296 btnClearName.setEnabled(true);
297 }
298
299 /**
300 * @param value
301 */
302 private void setSavedReference(ReferenceBase reference) {
303
304 savedReference = reference;
305
306 txtReference.setText(reference.getTitleCache());
307 txtReference.setEditable(false);
308
309 btnClearReference.setEnabled(true);
310 }
311
312 /**
313 *
314 */
315 protected void clearName() {
316 savedName = null;
317
318 txtName.setText("");
319 txtName.setEditable(true);
320
321 btnClearName.setEnabled(false);
322 }
323
324 /**
325 *
326 */
327 protected void clearReference() {
328 savedReference = null;
329
330 txtReference.setText("");
331 txtReference.setEditable(true);
332
333 btnClearReference.setEnabled(false);
334 }
335
336 @Override
337 public boolean canFlipToNextPage() {
338 return isPageComplete();
339 }
340
341 public boolean isPageComplete() {
342 return (txtName.getText().length() > 0);
343 }
344
345 private void updatePage() {
346
347 getWizard().getContainer().updateButtons();
348 }
349
350 public void setPageComplete(boolean complete) {
351 super.setPageComplete(complete);
352
353 if (complete) {
354
355 TaxonNameBase typeName = null;
356 if (savedName != null) {
357 typeName = savedName;
358 } else {
359 if (!txtName.getText().equals("")) {
360 typeName = PreferencesController.getInstanceOfPreferredNameClass();
361 typeName.setTitleCache(txtName.getText());
362 }
363 }
364
365 ReferenceBase citation = null;
366 if (savedReference != null) {
367 citation = savedReference;
368 } else {
369 if (!txtReference.getText().equals("")) {
370 citation = Generic.NewInstance();
371 citation.setTitleCache(txtReference.getText());
372 }
373 }
374
375 if (typeDesignation == null) {
376 typeDesignation = new TemporaryNameTypeDesignation(typeName, citation, null,
377 null, btnRejected.getSelection(), btnConserved.getSelection(), btnNotDesignated.getSelection());
378 typeDesignation.setLectoType(btnLecto.getSelection());
379 } else {
380 typeDesignation.setRejectedType(btnRejected.getSelection());
381 typeDesignation.setConservedType(btnConserved.getSelection());
382 typeDesignation.setLectoType(btnLecto.getSelection());
383 typeDesignation.setNotDesignated(btnNotDesignated.getSelection());
384 typeDesignation.setCitation(citation);
385 }
386
387 typeDesignationsList.remove(typeDesignation);
388 typeDesignationsList.add(typeDesignation);
389 }
390 }
391
392 public class TemporaryNameTypeDesignation extends
393 NameTypeDesignation {
394
395 /**
396 * @param typeName
397 * @param citation
398 * @param object
399 * @param object2
400 * @param selection
401 * @param selection2
402 * @param selection3
403 */
404 // public TemporaryNameTypeDesignation() {
405 // super(null, null, null, null, false, false, false);
406 // setLectoType(false);
407 // }
408
409 public TemporaryNameTypeDesignation(TaxonNameBase typeName, ReferenceBase citation, String citationMicroReference,
410 String originalNameString, boolean isRejectedType, boolean isConservedType, boolean isNotDesignated) {
411 super(typeName, citation, citationMicroReference, originalNameString,
412 isRejectedType, isConservedType, isNotDesignated);
413 }
414 }
415 }