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