461336da77e38697e48d78ad218a277ef2138d70
[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 if (o1.equals(o2)){
244 return 0;
245 }
246 int result = o1.getTitleCache().compareTo(o2.getTitleCache());
247 if (result == 0){
248 return o1.getUuid().compareTo(o2.getUuid());
249 }
250 return result;
251 }
252 });
253
254 selectedClassification = classifications.iterator().next();
255 }
256
257
258 if(csvExport || csvPrintExport){
259 Label comboBoxLabel = new Label(composite, SWT.NONE);
260 comboBoxLabel.setText("Classification");
261
262 createClassificationSelectionCombo(composite);
263 classificationSelectionCombo.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
264 true, false, 2, 1));
265 }
266
267
268
269
270 Label fileLabel = new Label(composite, SWT.NONE);
271 fileLabel.setText("File");
272
273 text_exportFileName = new Text(composite, SWT.BORDER);
274 text_exportFileName.setText(generateFilename());
275 text_exportFileName.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
276 true, false, 2, 1));
277
278 Label folderLabel = new Label(composite, SWT.NONE);
279 folderLabel.setText("Folder");
280
281 folderDialog = new DirectoryDialog(parent.getShell());
282
283 text_folder = new Text(composite, SWT.BORDER);
284 text_folder.setEditable(false);
285 text_folder.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
286 false));
287
288 Button button = new Button(composite, SWT.PUSH);
289 button.setText("Browse...");
290
291 button.addSelectionListener(new SelectionAdapter() {
292 /*
293 * (non-Javadoc)
294 *
295 * @see
296 * org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse
297 * .swt.events.SelectionEvent)
298 */
299 @Override
300 public void widgetSelected(SelectionEvent e) {
301 super.widgetSelected(e);
302 String path = folderDialog.open();
303 if (path != null) { // a folder was selected
304 text_folder.setText(path);
305 setPageComplete(true);
306 }
307 }
308 });
309
310 // make the composite the wizard pages control
311 setControl(composite);
312 }
313
314 protected String generateFilename() {
315 StringBuffer buffer = new StringBuffer();
316
317 Calendar cal = Calendar.getInstance();
318 SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
319 buffer.append(sdf.format(cal.getTime()));
320
321 buffer.append("-");
322
323 buffer.append(type + "_export-");
324 try {
325 buffer.append(CdmDataSourceRepository.getCurrentCdmSource());
326 } catch (CdmRemoteSourceException e) {
327 buffer.append("Unknown");
328 }
329
330 buffer.append(".");
331 buffer.append(extension);
332
333 return buffer.toString();
334 }
335
336 /**
337 * <p>
338 * getExportFileName
339 * </p>
340 *
341 * @return the exportFileName
342 */
343 public String getExportFileName() {
344 return text_exportFileName.getText();
345 }
346
347 /**
348 * <p>
349 * getFolderText
350 * </p>
351 *
352 * @return the folderText
353 */
354 public String getFolderText() {
355 return text_folder.getText();
356 }
357
358 private Control createClassificationSelectionCombo(Composite parent){
359 // classifications = CdmStore.getTaxonTreeService().list(null, null, null, null, null);
360
361 Composite classificationSelection = new Composite(parent, SWT.NULL);
362 classificationSelection.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
363
364 GridLayout layout = new GridLayout();
365 classificationSelection.setLayout(layout);
366
367 Label label = new Label(classificationSelection, SWT.NULL);
368 // TODO not working is not really true but leave it here to remind everyone that this is under construction
369 label.setText("Select Classification");
370 classificationSelectionCombo = new Combo(classificationSelection, SWT.BORDER | SWT.READ_ONLY);
371 classificationSelectionCombo.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, true));
372
373 for(Classification tree : classifications){
374 classificationSelectionCombo.add(tree.getName().getText(), classifications.indexOf(tree));
375
376 }
377
378 classificationSelectionCombo.select(classifications.indexOf(selectedClassification));
379
380 // TODO remember last selection
381 classificationSelectionCombo.addSelectionListener(this);
382
383
384
385 return classificationSelection;
386 }
387
388 public Combo getCombo(){
389 return classificationSelectionCombo;
390 }
391
392 @Override
393 public void widgetSelected(SelectionEvent e) {
394 selectedClassification = classifications.get(classificationSelectionCombo.getSelectionIndex());
395
396 }
397
398 @Override
399 public void widgetDefaultSelected(SelectionEvent e) {
400 //not needed here
401
402 }
403
404 public UUID getSelectedClassificationUUID() {
405
406 return selectedClassification.getUuid();
407 }
408
409 }