Merge branch 'develop' into LibrAlign
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / io / wizard / ExportToFileDestinationWizardPage.java
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
65 public static final String CSV_EXPORT = "CSV_EXPORT";
66 public static final String CSV_NAME_EXPORT = "CSV_NAME_EXPORT";
67
68 public static final String XML = "xml";
69
70 public static final String CSV = "csv";
71
72
73
74 public static final String ZIP = "zip";
75
76 private static boolean csvExport = false;
77
78 private static boolean csvNameExport = false;
79
80 private static String CSV_PRINT_EXPORT ="CSV_PRINT_EXPORT";
81
82 private static boolean csvPrintExport = false;
83
84 private DirectoryDialog folderDialog;
85 private Text text_exportFileName;
86
87 private Text text_folder;
88
89 private final String type;
90
91 private final String extension;
92
93 private Combo classificationSelectionCombo;
94
95 private List<Classification> classifications;
96
97 private Classification selectedClassification;
98
99 /**
100 * @param pageName
101 * @param selection
102 */
103 protected ExportToFileDestinationWizardPage(String pageName, String type,
104 String title, String description, String extension) {
105 super(pageName);
106
107 this.type = type;
108 this.extension = extension;
109 this.setTitle(title);
110 this.setDescription(description);
111 }
112
113 /**
114 * <p>
115 * Jaxb
116 * </p>
117 *
118 * @return a
119 * {@link eu.etaxonomy.taxeditor.io.wizard.ExportToFileDestinationWizardPage}
120 * object.
121 */
122 public static ExportToFileDestinationWizardPage Jaxb() {
123 return new ExportToFileDestinationWizardPage(
124 JAXB_EXPORT,
125 "jaxb",
126 "JAXB Export",
127 "Exports the contents of the currently selected database into the cdm jaxb format.",
128 XML);
129 }
130
131 /**
132 * <p>
133 * Tcs
134 * </p>
135 *
136 * @return a
137 * {@link eu.etaxonomy.taxeditor.io.wizard.ExportToFileDestinationWizardPage}
138 * object.
139 */
140 public static ExportToFileDestinationWizardPage Tcs() {
141 return new ExportToFileDestinationWizardPage(
142 TCS_EXPORT,
143 "tcs",
144 "Tcs Export",
145 "Export the contents of the currently selected database into TCS format.",
146 XML);
147 }
148
149 /**
150 * <p>
151 * Sdd
152 * </p>
153 *
154 * @return a
155 * {@link eu.etaxonomy.taxeditor.io.wizard.ExportToFileDestinationWizardPage}
156 * object.
157 */
158 public static ExportToFileDestinationWizardPage Sdd() {
159 return new ExportToFileDestinationWizardPage(
160 SDD_EXPORT,
161 "sdd",
162 "Sdd Export",
163 "Export the contents of the currently selected database into SDD format.",
164 XML);
165 }
166
167 /**
168 * @return
169 */
170 public static ExportToFileDestinationWizardPage Dwca() {
171 return new ExportToFileDestinationWizardPage(
172 DWCA_EXPORT,
173 "dwca",
174 "DwC-Archive Export",
175 "Export the contents of the currently selected database into Darwin Core Archive format.",
176 ZIP);
177 }
178
179
180
181 /**
182 * @return
183 */
184 public static ExportToFileDestinationWizardPage Csv() {
185 csvExport = true;
186 return new ExportToFileDestinationWizardPage(
187 CSV_EXPORT,
188 "csv",
189 "CSV Export",
190 "Export the contents of the currently selected database into Comma Separated Value format.",
191 CSV);
192 }
193
194 /**
195 * @return
196 */
197 public static ExportToFileDestinationWizardPage CsvNames() {
198 csvNameExport = true;
199 return new ExportToFileDestinationWizardPage(
200 CSV_NAME_EXPORT,
201 "csvNames",
202 "CSV Name Export",
203 "Export the names of the currently selected database into Semicolon Separated Value format.",
204 CSV);
205 }
206
207 /**
208 * @return
209 */
210 public static ExportToFileDestinationWizardPage CsvPrint() {
211 csvPrintExport = true;
212 return new ExportToFileDestinationWizardPage(
213 CSV_PRINT_EXPORT,
214 "csvPrint",
215 "CSV Print Export",
216 "Export the content of the currently selected database into Semicolon Separated Value format.",
217 CSV);
218 }
219
220 /*
221 * (non-Javadoc)
222 *
223 * @see
224 * org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets
225 * .Composite)
226 */
227 /** {@inheritDoc} */
228 @Override
229 public void createControl(Composite parent) {
230
231 setPageComplete(false);
232
233 Composite composite = new Composite(parent, SWT.NONE);
234 GridLayout gridLayout = new GridLayout();
235 gridLayout.numColumns = 3;
236 composite.setLayout(gridLayout);
237 if(classifications == null){
238 classifications = CdmStore.getService(IClassificationService.class).list(null, null, null, null, null);
239 Collections.sort(classifications, new Comparator<Classification>() {
240
241 @Override
242 public int compare(Classification o1, Classification o2) {
243 return o1.getTitleCache().compareTo(o2.getTitleCache());
244 }
245 });
246
247 selectedClassification = classifications.iterator().next();
248 }
249
250
251 if(csvExport || csvPrintExport){
252 Label comboBoxLabel = new Label(composite, SWT.NONE);
253 comboBoxLabel.setText("Classification");
254
255 createClassificationSelectionCombo(composite);
256 classificationSelectionCombo.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
257 true, false, 2, 1));
258 }
259
260
261
262
263 Label fileLabel = new Label(composite, SWT.NONE);
264 fileLabel.setText("File");
265
266 text_exportFileName = new Text(composite, SWT.BORDER);
267 text_exportFileName.setText(generateFilename());
268 text_exportFileName.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
269 true, false, 2, 1));
270
271 Label folderLabel = new Label(composite, SWT.NONE);
272 folderLabel.setText("Folder");
273
274 folderDialog = new DirectoryDialog(parent.getShell());
275
276 text_folder = new Text(composite, SWT.BORDER);
277 text_folder.setEditable(false);
278 text_folder.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
279 false));
280
281 Button button = new Button(composite, SWT.PUSH);
282 button.setText("Browse...");
283
284 button.addSelectionListener(new SelectionAdapter() {
285 /*
286 * (non-Javadoc)
287 *
288 * @see
289 * org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse
290 * .swt.events.SelectionEvent)
291 */
292 @Override
293 public void widgetSelected(SelectionEvent e) {
294 super.widgetSelected(e);
295 String path = folderDialog.open();
296 if (path != null) { // a folder was selected
297 text_folder.setText(path);
298 setPageComplete(true);
299 }
300 }
301 });
302
303 // make the composite the wizard pages control
304 setControl(composite);
305 }
306
307 protected String generateFilename() {
308 StringBuffer buffer = new StringBuffer();
309
310 Calendar cal = Calendar.getInstance();
311 SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
312 buffer.append(sdf.format(cal.getTime()));
313
314 buffer.append("-");
315
316 buffer.append(type + "_export-");
317 try {
318 buffer.append(CdmDataSourceRepository.getCurrentCdmSource());
319 } catch (CdmRemoteSourceException e) {
320 buffer.append("Unknown");
321 }
322
323 buffer.append(".");
324 buffer.append(extension);
325
326 return buffer.toString();
327 }
328
329 /**
330 * <p>
331 * getExportFileName
332 * </p>
333 *
334 * @return the exportFileName
335 */
336 public String getExportFileName() {
337 return text_exportFileName.getText();
338 }
339
340 /**
341 * <p>
342 * getFolderText
343 * </p>
344 *
345 * @return the folderText
346 */
347 public String getFolderText() {
348 return text_folder.getText();
349 }
350
351 private Control createClassificationSelectionCombo(Composite parent){
352 // classifications = CdmStore.getTaxonTreeService().list(null, null, null, null, null);
353
354 Composite classificationSelection = new Composite(parent, SWT.NULL);
355 classificationSelection.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
356
357 GridLayout layout = new GridLayout();
358 classificationSelection.setLayout(layout);
359
360 Label label = new Label(classificationSelection, SWT.NULL);
361 // TODO not working is not really true but leave it here to remind everyone that this is under construction
362 label.setText("Select Classification");
363 classificationSelectionCombo = new Combo(classificationSelection, SWT.BORDER | SWT.READ_ONLY);
364 classificationSelectionCombo.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, true));
365
366 for(Classification tree : classifications){
367 classificationSelectionCombo.add(tree.getName().getText(), classifications.indexOf(tree));
368
369 }
370
371 classificationSelectionCombo.select(classifications.indexOf(selectedClassification));
372
373 // TODO remember last selection
374 classificationSelectionCombo.addSelectionListener(this);
375
376
377
378 return classificationSelection;
379 }
380
381 public Combo getCombo(){
382 return classificationSelectionCombo;
383 }
384
385 @Override
386 public void widgetSelected(SelectionEvent e) {
387 selectedClassification = classifications.get(classificationSelectionCombo.getSelectionIndex());
388
389 }
390
391 @Override
392 public void widgetDefaultSelected(SelectionEvent e) {
393 //not needed here
394
395 }
396
397 public UUID getSelectedClassificationUUID() {
398
399 return selectedClassification.getUuid();
400 }
401
402 }