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