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