1
|
/**
|
2
|
* Copyright (C) 2007 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.printpublisher.wizard;
|
10
|
|
11
|
import java.lang.reflect.InvocationTargetException;
|
12
|
import java.net.MalformedURLException;
|
13
|
import java.net.URL;
|
14
|
import java.util.List;
|
15
|
|
16
|
import org.eclipse.core.runtime.IProgressMonitor;
|
17
|
import org.eclipse.jface.operation.IRunnableWithProgress;
|
18
|
import org.eclipse.jface.viewers.ColumnLabelProvider;
|
19
|
import org.eclipse.jface.viewers.ILabelProvider;
|
20
|
import org.eclipse.jface.viewers.ISelectionChangedListener;
|
21
|
import org.eclipse.jface.viewers.ITreeContentProvider;
|
22
|
import org.eclipse.jface.viewers.SelectionChangedEvent;
|
23
|
import org.eclipse.jface.viewers.StructuredSelection;
|
24
|
import org.eclipse.jface.viewers.TreeViewer;
|
25
|
import org.eclipse.jface.viewers.Viewer;
|
26
|
import org.eclipse.swt.SWT;
|
27
|
import org.eclipse.swt.events.ModifyEvent;
|
28
|
import org.eclipse.swt.events.ModifyListener;
|
29
|
import org.eclipse.swt.events.SelectionAdapter;
|
30
|
import org.eclipse.swt.events.SelectionEvent;
|
31
|
import org.eclipse.swt.layout.GridData;
|
32
|
import org.eclipse.swt.layout.GridLayout;
|
33
|
import org.eclipse.swt.widgets.Button;
|
34
|
import org.eclipse.swt.widgets.Composite;
|
35
|
import org.eclipse.swt.widgets.Display;
|
36
|
import org.eclipse.swt.widgets.Group;
|
37
|
import org.eclipse.swt.widgets.Text;
|
38
|
import org.jdom.Element;
|
39
|
|
40
|
import eu.etaxonomy.cdm.print.IXMLEntityFactory;
|
41
|
import eu.etaxonomy.cdm.print.PublishConfigurator;
|
42
|
import eu.etaxonomy.cdm.print.XMLHelper;
|
43
|
import eu.etaxonomy.cdm.print.XMLHelper.EntityType;
|
44
|
import eu.etaxonomy.taxeditor.model.CdmProgressMonitorAdapter;
|
45
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
46
|
|
47
|
/**
|
48
|
* <p>SelectServiceWizardPage class.</p>
|
49
|
*
|
50
|
* @author n.hoffmann
|
51
|
* @created Apr 6, 2010
|
52
|
*/
|
53
|
public class SelectServiceWizardPage extends AbstractPublishWizardPage {
|
54
|
|
55
|
private Composite composite;
|
56
|
private Button button_local;
|
57
|
private Button button_remote;
|
58
|
private Text text_serviceUrl;
|
59
|
|
60
|
private TreeViewer treeViewer;
|
61
|
|
62
|
protected SelectServiceWizardPage(String pageName) {
|
63
|
super(pageName);
|
64
|
setTitle("Select a Service");
|
65
|
}
|
66
|
|
67
|
public void createControl(Composite parent) {
|
68
|
|
69
|
composite = new Composite(parent, SWT.NULL);
|
70
|
composite.setLayout(new GridLayout());
|
71
|
|
72
|
RadioSelectionListener listener = new RadioSelectionListener();
|
73
|
|
74
|
Group radioGroup = new Group(composite, SWT.SHADOW_ETCHED_IN);
|
75
|
radioGroup.setLayout(new GridLayout());
|
76
|
|
77
|
button_local = new Button(radioGroup, SWT.RADIO);
|
78
|
|
79
|
button_local.setText("Local (By selecting this option the database you are currently " +
|
80
|
"connected to will be used to gather data.)");
|
81
|
|
82
|
button_local.addSelectionListener(listener);
|
83
|
|
84
|
button_remote = new Button(radioGroup, SWT.RADIO);
|
85
|
button_remote.setText("Remote");
|
86
|
button_remote.addSelectionListener(listener);
|
87
|
|
88
|
text_serviceUrl = new Text(radioGroup, SWT.BORDER);
|
89
|
text_serviceUrl.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
|
90
|
text_serviceUrl.setText("http://");
|
91
|
text_serviceUrl.addModifyListener(new ModifyListener() {
|
92
|
|
93
|
public void modifyText(ModifyEvent e) {
|
94
|
String text = text_serviceUrl.getText();
|
95
|
|
96
|
if(!text.endsWith("/")){
|
97
|
SelectServiceWizardPage.this.setErrorMessage("Webservice URL has to end with \"/\"");
|
98
|
setPageComplete(false);
|
99
|
return;
|
100
|
}
|
101
|
|
102
|
URL url = null;
|
103
|
try {
|
104
|
url = new URL(text);
|
105
|
} catch (MalformedURLException e1) {
|
106
|
SelectServiceWizardPage.this.setErrorMessage("Webservice URL is malformed.");
|
107
|
setPageComplete(false);
|
108
|
return;
|
109
|
}
|
110
|
|
111
|
getConfigurator().setWebserviceUrl(url);
|
112
|
|
113
|
SelectServiceWizardPage.this.setErrorMessage(null);
|
114
|
|
115
|
}
|
116
|
});
|
117
|
|
118
|
treeViewer = new TreeViewer(composite);
|
119
|
|
120
|
treeViewer.setContentProvider(new ContentProvider());
|
121
|
treeViewer.setLabelProvider(new LabelProvider());
|
122
|
|
123
|
treeViewer.addSelectionChangedListener(new SelectionChangedListener());
|
124
|
|
125
|
treeViewer.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
|
126
|
|
127
|
// if(CdmStore.isActive()){
|
128
|
// enableLocal();
|
129
|
// }else{
|
130
|
enableRemote();
|
131
|
button_local.setEnabled(false);
|
132
|
// }
|
133
|
|
134
|
setControl(composite);
|
135
|
}
|
136
|
|
137
|
private class RadioSelectionListener extends SelectionAdapter{
|
138
|
@Override
|
139
|
public void widgetSelected(SelectionEvent e) {
|
140
|
if(button_local.getSelection()){
|
141
|
enableLocal();
|
142
|
}else if(button_remote.getSelection()){
|
143
|
enableRemote();
|
144
|
}
|
145
|
}
|
146
|
}
|
147
|
|
148
|
private void enableRemote() {
|
149
|
button_local.setSelection(false);
|
150
|
button_remote.setSelection(true);
|
151
|
|
152
|
text_serviceUrl.setEnabled(true);
|
153
|
|
154
|
setConfigurator(PublishConfigurator.NewRemoteInstance());
|
155
|
getConfigurator().addOutputModule(getOutputModule());
|
156
|
}
|
157
|
|
158
|
/**
|
159
|
* @deprecated real local is not supported anymore, also the currently active
|
160
|
* database is a "remoting" database
|
161
|
*/
|
162
|
@Deprecated
|
163
|
private void enableLocal() {
|
164
|
button_remote.setSelection(false);
|
165
|
button_local.setSelection(true);
|
166
|
|
167
|
text_serviceUrl.setEnabled(false);
|
168
|
|
169
|
setConfigurator(PublishConfigurator.NewRemoteInstance());
|
170
|
getConfigurator().addOutputModule(getOutputModule());
|
171
|
refresh();
|
172
|
}
|
173
|
|
174
|
@Override
|
175
|
public boolean canFlipToNextPage() {
|
176
|
return isPageComplete();
|
177
|
}
|
178
|
|
179
|
public void refresh(){
|
180
|
|
181
|
if(getConfigurator() != null){
|
182
|
|
183
|
IRunnableWithProgress runnable = new IRunnableWithProgress(){
|
184
|
|
185
|
@Override
|
186
|
public void run(IProgressMonitor monitor) {
|
187
|
monitor.beginTask("Loading classifications", IProgressMonitor.UNKNOWN);
|
188
|
IXMLEntityFactory factory = getConfigurator().getFactory();
|
189
|
final List<Element> classifications = factory.getClassifications();
|
190
|
|
191
|
Display.getDefault().asyncExec(new Runnable(){
|
192
|
|
193
|
@Override
|
194
|
public void run() {
|
195
|
treeViewer.setInput(classifications);
|
196
|
}
|
197
|
|
198
|
});
|
199
|
monitor.done();
|
200
|
}
|
201
|
|
202
|
};
|
203
|
try {
|
204
|
getContainer().run(true, false, runnable);
|
205
|
} catch (InvocationTargetException e) {
|
206
|
MessagingUtils.error(this.getClass(), e);
|
207
|
} catch (InterruptedException e) {
|
208
|
MessagingUtils.error(this.getClass(), e);
|
209
|
}
|
210
|
}
|
211
|
}
|
212
|
|
213
|
@Override
|
214
|
public boolean isPageComplete() {
|
215
|
boolean complete = true;
|
216
|
if(getConfigurator().isLocal()){
|
217
|
complete &= true;
|
218
|
}else if(getConfigurator().isRemote()
|
219
|
&& getConfigurator().getWebserviceUrl() != null
|
220
|
&& getConfigurator().getWebserviceUrl().toString().endsWith("/")){
|
221
|
complete &= true;
|
222
|
}else{
|
223
|
return false;
|
224
|
}
|
225
|
|
226
|
List<Element> selectedTaxonNodes = getConfigurator().getSelectedTaxonNodeElements();
|
227
|
|
228
|
complete &= !selectedTaxonNodes.isEmpty();
|
229
|
|
230
|
return complete;
|
231
|
}
|
232
|
|
233
|
private class SelectionChangedListener implements ISelectionChangedListener {
|
234
|
|
235
|
public void selectionChanged(SelectionChangedEvent event) {
|
236
|
StructuredSelection selection = (StructuredSelection) treeViewer.getSelection();
|
237
|
|
238
|
List<Element> selectedElements = selection.toList();
|
239
|
if(selectedElements.size() > 0){
|
240
|
getConfigurator().setSelectedTaxonNodeElements(selectedElements);
|
241
|
setPageComplete(true);
|
242
|
}
|
243
|
}
|
244
|
|
245
|
}
|
246
|
|
247
|
private class ContentProvider implements ITreeContentProvider{
|
248
|
|
249
|
public Object[] getChildren(Object parentElement) {
|
250
|
if(parentElement instanceof List){
|
251
|
return ((List)parentElement).toArray();
|
252
|
}
|
253
|
else if(parentElement instanceof Element){
|
254
|
Element element = (Element) parentElement;
|
255
|
|
256
|
IXMLEntityFactory factory = getConfigurator().getFactory();
|
257
|
|
258
|
return factory != null ? factory.getChildNodes(element).toArray() : new Object[]{};
|
259
|
|
260
|
}
|
261
|
|
262
|
return new Object[]{};
|
263
|
}
|
264
|
|
265
|
public Object getParent(Object element) {
|
266
|
return null;
|
267
|
}
|
268
|
|
269
|
public boolean hasChildren(Object element) {
|
270
|
return getChildren(element).length > 0;
|
271
|
}
|
272
|
|
273
|
public Object[] getElements(Object inputElement) {
|
274
|
return getChildren(inputElement);
|
275
|
}
|
276
|
|
277
|
public void dispose() {
|
278
|
|
279
|
}
|
280
|
|
281
|
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
|
282
|
|
283
|
}
|
284
|
}
|
285
|
|
286
|
private class LabelProvider extends ColumnLabelProvider
|
287
|
implements ILabelProvider{
|
288
|
|
289
|
@Override
|
290
|
public String getText(Object element) {
|
291
|
if(element instanceof Element){
|
292
|
Element xmlElement = (Element) element;
|
293
|
EntityType entityType = XMLHelper.getEntityType(xmlElement);
|
294
|
if(EntityType.TAXON_NODE.equals(entityType)){
|
295
|
xmlElement = getConfigurator().getFactory().getTaxonForTaxonNode(xmlElement);
|
296
|
}
|
297
|
return XMLHelper.getTitleCache(xmlElement);
|
298
|
}
|
299
|
return "no title cache";
|
300
|
}
|
301
|
}
|
302
|
}
|