Moving editor sources back into trunk
[taxeditor.git] / taxeditor-editor / src / main / java / eu / etaxonomy / taxeditor / editor / reference / ReferenceSelectComposite.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.reference;
12
13 import org.apache.log4j.Logger;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.events.SelectionAdapter;
16 import org.eclipse.swt.events.SelectionEvent;
17 import org.eclipse.swt.layout.GridData;
18 import org.eclipse.swt.layout.GridLayout;
19 import org.eclipse.swt.widgets.Button;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.Dialog;
22 import org.eclipse.swt.widgets.Label;
23 import org.eclipse.swt.widgets.Text;
24
25 import eu.etaxonomy.cdm.common.CdmUtils;
26 import eu.etaxonomy.cdm.model.reference.Generic;
27 import eu.etaxonomy.cdm.model.reference.ReferenceBase;
28 import eu.etaxonomy.taxeditor.store.model.Resources;
29
30 /**
31 * @author p.ciardelli
32 * @created 07.04.2009
33 * @version 1.0
34 */
35 public class ReferenceSelectComposite extends Composite {
36 private static final Logger logger = Logger
37 .getLogger(ReferenceSelectComposite.class);
38 private ReferenceBase savedReference;
39 private Text txtReference;
40 private Button btnClearReference;
41 private Text txtMicroRef;
42
43 /**
44 * @param parent
45 * @param style
46 */
47 public ReferenceSelectComposite(Composite parent) {
48 super(parent, SWT.NULL);
49 createContent(parent);
50 }
51
52 private void createContent(Composite xcontainer) {
53 setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
54 GridLayout gridLayout = new GridLayout();
55 gridLayout.marginHeight = 0;
56 gridLayout.marginWidth = 0;
57 setLayout(gridLayout);
58
59 Label lblReference = new Label(this, SWT.NONE);
60 lblReference.setLayoutData(new GridData());
61 lblReference.setText("Choose a reference either by searching or entering it as free text:");
62
63 // Create 3-columned composite for reference input
64 Composite refComposite = new Composite(this, SWT.NONE);
65 GridLayout gridLayout2 = new GridLayout();
66 gridLayout2.numColumns = 3;
67 gridLayout2.marginHeight = 0;
68 gridLayout2.marginWidth = 0;
69 refComposite.setLayout(gridLayout2);
70 refComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
71
72 // Create reference input
73 txtReference = new Text(refComposite, SWT.BORDER);
74 txtReference.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
75
76 // Create reference search button
77 Button btnSearchReference = new Button(refComposite, SWT.NONE);
78 btnSearchReference.setEnabled(true);
79 btnSearchReference.setLayoutData(new GridData());
80 btnSearchReference.setText("Search ...");
81 btnSearchReference.addSelectionListener(new SelectionAdapter() {
82
83 // Popup reference search
84 public void widgetSelected(SelectionEvent e) {
85 popupSearch();
86 }
87 });
88
89 // Create clear reference button
90 btnClearReference = new Button(refComposite, SWT.NONE);
91 btnClearReference.setEnabled(false);
92 btnClearReference.setText("Clear");
93 btnClearReference.addSelectionListener(new SelectionAdapter() {
94
95 // Clear selected reference
96 public void widgetSelected(SelectionEvent e) {
97 clearReference();
98 }
99 });
100
101 // Create message re: editing references
102 Label lblNewRefFeedback = new Label(this, SWT.NONE);
103 lblNewRefFeedback.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, false));
104 lblNewRefFeedback.setFont(
105 Resources.italicizeFont(lblNewRefFeedback.getFont()));
106 lblNewRefFeedback.setText("Existing references can only be edited in property sheet.");
107
108 Label lblMicroref = new Label(this, SWT.NONE);
109 lblMicroref.setText("Enter reference detail (e.g., page no.):");
110
111 // Create microref input
112 txtMicroRef = new Text(this, SWT.BORDER);
113 txtMicroRef.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false));
114 }
115
116 public void setReference(ReferenceBase reference) {
117 savedReference = reference;
118 txtReference.setText(reference.getTitleCache());
119 txtReference.setEditable(false);
120
121 btnClearReference.setEnabled(true);
122 }
123
124 public void setMicroReference(String microref) {
125 txtMicroRef.setText(CdmUtils.Nz(microref));
126 }
127
128 /**
129 *
130 */
131 protected void popupSearch() {
132 Dialog dialog = new ReferenceSearchDialog(getShell(),
133 IReferenceSearch.BIBREF);
134 Object value = ((ReferenceSearchDialog) dialog).open();
135
136 if (value instanceof ReferenceBase) {
137 setSavedReference((ReferenceBase) value);
138 }
139 }
140
141 /**
142 * @param value
143 */
144 private void setSavedReference(ReferenceBase reference) {
145
146 savedReference = reference;
147
148 txtReference.setText(reference.getTitleCache());
149 txtReference.setEditable(false);
150
151 btnClearReference.setEnabled(true);
152 }
153
154 protected void clearReference() {
155 savedReference = null;
156
157 txtReference.setText("");
158 txtReference.setEditable(true);
159
160 btnClearReference.setEnabled(false);
161 }
162
163 /**
164 * Returns reference object, if any. Otherwise, creates a new Generic
165 * reference object using text the user has input.
166 * If no text, returns null.
167 *
168 * @return
169 */
170 public ReferenceBase getReference() {
171 ReferenceBase reference = null;
172 if (savedReference != null) {
173 reference = savedReference;
174 } else {
175 if (!txtReference.getText().equals("")) {
176 reference = Generic.NewInstance();
177 reference.setTitleCache(txtReference.getText());
178 }
179 }
180 return reference;
181 }
182
183 public String getMicroReference() {
184 return txtMicroRef.getText();
185 }
186 }