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