3eeb862ddb5851c406036db28c3327df010904e6
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / eu / etaxonomy / taxeditor / propertysheet / type / wizard / ListTypeWizardPage.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.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.Wizard;
23 import org.eclipse.jface.wizard.WizardDialog;
24 import org.eclipse.jface.wizard.WizardPage;
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.events.SelectionAdapter;
27 import org.eclipse.swt.events.SelectionEvent;
28 import org.eclipse.swt.graphics.Image;
29 import org.eclipse.swt.layout.GridData;
30 import org.eclipse.swt.layout.GridLayout;
31 import org.eclipse.swt.widgets.Button;
32 import org.eclipse.swt.widgets.Composite;
33 import org.eclipse.swt.widgets.Table;
34 import org.eclipse.swt.widgets.TableItem;
35
36 import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
37 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
38 import eu.etaxonomy.taxeditor.ITaxEditorConstants;
39 import eu.etaxonomy.taxeditor.TaxEditorPlugin;
40 import eu.etaxonomy.taxeditor.actions.ui.OpenNameRelationWizardAction;
41 import eu.etaxonomy.taxeditor.controller.GlobalController;
42 import eu.etaxonomy.taxeditor.model.CdmUtil;
43 import eu.etaxonomy.taxeditor.propertysheet.namerelationswizard.NameRelationWizard;
44
45 /**
46 * @author p.ciardelli
47 * @created 11.02.2009
48 * @version 1.0
49 */
50 public class ListTypeWizardPage extends WizardPage {
51 private static Logger logger = Logger.getLogger(ListTypeWizardPage.class);
52 private TaxonNameBase name;
53
54 /**
55 * @param name
56 */
57 public ListTypeWizardPage(TaxonNameBase name) {
58 super("");
59 this.name = name;
60
61 this.typeDesignationsList.addAll(name.getSpecimenTypeDesignations());
62
63 setTitle("Type designations for \""
64 + CdmUtil.getDisplayName(name) + "\".");
65 setDescription("Select a type designation and click \"Edit ...\" or \"Remove\", or click \"Add ...\" to create a new designation.");
66 }
67
68 private Table typeDesignationsTable;
69 private WritableList typeDesignationsList = new WritableList();
70
71 private Button btnRemove;
72 private Button btnEdit;
73
74 private boolean isZoological = false;
75 private SpecimenTypeDesignation typeDesignation;
76
77
78 public void createControl(Composite parent) {
79 Composite container = new Composite(parent, SWT.NULL);
80 final GridLayout gridLayout = new GridLayout();
81 gridLayout.numColumns = 3;
82 container.setLayout(gridLayout);
83
84 setControl(container);
85
86 btnEdit = new Button(container, SWT.NONE);
87 btnEdit.setText("Edit ...");
88
89 btnRemove = new Button(container, SWT.NONE);
90 btnRemove.setText("Remove");
91 btnRemove.addSelectionListener(new SelectionAdapter() {
92 public void widgetSelected(SelectionEvent e) {
93 if (getSelectedTypeDesignation() != null) {
94 // new DeleteNameRelationAction(name, getSelectedRelation()).run();
95 typeDesignationsList.remove(getSelectedTypeDesignation());
96 setSelectedTypeDesignation(null);
97 }
98 }
99 });
100
101 setEnableTypeDesignationButtons(false);
102
103 final Button btnAdd = new Button(container, SWT.NONE);
104 final GridData gd_btnAdd = new GridData(SWT.RIGHT, SWT.CENTER, true,
105 false);
106 btnAdd.setLayoutData(gd_btnAdd);
107 btnAdd.setText("Add ...");
108 btnAdd.addSelectionListener(new SelectionAdapter() {
109 public void widgetSelected(SelectionEvent e) {
110 createTypeDesignationWizard(null);
111 }
112 });
113
114 final TableViewer tableViewer = new TableViewer(container, SWT.BORDER | SWT.SINGLE);
115
116 typeDesignationsTable = tableViewer.getTable();
117 typeDesignationsTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
118 true, 3, 1));
119 typeDesignationsTable.addSelectionListener(new SelectionAdapter() {
120 public void widgetSelected(SelectionEvent e) {
121 // TODO: As long as onNameBase.removeNameRelationship() doesn't work, "Remove" stays disabled ...
122 setEnableTypeDesignationButtons(true);
123 TableItem[] selectedItem = typeDesignationsTable.getSelection();
124 if (e.item.getData() instanceof SpecimenTypeDesignation) {
125 setSelectedTypeDesignation((SpecimenTypeDesignation) e.item.getData());
126 }
127 }
128 });
129
130 ObservableListContentProvider providerList = new ObservableListContentProvider();
131 tableViewer.setContentProvider(providerList);
132
133 IObservableMap[] providerMaps = BeansObservables.observeMaps(
134 providerList.getKnownElements(), SpecimenTypeDesignation.class,
135 new String[] { "typeSpecimen", "typeStatus" });
136 tableViewer.setLabelProvider(new ObservableMapLabelProvider(
137 providerMaps) {
138 public String getColumnText(Object element, int columnIndex) {
139 if (element instanceof SpecimenTypeDesignation) {
140 SpecimenTypeDesignation typeDesignation = (SpecimenTypeDesignation) element;
141 return getTypeDesignationString(typeDesignation);
142 }
143 return "";
144 }
145
146 public Image getColumnImage(Object element, int columnIndex) {
147 if (element instanceof SpecimenTypeDesignation) {
148 SpecimenTypeDesignation typeDesignation = (SpecimenTypeDesignation) element;
149 return getTypeDesignationImage(typeDesignation);
150 }
151 return null;
152 }
153 });
154
155 tableViewer.setInput(typeDesignationsList);
156 tableViewer.addDoubleClickListener(new IDoubleClickListener() {
157 public void doubleClick(DoubleClickEvent event) {
158 if (((StructuredSelection) event.getSelection())
159 .getFirstElement() instanceof SpecimenTypeDesignation) {
160 SpecimenTypeDesignation typeDesignation = (SpecimenTypeDesignation)
161 ((StructuredSelection) event.getSelection()).getFirstElement();
162 createTypeDesignationWizard(typeDesignation);
163 }
164 }
165 });
166 }
167
168 private void setSelectedTypeDesignation(SpecimenTypeDesignation typeDesignation) {
169 this.typeDesignation = typeDesignation;
170 }
171
172 private SpecimenTypeDesignation getSelectedTypeDesignation() {
173 return typeDesignation;
174 }
175
176 private void createTypeDesignationWizard(SpecimenTypeDesignation typeDesignation) {
177 // if (relation == null) {
178 // new OpenNameRelationWizardAction(name, typeDesignationsList).run();
179 // } else {
180 // new OpenNameRelationWizardAction(name, relation).run();
181 // }
182
183 Wizard wizard = new TypeDesignationWizard(typeDesignation, name);
184
185 WizardDialog dialog = new WizardDialog(GlobalController.getShell(), wizard);
186 dialog.create();
187 dialog.open();
188 }
189
190 protected void setEnableTypeDesignationButtons(boolean enabled) {
191 btnRemove.setEnabled(enabled);
192 btnEdit.setEnabled(false);
193 }
194
195 private Image getTypeDesignationImage(SpecimenTypeDesignation typeDesignation) {
196 return null;
197 }
198
199 private String getTypeDesignationString(SpecimenTypeDesignation typeDesignation) {
200 return "Placeholder";
201 }
202
203 }