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
|
|
10
|
package eu.etaxonomy.taxeditor.io.wizard;
|
11
|
|
12
|
import java.text.SimpleDateFormat;
|
13
|
import java.util.Calendar;
|
14
|
import java.util.Collections;
|
15
|
import java.util.Comparator;
|
16
|
import java.util.List;
|
17
|
import java.util.UUID;
|
18
|
|
19
|
import org.eclipse.jface.wizard.WizardPage;
|
20
|
import org.eclipse.swt.SWT;
|
21
|
import org.eclipse.swt.events.SelectionAdapter;
|
22
|
import org.eclipse.swt.events.SelectionEvent;
|
23
|
import org.eclipse.swt.events.SelectionListener;
|
24
|
import org.eclipse.swt.layout.GridData;
|
25
|
import org.eclipse.swt.layout.GridLayout;
|
26
|
import org.eclipse.swt.widgets.Button;
|
27
|
import org.eclipse.swt.widgets.Combo;
|
28
|
import org.eclipse.swt.widgets.Composite;
|
29
|
import org.eclipse.swt.widgets.Control;
|
30
|
import org.eclipse.swt.widgets.DirectoryDialog;
|
31
|
import org.eclipse.swt.widgets.Label;
|
32
|
import org.eclipse.swt.widgets.Listener;
|
33
|
import org.eclipse.swt.widgets.Text;
|
34
|
|
35
|
import eu.etaxonomy.cdm.api.service.IClassificationService;
|
36
|
import eu.etaxonomy.cdm.model.taxon.Classification;
|
37
|
import eu.etaxonomy.taxeditor.datasource.CdmDataSourceRepository;
|
38
|
import eu.etaxonomy.taxeditor.remoting.source.CdmRemoteSourceException;
|
39
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
40
|
|
41
|
/**
|
42
|
* <p>
|
43
|
* ExportToFileDestinationWizardPage class.
|
44
|
* </p>
|
45
|
*
|
46
|
* @author n.hoffmann
|
47
|
* @created 15.06.2009
|
48
|
* @version 1.0
|
49
|
*/
|
50
|
public class ExportToFileDestinationWizardPage extends WizardPage implements SelectionListener {
|
51
|
|
52
|
/** Constant <code>DATE_FORMAT_NOW="yyyyMMddHHmm"</code> */
|
53
|
public static final String DATE_FORMAT_NOW = "yyyyMMddHHmm";
|
54
|
|
55
|
/** Constant <code>JAXB_EXPORT="JAXB_EXPORT"</code> */
|
56
|
public static final String JAXB_EXPORT = "JAXB_EXPORT";
|
57
|
|
58
|
/** Constant <code>TCS_EXPORT="TCS_EXPORT"</code> */
|
59
|
public static final String TCS_EXPORT = "TCS_EXPORT";
|
60
|
|
61
|
/** Constant <code>SDD_EXPORT="SDD_EXPORT"</code> */
|
62
|
public static final String SDD_EXPORT = "SDD_EXPORT";
|
63
|
|
64
|
public static final String DWCA_EXPORT = "DWCA_EXPORT";
|
65
|
public static final String OUTPUT_MODEL_EXPORT = "CDM_LIGHT(CSV)";
|
66
|
public static final String CSV_EXPORT = "CSV_EXPORT";
|
67
|
public static final String CSV_NAME_EXPORT = "CSV_NAME_EXPORT";
|
68
|
private static final String CSV_PRINT_EXPORT ="CSV_PRINT_EXPORT";
|
69
|
|
70
|
public static final String XML = "xml";
|
71
|
|
72
|
public static final String CSV = "csv";
|
73
|
|
74
|
|
75
|
|
76
|
public static final String ZIP = "zip";
|
77
|
|
78
|
private boolean csvExport = false;
|
79
|
|
80
|
private boolean csvNameExport = false;
|
81
|
|
82
|
|
83
|
|
84
|
private boolean csvPrintExport = false;
|
85
|
|
86
|
private boolean outputModelExport = false;
|
87
|
|
88
|
private DirectoryDialog folderDialog;
|
89
|
private Text text_exportFileName;
|
90
|
|
91
|
private Text text_folder;
|
92
|
|
93
|
private final String type;
|
94
|
|
95
|
private final String extension;
|
96
|
|
97
|
private Combo classificationSelectionCombo;
|
98
|
|
99
|
private List<Classification> classifications;
|
100
|
|
101
|
private Classification selectedClassification;
|
102
|
private String lastDirectory;
|
103
|
|
104
|
/**
|
105
|
* @param pageName
|
106
|
* @param selection
|
107
|
*/
|
108
|
protected ExportToFileDestinationWizardPage(String pageName, String type,
|
109
|
String title, String description, String extension) {
|
110
|
super(pageName);
|
111
|
|
112
|
this.type = type;
|
113
|
switch(type) {
|
114
|
case CSV_EXPORT :
|
115
|
csvExport = true;
|
116
|
break;
|
117
|
case CSV_NAME_EXPORT:
|
118
|
csvNameExport = true;
|
119
|
break;
|
120
|
case OUTPUT_MODEL_EXPORT:
|
121
|
outputModelExport = true;
|
122
|
break;
|
123
|
case CSV_PRINT_EXPORT:
|
124
|
csvPrintExport = true;
|
125
|
break;
|
126
|
|
127
|
}
|
128
|
this.extension = extension;
|
129
|
this.setTitle(title);
|
130
|
this.setDescription(description);
|
131
|
}
|
132
|
|
133
|
/**
|
134
|
* <p>
|
135
|
* Jaxb
|
136
|
* </p>
|
137
|
*
|
138
|
* @return a
|
139
|
* {@link eu.etaxonomy.taxeditor.io.wizard.ExportToFileDestinationWizardPage}
|
140
|
* object.
|
141
|
*/
|
142
|
public static ExportToFileDestinationWizardPage Jaxb() {
|
143
|
return new ExportToFileDestinationWizardPage(
|
144
|
JAXB_EXPORT,
|
145
|
"jaxb",
|
146
|
"JAXB Export",
|
147
|
"Exports the contents of the currently selected database into the cdm jaxb format.",
|
148
|
XML);
|
149
|
}
|
150
|
|
151
|
/**
|
152
|
* <p>
|
153
|
* Tcs
|
154
|
* </p>
|
155
|
*
|
156
|
* @return a
|
157
|
* {@link eu.etaxonomy.taxeditor.io.wizard.ExportToFileDestinationWizardPage}
|
158
|
* object.
|
159
|
*/
|
160
|
public static ExportToFileDestinationWizardPage Tcs() {
|
161
|
return new ExportToFileDestinationWizardPage(
|
162
|
TCS_EXPORT,
|
163
|
"tcs",
|
164
|
"Tcs Export",
|
165
|
"Export the contents of the currently selected database into TCS format.",
|
166
|
XML);
|
167
|
}
|
168
|
|
169
|
/**
|
170
|
* <p>
|
171
|
* Sdd
|
172
|
* </p>
|
173
|
*
|
174
|
* @return a
|
175
|
* {@link eu.etaxonomy.taxeditor.io.wizard.ExportToFileDestinationWizardPage}
|
176
|
* object.
|
177
|
*/
|
178
|
public static ExportToFileDestinationWizardPage Sdd() {
|
179
|
return new ExportToFileDestinationWizardPage(
|
180
|
SDD_EXPORT,
|
181
|
"sdd",
|
182
|
"Sdd Export",
|
183
|
"Export the contents of the currently selected database into SDD format.",
|
184
|
XML);
|
185
|
}
|
186
|
|
187
|
/**
|
188
|
* @return
|
189
|
*/
|
190
|
public static ExportToFileDestinationWizardPage Dwca() {
|
191
|
return new ExportToFileDestinationWizardPage(
|
192
|
DWCA_EXPORT,
|
193
|
DWCA_EXPORT,
|
194
|
"DwC-Archive Export",
|
195
|
"Export the contents of the currently selected database into Darwin Core Archive format.",
|
196
|
ZIP);
|
197
|
}
|
198
|
|
199
|
/**
|
200
|
* @return
|
201
|
*/
|
202
|
public static ExportToFileDestinationWizardPage OutputModel() {
|
203
|
|
204
|
return new ExportToFileDestinationWizardPage(
|
205
|
OUTPUT_MODEL_EXPORT,
|
206
|
OUTPUT_MODEL_EXPORT,
|
207
|
"Output Model Export",
|
208
|
"Export the contents of the currently selected database into the output model format.",
|
209
|
CSV);
|
210
|
}
|
211
|
|
212
|
|
213
|
/**
|
214
|
* @return
|
215
|
*/
|
216
|
public static ExportToFileDestinationWizardPage Csv() {
|
217
|
|
218
|
return new ExportToFileDestinationWizardPage(
|
219
|
CSV_EXPORT,
|
220
|
CSV_EXPORT,
|
221
|
"CSV Export",
|
222
|
"Export the contents of the currently selected database into Comma Separated Value format.",
|
223
|
CSV);
|
224
|
}
|
225
|
|
226
|
/**
|
227
|
* @return
|
228
|
*/
|
229
|
public static ExportToFileDestinationWizardPage CsvNames() {
|
230
|
|
231
|
return new ExportToFileDestinationWizardPage(
|
232
|
CSV_NAME_EXPORT,
|
233
|
CSV_NAME_EXPORT,
|
234
|
"CSV Name Export",
|
235
|
"Export the names of the currently selected database into Semicolon Separated Value format.",
|
236
|
CSV);
|
237
|
}
|
238
|
|
239
|
/**
|
240
|
* @return
|
241
|
*/
|
242
|
public static ExportToFileDestinationWizardPage CsvPrint() {
|
243
|
|
244
|
return new ExportToFileDestinationWizardPage(
|
245
|
CSV_PRINT_EXPORT,
|
246
|
CSV_NAME_EXPORT,
|
247
|
"CSV Print Export",
|
248
|
"Export the content of the currently selected database into Semicolon Separated Value format.",
|
249
|
CSV);
|
250
|
}
|
251
|
|
252
|
/*
|
253
|
* (non-Javadoc)
|
254
|
*
|
255
|
* @see
|
256
|
* org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets
|
257
|
* .Composite)
|
258
|
*/
|
259
|
/** {@inheritDoc} */
|
260
|
@Override
|
261
|
public void createControl(Composite parent) {
|
262
|
|
263
|
setPageComplete(false);
|
264
|
|
265
|
Composite composite = new Composite(parent, SWT.NONE);
|
266
|
GridLayout gridLayout = new GridLayout();
|
267
|
|
268
|
gridLayout.numColumns = 2;
|
269
|
|
270
|
|
271
|
composite.setLayout(gridLayout);
|
272
|
if(classifications == null){
|
273
|
classifications = CdmStore.getService(IClassificationService.class).list(null, null, null, null, null);
|
274
|
Collections.sort(classifications, new Comparator<Classification>() {
|
275
|
|
276
|
@Override
|
277
|
public int compare(Classification o1, Classification o2) {
|
278
|
if (o1.equals(o2)){
|
279
|
return 0;
|
280
|
}
|
281
|
int result = o1.getTitleCache().compareTo(o2.getTitleCache());
|
282
|
if (result == 0){
|
283
|
return o1.getUuid().compareTo(o2.getUuid());
|
284
|
}
|
285
|
return result;
|
286
|
}
|
287
|
});
|
288
|
|
289
|
selectedClassification = classifications.iterator().next();
|
290
|
}
|
291
|
|
292
|
|
293
|
if(csvExport || csvPrintExport || outputModelExport|| csvNameExport){
|
294
|
// Label comboBoxLabel = new Label(composite, SWT.NONE);
|
295
|
// comboBoxLabel.setText("Classification");
|
296
|
|
297
|
createClassificationSelectionCombo(composite);
|
298
|
classificationSelectionCombo.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
|
299
|
true, false, 2, 1));
|
300
|
}
|
301
|
if (!outputModelExport){
|
302
|
Label fileLabel = new Label(composite, SWT.NONE);
|
303
|
fileLabel.setText("File");
|
304
|
text_exportFileName = new Text(composite, SWT.BORDER);
|
305
|
text_exportFileName.setText(generateFilename());
|
306
|
text_exportFileName.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
|
307
|
true, false));
|
308
|
}
|
309
|
|
310
|
Label folderLabel = new Label(composite, SWT.NONE);
|
311
|
folderLabel.setText("Select Folder for exported files");
|
312
|
folderLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
|
313
|
true, false, 2, 1));
|
314
|
folderDialog = new DirectoryDialog(parent.getShell());
|
315
|
|
316
|
text_folder = new Text(composite, SWT.BORDER);
|
317
|
text_folder.setEditable(false);
|
318
|
text_folder.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
|
319
|
false));
|
320
|
|
321
|
Button button = new Button(composite, SWT.PUSH);
|
322
|
button.setText("Browse...");
|
323
|
|
324
|
button.addSelectionListener(new SelectionAdapter() {
|
325
|
/*
|
326
|
* (non-Javadoc)
|
327
|
*
|
328
|
* @see
|
329
|
* org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse
|
330
|
* .swt.events.SelectionEvent)
|
331
|
*/
|
332
|
@Override
|
333
|
public void widgetSelected(SelectionEvent e) {
|
334
|
super.widgetSelected(e);
|
335
|
String path = folderDialog.open();
|
336
|
if (path != null) { // a folder was selected
|
337
|
text_folder.setText(path);
|
338
|
setPageComplete(true);
|
339
|
}
|
340
|
}
|
341
|
});
|
342
|
|
343
|
// make the composite the wizard pages control
|
344
|
setControl(composite);
|
345
|
}
|
346
|
|
347
|
protected String generateFilename() {
|
348
|
StringBuffer buffer = new StringBuffer();
|
349
|
|
350
|
Calendar cal = Calendar.getInstance();
|
351
|
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
|
352
|
buffer.append(sdf.format(cal.getTime()));
|
353
|
|
354
|
buffer.append("-");
|
355
|
|
356
|
buffer.append(type + "_export-");
|
357
|
try {
|
358
|
buffer.append(CdmDataSourceRepository.getCurrentCdmSource());
|
359
|
} catch (CdmRemoteSourceException e) {
|
360
|
buffer.append("Unknown");
|
361
|
}
|
362
|
|
363
|
buffer.append(".");
|
364
|
buffer.append(extension);
|
365
|
|
366
|
return buffer.toString();
|
367
|
}
|
368
|
|
369
|
/**
|
370
|
* <p>
|
371
|
* getExportFileName
|
372
|
* </p>
|
373
|
*
|
374
|
* @return the exportFileName
|
375
|
*/
|
376
|
public String getExportFileName() {
|
377
|
return text_exportFileName.getText();
|
378
|
}
|
379
|
|
380
|
/**
|
381
|
* <p>
|
382
|
* getFolderText
|
383
|
* </p>
|
384
|
*
|
385
|
* @return the folderText
|
386
|
*/
|
387
|
public String getFolderText() {
|
388
|
return text_folder.getText();
|
389
|
}
|
390
|
|
391
|
/**
|
392
|
* <p>
|
393
|
* getFolderComposite
|
394
|
* </p>
|
395
|
*
|
396
|
* @return the folderText
|
397
|
*/
|
398
|
public Text getFolderComposite() {
|
399
|
return text_folder;
|
400
|
}
|
401
|
|
402
|
private void createClassificationSelectionCombo(Composite parent){
|
403
|
// classifications = CdmStore.getTaxonTreeService().list(null, null, null, null, null);
|
404
|
|
405
|
// Composite classificationSelection = new Composite(parent, SWT.NULL);
|
406
|
// classificationSelection.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 2, 1));
|
407
|
|
408
|
// GridLayout layout = new GridLayout();
|
409
|
// classificationSelection.setLayout(layout);
|
410
|
|
411
|
Label label = new Label(parent, SWT.NULL);
|
412
|
// TODO not working is not really true but leave it here to remind everyone that this is under construction
|
413
|
label.setText("Select Classification");
|
414
|
|
415
|
classificationSelectionCombo = new Combo(parent, SWT.BORDER | SWT.READ_ONLY);
|
416
|
classificationSelectionCombo.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, true, 2,1));
|
417
|
|
418
|
for(Classification tree : classifications){
|
419
|
classificationSelectionCombo.add(tree.getName().getText(), classifications.indexOf(tree));
|
420
|
|
421
|
}
|
422
|
|
423
|
classificationSelectionCombo.select(classifications.indexOf(selectedClassification));
|
424
|
|
425
|
// TODO remember last selection
|
426
|
classificationSelectionCombo.addSelectionListener(this);
|
427
|
|
428
|
|
429
|
|
430
|
//return classificationSelection;
|
431
|
}
|
432
|
|
433
|
public Combo getCombo(){
|
434
|
return classificationSelectionCombo;
|
435
|
}
|
436
|
|
437
|
@Override
|
438
|
public void widgetSelected(SelectionEvent e) {
|
439
|
selectedClassification = classifications.get(classificationSelectionCombo.getSelectionIndex());
|
440
|
|
441
|
}
|
442
|
|
443
|
@Override
|
444
|
public void widgetDefaultSelected(SelectionEvent e) {
|
445
|
//not needed here
|
446
|
|
447
|
}
|
448
|
|
449
|
public UUID getSelectedClassificationUUID() {
|
450
|
|
451
|
return selectedClassification.getUuid();
|
452
|
}
|
453
|
|
454
|
|
455
|
|
456
|
private void loadLastState() {
|
457
|
if(lastDirectory!=null){
|
458
|
Text text = this.getFolderComposite();
|
459
|
Listener[] listeners = text.getListeners(SWT.Modify);
|
460
|
for (int i = 0; i < listeners.length; i++) {
|
461
|
text.removeListener(SWT.Modify, listeners[i]);
|
462
|
}
|
463
|
text.setText(lastDirectory);
|
464
|
for (int i = 0; i < listeners.length; i++) {
|
465
|
text.addListener(SWT.Modify, listeners[i]);
|
466
|
}
|
467
|
|
468
|
}
|
469
|
}
|
470
|
|
471
|
public void saveLastState() {
|
472
|
lastDirectory = this.getFolderText();
|
473
|
}
|
474
|
|
475
|
}
|