Project

General

Profile

Download (11.8 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.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.Text;
33

    
34
import eu.etaxonomy.cdm.api.service.IClassificationService;
35
import eu.etaxonomy.cdm.model.taxon.Classification;
36
import eu.etaxonomy.taxeditor.datasource.CdmDataSourceRepository;
37
import eu.etaxonomy.taxeditor.remoting.source.CdmRemoteSourceException;
38
import eu.etaxonomy.taxeditor.store.CdmStore;
39

    
40
/**
41
 * <p>
42
 * ExportToFileDestinationWizardPage class.
43
 * </p>
44
 *
45
 * @author n.hoffmann
46
 * @created 15.06.2009
47
 * @version 1.0
48
 */
49
public class ExportToFileDestinationWizardPage extends WizardPage implements SelectionListener {
50

    
51
	/** Constant <code>DATE_FORMAT_NOW="yyyyMMddHHmm"</code> */
52
	public static final String DATE_FORMAT_NOW = "yyyyMMddHHmm";
53

    
54
	/** Constant <code>JAXB_EXPORT="JAXB_EXPORT"</code> */
55
	public static final String JAXB_EXPORT = "JAXB_EXPORT";
56

    
57
	/** Constant <code>TCS_EXPORT="TCS_EXPORT"</code> */
58
	public static final String TCS_EXPORT = "TCS_EXPORT";
59

    
60
	/** Constant <code>SDD_EXPORT="SDD_EXPORT"</code> */
61
	public static final String SDD_EXPORT = "SDD_EXPORT";
62

    
63
	public static final String DWCA_EXPORT = "DWCA_EXPORT";
64
	public static final String OUTPUT_MODEL_EXPORT = "CDM_LIGHT(CSV)";
65
	public static final String CSV_EXPORT = "CSV_EXPORT";
66
	public static final String CSV_NAME_EXPORT = "CSV_NAME_EXPORT";
67
	private static final String CSV_PRINT_EXPORT ="CSV_PRINT_EXPORT";
68

    
69
	public static final String XML = "xml";
70

    
71
	public static final String CSV = "csv";
72

    
73

    
74

    
75
	public static final String ZIP = "zip";
76

    
77
    private boolean csvExport = false;
78

    
79
    private boolean csvNameExport = false;
80

    
81
	
82

    
83
	private boolean csvPrintExport = false;
84

    
85
	private boolean outputModelExport = false;
86

    
87
	private DirectoryDialog folderDialog;
88
	private Text text_exportFileName;
89

    
90
	private Text text_folder;
91

    
92
	private final String type;
93

    
94
	private final String extension;
95

    
96
    private Combo classificationSelectionCombo;
97

    
98
    private List<Classification> classifications;
99

    
100
    private Classification selectedClassification;
101

    
102
	/**
103
	 * @param pageName
104
	 * @param selection
105
	 */
106
	protected ExportToFileDestinationWizardPage(String pageName, String type,
107
			String title, String description, String extension) {
108
		super(pageName);
109

    
110
		this.type = type;
111
		switch(type) {
112
		   case CSV_EXPORT :
113
			   csvExport = true;
114
			   break;
115
		   case CSV_NAME_EXPORT:
116
			   csvNameExport = true;
117
			   break;
118
		   case OUTPUT_MODEL_EXPORT:
119
			   outputModelExport = true;
120
			   break;
121
		   case CSV_PRINT_EXPORT:
122
			   csvPrintExport = true;
123
			   break;
124
			  
125
		}
126
		this.extension = extension;
127
		this.setTitle(title);
128
		this.setDescription(description);
129
	}
130

    
131
	/**
132
	 * <p>
133
	 * Jaxb
134
	 * </p>
135
	 *
136
	 * @return a
137
	 *         {@link eu.etaxonomy.taxeditor.io.wizard.ExportToFileDestinationWizardPage}
138
	 *         object.
139
	 */
140
	public static ExportToFileDestinationWizardPage Jaxb() {
141
		return new ExportToFileDestinationWizardPage(
142
				JAXB_EXPORT,
143
				"jaxb",
144
				"JAXB Export",
145
				"Exports the contents of the currently selected database into the cdm jaxb format.",
146
				XML);
147
	}
148

    
149
	/**
150
	 * <p>
151
	 * Tcs
152
	 * </p>
153
	 *
154
	 * @return a
155
	 *         {@link eu.etaxonomy.taxeditor.io.wizard.ExportToFileDestinationWizardPage}
156
	 *         object.
157
	 */
158
	public static ExportToFileDestinationWizardPage Tcs() {
159
		return new ExportToFileDestinationWizardPage(
160
				TCS_EXPORT,
161
				"tcs",
162
				"Tcs Export",
163
				"Export the contents of the currently selected database into TCS format.",
164
				XML);
165
	}
166

    
167
	/**
168
	 * <p>
169
	 * Sdd
170
	 * </p>
171
	 *
172
	 * @return a
173
	 *         {@link eu.etaxonomy.taxeditor.io.wizard.ExportToFileDestinationWizardPage}
174
	 *         object.
175
	 */
176
	public static ExportToFileDestinationWizardPage Sdd() {
177
		return new ExportToFileDestinationWizardPage(
178
				SDD_EXPORT,
179
				"sdd",
180
				"Sdd Export",
181
				"Export the contents of the currently selected database into SDD format.",
182
				XML);
183
	}
184

    
185
	/**
186
	 * @return
187
	 */
188
	public static ExportToFileDestinationWizardPage Dwca() {
189
		return new ExportToFileDestinationWizardPage(
190
				DWCA_EXPORT,
191
				DWCA_EXPORT,
192
				"DwC-Archive Export",
193
				"Export the contents of the currently selected database into Darwin Core Archive format.",
194
				ZIP);
195
	}
196

    
197
	/**
198
     * @return
199
     */
200
    public static ExportToFileDestinationWizardPage OutputModel() {
201
        
202
        return new ExportToFileDestinationWizardPage(
203
                OUTPUT_MODEL_EXPORT,
204
                OUTPUT_MODEL_EXPORT,
205
                "Output Model Export",
206
                "Export the contents of the currently selected database into the output model format.",
207
                CSV);
208
    }
209

    
210

    
211
    /**
212
     * @return
213
     */
214
    public static ExportToFileDestinationWizardPage Csv() {
215
        
216
        return new ExportToFileDestinationWizardPage(
217
                CSV_EXPORT,
218
                CSV_EXPORT,
219
                "CSV Export",
220
                "Export the contents of the currently selected database into Comma Separated Value format.",
221
                CSV);
222
    }
223

    
224
    /**
225
     * @return
226
     */
227
    public static ExportToFileDestinationWizardPage CsvNames() {
228
    	
229
        return new ExportToFileDestinationWizardPage(
230
                CSV_NAME_EXPORT,
231
                CSV_NAME_EXPORT,
232
                "CSV Name Export",
233
                "Export the names of the currently selected database into Semicolon Separated Value format.",
234
                CSV);
235
    }
236

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

    
250
	/*
251
	 * (non-Javadoc)
252
	 *
253
	 * @see
254
	 * org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets
255
	 * .Composite)
256
	 */
257
	/** {@inheritDoc} */
258
	@Override
259
    public void createControl(Composite parent) {
260

    
261
		setPageComplete(false);
262

    
263
		Composite composite = new Composite(parent, SWT.NONE);
264
		GridLayout gridLayout = new GridLayout();
265
		
266
		gridLayout.numColumns = 2;
267
		
268
		
269
		composite.setLayout(gridLayout);
270
		if(classifications == null){
271
			classifications = CdmStore.getService(IClassificationService.class).list(null, null, null, null, null);
272
			Collections.sort(classifications, new Comparator<Classification>() {
273

    
274
                @Override
275
                public int compare(Classification o1, Classification o2) {
276
                    if (o1.equals(o2)){
277
                        return 0;
278
                    }
279
                    int result = o1.getTitleCache().compareTo(o2.getTitleCache());
280
                    if (result == 0){
281
                        return o1.getUuid().compareTo(o2.getUuid());
282
                    }
283
                    return result;
284
                }
285
            });
286

    
287
			selectedClassification = classifications.iterator().next();
288
		}
289

    
290

    
291
		if(csvExport || csvPrintExport || outputModelExport|| csvNameExport){
292
//		    Label comboBoxLabel = new Label(composite, SWT.NONE);
293
//		    comboBoxLabel.setText("Classification");
294

    
295
		    createClassificationSelectionCombo(composite);
296
		    classificationSelectionCombo.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
297
		                true, false, 2, 1));
298
		}
299
		if (!outputModelExport){
300
		    Label fileLabel = new Label(composite, SWT.NONE);
301
	        fileLabel.setText("File");
302
    		text_exportFileName = new Text(composite, SWT.BORDER);
303
    		text_exportFileName.setText(generateFilename());
304
    		text_exportFileName.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
305
    				true, false));
306
		}
307

    
308
		Label folderLabel = new Label(composite, SWT.NONE);
309
		folderLabel.setText("Select Folder for exported files");
310
		folderLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
311
        true, false, 2, 1));
312
		folderDialog = new DirectoryDialog(parent.getShell());
313

    
314
		text_folder = new Text(composite, SWT.BORDER);
315
		text_folder.setEditable(false);
316
		text_folder.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
317
				false));
318

    
319
		Button button = new Button(composite, SWT.PUSH);
320
		button.setText("Browse...");
321

    
322
		button.addSelectionListener(new SelectionAdapter() {
323
			/*
324
			 * (non-Javadoc)
325
			 *
326
			 * @see
327
			 * org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse
328
			 * .swt.events.SelectionEvent)
329
			 */
330
			@Override
331
			public void widgetSelected(SelectionEvent e) {
332
				super.widgetSelected(e);
333
				String path = folderDialog.open();
334
				if (path != null) { // a folder was selected
335
					text_folder.setText(path);
336
					setPageComplete(true);
337
				}
338
			}
339
		});
340

    
341
		// make the composite the wizard pages control
342
		setControl(composite);
343
	}
344

    
345
	protected String generateFilename() {
346
		StringBuffer buffer = new StringBuffer();
347

    
348
		Calendar cal = Calendar.getInstance();
349
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
350
		buffer.append(sdf.format(cal.getTime()));
351

    
352
		buffer.append("-");
353

    
354
		buffer.append(type + "_export-");
355
		try {
356
			buffer.append(CdmDataSourceRepository.getCurrentCdmSource());
357
		} catch (CdmRemoteSourceException e) {
358
			buffer.append("Unknown");
359
		}
360

    
361
		buffer.append(".");
362
		buffer.append(extension);
363

    
364
		return buffer.toString();
365
	}
366

    
367
	/**
368
	 * <p>
369
	 * getExportFileName
370
	 * </p>
371
	 *
372
	 * @return the exportFileName
373
	 */
374
	public String getExportFileName() {
375
		return text_exportFileName.getText();
376
	}
377

    
378
	/**
379
	 * <p>
380
	 * getFolderText
381
	 * </p>
382
	 *
383
	 * @return the folderText
384
	 */
385
	public String getFolderText() {
386
		return text_folder.getText();
387
	}
388

    
389
	private void createClassificationSelectionCombo(Composite parent){
390
//		classifications = CdmStore.getTaxonTreeService().list(null, null, null, null, null);
391

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

    
395
//		GridLayout layout = new GridLayout();
396
//		classificationSelection.setLayout(layout);
397

    
398
		Label label = new Label(parent, SWT.NULL);
399
		// TODO not working is not really true but leave it here to remind everyone that this is under construction
400
		label.setText("Select Classification");
401
		
402
		classificationSelectionCombo = new Combo(parent, SWT.BORDER | SWT.READ_ONLY);
403
		classificationSelectionCombo.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, true, 2,1));
404

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

    
408
		}
409

    
410
		classificationSelectionCombo.select(classifications.indexOf(selectedClassification));
411

    
412
		// TODO remember last selection
413
		classificationSelectionCombo.addSelectionListener(this);
414

    
415

    
416

    
417
		//return classificationSelection;
418
	}
419

    
420
	public Combo getCombo(){
421
	    return classificationSelectionCombo;
422
	}
423

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

    
428
	}
429

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

    
434
	}
435

    
436
	public UUID getSelectedClassificationUUID() {
437

    
438
		return selectedClassification.getUuid();
439
	}
440

    
441
}
(16-16/30)