1
|
/**
|
2
|
* Copyright (C) 2017 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.taxeditor.databaseAdmin.preferencePage;
|
10
|
|
11
|
|
12
|
import java.awt.Toolkit;
|
13
|
|
14
|
import javax.swing.event.DocumentEvent;
|
15
|
|
16
|
import org.apache.commons.lang.StringUtils;
|
17
|
import org.eclipse.swt.SWT;
|
18
|
import org.eclipse.swt.events.MouseEvent;
|
19
|
import org.eclipse.swt.events.MouseListener;
|
20
|
import org.eclipse.swt.graphics.Rectangle;
|
21
|
import org.eclipse.swt.layout.GridData;
|
22
|
import org.eclipse.swt.widgets.Button;
|
23
|
import org.eclipse.swt.widgets.Composite;
|
24
|
import org.eclipse.swt.widgets.List;
|
25
|
import org.eclipse.swt.widgets.Text;
|
26
|
|
27
|
import eu.etaxonomy.cdm.model.metadata.CdmPreference;
|
28
|
import eu.etaxonomy.taxeditor.l10n.Messages;
|
29
|
import eu.etaxonomy.taxeditor.preference.EditorPreferencePredicate;
|
30
|
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
|
31
|
|
32
|
|
33
|
/**
|
34
|
* @author k.luther
|
35
|
* @date 20.04.2017
|
36
|
*
|
37
|
*/
|
38
|
public class ListComponent
|
39
|
{
|
40
|
private List list;
|
41
|
|
42
|
private static final String addString = Messages.ListComponent_ADD_PROVIDER;
|
43
|
private static final String removeString = Messages.ListComponent_REMOVE_PROVIDER;
|
44
|
private static final String noProvider = Messages.ListComponent_NO_PROVIDER_AVAILABLE;
|
45
|
private Button removeButton;
|
46
|
private Text providerURI;
|
47
|
|
48
|
public ListComponent(Composite parent, int style) {
|
49
|
|
50
|
list = new List(parent, SWT.BORDER | SWT.V_SCROLL);
|
51
|
GridData gridData = new GridData(SWT.FILL, SWT.CENTER, true, true, 2, 3);
|
52
|
gridData.grabExcessHorizontalSpace = true;
|
53
|
list.setLayoutData(gridData);
|
54
|
|
55
|
Rectangle clientArea = parent.getShell().getClientArea ();
|
56
|
list.setBounds (clientArea.x, clientArea.y, 50, 500);
|
57
|
|
58
|
CdmPreference providerListPreference = PreferencesUtil.getPreferenceFromDB(EditorPreferencePredicate.BioCaseProvider);
|
59
|
String allProviderString = ""; //$NON-NLS-1$
|
60
|
if (providerListPreference != null){
|
61
|
allProviderString = providerListPreference.getValue();
|
62
|
}
|
63
|
// = PreferencesUtil.getPreferenceStore().getString(IPreferenceKeys.BIOCASE_PROVIDER_LIST);
|
64
|
//the string is structured like this: http://ww3.bgbm.org/biocase/pywrapper.cgi?dsa=DNA_Bank;http:...;
|
65
|
String[] providerArray = allProviderString.split(";"); //$NON-NLS-1$
|
66
|
for (String providerString : providerArray){
|
67
|
if (!StringUtils.isBlank(providerString)){
|
68
|
list.add(providerString);
|
69
|
}
|
70
|
}
|
71
|
if (list.getItemCount() == 0){
|
72
|
list.add(noProvider);
|
73
|
}
|
74
|
list.add(""); //$NON-NLS-1$
|
75
|
|
76
|
// list.setSelection(0);
|
77
|
GridData dataList = new GridData();
|
78
|
dataList.horizontalAlignment = GridData.FILL;
|
79
|
dataList.horizontalSpan = 3;
|
80
|
|
81
|
|
82
|
// dataList.grabExcessVerticalSpace = true;
|
83
|
|
84
|
list.setLayoutData(dataList);
|
85
|
|
86
|
|
87
|
providerURI = new Text(parent, SWT.BORDER | SWT.SINGLE | SWT.FULL_SELECTION);
|
88
|
GridData dataProviderUri = new GridData();
|
89
|
dataProviderUri.horizontalAlignment = GridData.FILL;
|
90
|
dataProviderUri.horizontalSpan = 3;
|
91
|
providerURI.setLayoutData(dataProviderUri);
|
92
|
|
93
|
Button addButton = new Button(parent, SWT.NULL);
|
94
|
AddListener addListener = new AddListener(addButton);
|
95
|
addButton.setText(addString);
|
96
|
addButton.addMouseListener(addListener);
|
97
|
|
98
|
removeButton = new Button(parent, SWT.NULL);
|
99
|
removeButton.setText(removeString);
|
100
|
removeButton.addMouseListener(new RemoveListener());
|
101
|
}
|
102
|
|
103
|
class RemoveListener implements MouseListener {
|
104
|
@Override
|
105
|
public void mouseUp(MouseEvent e) {
|
106
|
//This method can be called only if
|
107
|
//there's a valid selection
|
108
|
//so go ahead and remove whatever's selected.
|
109
|
int index = list.getSelectionIndex();
|
110
|
list.remove(index);
|
111
|
|
112
|
int size = list.getItemCount();
|
113
|
|
114
|
if (size == 0) { //Nothing's left, disable removing.
|
115
|
removeButton.setEnabled(false);
|
116
|
|
117
|
} else { //Select an index.
|
118
|
if (index == size) {
|
119
|
//removed item in last position
|
120
|
index--;
|
121
|
}
|
122
|
|
123
|
list.setSelection(index-1);
|
124
|
|
125
|
|
126
|
}
|
127
|
}
|
128
|
/**
|
129
|
* {@inheritDoc}
|
130
|
*/
|
131
|
@Override
|
132
|
public void mouseDoubleClick(MouseEvent e) {
|
133
|
// TODO Auto-generated method stub
|
134
|
|
135
|
}
|
136
|
|
137
|
|
138
|
/**
|
139
|
* {@inheritDoc}
|
140
|
*/
|
141
|
@Override
|
142
|
public void mouseDown(MouseEvent e) {
|
143
|
// TODO Auto-generated method stub
|
144
|
|
145
|
}
|
146
|
}
|
147
|
|
148
|
//This listener is shared by the text field and the add button.
|
149
|
class AddListener implements MouseListener {
|
150
|
private boolean alreadyEnabled = false;
|
151
|
private Button button;
|
152
|
|
153
|
public AddListener(Button button) {
|
154
|
this.button = button;
|
155
|
}
|
156
|
|
157
|
|
158
|
protected boolean alreadyInList(String name) {
|
159
|
return list.getData(name) != null;
|
160
|
}
|
161
|
|
162
|
|
163
|
private void enableButton() {
|
164
|
if (!alreadyEnabled) {
|
165
|
button.setEnabled(true);
|
166
|
}
|
167
|
}
|
168
|
|
169
|
private boolean handleEmptyTextField(DocumentEvent e) {
|
170
|
if (e.getDocument().getLength() <= 0) {
|
171
|
button.setEnabled(false);
|
172
|
alreadyEnabled = false;
|
173
|
return true;
|
174
|
}
|
175
|
return false;
|
176
|
}
|
177
|
|
178
|
|
179
|
|
180
|
/**
|
181
|
* {@inheritDoc}
|
182
|
*/
|
183
|
@Override
|
184
|
public void mouseDoubleClick(MouseEvent e) {
|
185
|
// TODO Auto-generated method stub
|
186
|
|
187
|
}
|
188
|
|
189
|
/**
|
190
|
* {@inheritDoc}
|
191
|
*/
|
192
|
@Override
|
193
|
public void mouseDown(MouseEvent e) {
|
194
|
// TODO Auto-generated method stub
|
195
|
|
196
|
}
|
197
|
|
198
|
/**
|
199
|
* {@inheritDoc}
|
200
|
*/
|
201
|
@Override
|
202
|
public void mouseUp(MouseEvent event) {
|
203
|
String name = providerURI.getText();
|
204
|
|
205
|
//User didn't type in a unique name...
|
206
|
if (name.equals("") || alreadyInList(name)) { //$NON-NLS-1$
|
207
|
Toolkit.getDefaultToolkit().beep();
|
208
|
providerURI.selectAll();
|
209
|
return;
|
210
|
}
|
211
|
|
212
|
int index = list.getSelectionIndex(); //get selected index
|
213
|
if (index == -1) { //no selection, so insert at beginning
|
214
|
index = 0;
|
215
|
} else { //add after the selected item
|
216
|
index++;
|
217
|
}
|
218
|
if(list.getItemCount() == 0){
|
219
|
index = 0;
|
220
|
}else if (list.getItem(0).equals(noProvider)){
|
221
|
list.remove(noProvider);
|
222
|
}
|
223
|
int itemCount = list.getItemCount();
|
224
|
|
225
|
|
226
|
list.add(providerURI.getText(), index);
|
227
|
list.setSelection(index);
|
228
|
list.update();
|
229
|
list.redraw();
|
230
|
|
231
|
//Select the new item
|
232
|
|
233
|
providerURI.setText(""); //$NON-NLS-1$
|
234
|
|
235
|
}
|
236
|
}
|
237
|
|
238
|
public String createAllProviderString(){
|
239
|
String allProviderString = null;
|
240
|
boolean first = true;
|
241
|
for (String item: list.getItems()){
|
242
|
if (first || allProviderString == null || allProviderString == ""){ //$NON-NLS-1$
|
243
|
allProviderString = item.trim();
|
244
|
first = false;
|
245
|
}else{
|
246
|
allProviderString +=";"+ item.trim(); //$NON-NLS-1$
|
247
|
}
|
248
|
}
|
249
|
|
250
|
return allProviderString;
|
251
|
|
252
|
}
|
253
|
|
254
|
|
255
|
|
256
|
}
|
257
|
|