/** * */ package com.onaro.sanscreen.client.view.actions; import java.io.File; import javax.swing.JComponent; import org.apache.commons.lang3.StringUtils; import com.onaro.client.leekui.jface.dialogs.ValidatableDialogPage; import com.onaro.commons.swing.OnaroSwingUtilities; import javax.swing.JPanel; import java.awt.GridBagLayout; import java.awt.Dimension; import javax.swing.JLabel; import java.awt.GridBagConstraints; import javax.swing.JFileChooser; import javax.swing.JTextField; import javax.swing.JButton; import javax.swing.JRadioButton; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ButtonGroup; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; import javax.swing.filechooser.FileFilter; import javax.swing.JCheckBox; import java.awt.FlowLayout; public class ExportGroupingTableToCSVOptionsDialogPage extends ValidatableDialogPage { private boolean initialValid = true; private JPanel contentsPanel = null; // @jve:decl-index=0:visual-constraint="55,45" private JLabel fileNameLabel = null; private JTextField fileNameTextField = null; private JButton fileBrowseButton = null; private JLabel exportRowsLabel = null; private JRadioButton allRowsRadioButton = null; private JRadioButton selectedRowsRadioButton = null; private JLabel exportColunnsLabel = null; private JRadioButton allColumnsRadioButton = null; private JRadioButton visibleColumnsRadioButton = null; private JPanel optionsPanel = null; private JLabel spacerLabel = null; private JLabel spacer2Label = null; private JCheckBox includeHeaderCheckBox = null; private ButtonGroup rowsButtonGroup = null; // @jve:decl-index=0:visual-constraint="561,126" private ButtonGroup columnsButtonGroup = null; // @jve:decl-index=0:visual-constraint="561,203" private JFileChooser fileChooser = null; // @jve:decl-index=0:visual-constraint="689,270" private JPanel checkPanel = null; private JCheckBox utf8EncodeCheckBox = null; public ExportGroupingTableToCSVOptionsDialogPage(String tableName) { setTitle(Messages.INSTANCE.getExportTableDialogHeader(tableName)); setDescription(Messages.INSTANCE.getExportTableDialogDesc()); setIcon(Resources.INSTANCE.getExportTableBannerIcon()); } @Override protected JComponent createComponent() { return getContentsPanel(); } public boolean isPageValid() { boolean valid = getExportFile() != null; if (!initialValid) { String errorMessage = valid ? null : Messages.INSTANCE.getExportTableFileInvalidError(); setErrorMessage(errorMessage); getContainer().updateMessage(); } initialValid = false; return valid; } public File getExportFile() { if (StringUtils.isBlank(getExportFileName())) { return null; } File f = new File(getExportFileName()); if (f.isDirectory()) { return null; } if (f.exists() && !f.canWrite()) { return null; } return f; } public String getExportFileName() { return getFileNameTextField().getText(); } public boolean getExportSelectedOnly() { return getSelectedRowsRadioButton().isSelected(); } public boolean getExportAllColumns() { return getAllColumnsRadioButton().isSelected(); } public boolean getIncludeHeaderRow() { return getIncludeHeaderCheckBox().isSelected(); } public boolean getUseUtf8Encoding() { return getUtf8EncodeCheckBox().isSelected(); } private void doSelectFile() { File existingSelection = getExportFile(); if (existingSelection != null) { getFileChooser().setSelectedFile(existingSelection); } int result = getFileChooser().showSaveDialog(getContentsPanel()); if (result == JFileChooser.APPROVE_OPTION) { File file = getFileChooser().getSelectedFile(); String fileName = file.getPath(); if (fileName.indexOf('.') == -1) { fileName = fileName + ".csv"; //$NON-NLS-1$ } getFileNameTextField().setText(fileName); } } private void doFileChanged() { getContainer().updateButtons(); } /** * This method initializes contentsPanel * * @return javax.swing.JPanel */ private JPanel getContentsPanel() { if (contentsPanel == null) { GridBagConstraints gridBagConstraints11 = new GridBagConstraints(); gridBagConstraints11.gridx = 0; gridBagConstraints11.fill = GridBagConstraints.BOTH; gridBagConstraints11.weighty = 1.0; gridBagConstraints11.gridy = 3; spacer2Label = new JLabel(); GridBagConstraints gridBagConstraints9 = new GridBagConstraints(); gridBagConstraints9.gridx = 0; gridBagConstraints9.gridwidth = 3; gridBagConstraints9.fill = GridBagConstraints.HORIZONTAL; gridBagConstraints9.anchor = GridBagConstraints.WEST; gridBagConstraints9.weightx = 1.0; gridBagConstraints9.insets = new Insets(10, 10, 10, 10); gridBagConstraints9.gridy = 0; exportColunnsLabel = new JLabel(); exportColunnsLabel.setText(Messages.INSTANCE.getExportTableExportColumnsLabel()); exportRowsLabel = new JLabel(); exportRowsLabel.setText(Messages.INSTANCE.getExportTableExportRowsLabel()); GridBagConstraints gridBagConstraints2 = new GridBagConstraints(); gridBagConstraints2.gridx = 2; gridBagConstraints2.insets = new Insets(5, 0, 10, 10); gridBagConstraints2.gridy = 2; GridBagConstraints gridBagConstraints1 = new GridBagConstraints(); gridBagConstraints1.fill = GridBagConstraints.HORIZONTAL; gridBagConstraints1.gridy = 2; gridBagConstraints1.weightx = 1.0; gridBagConstraints1.insets = new Insets(5, 0, 10, 5); gridBagConstraints1.gridx = 1; GridBagConstraints gridBagConstraints = new GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.insets = new Insets(5, 10, 10, 5); gridBagConstraints.gridy = 2; fileNameLabel = new JLabel(); fileNameLabel.setText(Messages.INSTANCE.getExportTableExportFileLabel()); contentsPanel = new JPanel(); contentsPanel.setLayout(new GridBagLayout()); contentsPanel.setSize(new Dimension(450, 250)); contentsPanel.add(fileNameLabel, gridBagConstraints); contentsPanel.add(getFileNameTextField(), gridBagConstraints1); contentsPanel.add(getFileBrowseButton(), gridBagConstraints2); contentsPanel.add(getOptionsPanel(), gridBagConstraints9); contentsPanel.add(spacer2Label, gridBagConstraints11); } return contentsPanel; } /** * This method initializes fileNameTextField * * @return javax.swing.JTextField */ private JTextField getFileNameTextField() { if (fileNameTextField == null) { fileNameTextField = new JTextField(); fileNameTextField.setName("fileNameTextField"); //$NON-NLS-1$ fileNameTextField.getDocument().addDocumentListener(new DocumentListener() { public void changedUpdate(DocumentEvent e) { doFileChanged(); } public void insertUpdate(DocumentEvent e) { doFileChanged(); } public void removeUpdate(DocumentEvent e) { doFileChanged(); } }); } return fileNameTextField; } /** * This method initializes fileBrowseButton * * @return javax.swing.JButton */ private JButton getFileBrowseButton() { if (fileBrowseButton == null) { fileBrowseButton = new JButton(); fileBrowseButton.setName("fileBrowseButton"); //$NON-NLS-1$ fileBrowseButton.setText(Messages.INSTANCE.getExportTableExportFileBrowseButton()); fileBrowseButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { doSelectFile(); } }); } return fileBrowseButton; } /** * This method initializes allRowsRadioButton * * @return javax.swing.JRadioButton */ private JRadioButton getAllRowsRadioButton() { if (allRowsRadioButton == null) { allRowsRadioButton = new JRadioButton(); allRowsRadioButton.setName("allRowsRadioButton"); //$NON-NLS-1$ allRowsRadioButton.setText(Messages.INSTANCE.getExportTableExportAllRowsOption()); getRowsButtonGroup().add(allRowsRadioButton); allRowsRadioButton.setSelected(true); } return allRowsRadioButton; } /** * This method initializes selectedRowsRadioButton * * @return javax.swing.JRadioButton */ private JRadioButton getSelectedRowsRadioButton() { if (selectedRowsRadioButton == null) { selectedRowsRadioButton = new JRadioButton(); selectedRowsRadioButton.setName("selectedRowsRadioButton"); //$NON-NLS-1$ selectedRowsRadioButton.setText(Messages.INSTANCE.getExportTableExportSelectedRowsOption()); getRowsButtonGroup().add(selectedRowsRadioButton); } return selectedRowsRadioButton; } /** * This method initializes allColumnsRadioButton * * @return javax.swing.JRadioButton */ private JRadioButton getAllColumnsRadioButton() { if (allColumnsRadioButton == null) { allColumnsRadioButton = new JRadioButton(); allColumnsRadioButton.setName("allColumnsRadioButton"); //$NON-NLS-1$ allColumnsRadioButton.setText(Messages.INSTANCE.getExportTableExportAllColumnsOption()); getColumnsButtonGroup().add(allColumnsRadioButton); } return allColumnsRadioButton; } /** * This method initializes visibleColumnsRadioButton * * @return javax.swing.JRadioButton */ private JRadioButton getVisibleColumnsRadioButton() { if (visibleColumnsRadioButton == null) { visibleColumnsRadioButton = new JRadioButton(); visibleColumnsRadioButton.setName("visibleColumnsRadioButton"); //$NON-NLS-1$ visibleColumnsRadioButton.setText(Messages.INSTANCE.getExportTableExportVisibleColumnsOption()); getColumnsButtonGroup().add(visibleColumnsRadioButton); visibleColumnsRadioButton.setSelected(true); } return visibleColumnsRadioButton; } /** * This method initializes optionsPanel * * @return javax.swing.JPanel */ private JPanel getOptionsPanel() { if (optionsPanel == null) { GridBagConstraints gridBagConstraints12 = new GridBagConstraints(); gridBagConstraints12.gridx = 0; gridBagConstraints12.gridwidth = 4; gridBagConstraints12.gridy = 2; GridBagConstraints gridBagConstraints10 = new GridBagConstraints(); gridBagConstraints10.gridx = 3; gridBagConstraints10.weightx = 1.0; gridBagConstraints10.gridy = 0; spacerLabel = new JLabel(); GridBagConstraints gridBagConstraints8 = new GridBagConstraints(); gridBagConstraints8.gridx = 2; gridBagConstraints8.insets = OnaroSwingUtilities.NO_INSETS; gridBagConstraints8.anchor = GridBagConstraints.WEST; gridBagConstraints8.gridy = 1; GridBagConstraints gridBagConstraints7 = new GridBagConstraints(); gridBagConstraints7.gridx = 1; gridBagConstraints7.insets = new Insets(0, 0, 0, 5); gridBagConstraints7.anchor = GridBagConstraints.WEST; gridBagConstraints7.gridy = 1; GridBagConstraints gridBagConstraints5 = new GridBagConstraints(); gridBagConstraints5.gridx = 2; gridBagConstraints5.insets = OnaroSwingUtilities.NO_INSETS; gridBagConstraints5.anchor = GridBagConstraints.WEST; gridBagConstraints5.gridy = 0; GridBagConstraints gridBagConstraints4 = new GridBagConstraints(); gridBagConstraints4.gridx = 1; gridBagConstraints4.insets = new Insets(0, 0, 0, 5); gridBagConstraints4.anchor = GridBagConstraints.WEST; gridBagConstraints4.gridy = 0; GridBagConstraints gridBagConstraints6 = new GridBagConstraints(); gridBagConstraints6.gridx = 0; gridBagConstraints6.anchor = GridBagConstraints.WEST; gridBagConstraints6.insets = new Insets(0, 0, 0, 5); gridBagConstraints6.gridy = 1; GridBagConstraints gridBagConstraints3 = new GridBagConstraints(); gridBagConstraints3.gridx = 0; gridBagConstraints3.anchor = GridBagConstraints.WEST; gridBagConstraints3.insets = new Insets(0, 0, 0, 5); gridBagConstraints3.gridy = 0; optionsPanel = new JPanel(); optionsPanel.setLayout(new GridBagLayout()); optionsPanel.add(exportRowsLabel, gridBagConstraints3); optionsPanel.add(exportColunnsLabel, gridBagConstraints6); optionsPanel.add(getAllRowsRadioButton(), gridBagConstraints4); optionsPanel.add(getSelectedRowsRadioButton(), gridBagConstraints5); optionsPanel.add(getAllColumnsRadioButton(), gridBagConstraints7); optionsPanel.add(getVisibleColumnsRadioButton(), gridBagConstraints8); optionsPanel.add(spacerLabel, gridBagConstraints10); optionsPanel.add(getCheckPanel(), gridBagConstraints12); } return optionsPanel; } /** * This method initializes includeHeaderCheckBox * * @return javax.swing.JCheckBox */ private JCheckBox getIncludeHeaderCheckBox() { if (includeHeaderCheckBox == null) { includeHeaderCheckBox = new JCheckBox(); includeHeaderCheckBox.setName("includeHeaderCheckBox"); //$NON-NLS-1$ includeHeaderCheckBox.setText(Messages.INSTANCE.getExportTableIncludeHeaderOption()); includeHeaderCheckBox.setSelected(true); } return includeHeaderCheckBox; } /** * This method initializes rowsButtonGroup * * @return javax.swing.ButtonGroup */ private ButtonGroup getRowsButtonGroup() { if (rowsButtonGroup == null) { rowsButtonGroup = new ButtonGroup(); } return rowsButtonGroup; } /** * This method initializes columnsButtonGroup * * @return javax.swing.ButtonGroup */ private ButtonGroup getColumnsButtonGroup() { if (columnsButtonGroup == null) { columnsButtonGroup = new ButtonGroup(); } return columnsButtonGroup; } /** * This method initializes fileChooser * * @return javax.swing.JFileChooser */ private JFileChooser getFileChooser() { if (fileChooser == null) { fileChooser = new JFileChooser(); fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); fileChooser.setAcceptAllFileFilterUsed(true); fileChooser.setFileFilter(new FileFilter() { @Override public boolean accept(File f) { if (f.isDirectory()) { return true; } return f.getName().endsWith(".csv"); //$NON-NLS-1$ } @Override public String getDescription() { return Messages.INSTANCE.getExportTableCSVFilterDesc(); } }); } return fileChooser; } /** * This method initializes checkPanel * * @return javax.swing.JPanel */ private JPanel getCheckPanel() { if (checkPanel == null) { FlowLayout flowLayout = new FlowLayout(); flowLayout.setVgap(0); checkPanel = new JPanel(); checkPanel.setLayout(flowLayout); checkPanel.add(getIncludeHeaderCheckBox(), null); checkPanel.add(getUtf8EncodeCheckBox(), null); } return checkPanel; } /** * This method initializes utf8EncodeCheckBox * * @return javax.swing.JCheckBox */ private JCheckBox getUtf8EncodeCheckBox() { if (utf8EncodeCheckBox == null) { utf8EncodeCheckBox = new JCheckBox(); utf8EncodeCheckBox.setName("utf8EncodeCheckBox"); //$NON-NLS-1$ utf8EncodeCheckBox.setText(Messages.INSTANCE.getExportTableUseUtf8Option()); } return utf8EncodeCheckBox; } }