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
.eclipse
.swt
.SWT
;
17 import org
.eclipse
.swt
.events
.ModifyEvent
;
18 import org
.eclipse
.swt
.events
.ModifyListener
;
19 import org
.eclipse
.swt
.events
.SelectionAdapter
;
20 import org
.eclipse
.swt
.events
.SelectionEvent
;
21 import org
.eclipse
.swt
.layout
.GridData
;
22 import org
.eclipse
.swt
.layout
.GridLayout
;
23 import org
.eclipse
.swt
.widgets
.Button
;
24 import org
.eclipse
.swt
.widgets
.Composite
;
25 import org
.eclipse
.swt
.widgets
.Dialog
;
26 import org
.eclipse
.swt
.widgets
.Label
;
27 import org
.eclipse
.swt
.widgets
.Text
;
29 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
30 import eu
.etaxonomy
.taxeditor
.model
.Resources
;
31 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
38 public class NameSelectComposite
extends Composite
{
40 private TaxonNameBase
<?
, ?
> savedName
;
42 private Button btnClearName
;
44 PropertyChangeSupport propertyChangeSupport
;
45 public static final String NAME
= "name";
51 public NameSelectComposite(Composite parent
) {
52 super(parent
, SWT
.NULL
);
53 createContent(parent
);
56 private void createContent(Composite container
) {
58 propertyChangeSupport
= new PropertyChangeSupport(this);
60 setLayoutData(new GridData(SWT
.FILL
, SWT
.TOP
, true, false));
61 GridLayout gridLayout
= new GridLayout();
62 gridLayout
.marginHeight
= 0;
63 gridLayout
.marginWidth
= 0;
64 setLayout(gridLayout
);
66 Label lblName
= new Label(this, SWT
.NONE
);
67 lblName
.setLayoutData(new GridData());
68 lblName
.setText("Choose a name either by searching or by entering it as free text:");
70 // Create 3-columned composite for name input
71 Composite nameComposite
= new Composite(this, SWT
.NONE
);
72 GridLayout gridLayout2
= new GridLayout();
73 gridLayout2
.numColumns
= 3;
74 gridLayout2
.marginHeight
= 0;
75 gridLayout2
.marginWidth
= 0;
76 nameComposite
.setLayout(gridLayout2
);
77 nameComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
80 txtName
= new Text(nameComposite
, SWT
.BORDER
);
81 txtName
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
82 txtName
.addModifyListener(new ModifyListener() {
83 public void modifyText(ModifyEvent e
) {
88 // Create name search button
89 Button btnSearchName
= new Button(nameComposite
, SWT
.NONE
);
90 btnSearchName
.setEnabled(true);
91 btnSearchName
.setLayoutData(new GridData());
92 btnSearchName
.setText("Search ...");
93 btnSearchName
.addSelectionListener(new SelectionAdapter() {
96 public void widgetSelected(SelectionEvent e
) {
101 // Create clear name button
102 btnClearName
= new Button(nameComposite
, SWT
.NONE
);
103 btnClearName
.setEnabled(false);
104 btnClearName
.setText("Clear");
105 btnClearName
.addSelectionListener(new SelectionAdapter() {
107 // Clear selected name
108 public void widgetSelected(SelectionEvent e
) {
113 // Create message re: editing names
114 Label lblNewNameFeedback
= new Label(this, SWT
.NONE
);
115 lblNewNameFeedback
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, true, false));
116 lblNewNameFeedback
.setFont(
117 Resources
.italicizeFont(lblNewNameFeedback
.getFont()));
118 lblNewNameFeedback
.setText("Existing names can not be edited.");
124 protected void notifyListeners() {
125 propertyChangeSupport
.firePropertyChange(NAME
, null, null);
128 public void addPropertyChangeListener(PropertyChangeListener listener
) {
129 propertyChangeSupport
.addPropertyChangeListener(NAME
, listener
);
132 public void setName(TaxonNameBase
<?
, ?
> name
) {
134 txtName
.setText(name
.getTitleCache());
135 txtName
.setEditable(false);
137 btnClearName
.setEnabled(true);
143 protected void popupSearch() {
144 Dialog dialog
= new NameSearchDialog(getShell());
145 Object value
= ((NameSearchDialog
) dialog
).open();
147 if (value
instanceof TaxonNameBase
) {
148 setSavedName((TaxonNameBase
<?
, ?
>) value
);
155 private void setSavedName(TaxonNameBase
<?
, ?
> name
) {
159 txtName
.setText(name
.getTitleCache());
160 txtName
.setEditable(false);
162 btnClearName
.setEnabled(true);
165 protected void clearName() {
169 txtName
.setEditable(true);
171 btnClearName
.setEnabled(false);
175 * Returns name object, if any. Otherwise, creates a new
176 * name object using text the user has input. Zero-length name
181 public TaxonNameBase
<?
, ?
> getName() {
182 TaxonNameBase
<?
, ?
> name
= null;
183 if (savedName
!= null) {
186 if (!txtName
.getText().equals("")) {
187 name
= PreferencesUtil
.getInstanceOfPreferredNameClass();
188 name
.setTitleCache(txtName
.getText());