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