Project

General

Profile

Download (16.7 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * Copyright (C) 2007 EDIT
3
 * European Distributed Institute of Taxonomy
4
 * http://www.e-taxonomy.eu
5
 *
6
 * The content 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.awt.Checkbox;
13
import java.text.SimpleDateFormat;
14
import java.util.Calendar;
15
import java.util.Collections;
16
import java.util.Comparator;
17
import java.util.List;
18
import java.util.UUID;
19

    
20
import org.apache.commons.lang.StringUtils;
21
import org.eclipse.jface.wizard.WizardPage;
22
import org.eclipse.swt.SWT;
23
import org.eclipse.swt.events.SelectionAdapter;
24
import org.eclipse.swt.events.SelectionEvent;
25
import org.eclipse.swt.events.SelectionListener;
26
import org.eclipse.swt.layout.GridData;
27
import org.eclipse.swt.layout.GridLayout;
28
import org.eclipse.swt.widgets.Button;
29
import org.eclipse.swt.widgets.Combo;
30
import org.eclipse.swt.widgets.Composite;
31
import org.eclipse.swt.widgets.Control;
32
import org.eclipse.swt.widgets.DirectoryDialog;
33
import org.eclipse.swt.widgets.Event;
34
import org.eclipse.swt.widgets.Label;
35
import org.eclipse.swt.widgets.Listener;
36
import org.eclipse.swt.widgets.Text;
37

    
38
import eu.etaxonomy.cdm.api.application.CdmApplicationState;
39
import eu.etaxonomy.cdm.api.service.IClassificationService;
40
import eu.etaxonomy.cdm.io.cdmLight.CdmLightExportConfigurator;
41
import eu.etaxonomy.cdm.io.common.ExportConfiguratorBase;
42
import eu.etaxonomy.cdm.io.csv.caryophyllales.out.CsvNameExportConfigurator;
43
import eu.etaxonomy.cdm.io.csv.redlist.demo.CsvDemoExportConfigurator;
44
import eu.etaxonomy.cdm.io.csv.redlist.out.CsvTaxExportConfiguratorRedlist;
45
import eu.etaxonomy.cdm.io.dwca.out.DwcaTaxExportConfigurator;
46
import eu.etaxonomy.cdm.io.jaxb.JaxbExportConfigurator;
47
import eu.etaxonomy.cdm.io.sdd.out.SDDExportConfigurator;
48
import eu.etaxonomy.cdm.io.tcsxml.out.TcsXmlExportConfigurator;
49
import eu.etaxonomy.cdm.model.taxon.Classification;
50
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
51
import eu.etaxonomy.taxeditor.datasource.CdmDataSourceRepository;
52
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
53
import eu.etaxonomy.taxeditor.remoting.source.CdmRemoteSourceException;
54
import eu.etaxonomy.taxeditor.store.CdmStore;
55

    
56
/**
57
 * <p>
58
 * ExportToFileDestinationWizardPage class.
59
 * </p>
60
 *
61
 * @author n.hoffmann
62
 * @created 15.06.2009
63
 * @version 1.0
64
 */
65
public class ExportToFileDestinationWizardPage extends WizardPage implements SelectionListener {
66

    
67
	/** Constant <code>DATE_FORMAT_NOW="yyyyMMddHHmm"</code> */
68
	public static final String DATE_FORMAT_NOW = "yyyyMMddHHmm";
69

    
70
	/** Constant <code>JAXB_EXPORT="JAXB_EXPORT"</code> */
71
	public static final String JAXB_EXPORT = "JAXB_EXPORT";
72

    
73
	/** Constant <code>TCS_EXPORT="TCS_EXPORT"</code> */
74
	public static final String TCS_EXPORT = "TCS_EXPORT";
75

    
76
	/** Constant <code>SDD_EXPORT="SDD_EXPORT"</code> */
77
	public static final String SDD_EXPORT = "SDD_EXPORT";
78

    
79
	public static final String DWCA_EXPORT = "DWCA_EXPORT";
80
	public static final String OUTPUT_MODEL_EXPORT = "CDM_LIGHT(CSV)";
81
	public static final String CSV_EXPORT = "CSV_EXPORT";
82
	public static final String CSV_NAME_EXPORT = "CSV_NAME_EXPORT";
83
	private static final String CSV_PRINT_EXPORT ="CSV_PRINT_EXPORT";
84
	private static final String EXPORT_FOLDER ="EXPORT_FOLDER";
85
	public static final String XML = "xml";
86

    
87
	public static final String CSV = "csv";
88

    
89

    
90

    
91
	public static final String ZIP = "zip";
92

    
93
    private boolean csvExport = false;
94

    
95
    private boolean csvNameExport = false;
96

    
97
	
98

    
99
	private boolean csvPrintExport = false;
100

    
101
	private boolean outputModelExport = false;
102
	private boolean dwcaExport = false;
103

    
104
	private DirectoryDialog folderDialog;
105
	private Text text_exportFileName;
106

    
107
	private Text text_folder;
108

    
109
	private final String type;
110

    
111
	private final String extension;
112

    
113
    private Combo classificationSelectionCombo;
114

    
115
    private List<Classification> classifications;
116

    
117
    private Classification selectedClassification;
118
    private Label classificationLabel;
119
    
120
    private ExportConfiguratorBase configurator;
121
    private Button checkUseSelectedtaxonNode;
122

    
123
    private Button checkUseSelectedClassification;
124

    
125
	/**
126
	 * @param pageName
127
	 * @param selection
128
	 */
129
	protected ExportToFileDestinationWizardPage(String pageName, String type,
130
			String title, String description, String extension, ExportConfiguratorBase configurator) {
131
		super(pageName);
132
		this.configurator = configurator;
133
		this.type = type;
134
		switch(type) {
135
		   case CSV_EXPORT :
136
			   csvExport = true;
137
			   break;
138
		   case CSV_NAME_EXPORT:
139
			   csvNameExport = true;
140
			   break;
141
		   case OUTPUT_MODEL_EXPORT:
142
			   outputModelExport = true;
143
			   break;
144
		   case CSV_PRINT_EXPORT:
145
			   csvPrintExport = true;
146
			   break;
147
		   case DWCA_EXPORT:
148
			   dwcaExport = true;
149
			   break;
150
			  
151
		}
152
		this.extension = extension;
153
		this.setTitle(title);
154
		this.setDescription(description);
155
	}
156

    
157
	/**
158
	 * <p>
159
	 * Jaxb
160
	 * </p>
161
	 *
162
	 * @return a
163
	 *         {@link eu.etaxonomy.taxeditor.io.wizard.ExportToFileDestinationWizardPage}
164
	 *         object.
165
	 */
166
	public static ExportToFileDestinationWizardPage Jaxb(JaxbExportConfigurator configurator) {
167
		return new ExportToFileDestinationWizardPage(
168
				JAXB_EXPORT,
169
				"jaxb",
170
				"JAXB Export",
171
				"Exports the content of the currently selected database into the cdm jaxb format.",
172
				XML, configurator);
173
	}
174

    
175
	/**
176
	 * <p>
177
	 * Tcs
178
	 * </p>
179
	 *
180
	 * @return a
181
	 *         {@link eu.etaxonomy.taxeditor.io.wizard.ExportToFileDestinationWizardPage}
182
	 *         object.
183
	 */
184
	public static ExportToFileDestinationWizardPage Tcs(TcsXmlExportConfigurator config) {
185
		return new ExportToFileDestinationWizardPage(
186
				TCS_EXPORT,
187
				"tcs",
188
				"Tcs Export",
189
				"Export the content of the currently selected database into TCS format.",
190
				XML, config);
191
	}
192

    
193
	/**
194
	 * <p>
195
	 * Sdd
196
	 * </p>
197
	 *
198
	 * @return a
199
	 *         {@link eu.etaxonomy.taxeditor.io.wizard.ExportToFileDestinationWizardPage}
200
	 *         object.
201
	 */
202
	public static ExportToFileDestinationWizardPage Sdd(SDDExportConfigurator config) {
203
		return new ExportToFileDestinationWizardPage(
204
				SDD_EXPORT,
205
				"sdd",
206
				"Sdd Export",
207
				"Export the content of the currently selected database into SDD format.",
208
				XML, config);
209
	}
210

    
211
	/**
212
	 * @return
213
	 */
214
	public static ExportToFileDestinationWizardPage Dwca(DwcaTaxExportConfigurator config) {
215
		return new ExportToFileDestinationWizardPage(
216
				DWCA_EXPORT,
217
				DWCA_EXPORT,
218
				"DwC-Archive Export",
219
				"Export the content of the currently selected database into Darwin Core Archive format.",
220
				ZIP, config);
221
	}
222

    
223
	/**
224
     * @return
225
     */
226
    public static ExportToFileDestinationWizardPage OutputModel(CdmLightExportConfigurator config) {
227
        
228
        return new ExportToFileDestinationWizardPage(
229
                OUTPUT_MODEL_EXPORT,
230
                OUTPUT_MODEL_EXPORT,
231
                "CDM Light Export (csv)",
232
                "Export the content of the currently selected database into the CDM light (csv) format.",
233
                CSV,config);
234
    }
235

    
236

    
237
    /**
238
     * @return
239
     */
240
    public static ExportToFileDestinationWizardPage Csv(CsvDemoExportConfigurator config) {
241
        
242
        return new ExportToFileDestinationWizardPage(
243
                CSV_EXPORT,
244
                CSV_EXPORT,
245
                "CSV Export",
246
                "Export the content of the currently selected database into Comma Separated Value format.",
247
                CSV, config);
248
    }
249

    
250
    /**
251
     * @return
252
     */
253
    public static ExportToFileDestinationWizardPage CsvNames(CsvNameExportConfigurator config) {
254
    	
255
        return new ExportToFileDestinationWizardPage(
256
                CSV_NAME_EXPORT,
257
                CSV_NAME_EXPORT,
258
                "CSV Name Export",
259
                "Export the names of the currently selected database into Semicolon Separated Value format.",
260
                CSV, config);
261
    }
262

    
263
    /**
264
     * @return
265
     */
266
    public static ExportToFileDestinationWizardPage CsvPrint(CsvNameExportConfigurator config) {
267
    	
268
        return new ExportToFileDestinationWizardPage(
269
                CSV_PRINT_EXPORT,
270
                CSV_NAME_EXPORT,
271
                "CSV Print Export",
272
                "Export the content of the currently selected database into Semicolon Separated Value format.",
273
                CSV, config);
274
    }
275

    
276
	/*
277
	 * (non-Javadoc)
278
	 *
279
	 * @see
280
	 * org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets
281
	 * .Composite)
282
	 */
283
	/** {@inheritDoc} */
284
	@Override
285
    public void createControl(Composite parent) {
286

    
287
		setPageComplete(false);
288

    
289
		Composite composite = new Composite(parent, SWT.NONE);
290
		GridLayout gridLayout = new GridLayout();
291
		
292
		gridLayout.numColumns = 2;
293
		TaxonNode node = null;
294
		
295
		composite.setLayout(gridLayout);
296
		if(classifications == null){
297
			classifications = CdmStore.getService(IClassificationService.class).list(null, null, null, null, null);
298
			Collections.sort(classifications, new Comparator<Classification>() {
299

    
300
                @Override
301
                public int compare(Classification o1, Classification o2) {
302
                    if (o1.equals(o2)){
303
                        return 0;
304
                    }
305
                    int result = o1.getTitleCache().compareTo(o2.getTitleCache());
306
                    if (result == 0){
307
                        return o1.getUuid().compareTo(o2.getUuid());
308
                    }
309
                    return result;
310
                }
311
            });
312
			if (!configurator.getTaxonNodeFilter().getClassificationFilter().isEmpty()){
313
				selectedClassification = CdmStore.getService(IClassificationService.class).load(configurator.getTaxonNodeFilter().getClassificationFilter().get(0).getUuid());
314
			}else{
315
				
316
				selectedClassification = classifications.iterator().next();
317
			}
318
		}
319

    
320

    
321
		if(csvExport || csvPrintExport || outputModelExport|| csvNameExport || dwcaExport){
322
//		    Label comboBoxLabel = new Label(composite, SWT.NONE);
323
//		    comboBoxLabel.setText("Classification");
324
			
325
			if ((outputModelExport || dwcaExport)&& !configurator.getTaxonNodeFilter().getSubtreeFilter().isEmpty()){
326
				Composite selectNodeOrClassification = new Composite(composite, SWT.NONE);
327
				GridLayout grid = new GridLayout();
328
				grid.numColumns = 1;
329
				selectNodeOrClassification.setLayout(grid);
330
				checkUseSelectedtaxonNode= new Button(selectNodeOrClassification,  SWT.RADIO);
331
				String taxonStr = "";
332
				node = CdmApplicationState.getCurrentAppConfig().getTaxonNodeService().load(configurator.getTaxonNodeFilter().getSubtreeFilter().get(0).getUuid());
333
				if (node.hasTaxon()){
334
					taxonStr = node.getTaxon().getName().getTitleCache();
335
				}
336
				checkUseSelectedtaxonNode.setText("Export selected subtree ("+  taxonStr+")");
337
				checkUseSelectedtaxonNode.addListener(SWT.Selection, new Listener() {
338
					@Override
339
					public void handleEvent(Event e) {
340
						Button b = (Button) e.widget;
341
		                GridData data = (GridData)  classificationSelectionCombo.getLayoutData();
342
		                data.exclude = b.getSelection();
343
		                classificationSelectionCombo.setVisible(!data.exclude);
344
		                classificationLabel.setVisible(!data.exclude);
345
		            	}
346
		        	});
347
				  checkUseSelectedtaxonNode.setSelection(true);
348
				  checkUseSelectedClassification= new Button(selectNodeOrClassification,  SWT.RADIO);
349
				  checkUseSelectedClassification.setText("Export complete classification");
350
				  checkUseSelectedClassification.addListener(SWT.Selection, new Listener() {
351
		               @Override
352
		               public void handleEvent(Event e) {
353
		            	   Button b = (Button) e.widget;
354
		                   GridData data = (GridData)  classificationSelectionCombo.getLayoutData();
355
		                   data.exclude = b.getSelection();
356
		                   classificationSelectionCombo.setVisible(data.exclude);
357
		                   classificationLabel.setVisible(data.exclude);
358
		                  }
359
				  	});
360
			}
361
			if (node!= null){
362
				selectedClassification = node.getClassification();
363
			}
364
			createClassificationSelectionCombo(composite);
365
			
366
			classificationSelectionCombo.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
367
			                true, false, 2, 1));
368
			if (checkUseSelectedtaxonNode != null){
369
				
370
				if (checkUseSelectedtaxonNode.getSelection()){
371
					 classificationSelectionCombo.setVisible(false);
372
	                 classificationLabel.setVisible(false);
373
				}
374
			}
375
			
376
			
377
		}
378
		
379

    
380
		Label folderLabel = new Label(composite, SWT.NONE);
381
		folderLabel.setText("Select Folder for exported files");
382
		folderLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
383
        true, false, 2, 1));
384
		folderDialog = new DirectoryDialog(parent.getShell());
385
		folderDialog.setFilterPath(PreferencesUtil.getPreferenceStore().getString(EXPORT_FOLDER));
386

    
387
		text_folder = new Text(composite, SWT.BORDER);
388
		text_folder.setEditable(false);
389
		text_folder.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
390
				false));
391
		if (PreferencesUtil.getPreferenceStore().getString(EXPORT_FOLDER) != null){
392
			text_folder.setText(PreferencesUtil.getPreferenceStore().getString(EXPORT_FOLDER));
393
			setPageComplete(true);
394
		}
395

    
396
		Button button = new Button(composite, SWT.PUSH);
397
		button.setText("Browse...");
398

    
399
		button.addSelectionListener(new SelectionAdapter() {
400
			/*
401
			 * (non-Javadoc)
402
			 *
403
			 * @see
404
			 * org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse
405
			 * .swt.events.SelectionEvent)
406
			 */
407
			@Override
408
			public void widgetSelected(SelectionEvent e) {
409
				super.widgetSelected(e);
410

    
411
				String path = folderDialog.open();
412
				if (path != null) { // a folder was selected
413
					text_folder.setText(path);
414
					PreferencesUtil.getPreferenceStore().setValue(EXPORT_FOLDER, path);
415
					setPageComplete(true);
416
				}
417
			}
418
		});
419
		if (!outputModelExport){
420
			
421
			 Label fileLabel = new Label(composite, SWT.NONE);
422
		        fileLabel.setText("File");
423
	    		text_exportFileName = new Text(composite, SWT.BORDER);
424
	    		text_exportFileName.setText(generateFilename());
425
	    		text_exportFileName.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
426
	    				true, false));
427
		}
428
		// make the composite the wizard pages control
429
		setControl(composite);
430
	}
431

    
432
	protected String generateFilename() {
433
		StringBuffer buffer = new StringBuffer();
434

    
435
		Calendar cal = Calendar.getInstance();
436
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
437
		buffer.append(sdf.format(cal.getTime()));
438

    
439
		buffer.append("-");
440

    
441
		buffer.append(type + "_export-");
442
		try {
443
			buffer.append(CdmDataSourceRepository.getCurrentCdmSource());
444
		} catch (CdmRemoteSourceException e) {
445
			buffer.append("Unknown");
446
		}
447

    
448
		buffer.append(".");
449
		buffer.append(extension);
450

    
451
		return buffer.toString();
452
	}
453

    
454
	/**
455
	 * <p>
456
	 * getExportFileName
457
	 * </p>
458
	 *
459
	 * @return the exportFileName
460
	 */
461
	public String getExportFileName() {
462
		return text_exportFileName.getText();
463
	}
464

    
465
	/**
466
	 * <p>
467
	 * getFolderText
468
	 * </p>
469
	 *
470
	 * @return the folderText
471
	 */
472
	public String getFolderText() {
473
		return text_folder.getText();
474
	}
475
	
476
	/**
477
	 * <p>
478
	 * getFolderComposite
479
	 * </p>
480
	 *
481
	 * @return the folderText
482
	 */
483
	public Text getFolderComposite() {
484
		return text_folder;
485
	}
486

    
487
	private void createClassificationSelectionCombo(Composite parent){
488
//		classifications = CdmStore.getTaxonTreeService().list(null, null, null, null, null);
489

    
490
		Composite classificationSelection = new Composite(parent, SWT.NULL);
491
		classificationSelection.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 2, 1));
492

    
493
		GridLayout layout = new GridLayout();
494
		classificationSelection.setLayout(layout);
495

    
496
		classificationLabel = new Label(classificationSelection, SWT.NULL);
497
		// TODO not working is not really true but leave it here to remind everyone that this is under construction
498
		classificationLabel.setText("Export complete classification");
499
		
500
		classificationSelectionCombo = new Combo(classificationSelection, SWT.BORDER | SWT.READ_ONLY);
501
		classificationSelectionCombo.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, true, 2,1));
502

    
503
		for(Classification tree : classifications){
504
			classificationSelectionCombo.add(tree.getName().getText(), classifications.indexOf(tree));
505

    
506
		}
507

    
508
		classificationSelectionCombo.select(classifications.indexOf(selectedClassification));
509

    
510
		// TODO remember last selection
511
		classificationSelectionCombo.addSelectionListener(this);
512

    
513

    
514

    
515
		//return classificationSelection;
516
	}
517

    
518
	public Combo getCombo(){
519
	    return classificationSelectionCombo;
520
	}
521

    
522
	@Override
523
	public void widgetSelected(SelectionEvent e) {
524
		selectedClassification = classifications.get(classificationSelectionCombo.getSelectionIndex());
525

    
526
	}
527

    
528
	@Override
529
	public void widgetDefaultSelected(SelectionEvent e) {
530
		//not needed here
531

    
532
	}
533

    
534
	public UUID getSelectedClassificationUUID() {
535

    
536
		return selectedClassification.getUuid();
537
	}
538

    
539
	public boolean getCheckUseSelectedTaxonNode() {
540
		if (checkUseSelectedtaxonNode== null){
541
			return false;
542
		}
543
		return checkUseSelectedtaxonNode.getSelection();
544
	}
545

    
546
	
547
	@Override
548
	public boolean canFlipToNextPage() {
549
        return  getFolderText() != null;
550
    }
551
	
552

    
553
   
554

    
555
}
(17-17/30)