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