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