Type module largely complete.
[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.window.Window;
23 import org.eclipse.jface.wizard.Wizard;
24 import org.eclipse.jface.wizard.WizardDialog;
25 import org.eclipse.jface.wizard.WizardPage;
26 import org.eclipse.swt.SWT;
27 import org.eclipse.swt.events.SelectionAdapter;
28 import org.eclipse.swt.events.SelectionEvent;
29 import org.eclipse.swt.graphics.Image;
30 import org.eclipse.swt.layout.GridData;
31 import org.eclipse.swt.layout.GridLayout;
32 import org.eclipse.swt.widgets.Button;
33 import org.eclipse.swt.widgets.Composite;
34 import org.eclipse.swt.widgets.Table;
35 import org.eclipse.swt.widgets.TableItem;
36
37 import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
38 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
39 import eu.etaxonomy.taxeditor.controller.GlobalController;
40 import eu.etaxonomy.taxeditor.model.CdmUtil;
41
42 /**
43 * @author p.ciardelli
44 * @created 11.02.2009
45 * @version 1.0
46 */
47 public class ListTypeWizardPage extends WizardPage {
48 private static Logger logger = Logger.getLogger(ListTypeWizardPage.class);
49 private TaxonNameBase name;
50
51 /**
52 * @param name
53 */
54 public ListTypeWizardPage(TaxonNameBase name, WritableList typeDesignationsList) {
55 super("");
56 this.name = name;
57
58 this.typeDesignationsList = typeDesignationsList;
59
60 setTitle("Type designations for \""
61 + CdmUtil.getDisplayName(name) + "\".");
62 setDescription("Select a type designation and click \"Edit ...\" or \"Remove\", or click \"Add ...\" to create a new designation.");
63 }
64
65 private Table typeDesignationsTable;
66 private WritableList typeDesignationsList = new WritableList();
67
68 private Button btnRemove;
69 private Button btnEdit;
70
71 private SpecimenTypeDesignation typeDesignation;
72 private TableViewer tableViewer;
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 btnEdit.addSelectionListener(new SelectionAdapter() {
86 public void widgetSelected(SelectionEvent e) {
87 if (getSelectedTypeDesignation() != null) {
88 createTypeDesignationWizard(getSelectedTypeDesignation());
89 }
90 }
91 });
92
93 btnRemove = new Button(container, SWT.NONE);
94 btnRemove.setText("Remove");
95 btnRemove.addSelectionListener(new SelectionAdapter() {
96 public void widgetSelected(SelectionEvent e) {
97 if (getSelectedTypeDesignation() != null) {
98 typeDesignationsList.remove(getSelectedTypeDesignation());
99 setSelectedTypeDesignation(null);
100 }
101 }
102 });
103
104 setEnableTypeDesignationButtons(false);
105
106 final Button btnAdd = new Button(container, SWT.NONE);
107 final GridData gd_btnAdd = new GridData(SWT.RIGHT, SWT.CENTER, true,
108 false);
109 btnAdd.setLayoutData(gd_btnAdd);
110 btnAdd.setText("Add ...");
111 btnAdd.addSelectionListener(new SelectionAdapter() {
112 public void widgetSelected(SelectionEvent e) {
113 createTypeDesignationWizard(null);
114 }
115 });
116
117 tableViewer = new TableViewer(container, SWT.BORDER | SWT.SINGLE);
118
119 typeDesignationsTable = tableViewer.getTable();
120 typeDesignationsTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
121 true, 3, 1));
122 typeDesignationsTable.addSelectionListener(new SelectionAdapter() {
123 public void widgetSelected(SelectionEvent e) {
124 setEnableTypeDesignationButtons(true);
125 TableItem[] selectedItem = typeDesignationsTable.getSelection();
126 if (e.item.getData() instanceof SpecimenTypeDesignation) {
127 setSelectedTypeDesignation((SpecimenTypeDesignation) 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(), SpecimenTypeDesignation.class,
137 new String[] { "typeSpecimen", "typeStatus" });
138 tableViewer.setLabelProvider(new ObservableMapLabelProvider(
139 providerMaps) {
140 public String getColumnText(Object element, int columnIndex) {
141 if (element instanceof SpecimenTypeDesignation) {
142 SpecimenTypeDesignation typeDesignation = (SpecimenTypeDesignation) element;
143 return getTypeDesignationString(typeDesignation);
144 }
145 return "";
146 }
147
148 public Image getColumnImage(Object element, int columnIndex) {
149 if (element instanceof SpecimenTypeDesignation) {
150 SpecimenTypeDesignation typeDesignation = (SpecimenTypeDesignation) element;
151 return getTypeDesignationImage(typeDesignation);
152 }
153 return null;
154 }
155 });
156
157 tableViewer.setInput(typeDesignationsList);
158 tableViewer.addDoubleClickListener(new IDoubleClickListener() {
159 public void doubleClick(DoubleClickEvent event) {
160 if (((StructuredSelection) event.getSelection())
161 .getFirstElement() instanceof SpecimenTypeDesignation) {
162 SpecimenTypeDesignation typeDesignation = (SpecimenTypeDesignation)
163 ((StructuredSelection) event.getSelection()).getFirstElement();
164 createTypeDesignationWizard(typeDesignation);
165 }
166 }
167 });
168 }
169
170 private void setSelectedTypeDesignation(SpecimenTypeDesignation typeDesignation) {
171 this.typeDesignation = typeDesignation;
172 }
173
174 private SpecimenTypeDesignation getSelectedTypeDesignation() {
175 return typeDesignation;
176 }
177
178 private void createTypeDesignationWizard(SpecimenTypeDesignation typeDesignation) {
179 Wizard wizard = new TypeDesignationWizard(typeDesignation, name, typeDesignationsList);
180
181 WizardDialog dialog = new WizardDialog(GlobalController.getShell(), wizard);
182 dialog.create();
183 dialog.open();
184 // if (dialog.open() == Window.OK) ...
185 }
186
187 protected void setEnableTypeDesignationButtons(boolean enabled) {
188 btnRemove.setEnabled(enabled);
189 btnEdit.setEnabled(enabled);
190 }
191
192 private Image getTypeDesignationImage(SpecimenTypeDesignation typeDesignation) {
193 return null;
194 }
195
196 private String getTypeDesignationString(SpecimenTypeDesignation typeDesignation) {
197 String str = "";
198 if (typeDesignation.getTypeSpecimen() != null) {
199 str += typeDesignation.getTypeSpecimen().getTitleCache();
200 } else {
201 str += "UNDEFINED";
202 }
203 if (typeDesignation.getTypeStatus() != null) {
204 str += " [" + typeDesignation.getTypeStatus().getLabel() + "]";
205 }
206
207 return str;
208 }
209 }