p2izing the editor
[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.ReferenceBase;
33 import eu.etaxonomy.taxeditor.TaxEditorPlugin;
34 import eu.etaxonomy.taxeditor.controller.PreferencesController;
35 import eu.etaxonomy.taxeditor.editor.reference.ReferenceSelectComposite;
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 }
79
80
81 /* (non-Javadoc)
82 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
83 */
84 public void createControl(Composite parent) {
85
86 // Create composite for popup dialog
87 Composite container = new Composite(parent, SWT.NULL);
88 container.setLayout(new GridLayout());
89 setControl(container);
90
91 // Create name text
92 final Label lblName = new Label(container, SWT.NONE);
93 lblName.setText("Choose a name either by searching or entering it as free text:");
94
95 // Create 3-columned composite for name
96 Composite nameComposite = new Composite(container, SWT.NULL);
97 nameComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
98 GridLayout gridLayout = new GridLayout();
99 gridLayout.numColumns = 3;
100 gridLayout.marginHeight = 0;
101 gridLayout.marginWidth = 0;
102 nameComposite.setLayout(gridLayout);
103
104 // Create name input
105 txtName = new Text(nameComposite, SWT.BORDER);
106 txtName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
107 txtName.addKeyListener(new KeyAdapter() {
108 @Override
109 public void keyReleased(KeyEvent e) {
110 updatePage();
111 }
112 });
113
114 // Create name search button
115 Button btnSearchName = new Button(nameComposite, SWT.NONE);
116 btnSearchName.setEnabled(true);
117 btnSearchName.setLayoutData(new GridData());
118 btnSearchName.setText("Search ...");
119 btnSearchName.addSelectionListener(new SelectionAdapter() {
120
121 // Popup reference search
122 public void widgetSelected(SelectionEvent e) {
123 popupNameSearch();
124 }
125 });
126
127 // Create clear name button
128 btnClearName = new Button(nameComposite, SWT.NONE);
129 btnClearName.setEnabled(false);
130 btnClearName.setText("Clear");
131 btnClearName.addSelectionListener(new SelectionAdapter() {
132
133 // Clear selected reference
134 public void widgetSelected(SelectionEvent e) {
135 clearName();
136 }
137 });
138
139 // Set text to name text if exists, disable ability to edit name
140 if (typeDesignation != null && typeDesignation.getTypeName() != null) {
141
142 savedName = typeDesignation.getTypeName();
143 txtName.setText(savedName.getTitleCache());
144 txtName.setEditable(false);
145
146 btnClearName.setEnabled(false);
147 btnSearchName.setEnabled(false);
148 }
149
150 // Tell user when he is entering a new name
151 Label lblNewNameFeedback = new Label(container, SWT.NONE);
152 lblNewNameFeedback.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, false));
153 lblNewNameFeedback.setFont(
154 TaxEditorPlugin.getDefault().italicizeFont(lblNewNameFeedback.getFont()));
155 lblNewNameFeedback.setText("Existing names can not be edited.");
156
157 // Create 2-columned composite for name
158 Composite booleansComposite = new Composite(container, SWT.NULL);
159 booleansComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
160 GridLayout gridLayout2 = new GridLayout();
161 gridLayout2.numColumns = 2;
162 gridLayout2.marginHeight = 15;
163 gridLayout2.marginWidth = 0;
164 booleansComposite.setLayout(gridLayout2);
165
166 btnRejected = new Button(booleansComposite, SWT.CHECK);
167 btnRejected.setText("Is rejected type");
168 btnRejected.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
169
170 btnConserved = new Button(booleansComposite, SWT.CHECK);
171 btnConserved.setText("Is conserved type");
172 btnConserved.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
173
174 btnLecto = new Button(booleansComposite, SWT.CHECK);
175 btnLecto.setText("Is lecto type");
176 btnLecto.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
177
178 btnNotDesignated = new Button(booleansComposite, SWT.CHECK);
179 btnNotDesignated.setText("Is not designated");
180 btnNotDesignated.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
181
182 // Set any boolean values
183 if (typeDesignation != null) {
184 if (typeDesignation.isRejectedType()) {
185 btnRejected.setSelection(true);
186 }
187 if (typeDesignation.isConservedType()) {
188 btnConserved.setSelection(true);
189 }
190 if (typeDesignation.isLectoType()) {
191 btnLecto.setSelection(true);
192 }
193 if (typeDesignation.isNotDesignated()) {
194 btnNotDesignated.setSelection(true);
195 }
196 }
197
198 // Create reference input composite
199 referenceComposite = new ReferenceSelectComposite(container);
200
201 // Set text to reference text if exists
202 if (typeDesignation != null && typeDesignation.getCitation() != null) {
203 referenceComposite.setMicroReference(typeDesignation.getCitationMicroReference());
204 if (typeDesignation.getCitation() != null) {
205 referenceComposite.setReference(typeDesignation.getCitation());
206 }
207 }
208 }
209
210 /**
211 *
212 */
213 protected void popupNameSearch() {
214 Dialog dialog = new NameSearchDialog(getShell());
215 Object value = ((NameSearchDialog) dialog).open();
216
217 if (value instanceof TaxonNameBase) {
218 setSavedName((TaxonNameBase) value);
219 updatePage();
220 }
221 }
222
223 /**
224 * @param value
225 */
226 private void setSavedName(TaxonNameBase savedName) {
227
228 this.savedName = savedName;
229
230 txtName.setText(savedName.getTitleCache());
231 txtName.setEditable(false);
232
233 btnClearName.setEnabled(true);
234 }
235
236 /**
237 *
238 */
239 protected void clearName() {
240 savedName = null;
241
242 txtName.setText("");
243 txtName.setEditable(true);
244
245 btnClearName.setEnabled(false);
246 }
247
248 @Override
249 public boolean canFlipToNextPage() {
250 return isPageComplete();
251 }
252
253 public boolean isPageComplete() {
254 return (txtName.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 TaxonNameBase typeName = null;
268 if (savedName != null) {
269 typeName = savedName;
270 } else {
271 if (!txtName.getText().equals("")) {
272 typeName = PreferencesController.getInstanceOfPreferredNameClass();
273 typeName.setTitleCache(txtName.getText());
274 }
275 }
276
277 ReferenceBase citation = referenceComposite.getReference();
278 String citationMicroReference = referenceComposite.getMicroReference();
279
280 if (typeDesignation == null) {
281 typeDesignation = new TemporaryNameTypeDesignation(typeName, citation, citationMicroReference,
282 null, btnRejected.getSelection(), btnConserved.getSelection(), btnLecto.getSelection(), btnNotDesignated.getSelection());
283 typeDesignation.setLectoType(btnLecto.getSelection());
284 } else {
285 typeDesignation.setRejectedType(btnRejected.getSelection());
286 typeDesignation.setConservedType(btnConserved.getSelection());
287 typeDesignation.setLectoType(btnLecto.getSelection());
288 typeDesignation.setNotDesignated(btnNotDesignated.getSelection());
289 typeDesignation.setCitation(citation);
290 typeDesignation.setCitationMicroReference(citationMicroReference);
291 }
292
293 typeDesignationsList.remove(typeDesignation);
294 typeDesignationsList.add(typeDesignation);
295 }
296 }
297
298 public class TemporaryNameTypeDesignation extends
299 NameTypeDesignation {
300
301 /**
302 * @param typeName
303 * @param citation
304 * @param object
305 * @param object2
306 * @param selection
307 * @param selection2
308 * @param selection3
309 */
310 // public TemporaryNameTypeDesignation() {
311 // super(null, null, null, null, false, false, false);
312 // setLectoType(false);
313 // }
314
315 public TemporaryNameTypeDesignation(TaxonNameBase typeName, ReferenceBase citation, String citationMicroReference,
316 String originalNameString, boolean isRejectedType, boolean isConservedType, boolean isLectoType, boolean isNotDesignated) {
317 super(typeName, citation, citationMicroReference, originalNameString,
318 isRejectedType, isConservedType, isLectoType, isNotDesignated);
319 }
320 }
321 }