.
[taxeditor.git] / taxeditor-editor / src / main / java / 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.ReferenceBase;
33 import eu.etaxonomy.taxeditor.editor.reference.ReferenceSelectComposite;
34 import eu.etaxonomy.taxeditor.model.Resources;
35 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
36
37 /**
38 * @author p.ciardelli
39 * @created 09.03.2009
40 * @version 1.0
41 */
42 public class ChooseNameTypeWizardPage extends WizardPage {
43 private static Logger logger = Logger
44 .getLogger(ChooseNameTypeWizardPage.class);
45
46 private TaxonNameBase<?, ?> name;
47 private NameTypeDesignation typeDesignation;
48 private TaxonNameBase<?, ?> savedName;
49
50 private Text txtName;
51 private Button btnClearName;
52 private Button btnRejected;
53 private Button btnConserved;
54 private Button btnLecto;
55 private Button btnNotDesignated;
56
57 private WritableList typeDesignationsList;
58
59 private ReferenceSelectComposite referenceComposite;
60
61 /**
62 * @param typeDesignation
63 * @param name
64 * @param typeDesignationsList
65 */
66 public ChooseNameTypeWizardPage(TypeDesignationBase<?> typeDesignation,
67 TaxonNameBase<?, ?> name, WritableList typeDesignationsList) {
68 super("");
69
70 Assert.isTrue(typeDesignation == null || typeDesignation instanceof NameTypeDesignation);
71
72 this.typeDesignation = (NameTypeDesignation) typeDesignation;
73 this.name = name;
74 this.typeDesignationsList = typeDesignationsList;
75
76 setTitle("Create or edit type designation");
77 setDescription("Create or edit type designation for '" + name.getTitleCache() + "\".");
78 logger.trace(this.getClass().getSimpleName() + " created.");
79 }
80
81
82 /* (non-Javadoc)
83 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
84 */
85 public void createControl(Composite parent) {
86
87 // Create composite for popup dialog
88 Composite container = new Composite(parent, SWT.NULL);
89 container.setLayout(new GridLayout());
90 setControl(container);
91
92 // Create name text
93 final Label lblName = new Label(container, SWT.NONE);
94 lblName.setText("Choose a name either by searching or entering it as free text:");
95
96 // Create 3-columned composite for name
97 Composite nameComposite = new Composite(container, SWT.NULL);
98 nameComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
99 GridLayout gridLayout = new GridLayout();
100 gridLayout.numColumns = 3;
101 gridLayout.marginHeight = 0;
102 gridLayout.marginWidth = 0;
103 nameComposite.setLayout(gridLayout);
104
105 // Create name input
106 // TODO replace w NameSelectComposite
107 txtName = new Text(nameComposite, SWT.BORDER);
108 txtName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
109 txtName.addKeyListener(new KeyAdapter() {
110 @Override
111 public void keyReleased(KeyEvent e) {
112 updatePage();
113 }
114 });
115
116 // Create name search button
117 Button btnSearchName = new Button(nameComposite, SWT.NONE);
118 btnSearchName.setEnabled(true);
119 btnSearchName.setLayoutData(new GridData());
120 btnSearchName.setText("Search ...");
121 btnSearchName.addSelectionListener(new SelectionAdapter() {
122
123 // Popup reference search
124 public void widgetSelected(SelectionEvent e) {
125 popupNameSearch();
126 }
127 });
128
129 // Create clear name button
130 btnClearName = new Button(nameComposite, SWT.NONE);
131 btnClearName.setEnabled(false);
132 btnClearName.setText("Clear");
133 btnClearName.addSelectionListener(new SelectionAdapter() {
134
135 // Clear selected reference
136 public void widgetSelected(SelectionEvent e) {
137 clearName();
138 }
139 });
140
141 // Set text to name text if exists, disable ability to edit name
142 if (typeDesignation != null && typeDesignation.getTypeName() != null) {
143
144 savedName = typeDesignation.getTypeName();
145 txtName.setText(savedName.getTitleCache());
146 txtName.setEditable(false);
147
148 btnClearName.setEnabled(false);
149 btnSearchName.setEnabled(false);
150 }
151
152 // Tell user when he is entering a new name
153 Label lblNewNameFeedback = new Label(container, SWT.NONE);
154 lblNewNameFeedback.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, false));
155 lblNewNameFeedback.setFont(
156 Resources.italicizeFont(lblNewNameFeedback.getFont()));
157 lblNewNameFeedback.setText("Existing names can not be edited.");
158
159 // Create 2-columned composite for name
160 Composite booleansComposite = new Composite(container, SWT.NULL);
161 booleansComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
162 GridLayout gridLayout2 = new GridLayout();
163 gridLayout2.numColumns = 2;
164 gridLayout2.marginHeight = 15;
165 gridLayout2.marginWidth = 0;
166 booleansComposite.setLayout(gridLayout2);
167
168 btnRejected = new Button(booleansComposite, SWT.CHECK);
169 btnRejected.setText("Is rejected type");
170 btnRejected.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
171
172 btnConserved = new Button(booleansComposite, SWT.CHECK);
173 btnConserved.setText("Is conserved type");
174 btnConserved.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
175
176 btnLecto = new Button(booleansComposite, SWT.CHECK);
177 btnLecto.setText("Is lecto type");
178 btnLecto.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
179
180 btnNotDesignated = new Button(booleansComposite, SWT.CHECK);
181 btnNotDesignated.setText("Is not designated");
182 btnNotDesignated.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
183
184 // Set any boolean values
185 if (typeDesignation != null) {
186 if (typeDesignation.isRejectedType()) {
187 btnRejected.setSelection(true);
188 }
189 if (typeDesignation.isConservedType()) {
190 btnConserved.setSelection(true);
191 }
192 if (typeDesignation.isLectoType()) {
193 btnLecto.setSelection(true);
194 }
195 if (typeDesignation.isNotDesignated()) {
196 btnNotDesignated.setSelection(true);
197 }
198 }
199
200 // Create reference input composite
201 referenceComposite = new ReferenceSelectComposite(container);
202
203 // Set text to reference text if exists
204 if (typeDesignation != null && typeDesignation.getCitation() != null) {
205 referenceComposite.setMicroReference(typeDesignation.getCitationMicroReference());
206 if (typeDesignation.getCitation() != null) {
207 referenceComposite.setReference(typeDesignation.getCitation());
208 }
209 }
210 }
211
212 /**
213 *
214 */
215 protected void popupNameSearch() {
216 Dialog dialog = new NameSearchDialog(getShell());
217 Object value = ((NameSearchDialog) dialog).open();
218
219 if (value instanceof TaxonNameBase) {
220 setSavedName((TaxonNameBase<?, ?>) value);
221 updatePage();
222 }
223 }
224
225 /**
226 * @param value
227 */
228 private void setSavedName(TaxonNameBase<?, ?> savedName) {
229
230 this.savedName = savedName;
231
232 txtName.setText(savedName.getTitleCache());
233 txtName.setEditable(false);
234
235 btnClearName.setEnabled(true);
236 }
237
238 /**
239 *
240 */
241 protected void clearName() {
242 savedName = null;
243
244 txtName.setText("");
245 txtName.setEditable(true);
246
247 btnClearName.setEnabled(false);
248 }
249
250 @Override
251 public boolean canFlipToNextPage() {
252 return isPageComplete();
253 }
254
255 public boolean isPageComplete() {
256 return (txtName.getText().length() > 0);
257 }
258
259 private void updatePage() {
260
261 getWizard().getContainer().updateButtons();
262 }
263
264 public void setPageComplete(boolean complete) {
265 super.setPageComplete(complete);
266
267 if (complete) {
268
269 TaxonNameBase<?, ?> typeName = null;
270 if (savedName != null) {
271 typeName = savedName;
272 } else {
273 if (!txtName.getText().equals("")) {
274 typeName = PreferencesUtil.getInstanceOfPreferredNameClass();
275 typeName.setTitleCache(txtName.getText());
276 }
277 }
278
279 ReferenceBase<?> citation = referenceComposite.getReference();
280 String citationMicroReference = referenceComposite.getMicroReference();
281
282 if (typeDesignation == null) {
283 typeDesignation = new TemporaryNameTypeDesignation(typeName, citation, citationMicroReference,
284 null, btnRejected.getSelection(), btnConserved.getSelection(), btnLecto.getSelection(), btnNotDesignated.getSelection());
285 typeDesignation.setLectoType(btnLecto.getSelection());
286 } else {
287 typeDesignation.setRejectedType(btnRejected.getSelection());
288 typeDesignation.setConservedType(btnConserved.getSelection());
289 typeDesignation.setLectoType(btnLecto.getSelection());
290 typeDesignation.setNotDesignated(btnNotDesignated.getSelection());
291 typeDesignation.setCitation(citation);
292 typeDesignation.setCitationMicroReference(citationMicroReference);
293 }
294
295 typeDesignationsList.remove(typeDesignation);
296 typeDesignationsList.add(typeDesignation);
297 }
298 }
299
300 public class TemporaryNameTypeDesignation extends
301 NameTypeDesignation {
302
303 /**
304 *
305 */
306 private static final long serialVersionUID = 7309597024482795166L;
307
308 public TemporaryNameTypeDesignation(TaxonNameBase<?, ?> typeName, ReferenceBase<?> citation, String citationMicroReference,
309 String originalNameString, boolean isRejectedType, boolean isConservedType, boolean isLectoType, boolean isNotDesignated) {
310 super(typeName, citation, citationMicroReference, originalNameString,
311 isRejectedType, isConservedType, isLectoType, isNotDesignated);
312 }
313 }
314 }