6544b905abc4e6184d65a41a3b2cb05d3d0c0f52
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / eu / etaxonomy / taxeditor / propertysheet / type / wizard / ChooseTypeDesignationWizardPage.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.jface.wizard.WizardPage;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.custom.CCombo;
16 import org.eclipse.swt.events.KeyAdapter;
17 import org.eclipse.swt.events.KeyEvent;
18 import org.eclipse.swt.events.SelectionAdapter;
19 import org.eclipse.swt.events.SelectionEvent;
20 import org.eclipse.swt.layout.GridData;
21 import org.eclipse.swt.layout.GridLayout;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.swt.widgets.Label;
24 import org.eclipse.swt.widgets.Text;
25
26 import eu.etaxonomy.cdm.model.common.TermVocabulary;
27 import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
28 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
29 import eu.etaxonomy.cdm.model.name.TypeDesignationStatus;
30 import eu.etaxonomy.cdm.model.occurrence.DerivedUnitBase;
31 import eu.etaxonomy.cdm.model.occurrence.Specimen;
32 import eu.etaxonomy.taxeditor.model.CdmSessionDataRepository;
33
34 /**
35 * @author p.ciardelli
36 * @created 13.02.2009
37 * @version 1.0
38 */
39 public class ChooseTypeDesignationWizardPage extends WizardPage {
40 private static Logger logger = Logger
41 .getLogger(ChooseTypeDesignationWizardPage.class);
42
43 private SpecimenTypeDesignation typeDesignation;
44 private CCombo statusCombo;
45
46 private Text txtDesignationType;
47
48 private TypeDesignationStatus[] typeStatusArray;
49
50 private TaxonNameBase name;
51
52 /**
53 * @param typeDesignation
54 */
55 public ChooseTypeDesignationWizardPage(
56 SpecimenTypeDesignation typeDesignation, TaxonNameBase name) {
57 super("");
58
59 this.typeDesignation = typeDesignation;
60 this.name = name;
61
62 setTitle("Create or edit type designation");
63 setDescription("Create or edit type designation for '" + name.getTitleCache() + "\".");
64 }
65
66 /* (non-Javadoc)
67 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
68 */
69 @Override
70 public void createControl(Composite parent) {
71
72 // Create composite for popup dialog
73 Composite container = new Composite(parent, SWT.NULL);
74 final GridLayout gridLayout = new GridLayout();
75 container.setLayout(gridLayout);
76 setControl(container);
77
78 // Create text
79 final Label lblChooseStatus = new Label(container, SWT.NONE);
80 lblChooseStatus.setText("Choose designation type status:");
81
82 // Create designation type status dropdown
83 statusCombo = new CCombo(container, SWT.BORDER);
84 statusCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
85 TermVocabulary<TypeDesignationStatus> typeStatusSet = CdmSessionDataRepository.getDefault().getTypeDesignationStatus();
86 typeStatusArray = new TypeDesignationStatus[CdmSessionDataRepository.getDefault().getTypeDesignationStatus().size()];
87 int i = 0;
88 int selectedIndex = -1;
89 for (TypeDesignationStatus typeStatus :
90 CdmSessionDataRepository.getDefault().getTypeDesignationStatus()) {
91 String label = typeStatus.getLabel();
92 typeStatusArray[i] = typeStatus;
93
94 if (typeDesignation != null) {
95 if (typeStatus.equals(typeDesignation.getTypeStatus())) {
96 selectedIndex = i;
97 }
98 }
99
100 i++;
101 statusCombo.add(label);
102 }
103
104 // Set menu to type designation status if exists
105 statusCombo.select(selectedIndex);
106
107 statusCombo.addSelectionListener(new SelectionAdapter() {
108 @Override
109 public void widgetSelected(SelectionEvent e) {
110 updatePage();
111 }
112 });
113
114 // Create text
115 final Label lblEnterText = new Label(container, SWT.NONE);
116 lblEnterText.setText("Enter designation type text:");
117
118 txtDesignationType = new Text(container, SWT.BORDER);
119 txtDesignationType.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
120 txtDesignationType.addKeyListener(new KeyAdapter() {
121 @Override
122 public void keyReleased(KeyEvent e) {
123 updatePage();
124 }
125 });
126
127 // Set text to specimen text if exists
128 if (typeDesignation != null && typeDesignation.getTypeSpecimen() != null) {
129 txtDesignationType.setText(typeDesignation.getTypeSpecimen().getTitleCache());
130 }
131 }
132
133 @Override
134 public boolean canFlipToNextPage() {
135 return isPageComplete();
136 }
137
138 public boolean isPageComplete() {
139 return (statusCombo.getSelectionIndex() > -1
140 && txtDesignationType.getText().length() > 0);
141 }
142
143 private void updatePage() {
144
145 getWizard().getContainer().updateButtons();
146 }
147
148 public void setPageComplete(boolean complete) {
149 super.setPageComplete(complete);
150
151 if (complete) {
152
153 TypeDesignationStatus status = typeStatusArray[statusCombo.getSelectionIndex()];
154 // String specimenText = txtDesignationType.getText();
155
156 Specimen specimen;
157 if (typeDesignation == null || typeDesignation.getTypeSpecimen() == null) {
158 specimen = Specimen.NewInstance();
159 } else {
160 specimen = (Specimen) typeDesignation.getTypeSpecimen();
161 }
162 specimen.setTitleCache(txtDesignationType.getText());
163
164 if (typeDesignation == null) {
165 name.addSpecimenTypeDesignation(specimen, status, null, null, null, true, true);
166 } else {
167 typeDesignation.setTypeStatus(status);
168
169 // if (typeDesignation.getTypeSpecimen() == null) {
170 // typeDesignation.setTypeSpecimen(Specimen.NewInstance());
171 // }
172 // typeDesignation.getTypeSpecimen().setTitleCache(specimenText);
173 }
174 }
175 }
176 }