Moving editor sources back into trunk
[taxeditor.git] / taxeditor-editor / src / main / java / eu / etaxonomy / taxeditor / propertysheet / namerelations / wizard / ChooseRelationWizardPage.java
1 /**
2 * Copyright (C) 2007 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.namerelations.wizard;
11
12 import java.beans.PropertyChangeEvent;
13 import java.beans.PropertyChangeListener;
14 import java.util.ArrayList;
15 import java.util.List;
16
17 import org.apache.log4j.Logger;
18 import org.eclipse.core.databinding.observable.list.WritableList;
19 import org.eclipse.jface.wizard.WizardPage;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.events.SelectionAdapter;
22 import org.eclipse.swt.events.SelectionEvent;
23 import org.eclipse.swt.events.SelectionListener;
24 import org.eclipse.swt.layout.GridData;
25 import org.eclipse.swt.layout.GridLayout;
26 import org.eclipse.swt.widgets.Button;
27 import org.eclipse.swt.widgets.Combo;
28 import org.eclipse.swt.widgets.Composite;
29 import org.eclipse.swt.widgets.Group;
30 import org.eclipse.swt.widgets.Label;
31
32 import eu.etaxonomy.cdm.model.name.NameRelationship;
33 import eu.etaxonomy.cdm.model.name.NameRelationshipType;
34 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
35 import eu.etaxonomy.cdm.model.name.ZoologicalName;
36 import eu.etaxonomy.cdm.model.reference.ReferenceBase;
37 import eu.etaxonomy.taxeditor.editor.name.NameSelectComposite;
38 import eu.etaxonomy.taxeditor.editor.reference.ReferenceSelectComposite;
39 import eu.etaxonomy.taxeditor.store.CdmStore;
40 import eu.etaxonomy.taxeditor.store.model.NameRelationsUtil;
41 import eu.etaxonomy.taxeditor.store.model.NameUtil;
42
43 /**
44 * An all-in-one relation wizard page where the user chooses a related name, a
45 * relationship type, and the relation's directionality.
46 *
47 * @author p.ciardelli
48 * @created 04.06.2008
49 * @version 1.0
50 */
51 public class ChooseRelationWizardPage extends WizardPage {
52 private static final Logger logger = Logger
53 .getLogger(ChooseRelationWizardPage.class);
54
55 private List<NameRelationshipType> relationTypes;
56
57 private Combo typeCombo;
58 private Label lblDirection;
59 private Button btnFromRelatedToBase;
60 private Button btnFromBaseToRelated;
61
62 private boolean typeSelected = false;
63 private boolean directionSelected = false;
64
65 private SelectionListener radioSelectionListener = new SelectionAdapter() {
66 public void widgetSelected(SelectionEvent e) {
67 directionSelected = true;
68 if (e.widget.equals(btnFromRelatedToBase)) {
69 fromName = relatedName;
70 toName = baseName;
71 }
72 if (e.widget.equals(btnFromBaseToRelated)) {
73 fromName = baseName;
74 toName = relatedName;
75 }
76 updatePage();
77 }
78 };
79
80 private NameSelectComposite nameComposite;
81
82 private boolean nameSelected;
83
84 private TaxonNameBase baseName;
85 private TaxonNameBase relatedName;
86 private TaxonNameBase fromName;
87 private TaxonNameBase toName;
88
89 private NameRelationshipType type;
90
91 private Label lblType;
92
93 private ReferenceSelectComposite referenceComposite;
94
95 private WritableList nameRelationsList;
96
97 public ChooseRelationWizardPage(TaxonNameBase name, WritableList nameRelationsList) {
98 super("");
99
100 this.baseName = name;
101 this.nameRelationsList = nameRelationsList;
102
103 setTitle("Create name relationship.");
104 setDescription("Create a relationship for \""
105 + NameUtil.getDisplayName(baseName) + "\".");
106 }
107
108 public void createControl(Composite parent) {
109 Composite container = new Composite(parent, SWT.NULL);
110 container.setLayout(new GridLayout());
111
112 setControl(container);
113
114 // Create name input composite
115 nameComposite = new NameSelectComposite(container);
116 nameComposite.addPropertyChangeListener(new PropertyChangeListener() {
117 public void propertyChange(PropertyChangeEvent evt) {
118 setName();
119 }
120 });
121
122 // Init relation type array
123 relationTypes = new ArrayList<NameRelationshipType>();
124
125 //
126 lblType = new Label(container, SWT.NONE);
127 lblType.setText("Choose relationship type:");
128
129 // Create relation type dropdown
130 typeCombo = new Combo(container, SWT.BORDER);
131 typeCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
132 for (NameRelationshipType relationType :
133 CdmStore.getDefault().getNameRelationshipTypes()) {
134
135 relationTypes.add(relationType);
136 boolean isZoological = baseName instanceof ZoologicalName;
137 String label = NameRelationsUtil.getNameRelationTypeLabel(relationType, isZoological);
138 typeCombo.add(label);
139 }
140 typeCombo.setVisibleItemCount(relationTypes.size());
141
142 typeCombo.addSelectionListener(new SelectionAdapter() {
143 @Override
144 public void widgetSelected(SelectionEvent e) {
145 setType();
146 }
147 });
148 typeCombo.setEnabled(false);
149
150 final Group group = new Group(container, SWT.NONE);
151 group.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
152 final GridLayout gridLayout = new GridLayout();
153 group.setLayout(gridLayout);
154
155 lblDirection = new Label(group, SWT.NONE);
156 final GridData gd_lblDirection = new GridData(SWT.FILL, SWT.CENTER, true, false);
157 gd_lblDirection.heightHint = 32;
158 gd_lblDirection.minimumHeight = 40;
159 lblDirection
160 .setLayoutData(gd_lblDirection);
161 lblDirection.setEnabled(false);
162 lblDirection.setText("Choose relationship direction:");
163
164 btnFromRelatedToBase = new Button(group, SWT.RADIO);
165 btnFromRelatedToBase
166 .setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
167 btnFromRelatedToBase.setVisible(false);
168 btnFromRelatedToBase.addSelectionListener(radioSelectionListener);
169
170 btnFromBaseToRelated = new Button(group, SWT.RADIO);
171 btnFromBaseToRelated
172 .setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
173 btnFromBaseToRelated.setVisible(false);
174 btnFromBaseToRelated.addSelectionListener(radioSelectionListener);
175 new Label(group, SWT.NONE);
176
177 // Create reference input composite
178 referenceComposite = new ReferenceSelectComposite(container);
179 }
180
181 /**
182 */
183 private void setName() {
184 nameSelected = nameComposite.getName() == null ? false : true;
185 if (nameSelected) {
186 this.relatedName = nameComposite.getName();
187 } else {
188 this.relatedName = null;
189 }
190 updatePage();
191 }
192
193 /**
194 *
195 */
196 private void setType() {
197 int typeIndex = typeCombo.getSelectionIndex();
198 type = relationTypes.get(typeIndex);
199 typeSelected = true;
200 updatePage();
201 }
202
203 private void setDirectionLabels() {
204 String strDirection1 = "\"" + NameUtil.getDisplayName(relatedName) + "\" "
205 + "is " + typeCombo.getText() + " " + "\""
206 + NameUtil.getDisplayName(baseName) + "\"";
207 String strDirection2 = "\"" + NameUtil.getDisplayName(baseName) + "\" "
208 + "is " + typeCombo.getText() + " " + "\""
209 + NameUtil.getDisplayName(relatedName) + "\"";
210
211 btnFromRelatedToBase.setText(strDirection1);
212 btnFromBaseToRelated.setText(strDirection2);
213 }
214
215 @Override
216 public boolean canFlipToNextPage() {
217 return isPageComplete();
218 }
219
220 public boolean isPageComplete() {
221 return (nameSelected && typeSelected && directionSelected);
222 }
223
224 private void updatePage() {
225 if (nameSelected) {
226 enableType(true);
227 if (typeSelected) {
228 enableDirection(true);
229 } else {
230 enableDirection(false);
231 }
232 } else {
233 enableType(false);
234 enableDirection(false);
235 }
236 getWizard().getContainer().updateButtons();
237 }
238
239 private void enableType(boolean enabled) {
240 lblType.setEnabled(enabled);
241 typeCombo.setEnabled(enabled);
242 }
243
244 private void enableDirection(boolean enabled) {
245 if (enabled) {
246 setDirectionLabels();
247 } else {
248 // directionSelected = false;
249 }
250 lblDirection.setEnabled(enabled);
251 btnFromRelatedToBase.setEnabled(enabled);
252 btnFromBaseToRelated.setEnabled(enabled);
253
254 btnFromRelatedToBase.setVisible(enabled);
255 btnFromBaseToRelated.setVisible(enabled);
256
257 }
258
259 /* (non-Javadoc)
260 * @see org.eclipse.jface.wizard.WizardPage#setPageComplete(boolean)
261 */
262 public void setPageComplete(boolean complete) {
263 super.setPageComplete(complete);
264
265 // Add a new relation to the list
266 if (complete) {
267 NameRelationship relation = new TemporaryNameRelationship(
268 toName, fromName, type, referenceComposite.getReference(),
269 referenceComposite.getMicroReference(), null);
270 nameRelationsList.add(relation);
271 }
272 }
273
274 public class TemporaryNameRelationship extends NameRelationship {
275
276 private TaxonNameBase tempToName;
277 private TaxonNameBase tempFromName;
278 private NameRelationshipType tempType;
279 private ReferenceBase tempCitation;
280 private String tempCitationMicroReference;
281 private String tempRuleConsidered;
282
283 /**
284 * @param toName
285 * @param fromName
286 * @param type
287 * @param citation
288 * @param citationMicroReference
289 * @param ruleConsidered
290 */
291 protected TemporaryNameRelationship(TaxonNameBase toName,
292 TaxonNameBase fromName, NameRelationshipType type,
293 ReferenceBase citation, String citationMicroReference,
294 String ruleConsidered) {
295 super(toName, fromName, type, citation, citationMicroReference, ruleConsidered);
296 // super(null, null, null, null, null, null);
297 //
298 // this.tempToName = toName;
299 // this.tempFromName = fromName;
300 // this.tempType = type;
301 // this.tempCitation = citation;
302 // this.tempCitationMicroReference = citationMicroReference;
303 // this.tempRuleConsidered = ruleConsidered;
304 }
305
306 // public TaxonNameBase getToName(){
307 // return tempToName;
308 // }
309 //
310 // public TaxonNameBase getFromName(){
311 // return tempFromName;
312 // }
313 //
314 // public String getRuleConsidered(){
315 // return tempRuleConsidered;
316 // }
317 }
318 }