acf7a2b0f7c7c193cc33a3cf870bde2d1d4d70f3
[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.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;
29
30 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
31 import eu.etaxonomy.taxeditor.store.model.Resources;
32 import eu.etaxonomy.taxeditor.store.preference.PreferencesUtil;
33
34 /**
35 * @author p.ciardelli
36 * @created 07.04.2009
37 * @version 1.0
38 */
39 public class NameSelectComposite extends Composite {
40 private static final Logger logger = Logger
41 .getLogger(NameSelectComposite.class);
42
43 private TaxonNameBase savedName;
44 private Text txtName;
45 private Button btnClearName;
46
47 PropertyChangeSupport propertyChangeSupport;
48 public static final String NAME = "name";
49
50 /**
51 * @param parent
52 * @param style
53 */
54 public NameSelectComposite(Composite parent) {
55 super(parent, SWT.NULL);
56 createContent(parent);
57 }
58
59 private void createContent(Composite container) {
60
61 propertyChangeSupport = new PropertyChangeSupport(this);
62
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);
68
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:");
72
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));
81
82 // Create name input
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) {
87 notifyListeners();
88 }
89 });
90
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() {
97
98 // Popup name search
99 public void widgetSelected(SelectionEvent e) {
100 popupSearch();
101 }
102 });
103
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() {
109
110 // Clear selected name
111 public void widgetSelected(SelectionEvent e) {
112 clearName();
113 }
114 });
115
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.");
122 }
123
124 /**
125 * @param nameExists
126 */
127 protected void notifyListeners() {
128 propertyChangeSupport.firePropertyChange(NAME, null, null);
129 }
130
131 public void addPropertyChangeListener(PropertyChangeListener listener) {
132 propertyChangeSupport.addPropertyChangeListener(NAME, listener);
133 }
134
135 public void setName(TaxonNameBase name) {
136 savedName = name;
137 txtName.setText(name.getTitleCache());
138 txtName.setEditable(false);
139
140 btnClearName.setEnabled(true);
141 }
142
143 /**
144 *
145 */
146 protected void popupSearch() {
147 Dialog dialog = new NameSearchDialog(getShell());
148 Object value = ((NameSearchDialog) dialog).open();
149
150 if (value instanceof TaxonNameBase) {
151 setSavedName((TaxonNameBase) value);
152 }
153 }
154
155 /**
156 * @param value
157 */
158 private void setSavedName(TaxonNameBase name) {
159
160 savedName = name;
161
162 txtName.setText(name.getTitleCache());
163 txtName.setEditable(false);
164
165 btnClearName.setEnabled(true);
166 }
167
168 protected void clearName() {
169 savedName = null;
170
171 txtName.setText("");
172 txtName.setEditable(true);
173
174 btnClearName.setEnabled(false);
175 }
176
177 /**
178 * Returns name object, if any. Otherwise, creates a new
179 * name object using text the user has input. Zero-length name
180 * returns NULL.
181 *
182 * @return
183 */
184 public TaxonNameBase getName() {
185 TaxonNameBase name = null;
186 if (savedName != null) {
187 name = savedName;
188 } else {
189 if (!txtName.getText().equals("")) {
190 name = PreferencesUtil.getInstanceOfPreferredNameClass();
191 name.setTitleCache(txtName.getText());
192 }
193 }
194 return name;
195 }
196 }