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.editor;
|
11
|
|
12
|
import java.util.ArrayList;
|
13
|
import java.util.List;
|
14
|
|
15
|
import org.apache.log4j.Logger;
|
16
|
import org.eclipse.jface.dialogs.Dialog;
|
17
|
import org.eclipse.jface.dialogs.IDialogConstants;
|
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.swt.SWT;
|
23
|
import org.eclipse.swt.events.FocusEvent;
|
24
|
import org.eclipse.swt.events.FocusListener;
|
25
|
import org.eclipse.swt.events.SelectionAdapter;
|
26
|
import org.eclipse.swt.events.SelectionEvent;
|
27
|
import org.eclipse.swt.graphics.Point;
|
28
|
import org.eclipse.swt.layout.FillLayout;
|
29
|
import org.eclipse.swt.layout.GridData;
|
30
|
import org.eclipse.swt.layout.GridLayout;
|
31
|
import org.eclipse.swt.layout.RowData;
|
32
|
import org.eclipse.swt.layout.RowLayout;
|
33
|
import org.eclipse.swt.widgets.Button;
|
34
|
import org.eclipse.swt.widgets.Composite;
|
35
|
import org.eclipse.swt.widgets.Control;
|
36
|
import org.eclipse.swt.widgets.Display;
|
37
|
import org.eclipse.swt.widgets.Event;
|
38
|
import org.eclipse.swt.widgets.Group;
|
39
|
import org.eclipse.swt.widgets.Label;
|
40
|
import org.eclipse.swt.widgets.Listener;
|
41
|
import org.eclipse.swt.widgets.Shell;
|
42
|
import org.eclipse.swt.widgets.Text;
|
43
|
import org.eclipse.swt.widgets.TreeItem;
|
44
|
|
45
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
46
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
47
|
import eu.etaxonomy.taxeditor.store.model.ImageResources;
|
48
|
import eu.etaxonomy.taxeditor.store.model.NameUtil;
|
49
|
import eu.etaxonomy.taxeditor.store.model.Resources;
|
50
|
|
51
|
/**
|
52
|
* This is a generic dialog that allows the user to choose a taxon via the
|
53
|
* taxonomic tree, recently selected names, or search.
|
54
|
*
|
55
|
* @author p.ciardelli
|
56
|
* @created 27.05.2008
|
57
|
* @version 1.0
|
58
|
*/
|
59
|
public class SelectTaxonDialog extends Dialog {
|
60
|
private static final Logger logger = Logger
|
61
|
.getLogger(SelectTaxonDialog.class);
|
62
|
|
63
|
public static final String TAXON_TO_SYNONYM = "taxon_to_synonym";
|
64
|
public static final String TAXON_TO_NEW_PARENT = "taxon_to_new_parent";
|
65
|
|
66
|
private String dialogType;
|
67
|
private String shellText;
|
68
|
private String descriptionText;
|
69
|
private Text taxonNameText;
|
70
|
|
71
|
// FIXME no direct dependencies
|
72
|
// private TaxonomicTreeViewer treeViewer;
|
73
|
private Taxon taxon;
|
74
|
private Taxon destinationTaxon;
|
75
|
|
76
|
private List<Button> buttons = new ArrayList<Button>();
|
77
|
|
78
|
private Composite selectComposite;
|
79
|
|
80
|
public SelectTaxonDialog(Shell parentShell, String dialogType) {
|
81
|
super(parentShell);
|
82
|
this.dialogType = dialogType;
|
83
|
}
|
84
|
|
85
|
public Taxon open(Taxon taxon) {
|
86
|
this.taxon = taxon;
|
87
|
|
88
|
if (dialogType.equals(TAXON_TO_SYNONYM)) {
|
89
|
this.shellText = "Choose an accepted name for \""
|
90
|
+ NameUtil.getDisplayName(taxon) + "\"";
|
91
|
this.descriptionText = "You have chosen to turn \""
|
92
|
+ NameUtil.getDisplayName(taxon)
|
93
|
+ "\" into a synonym. Please choose a new accepted name from one of the following options.";
|
94
|
}
|
95
|
|
96
|
if (dialogType.equals(TAXON_TO_NEW_PARENT)) {
|
97
|
this.shellText = "Choose a new parent for \""
|
98
|
+ NameUtil.getDisplayName(taxon) + "\"";
|
99
|
this.descriptionText = "You have chosen to select a new parent for \""
|
100
|
+ NameUtil.getDisplayName(taxon)
|
101
|
+ "\". Please choose the new parent taxon from one of the following options.";
|
102
|
}
|
103
|
|
104
|
int returnCode = super.open();
|
105
|
if (returnCode == OK) {
|
106
|
return destinationTaxon;
|
107
|
}
|
108
|
return null;
|
109
|
}
|
110
|
|
111
|
|
112
|
|
113
|
// FIXME looks like this is a complete copy of the treeviewer code. This is so not desirable.
|
114
|
// think about a different solution for this dialog
|
115
|
// /**
|
116
|
// * Create contents of the dialog
|
117
|
// *
|
118
|
// * @param parent
|
119
|
// */
|
120
|
// @Override
|
121
|
// protected Control createDialogArea(Composite parent) {
|
122
|
// parent.setRedraw(true);
|
123
|
// Composite container = (Composite) super.createDialogArea(parent);
|
124
|
// final GridLayout gridLayout = new GridLayout();
|
125
|
// gridLayout.numColumns = 2;
|
126
|
// container.setLayout(gridLayout);
|
127
|
//
|
128
|
// final Label descriptionLabel = new Label(container, SWT.WRAP);
|
129
|
// descriptionLabel.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, true,
|
130
|
// false, 2, 1));
|
131
|
// descriptionLabel.setText(descriptionText);
|
132
|
//
|
133
|
// final Group taxTreeGroup = new Group(container, SWT.NONE);
|
134
|
// taxTreeGroup.setLayout(new GridLayout());
|
135
|
// final GridData gd_group_1 = new GridData(SWT.FILL, SWT.FILL, true, true);
|
136
|
// gd_group_1.heightHint = 83;
|
137
|
// taxTreeGroup.setLayoutData(gd_group_1);
|
138
|
//
|
139
|
// final Button chooseTreeButton = new Button(taxTreeGroup, SWT.RADIO);
|
140
|
// buttons.add(chooseTreeButton);
|
141
|
// chooseTreeButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
|
142
|
// false));
|
143
|
// chooseTreeButton.setText("Choose from taxonomic tree");
|
144
|
//
|
145
|
// Composite taxTreeComposite = new Composite(taxTreeGroup, SWT.BORDER);
|
146
|
// taxTreeComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
|
147
|
// true));
|
148
|
// taxTreeComposite.setLayout(new FillLayout());
|
149
|
// taxTreeComposite.setBackground(Display.getCurrent().getSystemColor(
|
150
|
// SWT.COLOR_WHITE));
|
151
|
//
|
152
|
// treeViewer = new TaxonomicTreeViewer(taxTreeComposite);
|
153
|
//
|
154
|
// IObservableSet observableTaxonSet =
|
155
|
// CdmSessionDataRepository.getDefault().getObservableTaxa();
|
156
|
// treeViewer.setLabelProvider(new TaxonomicTreeLabelProvider(
|
157
|
// observableTaxonSet) {
|
158
|
//
|
159
|
// });
|
160
|
// treeViewer.getTree().addFocusListener(
|
161
|
// new SelectButtonFocusListener(chooseTreeButton));
|
162
|
// treeViewer.addDoubleClickListener(new SelectTaxonDoubleClickListener());
|
163
|
// treeViewer.getTree().addListener(SWT.PaintItem, new Listener() {
|
164
|
// /** If MenuItem with taxon is being drawn, put it in italics.
|
165
|
// *
|
166
|
// * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
|
167
|
// **/
|
168
|
// public void handleEvent(Event event) {
|
169
|
// Object data = event.item.getData();
|
170
|
// if (data instanceof Taxon && ((Taxon) data).equals(taxon)) {
|
171
|
// ((TreeItem) event.item).setFont(TaxEditorPlugin.getDefault().getFont
|
172
|
// (ICdmConstants.MENU_ITEM_ITALICS_FONT));
|
173
|
// }
|
174
|
// }
|
175
|
// });
|
176
|
//
|
177
|
// // Open up tree to reveal taxon
|
178
|
// treeViewer.revealTaxon(taxon);
|
179
|
//
|
180
|
// final Composite rightPaneComposite = new Composite(container, SWT.NONE);
|
181
|
// rightPaneComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
|
182
|
// true));
|
183
|
// rightPaneComposite.setLayout(new FillLayout(SWT.VERTICAL
|
184
|
// | SWT.HORIZONTAL));
|
185
|
//
|
186
|
// final Group recentNamesGroup = new Group(rightPaneComposite, SWT.NONE);
|
187
|
// recentNamesGroup.setLayout(new GridLayout());
|
188
|
//
|
189
|
// final Button chooseRecentNamesButton = new Button(recentNamesGroup,
|
190
|
// SWT.RADIO);
|
191
|
// buttons.add(chooseRecentNamesButton);
|
192
|
// chooseRecentNamesButton.setText("Choose from recently viewed taxa");
|
193
|
//
|
194
|
// Composite recentNamesComposite = new Composite(recentNamesGroup,
|
195
|
// SWT.BORDER);
|
196
|
// recentNamesComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
|
197
|
// true, true));
|
198
|
// recentNamesComposite.setLayout(new FillLayout());
|
199
|
// recentNamesComposite.setBackground(Display.getCurrent().getSystemColor(
|
200
|
// SWT.COLOR_WHITE));
|
201
|
//
|
202
|
// TableViewer recentNamesViewer = new RecentNamesTableViewer(
|
203
|
// recentNamesComposite);
|
204
|
// recentNamesViewer
|
205
|
// .addDoubleClickListener(new SelectTaxonDoubleClickListener());
|
206
|
// recentNamesViewer.getTable().addFocusListener(
|
207
|
// new SelectButtonFocusListener(chooseRecentNamesButton));
|
208
|
//
|
209
|
// final Group searchGroup = new Group(rightPaneComposite, SWT.NONE);
|
210
|
// final GridLayout gridLayout_1 = new GridLayout();
|
211
|
// gridLayout_1.numColumns = 2;
|
212
|
// searchGroup.setLayout(gridLayout_1);
|
213
|
//
|
214
|
// final Button searchForNameButton = new Button(searchGroup, SWT.RADIO);
|
215
|
// buttons.add(searchForNameButton);
|
216
|
// searchForNameButton.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER,
|
217
|
// true, false));
|
218
|
// searchForNameButton.setText("Search for name of accepted taxon");
|
219
|
// new Label(searchGroup, SWT.NONE);
|
220
|
//
|
221
|
// final Text searchText = new Text(searchGroup, SWT.BORDER);
|
222
|
// searchText
|
223
|
// .setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
|
224
|
// searchText.setForeground(SWTResourceManager.getColor(192, 192, 192));
|
225
|
// searchText.setText("Use \"*\" for wildcard searching");
|
226
|
// searchText.addFocusListener(new SelectButtonFocusListener(
|
227
|
// searchForNameButton) {
|
228
|
//
|
229
|
// public void focusGained(FocusEvent e) {
|
230
|
// selectButton(button);
|
231
|
// searchText.setForeground(SWTResourceManager.getColor(0, 0, 0));
|
232
|
// searchText.setText("");
|
233
|
// }
|
234
|
//
|
235
|
// public void focusLost(FocusEvent e) {
|
236
|
// if (searchText.getText() == "") {
|
237
|
// searchText.setForeground(SWTResourceManager.getColor(192,
|
238
|
// 192, 192));
|
239
|
// searchText.setText("Use \"*\" for wildcard searching");
|
240
|
// }
|
241
|
// }
|
242
|
//
|
243
|
// });
|
244
|
//
|
245
|
// final Button searchButton = new Button(searchGroup, SWT.NONE);
|
246
|
// searchButton.setLayoutData(new GridData());
|
247
|
// searchButton.setText("Search");
|
248
|
//
|
249
|
// selectComposite = new Composite(container, SWT.NONE);
|
250
|
// selectComposite.setLayout(new RowLayout());
|
251
|
// selectComposite.setLayoutData(new GridData(SWT.RIGHT, SWT.BOTTOM, true,
|
252
|
// false, 2, 1));
|
253
|
//
|
254
|
// taxonNameText = new Text(selectComposite, SWT.BORDER);
|
255
|
// taxonNameText.setFont(SWTResourceManager.getFont("", 12, SWT.BOLD));
|
256
|
// taxonNameText.setBackground(SWTResourceManager.getColor(255, 255, 255));
|
257
|
// final RowData rd_text = new RowData();
|
258
|
// rd_text.width = 400;
|
259
|
// taxonNameText.setLayoutData(rd_text);
|
260
|
// taxonNameText.setEditable(false);
|
261
|
//
|
262
|
// //
|
263
|
// initButtons();
|
264
|
//
|
265
|
// return container;
|
266
|
// }
|
267
|
|
268
|
public void setSelectedTaxon(Taxon taxon) {
|
269
|
|
270
|
this.destinationTaxon = taxon;
|
271
|
TaxonNameBase taxonName = taxon.getName();
|
272
|
if (taxonName == null) {
|
273
|
return;
|
274
|
}
|
275
|
|
276
|
taxonNameText.setText(taxonName.getTitleCache());
|
277
|
|
278
|
setOkEnabled(true);
|
279
|
}
|
280
|
|
281
|
/**
|
282
|
* Create contents of the button bar
|
283
|
*
|
284
|
* @param parent
|
285
|
*/
|
286
|
@Override
|
287
|
protected void createButtonsForButtonBar(Composite parent) {
|
288
|
createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
|
289
|
true);
|
290
|
setOkEnabled(false);
|
291
|
createButton(parent, IDialogConstants.CANCEL_ID,
|
292
|
IDialogConstants.CANCEL_LABEL, false);
|
293
|
}
|
294
|
|
295
|
public void setOkEnabled(boolean enabled) {
|
296
|
getButton(IDialogConstants.OK_ID).setEnabled(enabled);
|
297
|
}
|
298
|
|
299
|
private void initButtons() {
|
300
|
for (Button button : buttons) {
|
301
|
button.addSelectionListener(new SelectionAdapter() {
|
302
|
public void widgetSelected(final SelectionEvent e) {
|
303
|
selectButton((Button) e.getSource());
|
304
|
}
|
305
|
});
|
306
|
}
|
307
|
}
|
308
|
|
309
|
/**
|
310
|
* Since buttons are not in the same layout group, the others are not
|
311
|
* automatically deselected when one is selected. This method takes care of
|
312
|
* that.
|
313
|
*
|
314
|
* @param button
|
315
|
*/
|
316
|
private void selectButton(Button selectedButton) {
|
317
|
for (Button button : buttons) {
|
318
|
if (button == selectedButton)
|
319
|
button.setSelection(true);
|
320
|
else
|
321
|
button.setSelection(false);
|
322
|
}
|
323
|
}
|
324
|
|
325
|
/**
|
326
|
* Return the initial size of the dialog
|
327
|
*/
|
328
|
@Override
|
329
|
protected Point getInitialSize() {
|
330
|
return new Point(700, 500);
|
331
|
}
|
332
|
|
333
|
protected void configureShell(Shell newShell) {
|
334
|
super.configureShell(newShell);
|
335
|
newShell.setImage(ImageResources.getImage(
|
336
|
ImageResources.EDIT_ICON));
|
337
|
newShell.setText(shellText);
|
338
|
}
|
339
|
|
340
|
class SelectButtonFocusListener implements FocusListener {
|
341
|
|
342
|
/**
|
343
|
* The button whose group has gained focus.
|
344
|
*/
|
345
|
Button button;
|
346
|
|
347
|
SelectButtonFocusListener(Button button) {
|
348
|
this.button = button;
|
349
|
}
|
350
|
|
351
|
public void focusGained(FocusEvent e) {
|
352
|
selectButton(button);
|
353
|
}
|
354
|
|
355
|
public void focusLost(FocusEvent e) {
|
356
|
}
|
357
|
|
358
|
}
|
359
|
|
360
|
class SelectTaxonDoubleClickListener implements IDoubleClickListener {
|
361
|
|
362
|
public void doubleClick(DoubleClickEvent event) {
|
363
|
Taxon taxon = null;
|
364
|
try {
|
365
|
taxon = (Taxon) ((StructuredSelection) event.getSelection())
|
366
|
.getFirstElement();
|
367
|
} catch (Exception e) {
|
368
|
e.printStackTrace();
|
369
|
taxon = null;
|
370
|
}
|
371
|
if (taxon.equals(SelectTaxonDialog.this.taxon)) {
|
372
|
return;
|
373
|
}
|
374
|
|
375
|
setSelectedTaxon(taxon);
|
376
|
}
|
377
|
}
|
378
|
}
|