3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
11 package eu
.etaxonomy
.taxeditor
.editor
.name
;
13 import java
.beans
.PropertyChangeListener
;
14 import java
.beans
.PropertyChangeSupport
;
16 import org
.apache
.log4j
.Logger
;
17 import org
.eclipse
.swt
.SWT
;
18 import org
.eclipse
.swt
.events
.ModifyEvent
;
19 import org
.eclipse
.swt
.events
.ModifyListener
;
20 import org
.eclipse
.swt
.events
.SelectionAdapter
;
21 import org
.eclipse
.swt
.events
.SelectionEvent
;
22 import org
.eclipse
.swt
.layout
.GridData
;
23 import org
.eclipse
.swt
.layout
.GridLayout
;
24 import org
.eclipse
.swt
.widgets
.Button
;
25 import org
.eclipse
.swt
.widgets
.Composite
;
26 import org
.eclipse
.swt
.widgets
.Dialog
;
27 import org
.eclipse
.swt
.widgets
.Label
;
28 import org
.eclipse
.swt
.widgets
.Text
;
30 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
31 import eu
.etaxonomy
.taxeditor
.store
.model
.Resources
;
32 import eu
.etaxonomy
.taxeditor
.store
.preference
.PreferencesUtil
;
39 public class NameSelectComposite
extends Composite
{
40 private static final Logger logger
= Logger
41 .getLogger(NameSelectComposite
.class);
43 private TaxonNameBase savedName
;
45 private Button btnClearName
;
47 PropertyChangeSupport propertyChangeSupport
;
48 public static final String NAME
= "name";
54 public NameSelectComposite(Composite parent
) {
55 super(parent
, SWT
.NULL
);
56 createContent(parent
);
59 private void createContent(Composite container
) {
61 propertyChangeSupport
= new PropertyChangeSupport(this);
63 setLayoutData(new GridData(SWT
.FILL
, SWT
.TOP
, true, false));
64 GridLayout gridLayout
= new GridLayout();
65 gridLayout
.marginHeight
= 0;
66 gridLayout
.marginWidth
= 0;
67 setLayout(gridLayout
);
69 Label lblName
= new Label(this, SWT
.NONE
);
70 lblName
.setLayoutData(new GridData());
71 lblName
.setText("Choose a name either by searching or by entering it as free text:");
73 // Create 3-columned composite for name input
74 Composite nameComposite
= new Composite(this, SWT
.NONE
);
75 GridLayout gridLayout2
= new GridLayout();
76 gridLayout2
.numColumns
= 3;
77 gridLayout2
.marginHeight
= 0;
78 gridLayout2
.marginWidth
= 0;
79 nameComposite
.setLayout(gridLayout2
);
80 nameComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
83 txtName
= new Text(nameComposite
, SWT
.BORDER
);
84 txtName
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
85 txtName
.addModifyListener(new ModifyListener() {
86 public void modifyText(ModifyEvent e
) {
91 // Create name search button
92 Button btnSearchName
= new Button(nameComposite
, SWT
.NONE
);
93 btnSearchName
.setEnabled(true);
94 btnSearchName
.setLayoutData(new GridData());
95 btnSearchName
.setText("Search ...");
96 btnSearchName
.addSelectionListener(new SelectionAdapter() {
99 public void widgetSelected(SelectionEvent e
) {
104 // Create clear name button
105 btnClearName
= new Button(nameComposite
, SWT
.NONE
);
106 btnClearName
.setEnabled(false);
107 btnClearName
.setText("Clear");
108 btnClearName
.addSelectionListener(new SelectionAdapter() {
110 // Clear selected name
111 public void widgetSelected(SelectionEvent e
) {
116 // Create message re: editing names
117 Label lblNewNameFeedback
= new Label(this, SWT
.NONE
);
118 lblNewNameFeedback
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, true, false));
119 lblNewNameFeedback
.setFont(
120 Resources
.italicizeFont(lblNewNameFeedback
.getFont()));
121 lblNewNameFeedback
.setText("Existing names can not be edited.");
127 protected void notifyListeners() {
128 propertyChangeSupport
.firePropertyChange(NAME
, null, null);
131 public void addPropertyChangeListener(PropertyChangeListener listener
) {
132 propertyChangeSupport
.addPropertyChangeListener(NAME
, listener
);
135 public void setName(TaxonNameBase name
) {
137 txtName
.setText(name
.getTitleCache());
138 txtName
.setEditable(false);
140 btnClearName
.setEnabled(true);
146 protected void popupSearch() {
147 Dialog dialog
= new NameSearchDialog(getShell());
148 Object value
= ((NameSearchDialog
) dialog
).open();
150 if (value
instanceof TaxonNameBase
) {
151 setSavedName((TaxonNameBase
) value
);
158 private void setSavedName(TaxonNameBase name
) {
162 txtName
.setText(name
.getTitleCache());
163 txtName
.setEditable(false);
165 btnClearName
.setEnabled(true);
168 protected void clearName() {
172 txtName
.setEditable(true);
174 btnClearName
.setEnabled(false);
178 * Returns name object, if any. Otherwise, creates a new
179 * name object using text the user has input. Zero-length name
184 public TaxonNameBase
getName() {
185 TaxonNameBase name
= null;
186 if (savedName
!= null) {
189 if (!txtName
.getText().equals("")) {
190 name
= PreferencesUtil
.getInstanceOfPreferredNameClass();
191 name
.setTitleCache(txtName
.getText());