2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.editor
;
12 import java
.util
.ArrayList
;
13 import java
.util
.List
;
15 import org
.apache
.log4j
.Logger
;
16 import org
.eclipse
.core
.databinding
.observable
.set
.IObservableSet
;
17 import org
.eclipse
.jface
.dialogs
.Dialog
;
18 import org
.eclipse
.jface
.dialogs
.IDialogConstants
;
19 import org
.eclipse
.jface
.viewers
.DoubleClickEvent
;
20 import org
.eclipse
.jface
.viewers
.IDoubleClickListener
;
21 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
22 import org
.eclipse
.jface
.viewers
.TableViewer
;
23 import org
.eclipse
.swt
.SWT
;
24 import org
.eclipse
.swt
.events
.FocusEvent
;
25 import org
.eclipse
.swt
.events
.FocusListener
;
26 import org
.eclipse
.swt
.events
.SelectionAdapter
;
27 import org
.eclipse
.swt
.events
.SelectionEvent
;
28 import org
.eclipse
.swt
.graphics
.Point
;
29 import org
.eclipse
.swt
.layout
.FillLayout
;
30 import org
.eclipse
.swt
.layout
.GridData
;
31 import org
.eclipse
.swt
.layout
.GridLayout
;
32 import org
.eclipse
.swt
.layout
.RowData
;
33 import org
.eclipse
.swt
.layout
.RowLayout
;
34 import org
.eclipse
.swt
.widgets
.Button
;
35 import org
.eclipse
.swt
.widgets
.Composite
;
36 import org
.eclipse
.swt
.widgets
.Control
;
37 import org
.eclipse
.swt
.widgets
.Display
;
38 import org
.eclipse
.swt
.widgets
.Event
;
39 import org
.eclipse
.swt
.widgets
.Group
;
40 import org
.eclipse
.swt
.widgets
.Label
;
41 import org
.eclipse
.swt
.widgets
.Listener
;
42 import org
.eclipse
.swt
.widgets
.Shell
;
43 import org
.eclipse
.swt
.widgets
.Text
;
44 import org
.eclipse
.swt
.widgets
.TreeItem
;
46 import com
.swtdesigner
.SWTResourceManager
;
48 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
49 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
50 import eu
.etaxonomy
.taxeditor
.ITaxEditorConstants
;
51 import eu
.etaxonomy
.taxeditor
.TaxEditorPlugin
;
52 import eu
.etaxonomy
.taxeditor
.model
.CdmSessionDataRepository
;
53 import eu
.etaxonomy
.taxeditor
.model
.CdmUtil
;
54 import eu
.etaxonomy
.taxeditor
.navigation
.RecentNamesTableViewer
;
55 import eu
.etaxonomy
.taxeditor
.navigation
.TaxonomicTreeLabelProvider
;
56 import eu
.etaxonomy
.taxeditor
.navigation
.TaxonomicTreeViewer
;
59 * This is a generic dialog that allows the user to choose a taxon via the
60 * taxonomic tree, recently selected names, or search.
66 public class SelectTaxonDialog
extends Dialog
{
67 private static final Logger logger
= Logger
68 .getLogger(SelectTaxonDialog
.class);
70 public static final String TAXON_TO_SYNONYM
= "taxon_to_synonym";
71 public static final String TAXON_TO_NEW_PARENT
= "taxon_to_new_parent";
73 private String dialogType
;
74 private String shellText
;
75 private String descriptionText
;
76 private Text taxonNameText
;
78 private TaxonomicTreeViewer treeViewer
;
80 private Taxon destinationTaxon
;
82 private List
<Button
> buttons
= new ArrayList
<Button
>();
84 private Composite selectComposite
;
86 public SelectTaxonDialog(Shell parentShell
, String dialogType
) {
88 this.dialogType
= dialogType
;
91 public Taxon
open(Taxon taxon
) {
94 if (dialogType
.equals(TAXON_TO_SYNONYM
)) {
95 this.shellText
= "Choose an accepted name for \""
96 + CdmUtil
.getDisplayName(taxon
) + "\"";
97 this.descriptionText
= "You have chosen to turn \""
98 + CdmUtil
.getDisplayName(taxon
)
99 + "\" into a synonym. Please choose a new accepted name from one of the following options.";
102 if (dialogType
.equals(TAXON_TO_NEW_PARENT
)) {
103 this.shellText
= "Choose a new parent for \""
104 + CdmUtil
.getDisplayName(taxon
) + "\"";
105 this.descriptionText
= "You have chosen to select a new parent for \""
106 + CdmUtil
.getDisplayName(taxon
)
107 + "\". Please choose the new parent taxon from one of the following options.";
110 int returnCode
= super.open();
111 if (returnCode
== OK
) {
112 return destinationTaxon
;
118 * Create contents of the dialog
123 protected Control
createDialogArea(Composite parent
) {
124 parent
.setRedraw(true);
125 Composite container
= (Composite
) super.createDialogArea(parent
);
126 final GridLayout gridLayout
= new GridLayout();
127 gridLayout
.numColumns
= 2;
128 container
.setLayout(gridLayout
);
130 final Label descriptionLabel
= new Label(container
, SWT
.WRAP
);
131 descriptionLabel
.setLayoutData(new GridData(SWT
.LEFT
, SWT
.FILL
, true,
133 descriptionLabel
.setText(descriptionText
);
135 final Group taxTreeGroup
= new Group(container
, SWT
.NONE
);
136 taxTreeGroup
.setLayout(new GridLayout());
137 final GridData gd_group_1
= new GridData(SWT
.FILL
, SWT
.FILL
, true, true);
138 gd_group_1
.heightHint
= 83;
139 taxTreeGroup
.setLayoutData(gd_group_1
);
141 final Button chooseTreeButton
= new Button(taxTreeGroup
, SWT
.RADIO
);
142 buttons
.add(chooseTreeButton
);
143 chooseTreeButton
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true,
145 chooseTreeButton
.setText("Choose from taxonomic tree");
147 Composite taxTreeComposite
= new Composite(taxTreeGroup
, SWT
.BORDER
);
148 taxTreeComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true,
150 taxTreeComposite
.setLayout(new FillLayout());
151 taxTreeComposite
.setBackground(Display
.getCurrent().getSystemColor(
154 treeViewer
= new TaxonomicTreeViewer(taxTreeComposite
);
156 IObservableSet observableTaxonSet
=
157 CdmSessionDataRepository
.getDefault().getObservableTaxa();
158 treeViewer
.setLabelProvider(new TaxonomicTreeLabelProvider(
159 observableTaxonSet
) {
162 treeViewer
.getTree().addFocusListener(
163 new SelectButtonFocusListener(chooseTreeButton
));
164 treeViewer
.addDoubleClickListener(new SelectTaxonDoubleClickListener());
165 treeViewer
.getTree().addListener(SWT
.PaintItem
, new Listener() {
166 /** If MenuItem with taxon is being drawn, put it in italics.
168 * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
170 public void handleEvent(Event event
) {
171 Object data
= event
.item
.getData();
172 if (data
instanceof Taxon
&& ((Taxon
) data
).equals(taxon
)) {
173 ((TreeItem
) event
.item
).setFont(TaxEditorPlugin
.getDefault().getFont
174 (ITaxEditorConstants
.MENU_ITEM_ITALICS_FONT
));
179 // Open up tree to reveal taxon
180 treeViewer
.revealTaxon(taxon
);
182 final Composite rightPaneComposite
= new Composite(container
, SWT
.NONE
);
183 rightPaneComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true,
185 rightPaneComposite
.setLayout(new FillLayout(SWT
.VERTICAL
188 final Group recentNamesGroup
= new Group(rightPaneComposite
, SWT
.NONE
);
189 recentNamesGroup
.setLayout(new GridLayout());
191 final Button chooseRecentNamesButton
= new Button(recentNamesGroup
,
193 buttons
.add(chooseRecentNamesButton
);
194 chooseRecentNamesButton
.setText("Choose from recently viewed taxa");
196 Composite recentNamesComposite
= new Composite(recentNamesGroup
,
198 recentNamesComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
,
200 recentNamesComposite
.setLayout(new FillLayout());
201 recentNamesComposite
.setBackground(Display
.getCurrent().getSystemColor(
204 TableViewer recentNamesViewer
= new RecentNamesTableViewer(
205 recentNamesComposite
);
207 .addDoubleClickListener(new SelectTaxonDoubleClickListener());
208 recentNamesViewer
.getTable().addFocusListener(
209 new SelectButtonFocusListener(chooseRecentNamesButton
));
211 final Group searchGroup
= new Group(rightPaneComposite
, SWT
.NONE
);
212 final GridLayout gridLayout_1
= new GridLayout();
213 gridLayout_1
.numColumns
= 2;
214 searchGroup
.setLayout(gridLayout_1
);
216 final Button searchForNameButton
= new Button(searchGroup
, SWT
.RADIO
);
217 buttons
.add(searchForNameButton
);
218 searchForNameButton
.setLayoutData(new GridData(SWT
.LEFT
, SWT
.CENTER
,
220 searchForNameButton
.setText("Search for name of accepted taxon");
221 new Label(searchGroup
, SWT
.NONE
);
223 final Text searchText
= new Text(searchGroup
, SWT
.BORDER
);
225 .setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
226 searchText
.setForeground(SWTResourceManager
.getColor(192, 192, 192));
227 searchText
.setText("Use \"*\" for wildcard searching");
228 searchText
.addFocusListener(new SelectButtonFocusListener(
229 searchForNameButton
) {
231 public void focusGained(FocusEvent e
) {
232 selectButton(button
);
233 searchText
.setForeground(SWTResourceManager
.getColor(0, 0, 0));
234 searchText
.setText("");
237 public void focusLost(FocusEvent e
) {
238 if (searchText
.getText() == "") {
239 searchText
.setForeground(SWTResourceManager
.getColor(192,
241 searchText
.setText("Use \"*\" for wildcard searching");
247 final Button searchButton
= new Button(searchGroup
, SWT
.NONE
);
248 searchButton
.setLayoutData(new GridData());
249 searchButton
.setText("Search");
251 selectComposite
= new Composite(container
, SWT
.NONE
);
252 selectComposite
.setLayout(new RowLayout());
253 selectComposite
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.BOTTOM
, true,
256 taxonNameText
= new Text(selectComposite
, SWT
.BORDER
);
257 taxonNameText
.setFont(SWTResourceManager
.getFont("", 12, SWT
.BOLD
));
258 taxonNameText
.setBackground(SWTResourceManager
.getColor(255, 255, 255));
259 final RowData rd_text
= new RowData();
261 taxonNameText
.setLayoutData(rd_text
);
262 taxonNameText
.setEditable(false);
270 public void setSelectedTaxon(Taxon taxon
) {
272 this.destinationTaxon
= taxon
;
273 TaxonNameBase taxonName
= taxon
.getName();
274 if (taxonName
== null) {
278 taxonNameText
.setText(taxonName
.getTitleCache());
284 * Create contents of the button bar
289 protected void createButtonsForButtonBar(Composite parent
) {
290 createButton(parent
, IDialogConstants
.OK_ID
, IDialogConstants
.OK_LABEL
,
293 createButton(parent
, IDialogConstants
.CANCEL_ID
,
294 IDialogConstants
.CANCEL_LABEL
, false);
297 public void setOkEnabled(boolean enabled
) {
298 getButton(IDialogConstants
.OK_ID
).setEnabled(enabled
);
301 private void initButtons() {
302 for (Button button
: buttons
) {
303 button
.addSelectionListener(new SelectionAdapter() {
304 public void widgetSelected(final SelectionEvent e
) {
305 selectButton((Button
) e
.getSource());
312 * Since buttons are not in the same layout group, the others are not
313 * automatically deselected when one is selected. This method takes care of
318 private void selectButton(Button selectedButton
) {
319 for (Button button
: buttons
) {
320 if (button
== selectedButton
)
321 button
.setSelection(true);
323 button
.setSelection(false);
328 * Return the initial size of the dialog
331 protected Point
getInitialSize() {
332 return new Point(700, 500);
335 protected void configureShell(Shell newShell
) {
336 super.configureShell(newShell
);
337 newShell
.setImage(TaxEditorPlugin
.getDefault().getImage(
338 ITaxEditorConstants
.EDIT_ICON
));
339 newShell
.setText(shellText
);
342 class SelectButtonFocusListener
implements FocusListener
{
345 * The button whose group has gained focus.
349 SelectButtonFocusListener(Button button
) {
350 this.button
= button
;
353 public void focusGained(FocusEvent e
) {
354 selectButton(button
);
357 public void focusLost(FocusEvent e
) {
362 class SelectTaxonDoubleClickListener
implements IDoubleClickListener
{
364 public void doubleClick(DoubleClickEvent event
) {
367 taxon
= (Taxon
) ((StructuredSelection
) event
.getSelection())
369 } catch (Exception e
) {
373 if (taxon
.equals(SelectTaxonDialog
.this.taxon
)) {
377 setSelectedTaxon(taxon
);