Project

General

Profile

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

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

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

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

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

    
221
	/**
222
     * @return
223
     */
224
    public static ExportToFileDestinationWizardPage OutputModel(CdmLightExportConfigurator config) {
225
        
226
        return new ExportToFileDestinationWizardPage(
227
                OUTPUT_MODEL_EXPORT,
228
                OUTPUT_MODEL_EXPORT,
229
                "Output Model Export",
230
                "Export the contents of the currently selected database into the output model format.",
231
                CSV,config);
232
    }
233

    
234

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

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

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

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

    
285
		setPageComplete(false);
286

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

    
298
                @Override
299
                public int compare(Classification o1, Classification o2) {
300
                    if (o1.equals(o2)){
301
                        return 0;
302
                    }
303
                    int result = o1.getTitleCache().compareTo(o2.getTitleCache());
304
                    if (result == 0){
305
                        return o1.getUuid().compareTo(o2.getUuid());
306
                    }
307
                    return result;
308
                }
309
            });
310

    
311
			selectedClassification = classifications.iterator().next();
312
		}
313

    
314

    
315
		if(csvExport || csvPrintExport || outputModelExport|| csvNameExport || dwcaExport){
316
//		    Label comboBoxLabel = new Label(composite, SWT.NONE);
317
//		    comboBoxLabel.setText("Classification");
318
			
319
			if ((outputModelExport || dwcaExport)&& !configurator.getTaxonNodeFilter().getSubtreeFilter().isEmpty()){
320
				  checkUseSelectedtaxonNode= new Button(composite, SWT.CHECK);
321
				  String taxonStr = "";
322
				  TaxonNode node = CdmApplicationState.getCurrentAppConfig().getTaxonNodeService().load(configurator.getTaxonNodeFilter().getSubtreeFilter().get(0).getUuid());
323
				  if (node.hasTaxon()){
324
					  taxonStr = node.getTaxon().getName().getTitleCache();
325
				  }
326
				  checkUseSelectedtaxonNode.setText("Export selected subtree ("+  taxonStr+")");
327
				  checkUseSelectedtaxonNode.addListener(SWT.Selection, new Listener() {
328
		               @Override
329
		            public void handleEvent(Event e) {
330
		                   Button b = (Button) e.widget;
331
		                   GridData data = (GridData)  classificationSelectionCombo.getLayoutData();
332
		                   data.exclude = b.getSelection();
333
		                   classificationSelectionCombo.setVisible(!data.exclude);
334
		                   classificationLabel.setVisible(!data.exclude);
335
		                  
336
		               }
337
		        });
338
			}
339
			
340
			createClassificationSelectionCombo(composite);
341
			classificationSelectionCombo.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
342
			                true, false, 2, 1));
343
			
344
		}
345
		
346

    
347
		Label folderLabel = new Label(composite, SWT.NONE);
348
		folderLabel.setText("Select Folder for exported files");
349
		folderLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
350
        true, false, 2, 1));
351
		folderDialog = new DirectoryDialog(parent.getShell());
352
		folderDialog.setFilterPath(PreferencesUtil.getPreferenceStore().getString(EXPORT_FOLDER));
353

    
354
		text_folder = new Text(composite, SWT.BORDER);
355
		text_folder.setEditable(false);
356
		text_folder.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
357
				false));
358
		if (PreferencesUtil.getPreferenceStore().getString(EXPORT_FOLDER) != null){
359
			text_folder.setText(PreferencesUtil.getPreferenceStore().getString(EXPORT_FOLDER));
360
			setPageComplete(true);
361
		}
362

    
363
		Button button = new Button(composite, SWT.PUSH);
364
		button.setText("Browse...");
365

    
366
		button.addSelectionListener(new SelectionAdapter() {
367
			/*
368
			 * (non-Javadoc)
369
			 *
370
			 * @see
371
			 * org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse
372
			 * .swt.events.SelectionEvent)
373
			 */
374
			@Override
375
			public void widgetSelected(SelectionEvent e) {
376
				super.widgetSelected(e);
377

    
378
				String path = folderDialog.open();
379
				if (path != null) { // a folder was selected
380
					text_folder.setText(path);
381
					PreferencesUtil.getPreferenceStore().setValue(EXPORT_FOLDER, path);
382
					setPageComplete(true);
383
				}
384
			}
385
		});
386
		if (!outputModelExport){
387
			
388
			 Label fileLabel = new Label(composite, SWT.NONE);
389
		        fileLabel.setText("File");
390
	    		text_exportFileName = new Text(composite, SWT.BORDER);
391
	    		text_exportFileName.setText(generateFilename());
392
	    		text_exportFileName.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
393
	    				true, false));
394
		}
395
		// make the composite the wizard pages control
396
		setControl(composite);
397
	}
398

    
399
	protected String generateFilename() {
400
		StringBuffer buffer = new StringBuffer();
401

    
402
		Calendar cal = Calendar.getInstance();
403
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
404
		buffer.append(sdf.format(cal.getTime()));
405

    
406
		buffer.append("-");
407

    
408
		buffer.append(type + "_export-");
409
		try {
410
			buffer.append(CdmDataSourceRepository.getCurrentCdmSource());
411
		} catch (CdmRemoteSourceException e) {
412
			buffer.append("Unknown");
413
		}
414

    
415
		buffer.append(".");
416
		buffer.append(extension);
417

    
418
		return buffer.toString();
419
	}
420

    
421
	/**
422
	 * <p>
423
	 * getExportFileName
424
	 * </p>
425
	 *
426
	 * @return the exportFileName
427
	 */
428
	public String getExportFileName() {
429
		return text_exportFileName.getText();
430
	}
431

    
432
	/**
433
	 * <p>
434
	 * getFolderText
435
	 * </p>
436
	 *
437
	 * @return the folderText
438
	 */
439
	public String getFolderText() {
440
		return text_folder.getText();
441
	}
442
	
443
	/**
444
	 * <p>
445
	 * getFolderComposite
446
	 * </p>
447
	 *
448
	 * @return the folderText
449
	 */
450
	public Text getFolderComposite() {
451
		return text_folder;
452
	}
453

    
454
	private void createClassificationSelectionCombo(Composite parent){
455
//		classifications = CdmStore.getTaxonTreeService().list(null, null, null, null, null);
456

    
457
		Composite classificationSelection = new Composite(parent, SWT.NULL);
458
		classificationSelection.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 2, 1));
459

    
460
		GridLayout layout = new GridLayout();
461
		classificationSelection.setLayout(layout);
462

    
463
		classificationLabel = new Label(classificationSelection, SWT.NULL);
464
		// TODO not working is not really true but leave it here to remind everyone that this is under construction
465
		classificationLabel.setText("Export complete classification");
466
		
467
		classificationSelectionCombo = new Combo(classificationSelection, SWT.BORDER | SWT.READ_ONLY);
468
		classificationSelectionCombo.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, true, 2,1));
469

    
470
		for(Classification tree : classifications){
471
			classificationSelectionCombo.add(tree.getName().getText(), classifications.indexOf(tree));
472

    
473
		}
474

    
475
		classificationSelectionCombo.select(classifications.indexOf(selectedClassification));
476

    
477
		// TODO remember last selection
478
		classificationSelectionCombo.addSelectionListener(this);
479

    
480

    
481

    
482
		//return classificationSelection;
483
	}
484

    
485
	public Combo getCombo(){
486
	    return classificationSelectionCombo;
487
	}
488

    
489
	@Override
490
	public void widgetSelected(SelectionEvent e) {
491
		selectedClassification = classifications.get(classificationSelectionCombo.getSelectionIndex());
492

    
493
	}
494

    
495
	@Override
496
	public void widgetDefaultSelected(SelectionEvent e) {
497
		//not needed here
498

    
499
	}
500

    
501
	public UUID getSelectedClassificationUUID() {
502

    
503
		return selectedClassification.getUuid();
504
	}
505

    
506
	public boolean getCheckUseSelectedTaxonNode() {
507
		if (checkUseSelectedtaxonNode== null){
508
			return false;
509
		}
510
		return checkUseSelectedtaxonNode.getSelection();
511
	}
512

    
513
	
514
	
515
	
516

    
517
   
518

    
519
}
(17-17/30)