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
.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
;
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
;
52 * This is a generic dialog that allows the user to choose a taxon via the
53 * taxonomic tree, recently selected names, or search.
59 public class SelectTaxonDialog
extends Dialog
{
60 private static final Logger logger
= Logger
61 .getLogger(SelectTaxonDialog
.class);
63 public static final String TAXON_TO_SYNONYM
= "taxon_to_synonym";
64 public static final String TAXON_TO_NEW_PARENT
= "taxon_to_new_parent";
66 private String dialogType
;
67 private String shellText
;
68 private String descriptionText
;
69 private Text taxonNameText
;
71 // FIXME no direct dependencies
72 // private TaxonomicTreeViewer treeViewer;
74 private Taxon destinationTaxon
;
76 private List
<Button
> buttons
= new ArrayList
<Button
>();
78 private Composite selectComposite
;
80 public SelectTaxonDialog(Shell parentShell
, String dialogType
) {
82 this.dialogType
= dialogType
;
85 public Taxon
open(Taxon taxon
) {
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.";
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.";
104 int returnCode
= super.open();
105 if (returnCode
== OK
) {
106 return destinationTaxon
;
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
116 // * Create contents of the dialog
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);
128 // final Label descriptionLabel = new Label(container, SWT.WRAP);
129 // descriptionLabel.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, true,
131 // descriptionLabel.setText(descriptionText);
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);
139 // final Button chooseTreeButton = new Button(taxTreeGroup, SWT.RADIO);
140 // buttons.add(chooseTreeButton);
141 // chooseTreeButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
143 // chooseTreeButton.setText("Choose from taxonomic tree");
145 // Composite taxTreeComposite = new Composite(taxTreeGroup, SWT.BORDER);
146 // taxTreeComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
148 // taxTreeComposite.setLayout(new FillLayout());
149 // taxTreeComposite.setBackground(Display.getCurrent().getSystemColor(
150 // SWT.COLOR_WHITE));
152 // treeViewer = new TaxonomicTreeViewer(taxTreeComposite);
154 // IObservableSet observableTaxonSet =
155 // CdmSessionDataRepository.getDefault().getObservableTaxa();
156 // treeViewer.setLabelProvider(new TaxonomicTreeLabelProvider(
157 // observableTaxonSet) {
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.
166 // * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
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));
177 // // Open up tree to reveal taxon
178 // treeViewer.revealTaxon(taxon);
180 // final Composite rightPaneComposite = new Composite(container, SWT.NONE);
181 // rightPaneComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
183 // rightPaneComposite.setLayout(new FillLayout(SWT.VERTICAL
184 // | SWT.HORIZONTAL));
186 // final Group recentNamesGroup = new Group(rightPaneComposite, SWT.NONE);
187 // recentNamesGroup.setLayout(new GridLayout());
189 // final Button chooseRecentNamesButton = new Button(recentNamesGroup,
191 // buttons.add(chooseRecentNamesButton);
192 // chooseRecentNamesButton.setText("Choose from recently viewed taxa");
194 // Composite recentNamesComposite = new Composite(recentNamesGroup,
196 // recentNamesComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
198 // recentNamesComposite.setLayout(new FillLayout());
199 // recentNamesComposite.setBackground(Display.getCurrent().getSystemColor(
200 // SWT.COLOR_WHITE));
202 // TableViewer recentNamesViewer = new RecentNamesTableViewer(
203 // recentNamesComposite);
205 // .addDoubleClickListener(new SelectTaxonDoubleClickListener());
206 // recentNamesViewer.getTable().addFocusListener(
207 // new SelectButtonFocusListener(chooseRecentNamesButton));
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);
214 // final Button searchForNameButton = new Button(searchGroup, SWT.RADIO);
215 // buttons.add(searchForNameButton);
216 // searchForNameButton.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER,
218 // searchForNameButton.setText("Search for name of accepted taxon");
219 // new Label(searchGroup, SWT.NONE);
221 // final Text searchText = new Text(searchGroup, SWT.BORDER);
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) {
229 // public void focusGained(FocusEvent e) {
230 // selectButton(button);
231 // searchText.setForeground(SWTResourceManager.getColor(0, 0, 0));
232 // searchText.setText("");
235 // public void focusLost(FocusEvent e) {
236 // if (searchText.getText() == "") {
237 // searchText.setForeground(SWTResourceManager.getColor(192,
239 // searchText.setText("Use \"*\" for wildcard searching");
245 // final Button searchButton = new Button(searchGroup, SWT.NONE);
246 // searchButton.setLayoutData(new GridData());
247 // searchButton.setText("Search");
249 // selectComposite = new Composite(container, SWT.NONE);
250 // selectComposite.setLayout(new RowLayout());
251 // selectComposite.setLayoutData(new GridData(SWT.RIGHT, SWT.BOTTOM, true,
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);
268 public void setSelectedTaxon(Taxon taxon
) {
270 this.destinationTaxon
= taxon
;
271 TaxonNameBase taxonName
= taxon
.getName();
272 if (taxonName
== null) {
276 taxonNameText
.setText(taxonName
.getTitleCache());
282 * Create contents of the button bar
287 protected void createButtonsForButtonBar(Composite parent
) {
288 createButton(parent
, IDialogConstants
.OK_ID
, IDialogConstants
.OK_LABEL
,
291 createButton(parent
, IDialogConstants
.CANCEL_ID
,
292 IDialogConstants
.CANCEL_LABEL
, false);
295 public void setOkEnabled(boolean enabled
) {
296 getButton(IDialogConstants
.OK_ID
).setEnabled(enabled
);
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());
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
316 private void selectButton(Button selectedButton
) {
317 for (Button button
: buttons
) {
318 if (button
== selectedButton
)
319 button
.setSelection(true);
321 button
.setSelection(false);
326 * Return the initial size of the dialog
329 protected Point
getInitialSize() {
330 return new Point(700, 500);
333 protected void configureShell(Shell newShell
) {
334 super.configureShell(newShell
);
335 newShell
.setImage(ImageResources
.getImage(
336 ImageResources
.EDIT_ICON
));
337 newShell
.setText(shellText
);
340 class SelectButtonFocusListener
implements FocusListener
{
343 * The button whose group has gained focus.
347 SelectButtonFocusListener(Button button
) {
348 this.button
= button
;
351 public void focusGained(FocusEvent e
) {
352 selectButton(button
);
355 public void focusLost(FocusEvent e
) {
360 class SelectTaxonDoubleClickListener
implements IDoubleClickListener
{
362 public void doubleClick(DoubleClickEvent event
) {
365 taxon
= (Taxon
) ((StructuredSelection
) event
.getSelection())
367 } catch (Exception e
) {
371 if (taxon
.equals(SelectTaxonDialog
.this.taxon
)) {
375 setSelectedTaxon(taxon
);