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