Massive refactoring of the methodology in former class UiUtils
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / eu / etaxonomy / taxeditor / propertysheet / namerelationswizard / ListNameRelationsWizardPage.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.namerelationswizard;
11
12 import org.apache.log4j.Logger;
13 import org.eclipse.core.databinding.beans.BeansObservables;
14 import org.eclipse.core.databinding.observable.list.WritableList;
15 import org.eclipse.core.databinding.observable.map.IObservableMap;
16 import org.eclipse.jface.databinding.viewers.ObservableListContentProvider;
17 import org.eclipse.jface.databinding.viewers.ObservableMapLabelProvider;
18 import org.eclipse.jface.viewers.DoubleClickEvent;
19 import org.eclipse.jface.viewers.IDoubleClickListener;
20 import org.eclipse.jface.viewers.StructuredSelection;
21 import org.eclipse.jface.viewers.TableViewer;
22 import org.eclipse.jface.wizard.WizardPage;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.events.SelectionAdapter;
25 import org.eclipse.swt.events.SelectionEvent;
26 import org.eclipse.swt.graphics.Image;
27 import org.eclipse.swt.layout.GridData;
28 import org.eclipse.swt.layout.GridLayout;
29 import org.eclipse.swt.widgets.Button;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.swt.widgets.Table;
32 import org.eclipse.swt.widgets.TableItem;
33
34 import eu.etaxonomy.cdm.model.name.NameRelationship;
35 import eu.etaxonomy.cdm.model.name.NameRelationshipType;
36 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
37 import eu.etaxonomy.cdm.model.name.ZoologicalName;
38 import eu.etaxonomy.taxeditor.ITaxEditorConstants;
39 import eu.etaxonomy.taxeditor.TaxEditorPlugin;
40 import eu.etaxonomy.taxeditor.actions.cdm.DeleteNameRelationAction;
41 import eu.etaxonomy.taxeditor.actions.ui.OpenNameRelationWizardAction;
42 import eu.etaxonomy.taxeditor.controller.GlobalController;
43 import eu.etaxonomy.taxeditor.model.CdmUtil;
44
45 /**
46 * @author p.ciardelli
47 * @created 06.06.2008
48 * @version 1.0
49 */
50 public class ListNameRelationsWizardPage extends WizardPage {
51 private static final Logger logger = Logger
52 .getLogger(ListNameRelationsWizardPage.class);
53
54 private Table nameRelationsTable;
55 private WritableList nameRelationsList = new WritableList();
56 private TaxonNameBase name;
57
58 private Button btnRemove;
59 private Button btnEdit;
60
61 private NameRelationship selectedRelation;
62
63 private boolean isZoological = false;
64
65 public ListNameRelationsWizardPage(TaxonNameBase name) {
66 super("");
67
68 this.name = name;
69 if (name instanceof ZoologicalName) {
70 isZoological = true;
71 }
72
73 this.nameRelationsList.addAll(name.getNameRelations());
74
75 setTitle("Nomenclatural relations for \""
76 + CdmUtil.getDisplayName(name) + "\".");
77 setDescription("Select a relation and click \"Edit ...\" or \"Remove\", or click \"Add ...\" to create a new relation.");
78 }
79
80 public void createControl(Composite parent) {
81 Composite container = new Composite(parent, SWT.NULL);
82 final GridLayout gridLayout = new GridLayout();
83 gridLayout.numColumns = 3;
84 container.setLayout(gridLayout);
85
86 setControl(container);
87
88 btnEdit = new Button(container, SWT.NONE);
89 btnEdit.setText("Edit ...");
90
91 btnRemove = new Button(container, SWT.NONE);
92 btnRemove.setText("Remove");
93 btnRemove.addSelectionListener(new SelectionAdapter() {
94 public void widgetSelected(SelectionEvent e) {
95 if (getSelectedRelation() != null) {
96 new DeleteNameRelationAction(name, getSelectedRelation()).run();
97 nameRelationsList.remove(getSelectedRelation());
98 setSelectedRelation(null);
99 }
100 }
101 });
102
103 setEnableExistingRelationButtons(false);
104
105 final Button btnAdd = new Button(container, SWT.NONE);
106 final GridData gd_btnAdd = new GridData(SWT.RIGHT, SWT.CENTER, true,
107 false);
108 btnAdd.setLayoutData(gd_btnAdd);
109 btnAdd.setText("Add ...");
110 btnAdd.addSelectionListener(new SelectionAdapter() {
111 public void widgetSelected(SelectionEvent e) {
112 createRelationWizard(null);
113 }
114 });
115
116 final TableViewer tableViewer = new TableViewer(container, SWT.BORDER | SWT.SINGLE);
117
118 nameRelationsTable = tableViewer.getTable();
119 nameRelationsTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
120 true, 3, 1));
121 nameRelationsTable.addSelectionListener(new SelectionAdapter() {
122 public void widgetSelected(SelectionEvent e) {
123 // TODO: As long as onNameBase.removeNameRelationship() doesn't work, "Remove" stays disabled ...
124 setEnableExistingRelationButtons(true);
125 TableItem[] selectedItem = nameRelationsTable.getSelection();
126 if (e.item.getData() instanceof NameRelationship) {
127 setSelectedRelation((NameRelationship) e.item.getData());
128 }
129 }
130 });
131
132 ObservableListContentProvider providerList = new ObservableListContentProvider();
133 tableViewer.setContentProvider(providerList);
134
135 IObservableMap[] providerMaps = BeansObservables.observeMaps(
136 providerList.getKnownElements(), NameRelationship.class,
137 new String[] { "fromName", "toName", "type" });
138 tableViewer.setLabelProvider(new ObservableMapLabelProvider(
139 providerMaps) {
140 public String getColumnText(Object element, int columnIndex) {
141 if (element instanceof NameRelationship) {
142 NameRelationship nameRelationship = (NameRelationship) element;
143 return getRelationshipString(nameRelationship);
144 }
145 return "";
146 }
147
148 public Image getColumnImage(Object element, int columnIndex) {
149 if (element instanceof NameRelationship) {
150 NameRelationship nameRelationship = (NameRelationship) element;
151 return getRelationshipImage(nameRelationship);
152 }
153 return null;
154 }
155 });
156 // TODO try name.getNameRelations()
157 tableViewer.setInput(nameRelationsList);
158 tableViewer.addDoubleClickListener(new IDoubleClickListener() {
159 public void doubleClick(DoubleClickEvent event) {
160 if (((StructuredSelection) event.getSelection())
161 .getFirstElement() instanceof NameRelationship) {
162 NameRelationship relation = (NameRelationship) ((StructuredSelection) event
163 .getSelection()).getFirstElement();
164 createRelationWizard(relation);
165 }
166 }
167 });
168 }
169
170 private void setSelectedRelation(NameRelationship selectedRelation) {
171 this.selectedRelation = selectedRelation;
172 }
173
174 private NameRelationship getSelectedRelation() {
175 return selectedRelation;
176 }
177
178 private void createRelationWizard(NameRelationship relation) {
179 if (relation == null) {
180 new OpenNameRelationWizardAction(name, nameRelationsList).run();
181 } else {
182 new OpenNameRelationWizardAction(name, relation).run();
183 }
184 }
185
186 protected void setEnableExistingRelationButtons(boolean enabled) {
187 btnRemove.setEnabled(enabled);
188 btnEdit.setEnabled(false);
189 }
190
191 private Image getRelationshipImage(NameRelationship nameRelationship) {
192 if (nameRelationship.getType().equals(NameRelationshipType.BASIONYM())) {
193 if (isZoological) {
194 return null;
195 }
196 return TaxEditorPlugin.getDefault().getImage(
197 ITaxEditorConstants.BASIONYM_ICON);
198 }
199 if (nameRelationship.getType().equals(
200 NameRelationshipType.ORTHOGRAPHIC_VARIANT())) {
201 return TaxEditorPlugin.getDefault().getImage(
202 ITaxEditorConstants.ORTHOGRAPHIC_VARIANT_ICON);
203 }
204 // return TaxEditorPlugin.getDefault().getImage(
205 // ITaxEditorConstants.EDIT_ICON);
206 return null;
207 }
208
209 private String getRelationshipString(NameRelationship nameRelationship) {
210 String fromName = null;
211 String type = null;
212 String toName = null;
213 if (nameRelationship.getFromName() == null) {
214 fromName = "";
215 } else {
216 fromName = CdmUtil.getDisplayName(nameRelationship.getFromName());
217 }
218 if (nameRelationship.getType() == null) {
219 type = "";
220 } else {
221 if (isZoological &&
222 nameRelationship.getType().equals(NameRelationshipType.BASIONYM())) {
223 type = "original combination";
224 } else {
225 // type = nameRelationship.getType().getLabel();
226 type = GlobalController.getNameRelationLabelType(nameRelationship.getType());
227 }
228 }
229 if (nameRelationship.getToName() == null) {
230 toName = "";
231 } else {
232 toName = CdmUtil.getDisplayName(nameRelationship.getToName());
233 }
234 return "\"" + fromName + "\" is \"" + type + "\" of \"" + toName + "\"";
235 }
236 }