Project

General

Profile

Download (15.1 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.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.DirectoryDialog;
30
import org.eclipse.swt.widgets.Event;
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.application.CdmApplicationState;
36
import eu.etaxonomy.cdm.api.service.IClassificationService;
37
import eu.etaxonomy.cdm.io.cdmLight.CdmLightExportConfigurator;
38
import eu.etaxonomy.cdm.io.common.ExportConfiguratorBase;
39
import eu.etaxonomy.cdm.io.csv.caryophyllales.out.CsvNameExportConfigurator;
40
import eu.etaxonomy.cdm.io.csv.redlist.demo.CsvDemoExportConfigurator;
41
import eu.etaxonomy.cdm.io.dwca.out.DwcaTaxExportConfigurator;
42
import eu.etaxonomy.cdm.io.jaxb.JaxbExportConfigurator;
43
import eu.etaxonomy.cdm.io.sdd.out.SDDExportConfigurator;
44
import eu.etaxonomy.cdm.io.tcsxml.out.TcsXmlExportConfigurator;
45
import eu.etaxonomy.cdm.model.taxon.Classification;
46
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
47
import eu.etaxonomy.taxeditor.datasource.CdmDataSourceRepository;
48
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
49
import eu.etaxonomy.taxeditor.remoting.source.CdmRemoteSourceException;
50
import eu.etaxonomy.taxeditor.store.CdmStore;
51

    
52
/**
53
 * <p>
54
 * ExportToFileDestinationWizardPage class.
55
 * </p>
56
 *
57
 * @author n.hoffmann
58
 * @created 15.06.2009
59
 * @version 1.0
60
 */
61
public class ExportToFileDestinationWizardPage extends WizardPage implements SelectionListener {
62

    
63
	/** Constant <code>DATE_FORMAT_NOW="yyyyMMddHHmm"</code> */
64
	public static final String DATE_FORMAT_NOW = "yyyyMMddHHmm";
65

    
66
	/** Constant <code>JAXB_EXPORT="JAXB_EXPORT"</code> */
67
	public static final String JAXB_EXPORT = "JAXB_EXPORT";
68

    
69
	/** Constant <code>TCS_EXPORT="TCS_EXPORT"</code> */
70
	public static final String TCS_EXPORT = "TCS_EXPORT";
71

    
72
	/** Constant <code>SDD_EXPORT="SDD_EXPORT"</code> */
73
	public static final String SDD_EXPORT = "SDD_EXPORT";
74

    
75
	public static final String DWCA_EXPORT = "DWCA_EXPORT";
76
	public static final String OUTPUT_MODEL_EXPORT = "CDM_LIGHT(CSV)";
77
	public static final String CSV_EXPORT = "CSV_EXPORT";
78
	public static final String CSV_NAME_EXPORT = "CSV_NAME_EXPORT";
79
	private static final String CSV_PRINT_EXPORT ="CSV_PRINT_EXPORT";
80
	private static final String EXPORT_FOLDER ="EXPORT_FOLDER";
81
	public static final String XML = "xml";
82

    
83
	public static final String CSV = "csv";
84

    
85
	public static final String ZIP = "zip";
86

    
87
    private boolean csvExport = false;
88

    
89
    private boolean csvNameExport = false;
90

    
91
	private boolean csvPrintExport = false;
92

    
93
	private boolean outputModelExport = false;
94
	private boolean dwcaExport = false;
95

    
96
	private DirectoryDialog folderDialog;
97
	private Text text_exportFileName;
98

    
99
	private Text text_folder;
100

    
101
	private final String type;
102

    
103
	private final String extension;
104

    
105
    private Combo classificationSelectionCombo;
106

    
107
    private List<Classification> classifications;
108

    
109
    private Classification selectedClassification;
110
    private Label classificationLabel;
111

    
112
    private ExportConfiguratorBase configurator;
113
    private Button checkUseSelectedtaxonNode;
114

    
115
    private Button checkUseSelectedClassification;
116

    
117
	protected ExportToFileDestinationWizardPage(String pageName, String type,
118
			String title, String description, String extension, ExportConfiguratorBase configurator) {
119
		super(pageName);
120
		this.configurator = configurator;
121
		this.type = type;
122
		switch(type) {
123
		   case CSV_EXPORT :
124
			   csvExport = true;
125
			   break;
126
		   case CSV_NAME_EXPORT:
127
			   csvNameExport = true;
128
			   break;
129
		   case OUTPUT_MODEL_EXPORT:
130
			   outputModelExport = true;
131
			   break;
132
		   case CSV_PRINT_EXPORT:
133
			   csvPrintExport = true;
134
			   break;
135
		   case DWCA_EXPORT:
136
			   dwcaExport = true;
137
			   break;
138

    
139
		}
140
		this.extension = extension;
141
		this.setTitle(title);
142
		this.setDescription(description);
143
	}
144

    
145
	public static ExportToFileDestinationWizardPage Jaxb(JaxbExportConfigurator configurator) {
146
		return new ExportToFileDestinationWizardPage(
147
				JAXB_EXPORT,
148
				"jaxb",
149
				"JAXB Export",
150
				"Exports the content of the currently selected database into the cdm jaxb format.",
151
				XML, configurator);
152
	}
153

    
154
	public static ExportToFileDestinationWizardPage Tcs(TcsXmlExportConfigurator config) {
155
		return new ExportToFileDestinationWizardPage(
156
				TCS_EXPORT,
157
				"tcs",
158
				"Tcs Export",
159
				"Export the content of the currently selected database into TCS format.",
160
				XML, config);
161
	}
162

    
163
	public static ExportToFileDestinationWizardPage Sdd(SDDExportConfigurator config) {
164
		return new ExportToFileDestinationWizardPage(
165
				SDD_EXPORT,
166
				"sdd",
167
				"Sdd Export",
168
				"Export the content of the currently selected database into SDD format.",
169
				XML, config);
170
	}
171

    
172
	public static ExportToFileDestinationWizardPage Dwca(DwcaTaxExportConfigurator config) {
173
		return new ExportToFileDestinationWizardPage(
174
				DWCA_EXPORT,
175
				DWCA_EXPORT,
176
				"DwC-Archive Export",
177
				"Export the content of the currently selected database into Darwin Core Archive format.",
178
				ZIP, config);
179
	}
180

    
181
    public static ExportToFileDestinationWizardPage OutputModel(CdmLightExportConfigurator config) {
182

    
183
        return new ExportToFileDestinationWizardPage(
184
                OUTPUT_MODEL_EXPORT,
185
                OUTPUT_MODEL_EXPORT,
186
                "CDM Light Export (csv)",
187
                "Export the content of the currently selected database into the CDM light (csv) format.",
188
                CSV,config);
189
    }
190

    
191
    public static ExportToFileDestinationWizardPage Csv(CsvDemoExportConfigurator config) {
192

    
193
        return new ExportToFileDestinationWizardPage(
194
                CSV_EXPORT,
195
                CSV_EXPORT,
196
                "CSV Export",
197
                "Export the content of the currently selected database into Comma Separated Value format.",
198
                CSV, config);
199
    }
200

    
201
    public static ExportToFileDestinationWizardPage CsvNames(CsvNameExportConfigurator config) {
202

    
203
        return new ExportToFileDestinationWizardPage(
204
                CSV_NAME_EXPORT,
205
                CSV_NAME_EXPORT,
206
                "CSV Name Export",
207
                "Export the names of the currently selected database into Semicolon Separated Value format.",
208
                CSV, config);
209
    }
210

    
211
    public static ExportToFileDestinationWizardPage CsvPrint(CsvNameExportConfigurator config) {
212

    
213
        return new ExportToFileDestinationWizardPage(
214
                CSV_PRINT_EXPORT,
215
                CSV_NAME_EXPORT,
216
                "CSV Print Export",
217
                "Export the content of the currently selected database into Semicolon Separated Value format.",
218
                CSV, config);
219
    }
220

    
221
	/** {@inheritDoc} */
222
	@Override
223
    public void createControl(Composite parent) {
224

    
225
		setPageComplete(false);
226

    
227
		Composite composite = new Composite(parent, SWT.NONE);
228
		GridLayout gridLayout = new GridLayout();
229

    
230
		gridLayout.numColumns = 2;
231
		TaxonNode node = null;
232

    
233
		composite.setLayout(gridLayout);
234
		if(classifications == null){
235
			classifications = CdmStore.getService(IClassificationService.class).list(null, null, null, null, null);
236
			Collections.sort(classifications, new Comparator<Classification>() {
237

    
238
                @Override
239
                public int compare(Classification o1, Classification o2) {
240
                    if (o1.equals(o2)){
241
                        return 0;
242
                    }
243
                    int result = o1.getTitleCache().compareTo(o2.getTitleCache());
244
                    if (result == 0){
245
                        return o1.getUuid().compareTo(o2.getUuid());
246
                    }
247
                    return result;
248
                }
249
            });
250
			if (!configurator.getTaxonNodeFilter().getClassificationFilter().isEmpty()){
251
				selectedClassification = CdmStore.getService(IClassificationService.class).load(configurator.getTaxonNodeFilter().getClassificationFilter().get(0).getUuid());
252
			}else{
253

    
254
				selectedClassification = classifications.iterator().next();
255
			}
256
		}
257

    
258

    
259
		if(csvExport || csvPrintExport || outputModelExport|| csvNameExport || dwcaExport){
260
			if ((outputModelExport || dwcaExport)&& !configurator.getTaxonNodeFilter().getSubtreeFilter().isEmpty()){
261
				Composite selectNodeOrClassification = new Composite(composite, SWT.NONE);
262
				GridLayout grid = new GridLayout();
263
				grid.numColumns = 1;
264
				selectNodeOrClassification.setLayout(grid);
265
				checkUseSelectedtaxonNode= new Button(selectNodeOrClassification,  SWT.RADIO);
266
				String taxonStr = "";
267
				node = CdmApplicationState.getCurrentAppConfig().getTaxonNodeService().load(configurator.getTaxonNodeFilter().getSubtreeFilter().get(0).getUuid());
268
				if (node.hasTaxon()){
269
					taxonStr = node.getTaxon().getName().getTitleCache();
270
				}
271
				checkUseSelectedtaxonNode.setText("Export selected subtree ("+  taxonStr+")");
272
				checkUseSelectedtaxonNode.addListener(SWT.Selection, new Listener() {
273
					@Override
274
					public void handleEvent(Event e) {
275
						Button b = (Button) e.widget;
276
		                GridData data = (GridData)  classificationSelectionCombo.getLayoutData();
277
		                data.exclude = b.getSelection();
278
		                classificationSelectionCombo.setVisible(!data.exclude);
279
		                classificationLabel.setVisible(!data.exclude);
280
		            	}
281
		        	});
282
				  checkUseSelectedtaxonNode.setSelection(true);
283
				  checkUseSelectedClassification= new Button(selectNodeOrClassification,  SWT.RADIO);
284
				  checkUseSelectedClassification.setText("Export complete classification");
285
				  checkUseSelectedClassification.addListener(SWT.Selection, new Listener() {
286
		               @Override
287
		               public void handleEvent(Event e) {
288
		            	   Button b = (Button) e.widget;
289
		                   GridData data = (GridData)  classificationSelectionCombo.getLayoutData();
290
		                   data.exclude = b.getSelection();
291
		                   classificationSelectionCombo.setVisible(data.exclude);
292
		                   classificationLabel.setVisible(data.exclude);
293
		                  }
294
				  	});
295
			}
296
			if (node!= null){
297
				selectedClassification = node.getClassification();
298
			}
299
			createClassificationSelectionCombo(composite);
300

    
301
			classificationSelectionCombo.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
302
			                true, false, 2, 1));
303
			if (checkUseSelectedtaxonNode != null){
304

    
305
				if (checkUseSelectedtaxonNode.getSelection()){
306
					 classificationSelectionCombo.setVisible(false);
307
	                 classificationLabel.setVisible(false);
308
				}
309
			}
310

    
311
		}
312

    
313
		Label folderLabel = new Label(composite, SWT.NONE);
314
		folderLabel.setText("Select Folder for exported files");
315
		folderLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
316
        true, false, 2, 1));
317
		folderDialog = new DirectoryDialog(parent.getShell());
318
		folderDialog.setFilterPath(PreferencesUtil.getPreferenceStore().getString(EXPORT_FOLDER));
319

    
320
		text_folder = new Text(composite, SWT.BORDER);
321
		text_folder.setEditable(false);
322
		text_folder.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
323
				false));
324
		if (PreferencesUtil.getPreferenceStore().getString(EXPORT_FOLDER) != null){
325
			text_folder.setText(PreferencesUtil.getPreferenceStore().getString(EXPORT_FOLDER));
326
			setPageComplete(true);
327
		}
328

    
329
		Button button = new Button(composite, SWT.PUSH);
330
		button.setText("Browse...");
331

    
332
		button.addSelectionListener(new SelectionAdapter() {
333
			@Override
334
			public void widgetSelected(SelectionEvent e) {
335
				super.widgetSelected(e);
336

    
337
				String path = folderDialog.open();
338
				if (path != null) { // a folder was selected
339
					text_folder.setText(path);
340
					PreferencesUtil.getPreferenceStore().setValue(EXPORT_FOLDER, path);
341
					setPageComplete(true);
342
				}
343
			}
344
		});
345
		if (!outputModelExport){
346

    
347
			 Label fileLabel = new Label(composite, SWT.NONE);
348
		        fileLabel.setText("File");
349
	    		text_exportFileName = new Text(composite, SWT.BORDER);
350
	    		text_exportFileName.setText(generateFilename());
351
	    		text_exportFileName.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
352
	    				true, false));
353
		}
354
		// make the composite the wizard pages control
355
		setControl(composite);
356
	}
357

    
358
	protected String generateFilename() {
359
		StringBuffer buffer = new StringBuffer();
360

    
361
		Calendar cal = Calendar.getInstance();
362
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
363
		buffer.append(sdf.format(cal.getTime()));
364

    
365
		buffer.append("-");
366

    
367
		buffer.append(type + "_export-");
368
		try {
369
			buffer.append(CdmDataSourceRepository.getCurrentCdmSource());
370
		} catch (CdmRemoteSourceException e) {
371
			buffer.append("Unknown");
372
		}
373

    
374
		buffer.append(".");
375
		buffer.append(extension);
376

    
377
		return buffer.toString();
378
	}
379

    
380
	public String getExportFileName() {
381
		return text_exportFileName.getText();
382
	}
383

    
384
	public String getFolderText() {
385
		return text_folder.getText();
386
	}
387

    
388
	public Text getFolderComposite() {
389
		return text_folder;
390
	}
391

    
392
	private void createClassificationSelectionCombo(Composite parent){
393

    
394
		Composite classificationSelection = new Composite(parent, SWT.NULL);
395
		classificationSelection.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 2, 1));
396

    
397
		GridLayout layout = new GridLayout();
398
		classificationSelection.setLayout(layout);
399

    
400
		classificationLabel = new Label(classificationSelection, SWT.NULL);
401
		// TODO not working is not really true but leave it here to remind everyone that this is under construction
402
		classificationLabel.setText("Export complete classification");
403

    
404
		classificationSelectionCombo = new Combo(classificationSelection, SWT.BORDER | SWT.READ_ONLY);
405
		classificationSelectionCombo.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, true, 2,1));
406

    
407
		for(Classification tree : classifications){
408
			classificationSelectionCombo.add(tree.getName().getText(), classifications.indexOf(tree));
409

    
410
		}
411

    
412
		classificationSelectionCombo.select(classifications.indexOf(selectedClassification));
413

    
414
		// TODO remember last selection
415
		classificationSelectionCombo.addSelectionListener(this);
416

    
417

    
418

    
419
		//return classificationSelection;
420
	}
421

    
422
	public Combo getCombo(){
423
	    return classificationSelectionCombo;
424
	}
425

    
426
	@Override
427
	public void widgetSelected(SelectionEvent e) {
428
		selectedClassification = classifications.get(classificationSelectionCombo.getSelectionIndex());
429

    
430
	}
431

    
432
	@Override
433
	public void widgetDefaultSelected(SelectionEvent e) {
434
		//not needed here
435

    
436
	}
437

    
438
	public UUID getSelectedClassificationUUID() {
439

    
440
		return selectedClassification.getUuid();
441
	}
442

    
443
	public boolean getCheckUseSelectedTaxonNode() {
444
		if (checkUseSelectedtaxonNode== null){
445
			return false;
446
		}
447
		return checkUseSelectedtaxonNode.getSelection();
448
	}
449

    
450
	@Override
451
	public boolean canFlipToNextPage() {
452
        return  getFolderText() != null;
453
    }
454

    
455
}
(17-17/30)