001: /**
002: * ===========================================
003: * JFreeReport : a free Java reporting library
004: * ===========================================
005: *
006: * Project Info: http://reporting.pentaho.org/
007: *
008: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
009: *
010: * This library is free software; you can redistribute it and/or modify it under the terms
011: * of the GNU Lesser General Public License as published by the Free Software Foundation;
012: * either version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: * See the GNU Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public License along with this
019: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: *
022: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023: * in the United States and other countries.]
024: *
025: * ------------
026: * CSVDataExportDialog.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.modules.gui.csv;
030:
031: import java.awt.BorderLayout;
032: import java.awt.Dialog;
033: import java.awt.Frame;
034: import java.awt.GridBagConstraints;
035: import java.awt.GridBagLayout;
036: import java.awt.GridLayout;
037: import java.awt.Insets;
038: import java.awt.event.ActionEvent;
039: import java.io.File;
040: import java.text.MessageFormat;
041: import java.util.Locale;
042: import java.util.ResourceBundle;
043: import javax.swing.AbstractAction;
044: import javax.swing.Action;
045: import javax.swing.BorderFactory;
046: import javax.swing.ButtonGroup;
047: import javax.swing.JButton;
048: import javax.swing.JCheckBox;
049: import javax.swing.JComboBox;
050: import javax.swing.JFileChooser;
051: import javax.swing.JLabel;
052: import javax.swing.JOptionPane;
053: import javax.swing.JPanel;
054: import javax.swing.JRadioButton;
055: import javax.swing.JTabbedPane;
056: import javax.swing.JTextField;
057: import javax.swing.border.TitledBorder;
058:
059: import org.jfree.base.config.ModifiableConfiguration;
060: import org.jfree.fonts.encoding.EncodingRegistry;
061: import org.jfree.report.JFreeReportBoot;
062: import org.jfree.report.modules.gui.commonswing.AbstractExportDialog;
063: import org.jfree.report.modules.gui.commonswing.EncodingComboBoxModel;
064: import org.jfree.report.modules.gui.commonswing.JStatusBar;
065: import org.jfree.report.modules.gui.commonswing.StatusType;
066: import org.jfree.report.modules.output.csv.CSVProcessor;
067: import org.jfree.report.modules.output.table.csv.CSVTableModule;
068: import org.jfree.ui.ExtensionFileFilter;
069: import org.jfree.ui.LengthLimitingDocument;
070: import org.jfree.ui.action.ActionButton;
071: import org.jfree.util.Configuration;
072: import org.jfree.util.DefaultConfiguration;
073: import org.jfree.util.StringUtils;
074:
075: /**
076: * A dialog for exporting a report to CSV format.
077: *
078: * @author Thomas Morgner.
079: */
080: public class CSVDataExportDialog extends AbstractExportDialog {
081: /**
082: * The 'CSV encoding' property key.
083: */
084: public static final String CSV_OUTPUT_ENCODING = "org.jfree.report.modules.gui.csv.Encoding"; //$NON-NLS-1$
085: /**
086: * A default value of the 'CSV encoding' property key.
087: */
088: public static final String CSV_OUTPUT_ENCODING_DEFAULT = EncodingRegistry
089: .getPlatformDefaultEncoding();
090:
091: /**
092: * Internal action class to confirm the dialog and to validate the input.
093: */
094: private class ActionSelectSeparator extends AbstractAction {
095: /**
096: * Default constructor.
097: */
098: protected ActionSelectSeparator() {
099: }
100:
101: /**
102: * Receives notification that the action has occurred.
103: *
104: * @param e the action event.
105: */
106: public void actionPerformed(final ActionEvent e) {
107: performSeparatorSelection();
108: }
109: }
110:
111: /**
112: * Internal action class to select a target file.
113: */
114: private class ActionSelectFile extends AbstractAction {
115: /**
116: * Default constructor.
117: */
118: protected ActionSelectFile(final ResourceBundle resources) {
119: putValue(Action.NAME, resources
120: .getString("csvexportdialog.selectFile")); //$NON-NLS-1$
121: }
122:
123: /**
124: * Receives notification that the action has occurred.
125: *
126: * @param e the action event.
127: */
128: public void actionPerformed(final ActionEvent e) {
129: performSelectFile();
130: }
131: }
132:
133: /**
134: * Filename text field.
135: */
136: private JTextField txFilename;
137:
138: /**
139: * The encoding combo-box.
140: */
141: private JComboBox cbEncoding;
142:
143: /**
144: * The encoding model.
145: */
146: private EncodingComboBoxModel encodingModel;
147:
148: private JCheckBox cbxEnableReportHeader;
149: private JCheckBox cbxEnableReportFooter;
150: private JCheckBox cbxEnableGroupHeader;
151: private JCheckBox cbxEnableGroupFooter;
152: private JCheckBox cbxEnableItemband;
153: private JCheckBox cbxWriteStateColumns;
154:
155: /**
156: * The columnnames-as-first-row layout check-box.
157: */
158: private JCheckBox cbxColumnNamesAsFirstRow;
159:
160: /**
161: * A radio button for tab separators.
162: */
163: private JRadioButton rbSeparatorTab;
164:
165: /**
166: * A radio button for colon separators.
167: */
168: private JRadioButton rbSeparatorColon;
169:
170: /**
171: * A radio button for semi-colon separators.
172: */
173: private JRadioButton rbSeparatorSemicolon;
174:
175: /**
176: * A radio button for other separators.
177: */
178: private JRadioButton rbSeparatorOther;
179:
180: /**
181: * A text field for the 'other' separator.
182: */
183: private JTextField txSeparatorOther;
184:
185: private JStatusBar statusBar;
186:
187: /**
188: * A file chooser.
189: */
190: private JFileChooser fileChooser;
191:
192: private static final String COMMA_SEPARATOR = ","; //$NON-NLS-1$
193: private static final String SEMICOLON_SEPARATOR = ";"; //$NON-NLS-1$
194: private static final String TAB_SEPARATOR = "\t"; //$NON-NLS-1$
195: private static final String CSV_FILE_EXTENSION = ".csv"; //$NON-NLS-1$
196:
197: /**
198: * Creates a new CSV export dialog.
199: *
200: * @param owner the dialog owner.
201: */
202: public CSVDataExportDialog(final Frame owner) {
203: super (owner);
204: initConstructor();
205: }
206:
207: /**
208: * Creates a new CSV export dialog.
209: *
210: * @param owner the dialog owner.
211: */
212: public CSVDataExportDialog(final Dialog owner) {
213: super (owner);
214: initConstructor();
215: }
216:
217: /**
218: * Creates a new CSV export dialog. The created dialog is modal.
219: */
220: public CSVDataExportDialog() {
221: initConstructor();
222: }
223:
224: /**
225: * Initialisation.
226: */
227: private void initConstructor() {
228: statusBar = new JStatusBar();
229: setTitle(getResources()
230: .getString("csvexportdialog.dialogtitle")); //$NON-NLS-1$
231: initialize();
232: clear();
233: getFormValidator().setEnabled(true);
234: }
235:
236: public JStatusBar getStatusBar() {
237: return statusBar;
238: }
239:
240: protected String getResourceBaseName() {
241: return CSVDataExportPlugin.BASE_RESOURCE_CLASS;
242: }
243:
244: /**
245: * Initializes the Swing components of this dialog.
246: */
247: private void initialize() {
248: cbxWriteStateColumns = new JCheckBox(getResources().getString(
249: "csvexportdialog.write-state-columns")); //$NON-NLS-1$
250: cbxColumnNamesAsFirstRow = new JCheckBox(getResources()
251: .getString("cvsexportdialog.export.columnnames")); //$NON-NLS-1$
252:
253: getFormValidator().registerButton(cbxColumnNamesAsFirstRow);
254: cbxEnableReportHeader = new JCheckBox(getResources().getString(
255: "csvexportdialog.enable-report-header")); //$NON-NLS-1$
256: cbxEnableReportFooter = new JCheckBox(getResources().getString(
257: "csvexportdialog.enable-report-footer")); //$NON-NLS-1$
258: cbxEnableItemband = new JCheckBox(getResources().getString(
259: "csvexportdialog.enable-itemband")); //$NON-NLS-1$
260: cbxEnableGroupHeader = new JCheckBox(getResources().getString(
261: "csvexportdialog.enable-group-header")); //$NON-NLS-1$
262: cbxEnableGroupFooter = new JCheckBox(getResources().getString(
263: "csvexportdialog.enable-group-footer")); //$NON-NLS-1$
264:
265: getFormValidator().registerButton(cbxEnableGroupFooter);
266: getFormValidator().registerButton(cbxEnableGroupHeader);
267: getFormValidator().registerButton(cbxEnableItemband);
268: getFormValidator().registerButton(cbxEnableReportFooter);
269: getFormValidator().registerButton(cbxEnableReportHeader);
270:
271: txFilename = new JTextField();
272: txFilename.setColumns(30);
273: encodingModel = EncodingComboBoxModel.createDefaultModel(Locale
274: .getDefault());
275: encodingModel.sort();
276: cbEncoding = new JComboBox(encodingModel);
277:
278: final JPanel exportPane = createExportPane();
279:
280: final Configuration config = JFreeReportBoot.getInstance()
281: .getGlobalConfig();
282: if ("true"
283: .equals(config
284: .getConfigProperty("org.jfree.report.modules.gui.csv.data.AdvancedSettingsAvailable"))) {
285: final JTabbedPane tabbedPane = new JTabbedPane();
286: tabbedPane.add(getResources().getString(
287: "csvexportdialog.export-settings"), exportPane); //$NON-NLS-1$
288: tabbedPane
289: .add(
290: getResources()
291: .getString(
292: "csvexportdialog.advanced-settings"), createAdvancedOptionsPanel()); //$NON-NLS-1$
293:
294: setContentPane(createContentPane(tabbedPane));
295: } else {
296: setContentPane(createContentPane(exportPane));
297: }
298:
299: getFormValidator().registerTextField(txFilename);
300: getFormValidator().registerComboBox(cbEncoding);
301: }
302:
303: private JPanel createAdvancedOptionsPanel() {
304: final JPanel advancedOptionsPane = new JPanel();
305: advancedOptionsPane.setLayout(new GridBagLayout());
306:
307: GridBagConstraints gbc = new GridBagConstraints();
308: gbc.fill = GridBagConstraints.BOTH;
309: gbc.weighty = 1;
310: gbc.weightx = 1;
311: gbc.gridx = 0;
312: gbc.gridy = 0;
313: gbc.gridwidth = 3;
314: gbc.insets = new Insets(10, 1, 1, 1);
315: advancedOptionsPane.add(createExportOptionsPanel(), gbc);
316:
317: gbc = new GridBagConstraints();
318: gbc.fill = GridBagConstraints.BOTH;
319: gbc.weighty = 1;
320: gbc.weightx = 1;
321: gbc.gridx = 0;
322: gbc.gridy = 1;
323: gbc.gridwidth = 3;
324: gbc.insets = new Insets(10, 1, 1, 1);
325: advancedOptionsPane.add(createSeparatorPanel(), gbc);
326:
327: gbc = new GridBagConstraints();
328: gbc.fill = GridBagConstraints.BOTH;
329: gbc.weighty = 1;
330: gbc.weightx = 1;
331: gbc.gridx = 0;
332: gbc.gridy = 2;
333: gbc.gridwidth = 3;
334: gbc.insets = new Insets(10, 1, 1, 1);
335: advancedOptionsPane.add(createExportTypePanel(), gbc);
336:
337: gbc = new GridBagConstraints();
338: gbc.gridx = 0;
339: gbc.gridy = 2;
340: gbc.gridwidth = 1;
341: gbc.weightx = 1;
342: gbc.fill = GridBagConstraints.REMAINDER;
343: gbc.insets = new Insets(10, 1, 1, 1);
344: advancedOptionsPane.add(new JPanel(), gbc);
345: return advancedOptionsPane;
346: }
347:
348: private JPanel createExportPane() {
349: final JPanel contentPane = new JPanel();
350: contentPane.setLayout(new GridBagLayout());
351: contentPane.setBorder(BorderFactory.createEmptyBorder(3, 3, 3,
352: 3));
353:
354: final JLabel lblFileName = new JLabel(getResources().getString(
355: "csvexportdialog.filename")); //$NON-NLS-1$
356: final JButton btnSelect = new ActionButton(
357: new ActionSelectFile(getResources()));
358:
359: GridBagConstraints gbc = new GridBagConstraints();
360: gbc.gridx = 0;
361: gbc.gridy = 0;
362: gbc.anchor = GridBagConstraints.WEST;
363: gbc.insets = new Insets(3, 1, 1, 5);
364: contentPane.add(lblFileName, gbc);
365:
366: gbc = new GridBagConstraints();
367: gbc.fill = GridBagConstraints.HORIZONTAL;
368: gbc.weightx = 1;
369: gbc.gridx = 1;
370: gbc.gridy = 0;
371: gbc.gridwidth = 1;
372: gbc.insets = new Insets(3, 1, 5, 1);
373: contentPane.add(txFilename, gbc);
374:
375: gbc = new GridBagConstraints();
376: gbc.anchor = GridBagConstraints.NORTHWEST;
377: gbc.gridx = 2;
378: gbc.gridy = 0;
379: gbc.gridheight = 2;
380: gbc.insets = new Insets(1, 5, 5, 1);
381: contentPane.add(btnSelect, gbc);
382:
383: gbc = new GridBagConstraints();
384: gbc.gridx = 0;
385: gbc.gridy = 2;
386: gbc.gridwidth = 2;
387: gbc.weightx = 1;
388: gbc.weighty = 1;
389: gbc.fill = GridBagConstraints.REMAINDER;
390: gbc.insets = new Insets(10, 1, 1, 1);
391: contentPane.add(new JPanel(), gbc);
392:
393: return contentPane;
394: }
395:
396: /**
397: * Creates a panel for the export type.
398: *
399: * @return The panel.
400: */
401: private JPanel createExportTypePanel() {
402: // separator panel
403: final JPanel exportTypePanel = new JPanel();
404: exportTypePanel.setLayout(new BorderLayout());
405:
406: final TitledBorder tb = new TitledBorder(getResources()
407: .getString("csvexportdialog.exported-bands")); //$NON-NLS-1$
408: exportTypePanel.setBorder(tb);
409:
410: final JPanel rowTypePanel = new JPanel();
411: rowTypePanel.setLayout(new GridLayout(2, 3, 5, 2));
412: rowTypePanel.add(cbxEnableReportHeader);
413: rowTypePanel.add(cbxEnableGroupHeader);
414: rowTypePanel.add(cbxEnableItemband);
415: rowTypePanel.add(cbxEnableReportFooter);
416: rowTypePanel.add(cbxEnableGroupFooter);
417: exportTypePanel.add(rowTypePanel, BorderLayout.WEST);
418: return exportTypePanel;
419: }
420:
421: /**
422: * Creates a panel for the export type.
423: *
424: * @return The panel.
425: */
426: private JPanel createExportOptionsPanel() {
427: // separator panel
428: final JPanel exportTypePanel = new JPanel();
429: exportTypePanel.setLayout(new GridBagLayout());
430: final JLabel lblEncoding = new JLabel(getResources().getString(
431: "csvexportdialog.encoding")); //$NON-NLS-1$
432:
433: final TitledBorder tb = new TitledBorder(getResources()
434: .getString("csvexportdialog.export-options")); //$NON-NLS-1$
435: exportTypePanel.setBorder(tb);
436:
437: GridBagConstraints gbc = new GridBagConstraints();
438: gbc.anchor = GridBagConstraints.WEST;
439: gbc.gridx = 0;
440: gbc.gridy = 0;
441: gbc.insets = new Insets(1, 1, 1, 5);
442: exportTypePanel.add(lblEncoding, gbc);
443:
444: gbc = new GridBagConstraints();
445: gbc.fill = GridBagConstraints.HORIZONTAL;
446: gbc.weightx = 0;
447: gbc.gridx = 1;
448: gbc.gridy = 0;
449: gbc.gridwidth = 1;
450: gbc.insets = new Insets(5, 1, 1, 1);
451: exportTypePanel.add(cbEncoding, gbc);
452:
453: gbc = new GridBagConstraints();
454: gbc.fill = GridBagConstraints.HORIZONTAL;
455: gbc.anchor = GridBagConstraints.WEST;
456: gbc.weightx = 0;
457: gbc.gridx = 1;
458: gbc.gridy = 1;
459: gbc.gridwidth = 1;
460: gbc.insets = new Insets(5, 1, 1, 1);
461: exportTypePanel.add(cbxColumnNamesAsFirstRow, gbc);
462:
463: gbc = new GridBagConstraints();
464: gbc.fill = GridBagConstraints.HORIZONTAL;
465: gbc.anchor = GridBagConstraints.WEST;
466: gbc.weightx = 0;
467: gbc.gridx = 1;
468: gbc.gridy = 2;
469: gbc.gridwidth = 1;
470: gbc.insets = new Insets(1, 1, 1, 1);
471: exportTypePanel.add(cbxWriteStateColumns, gbc);
472:
473: return exportTypePanel;
474: }
475:
476: /**
477: * Creates a separator panel.
478: *
479: * @return The panel.
480: */
481: private JPanel createSeparatorPanel() {
482: // separator panel
483: final JPanel separatorPanel = new JPanel();
484: separatorPanel.setLayout(new GridBagLayout());
485:
486: final TitledBorder tb = new TitledBorder(getResources()
487: .getString("csvexportdialog.separatorchar")); //$NON-NLS-1$
488: separatorPanel.setBorder(tb);
489:
490: rbSeparatorTab = new JRadioButton(getResources().getString(
491: "csvexportdialog.separator.tab")); //$NON-NLS-1$
492: rbSeparatorColon = new JRadioButton(getResources().getString(
493: "csvexportdialog.separator.colon")); //$NON-NLS-1$
494: rbSeparatorSemicolon = new JRadioButton(getResources()
495: .getString("csvexportdialog.separator.semicolon")); //$NON-NLS-1$
496: rbSeparatorOther = new JRadioButton(getResources().getString(
497: "csvexportdialog.separator.other")); //$NON-NLS-1$
498:
499: getFormValidator().registerButton(rbSeparatorColon);
500: getFormValidator().registerButton(rbSeparatorOther);
501: getFormValidator().registerButton(rbSeparatorSemicolon);
502: getFormValidator().registerButton(rbSeparatorTab);
503:
504: final ButtonGroup btg = new ButtonGroup();
505: btg.add(rbSeparatorTab);
506: btg.add(rbSeparatorColon);
507: btg.add(rbSeparatorSemicolon);
508: btg.add(rbSeparatorOther);
509:
510: final Action selectAction = new ActionSelectSeparator();
511: rbSeparatorTab.addActionListener(selectAction);
512: rbSeparatorColon.addActionListener(selectAction);
513: rbSeparatorSemicolon.addActionListener(selectAction);
514: rbSeparatorOther.addActionListener(selectAction);
515:
516: final LengthLimitingDocument ldoc = new LengthLimitingDocument(
517: 1);
518: txSeparatorOther = new JTextField();
519: txSeparatorOther.setDocument(ldoc);
520: txSeparatorOther.setColumns(5);
521: getFormValidator().registerTextField(txSeparatorOther);
522:
523: GridBagConstraints gbc = new GridBagConstraints();
524: gbc.anchor = GridBagConstraints.WEST;
525: gbc.fill = GridBagConstraints.HORIZONTAL;
526: gbc.gridx = 0;
527: gbc.gridy = 0;
528: gbc.insets = new Insets(1, 1, 1, 1);
529: separatorPanel.add(rbSeparatorTab, gbc);
530:
531: gbc = new GridBagConstraints();
532: gbc.anchor = GridBagConstraints.WEST;
533: gbc.fill = GridBagConstraints.HORIZONTAL;
534: gbc.gridx = 0;
535: gbc.gridy = 1;
536: gbc.insets = new Insets(1, 1, 1, 1);
537: separatorPanel.add(rbSeparatorColon, gbc);
538:
539: gbc = new GridBagConstraints();
540: gbc.anchor = GridBagConstraints.WEST;
541: gbc.fill = GridBagConstraints.HORIZONTAL;
542: gbc.gridx = 0;
543: gbc.gridy = 2;
544: gbc.insets = new Insets(1, 1, 1, 1);
545: separatorPanel.add(rbSeparatorSemicolon, gbc);
546:
547: gbc = new GridBagConstraints();
548: gbc.anchor = GridBagConstraints.WEST;
549: gbc.fill = GridBagConstraints.NONE;
550: gbc.weightx = 0;
551: gbc.gridx = 0;
552: gbc.gridy = 3;
553: gbc.insets = new Insets(1, 1, 1, 1);
554: separatorPanel.add(rbSeparatorOther, gbc);
555:
556: gbc = new GridBagConstraints();
557: gbc.anchor = GridBagConstraints.WEST;
558: gbc.fill = GridBagConstraints.NONE;
559: gbc.weightx = 1;
560: gbc.gridx = 1;
561: gbc.gridy = 3;
562: gbc.insets = new Insets(1, 1, 1, 1);
563: separatorPanel.add(txSeparatorOther, gbc);
564:
565: return separatorPanel;
566: }
567:
568: /**
569: * Returns the export file name.
570: *
571: * @return The file name.
572: */
573: public String getFilename() {
574: return txFilename.getText();
575: }
576:
577: /**
578: * Sets the export file name.
579: *
580: * @param filename the file name.
581: */
582: public void setFilename(final String filename) {
583: this .txFilename.setText(filename);
584: }
585:
586: /**
587: * Clears all selections, input fields and sets the selected encryption level
588: * to none.
589: */
590: public void clear() {
591: txFilename.setText(""); //$NON-NLS-1$
592: cbEncoding
593: .setSelectedIndex(encodingModel
594: .indexOf(EncodingRegistry
595: .getPlatformDefaultEncoding()));
596: rbSeparatorColon.setSelected(true);
597: cbxColumnNamesAsFirstRow.setSelected(false);
598: performSeparatorSelection();
599: }
600:
601: /**
602: * Returns a new (and not connected to the default config from the job)
603: * configuration containing all properties from the dialog.
604: *
605: * @param full
606: * @return
607: */
608: protected Configuration grabDialogContents(final boolean full) {
609: final ModifiableConfiguration config = new DefaultConfiguration();
610: config.setConfigProperty(CSVProcessor.CSV_SEPARATOR,
611: getSeparatorString());
612: config.setConfigProperty(CSVTableModule.CONFIGURATION_PREFIX
613: + '.' + CSVTableModule.SEPARATOR_KEY,
614: getSeparatorString());
615: config.setConfigProperty(CSVProcessor.CSV_DATAROWNAME, String
616: .valueOf(isColumnNamesAsFirstRow()));
617: config.setConfigProperty(CSV_OUTPUT_ENCODING, getEncoding());
618:
619: config.setConfigProperty(CSVProcessor.CSV_ENABLE_GROUPFOOTERS,
620: String.valueOf(isEnableGroupFooter()));
621: config.setConfigProperty(CSVProcessor.CSV_ENABLE_GROUPHEADERS,
622: String.valueOf(isEnableGroupHeader()));
623: config.setConfigProperty(CSVProcessor.CSV_ENABLE_ITEMBANDS,
624: String.valueOf(isEnableItembands()));
625: config.setConfigProperty(CSVProcessor.CSV_ENABLE_REPORTFOOTER,
626: String.valueOf(isEnableReportFooter()));
627: config.setConfigProperty(CSVProcessor.CSV_ENABLE_REPORTHEADER,
628: String.valueOf(isEnableReportHeader()));
629:
630: config
631: .setConfigProperty(
632: "org.jfree.report.modules.gui.csv.FileName", getFilename()); //$NON-NLS-1$
633: config.setConfigProperty(CSVProcessor.CSV_WRITE_STATECOLUMNS,
634: "false"); //$NON-NLS-1$
635:
636: return config;
637: }
638:
639: /**
640: * Initialises the CSV export dialog from the settings in the report
641: * configuration.
642: *
643: * @param config the report configuration.
644: */
645: protected void setDialogContents(final Configuration config) {
646: // the CSV separator has two sources, either the data CSV or the
647: // table CSV. As we have only one input field for that property,
648: // we use a cascading schema to resolve this. The data oriented
649: // separator is preferred ...
650: final String tableCSVSeparator = config
651: .getConfigProperty(CSVTableModule.CONFIGURATION_PREFIX
652: + '.' + CSVTableModule.SEPARATOR_KEY,
653: COMMA_SEPARATOR);
654: setSeparatorString(config.getConfigProperty(
655: CSVProcessor.CSV_SEPARATOR, tableCSVSeparator));
656:
657: final String colNames = config.getConfigProperty(
658: CSVProcessor.CSV_DATAROWNAME, "false"); //$NON-NLS-1$
659: setColumnNamesAsFirstRow("true".equals(colNames)); //$NON-NLS-1$
660:
661: final String encoding = config.getConfigProperty(
662: CSV_OUTPUT_ENCODING, CSV_OUTPUT_ENCODING_DEFAULT);
663: encodingModel.ensureEncodingAvailable(encoding);
664: setEncoding(encoding);
665:
666: final String stateCols = config.getConfigProperty(
667: CSVProcessor.CSV_WRITE_STATECOLUMNS, "false"); //$NON-NLS-1$
668: setWriteStateColumns("true".equals(stateCols)); //$NON-NLS-1$
669:
670: final String enableReportHeader = config.getConfigProperty(
671: CSVProcessor.CSV_ENABLE_REPORTHEADER, "false"); //$NON-NLS-1$
672: setEnableReportHeader("true".equals(enableReportHeader)); //$NON-NLS-1$
673:
674: final String enableReportFooter = config.getConfigProperty(
675: CSVProcessor.CSV_ENABLE_REPORTFOOTER, "false"); //$NON-NLS-1$
676: setEnableReportFooter("true".equals(enableReportFooter)); //$NON-NLS-1$
677:
678: final String enableGroupHeader = config.getConfigProperty(
679: CSVProcessor.CSV_ENABLE_GROUPHEADERS, "false"); //$NON-NLS-1$
680: setEnableGroupHeader("true".equals(enableGroupHeader)); //$NON-NLS-1$
681:
682: final String enableGroupFooter = config.getConfigProperty(
683: CSVProcessor.CSV_ENABLE_GROUPFOOTERS, "false"); //$NON-NLS-1$
684: setEnableGroupFooter("true".equals(enableGroupFooter)); //$NON-NLS-1$
685:
686: final String enableItemBand = config.getConfigProperty(
687: CSVProcessor.CSV_ENABLE_ITEMBANDS, "false"); //$NON-NLS-1$
688: setEnableItembands("true".equals(enableItemBand)); //$NON-NLS-1$
689:
690: final String defaultFileName = config
691: .getConfigProperty("org.jfree.report.modules.gui.csv.FileName"); //$NON-NLS-1$
692: if (defaultFileName != null) {
693: setFilename(resolvePath(defaultFileName).getAbsolutePath());
694: } else {
695: setFilename(""); //$NON-NLS-1$
696: }
697: }
698:
699: /**
700: * Returns the separator string, which is controlled by the selection of radio
701: * buttons.
702: *
703: * @return The separator string.
704: */
705: public String getSeparatorString() {
706: if (rbSeparatorColon.isSelected()) {
707: return COMMA_SEPARATOR;
708: }
709: if (rbSeparatorSemicolon.isSelected()) {
710: return SEMICOLON_SEPARATOR;
711: }
712: if (rbSeparatorTab.isSelected()) {
713: return TAB_SEPARATOR;
714: }
715: if (rbSeparatorOther.isSelected()) {
716: return txSeparatorOther.getText();
717: }
718: return ""; //$NON-NLS-1$
719: }
720:
721: /**
722: * Sets the separator string.
723: *
724: * @param s the separator.
725: */
726: public void setSeparatorString(final String s) {
727: if (s == null) {
728: rbSeparatorOther.setSelected(true);
729: txSeparatorOther.setText(""); //$NON-NLS-1$
730: } else if (s.equals(COMMA_SEPARATOR)) {
731: rbSeparatorColon.setSelected(true);
732: } else if (s.equals(SEMICOLON_SEPARATOR)) {
733: rbSeparatorSemicolon.setSelected(true);
734: } else if (s.equals(TAB_SEPARATOR)) {
735: rbSeparatorTab.setSelected(true);
736: } else {
737: rbSeparatorOther.setSelected(true);
738: txSeparatorOther.setText(s);
739: }
740: performSeparatorSelection();
741: }
742:
743: /**
744: * Returns the encoding.
745: *
746: * @return The encoding.
747: */
748: public String getEncoding() {
749: if (cbEncoding.getSelectedIndex() == -1) {
750: return EncodingRegistry.getPlatformDefaultEncoding();
751: } else {
752: return encodingModel.getEncoding(cbEncoding
753: .getSelectedIndex());
754: }
755: }
756:
757: /**
758: * Sets the encoding.
759: *
760: * @param encoding the encoding.
761: */
762: public void setEncoding(final String encoding) {
763: cbEncoding.setSelectedIndex(encodingModel.indexOf(encoding));
764: }
765:
766: /**
767: * Selects a file to use as target for the report processing.
768: */
769: protected void performSelectFile() {
770: if (fileChooser == null) {
771: fileChooser = new JFileChooser();
772: final ExtensionFileFilter filter = new ExtensionFileFilter(
773: getResources().getString(
774: "csvexportdialog.csv-file-description"), CSV_FILE_EXTENSION); //$NON-NLS-1$
775: fileChooser.addChoosableFileFilter(filter);
776: fileChooser.setMultiSelectionEnabled(false);
777: }
778:
779: fileChooser.setSelectedFile(new File(getFilename()));
780: final int option = fileChooser.showSaveDialog(this );
781: if (option == JFileChooser.APPROVE_OPTION) {
782: final File selFile = fileChooser.getSelectedFile();
783: String selFileName = selFile.getAbsolutePath();
784:
785: // Test if ends on csv
786: if (StringUtils.endsWithIgnoreCase(selFileName,
787: CSV_FILE_EXTENSION) == false) {
788: selFileName = selFileName + CSV_FILE_EXTENSION;
789: }
790: setFilename(selFileName);
791: }
792: }
793:
794: /**
795: * Validates the contents of the dialog's input fields. If the selected file
796: * exists, it is also checked for validity.
797: *
798: * @return <code>true</code> if the input is valid, <code>false</code>
799: * otherwise
800: */
801: protected boolean performValidate() {
802: getStatusBar().clear();
803:
804: final String filename = getFilename();
805: if (filename.trim().length() == 0) {
806: getStatusBar().setStatus(
807: StatusType.ERROR,
808: getResources().getString(
809: "csvexportdialog.targetIsEmpty")); //$NON-NLS-1$
810: return false;
811: }
812: final File f = new File(filename);
813: if (f.exists()) {
814: if (f.isFile() == false) {
815: getStatusBar().setStatus(
816: StatusType.ERROR,
817: getResources().getString(
818: "csvexportdialog.targetIsNoFile")); //$NON-NLS-1$
819: return false;
820: }
821: if (f.canWrite() == false) {
822: getStatusBar().setStatus(
823: StatusType.ERROR,
824: getResources().getString(
825: "csvexportdialog.targetIsNotWritable")); //$NON-NLS-1$
826: return false;
827: }
828:
829: final String message = MessageFormat.format(getResources()
830: .getString("csvexportdialog.targetExistsWarning"), //$NON-NLS-1$
831: new Object[] { filename });
832: getStatusBar().setStatus(StatusType.WARNING, message);
833:
834: }
835:
836: if (cbxEnableGroupFooter.isSelected() == false
837: && cbxEnableGroupHeader.isSelected() == false
838: && cbxEnableReportFooter.isSelected() == false
839: && cbxEnableReportHeader.isSelected() == false
840: && cbxEnableItemband.isSelected() == false) {
841: getStatusBar().setStatus(
842: StatusType.ERROR,
843: getResources().getString(
844: "csvexportdialog.noContentForExport")); //$NON-NLS-1$
845: return false;
846: }
847:
848: return true;
849: }
850:
851: protected boolean performConfirm() {
852: final File f = new File(getFilename());
853: if (f.exists()) {
854: final String key1 = "csvexportdialog.targetOverwriteConfirmation"; //$NON-NLS-1$
855: final String key2 = "csvexportdialog.targetOverwriteTitle"; //$NON-NLS-1$
856: if (JOptionPane.showConfirmDialog(this , MessageFormat
857: .format(getResources().getString(key1),
858: new Object[] { getFilename() }),
859: getResources().getString(key2),
860: JOptionPane.YES_NO_OPTION,
861: JOptionPane.QUESTION_MESSAGE) == JOptionPane.NO_OPTION) {
862: return false;
863: }
864: }
865: return true;
866: }
867:
868: /**
869: * Enables or disables the 'other' separator text field.
870: */
871: protected void performSeparatorSelection() {
872: if (rbSeparatorOther.isSelected()) {
873: txSeparatorOther.setEnabled(true);
874: } else {
875: txSeparatorOther.setEnabled(false);
876: }
877: }
878:
879: public boolean isColumnNamesAsFirstRow() {
880: return cbxColumnNamesAsFirstRow.isSelected();
881: }
882:
883: public void setColumnNamesAsFirstRow(final boolean colsAsFirstRow) {
884: cbxColumnNamesAsFirstRow.setSelected(colsAsFirstRow);
885: }
886:
887: public boolean isWriteStateColumns() {
888: return cbxWriteStateColumns.isSelected();
889: }
890:
891: public void setWriteStateColumns(final boolean writeStateColumns) {
892: this .cbxWriteStateColumns.setSelected(writeStateColumns);
893: }
894:
895: public boolean isEnableGroupFooter() {
896: return cbxEnableGroupFooter.isSelected();
897: }
898:
899: public void setEnableGroupFooter(final boolean enableGroupFooter) {
900: this .cbxEnableGroupFooter.setSelected(enableGroupFooter);
901: }
902:
903: public boolean isEnableGroupHeader() {
904: return cbxEnableGroupHeader.isSelected();
905: }
906:
907: public void setEnableGroupHeader(final boolean enableGroupHeader) {
908: this .cbxEnableGroupHeader.setSelected(enableGroupHeader);
909: }
910:
911: public boolean isEnableItembands() {
912: return cbxEnableItemband.isSelected();
913: }
914:
915: public void setEnableItembands(final boolean enableItembands) {
916: this .cbxEnableItemband.setSelected(enableItembands);
917: }
918:
919: public boolean isEnableReportFooter() {
920: return cbxEnableReportFooter.isSelected();
921: }
922:
923: public void setEnableReportFooter(final boolean enableReportFooter) {
924: this .cbxEnableReportFooter.setSelected(enableReportFooter);
925: }
926:
927: public boolean isEnableReportHeader() {
928: return cbxEnableReportHeader.isSelected();
929: }
930:
931: public void setEnableReportHeader(final boolean enableReportHeader) {
932: this .cbxEnableReportHeader.setSelected(enableReportHeader);
933: }
934:
935: protected String getConfigurationSuffix() {
936: return "_csvexport"; //$NON-NLS-1$
937: }
938:
939: protected String getConfigurationPrefix() {
940: return "org.jfree.report.modules.gui.csv."; //$NON-NLS-1$
941: }
942: }
|