Project

General

Profile

Download (16.3 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
		
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
				selectedClassification = classifications.iterator().next();
316
			}
317
		}
318

    
319

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

    
369
		Label folderLabel = new Label(composite, SWT.NONE);
370
		folderLabel.setText("Select Folder for exported files");
371
		folderLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
372
        true, false, 2, 1));
373
		folderDialog = new DirectoryDialog(parent.getShell());
374
		folderDialog.setFilterPath(PreferencesUtil.getPreferenceStore().getString(EXPORT_FOLDER));
375

    
376
		text_folder = new Text(composite, SWT.BORDER);
377
		text_folder.setEditable(false);
378
		text_folder.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
379
				false));
380
		if (PreferencesUtil.getPreferenceStore().getString(EXPORT_FOLDER) != null){
381
			text_folder.setText(PreferencesUtil.getPreferenceStore().getString(EXPORT_FOLDER));
382
			setPageComplete(true);
383
		}
384

    
385
		Button button = new Button(composite, SWT.PUSH);
386
		button.setText("Browse...");
387

    
388
		button.addSelectionListener(new SelectionAdapter() {
389
			/*
390
			 * (non-Javadoc)
391
			 *
392
			 * @see
393
			 * org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse
394
			 * .swt.events.SelectionEvent)
395
			 */
396
			@Override
397
			public void widgetSelected(SelectionEvent e) {
398
				super.widgetSelected(e);
399

    
400
				String path = folderDialog.open();
401
				if (path != null) { // a folder was selected
402
					text_folder.setText(path);
403
					PreferencesUtil.getPreferenceStore().setValue(EXPORT_FOLDER, path);
404
					setPageComplete(true);
405
				}
406
			}
407
		});
408
		if (!outputModelExport){
409
			
410
			 Label fileLabel = new Label(composite, SWT.NONE);
411
		        fileLabel.setText("File");
412
	    		text_exportFileName = new Text(composite, SWT.BORDER);
413
	    		text_exportFileName.setText(generateFilename());
414
	    		text_exportFileName.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
415
	    				true, false));
416
		}
417
		// make the composite the wizard pages control
418
		setControl(composite);
419
	}
420

    
421
	protected String generateFilename() {
422
		StringBuffer buffer = new StringBuffer();
423

    
424
		Calendar cal = Calendar.getInstance();
425
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
426
		buffer.append(sdf.format(cal.getTime()));
427

    
428
		buffer.append("-");
429

    
430
		buffer.append(type + "_export-");
431
		try {
432
			buffer.append(CdmDataSourceRepository.getCurrentCdmSource());
433
		} catch (CdmRemoteSourceException e) {
434
			buffer.append("Unknown");
435
		}
436

    
437
		buffer.append(".");
438
		buffer.append(extension);
439

    
440
		return buffer.toString();
441
	}
442

    
443
	/**
444
	 * <p>
445
	 * getExportFileName
446
	 * </p>
447
	 *
448
	 * @return the exportFileName
449
	 */
450
	public String getExportFileName() {
451
		return text_exportFileName.getText();
452
	}
453

    
454
	/**
455
	 * <p>
456
	 * getFolderText
457
	 * </p>
458
	 *
459
	 * @return the folderText
460
	 */
461
	public String getFolderText() {
462
		return text_folder.getText();
463
	}
464
	
465
	/**
466
	 * <p>
467
	 * getFolderComposite
468
	 * </p>
469
	 *
470
	 * @return the folderText
471
	 */
472
	public Text getFolderComposite() {
473
		return text_folder;
474
	}
475

    
476
	private void createClassificationSelectionCombo(Composite parent){
477
//		classifications = CdmStore.getTaxonTreeService().list(null, null, null, null, null);
478

    
479
		Composite classificationSelection = new Composite(parent, SWT.NULL);
480
		classificationSelection.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 2, 1));
481

    
482
		GridLayout layout = new GridLayout();
483
		classificationSelection.setLayout(layout);
484

    
485
		classificationLabel = new Label(classificationSelection, SWT.NULL);
486
		// TODO not working is not really true but leave it here to remind everyone that this is under construction
487
		classificationLabel.setText("Export complete classification");
488
		
489
		classificationSelectionCombo = new Combo(classificationSelection, SWT.BORDER | SWT.READ_ONLY);
490
		classificationSelectionCombo.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, true, 2,1));
491

    
492
		for(Classification tree : classifications){
493
			classificationSelectionCombo.add(tree.getName().getText(), classifications.indexOf(tree));
494

    
495
		}
496

    
497
		classificationSelectionCombo.select(classifications.indexOf(selectedClassification));
498

    
499
		// TODO remember last selection
500
		classificationSelectionCombo.addSelectionListener(this);
501

    
502

    
503

    
504
		//return classificationSelection;
505
	}
506

    
507
	public Combo getCombo(){
508
	    return classificationSelectionCombo;
509
	}
510

    
511
	@Override
512
	public void widgetSelected(SelectionEvent e) {
513
		selectedClassification = classifications.get(classificationSelectionCombo.getSelectionIndex());
514

    
515
	}
516

    
517
	@Override
518
	public void widgetDefaultSelected(SelectionEvent e) {
519
		//not needed here
520

    
521
	}
522

    
523
	public UUID getSelectedClassificationUUID() {
524

    
525
		return selectedClassification.getUuid();
526
	}
527

    
528
	public boolean getCheckUseSelectedTaxonNode() {
529
		if (checkUseSelectedtaxonNode== null){
530
			return false;
531
		}
532
		return checkUseSelectedtaxonNode.getSelection();
533
	}
534

    
535
	
536
	
537
	
538

    
539
   
540

    
541
}
(17-17/30)