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