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