.
[taxeditor.git] / taxeditor-editor / src / main / java / eu / etaxonomy / taxeditor / editor / name / NameSelectComposite.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
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.
9 */
10
11 package eu.etaxonomy.taxeditor.editor.name;
12
13 import java.beans.PropertyChangeListener;
14 import java.beans.PropertyChangeSupport;
15
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;
28
29 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
30 import eu.etaxonomy.taxeditor.model.Resources;
31 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
32
33 /**
34 * @author p.ciardelli
35 * @created 07.04.2009
36 * @version 1.0
37 */
38 public class NameSelectComposite extends Composite {
39
40 private TaxonNameBase<?, ?> savedName;
41 private Text txtName;
42 private Button btnClearName;
43
44 PropertyChangeSupport propertyChangeSupport;
45 public static final String NAME = "name";
46
47 /**
48 * @param parent
49 * @param style
50 */
51 public NameSelectComposite(Composite parent) {
52 super(parent, SWT.NULL);
53 createContent(parent);
54 }
55
56 private void createContent(Composite container) {
57
58 propertyChangeSupport = new PropertyChangeSupport(this);
59
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);
65
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:");
69
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));
78
79 // Create name input
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) {
84 notifyListeners();
85 }
86 });
87
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() {
94
95 // Popup name search
96 public void widgetSelected(SelectionEvent e) {
97 popupSearch();
98 }
99 });
100
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() {
106
107 // Clear selected name
108 public void widgetSelected(SelectionEvent e) {
109 clearName();
110 }
111 });
112
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.");
119 }
120
121 /**
122 * @param nameExists
123 */
124 protected void notifyListeners() {
125 propertyChangeSupport.firePropertyChange(NAME, null, null);
126 }
127
128 public void addPropertyChangeListener(PropertyChangeListener listener) {
129 propertyChangeSupport.addPropertyChangeListener(NAME, listener);
130 }
131
132 public void setName(TaxonNameBase<?, ?> name) {
133 savedName = name;
134 txtName.setText(name.getTitleCache());
135 txtName.setEditable(false);
136
137 btnClearName.setEnabled(true);
138 }
139
140 /**
141 *
142 */
143 protected void popupSearch() {
144 Dialog dialog = new NameSearchDialog(getShell());
145 Object value = ((NameSearchDialog) dialog).open();
146
147 if (value instanceof TaxonNameBase) {
148 setSavedName((TaxonNameBase<?, ?>) value);
149 }
150 }
151
152 /**
153 * @param value
154 */
155 private void setSavedName(TaxonNameBase<?, ?> name) {
156
157 savedName = name;
158
159 txtName.setText(name.getTitleCache());
160 txtName.setEditable(false);
161
162 btnClearName.setEnabled(true);
163 }
164
165 protected void clearName() {
166 savedName = null;
167
168 txtName.setText("");
169 txtName.setEditable(true);
170
171 btnClearName.setEnabled(false);
172 }
173
174 /**
175 * Returns name object, if any. Otherwise, creates a new
176 * name object using text the user has input. Zero-length name
177 * returns NULL.
178 *
179 * @return
180 */
181 public TaxonNameBase<?, ?> getName() {
182 TaxonNameBase<?, ?> name = null;
183 if (savedName != null) {
184 name = savedName;
185 } else {
186 if (!txtName.getText().equals("")) {
187 name = PreferencesUtil.getInstanceOfPreferredNameClass();
188 name.setTitleCache(txtName.getText());
189 }
190 }
191 return name;
192 }
193 }