ActionSaveTab.java

/*******************************************************************************
 * Copyhacked (H) 2012-2020.
 * This program and the accompanying materials
 * are made available under no term at all, use it like
 * you want, but share and discuss about it
 * every time possible with every body.
 * 
 * Contributors:
 *      ron190 at ymail dot com - initial implementation
 ******************************************************************************/
package com.jsql.view.swing.action;

import com.jsql.util.LogLevelUtil;
import com.jsql.view.swing.dialog.ReplaceFileChooser;
import com.jsql.view.swing.scrollpane.LightScrollPane;
import com.jsql.view.swing.table.PanelTable;
import com.jsql.view.swing.util.MediatorHelper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.swing.*;
import javax.swing.text.JTextComponent;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * Save the content of tab in a file.
 */
public class ActionSaveTab extends AbstractAction {
    
    /**
     * Log4j logger sent to view.
     */
    private static final Logger LOGGER = LogManager.getRootLogger();
    
    private final ReplaceFileChooser replaceFileChooser = new ReplaceFileChooser(
        MediatorHelper.model().getMediatorUtils().getPreferencesUtil().getPathFile()
    );

    public ActionSaveTab() {
        
        this.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_DOWN_MASK));
        this.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_S);
        this.putValue(Action.NAME, "Save Tab As...");
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        
        this.replaceFileChooser.setDialogTitle("Save Tab As");

        var componentResult = MediatorHelper.tabResults().getSelectedComponent();
        
        if (componentResult instanceof PanelTable) {
            
            JTable table = ((PanelTable) componentResult).getTableValues();
            this.saveToFile(table);
            
        } else if (
            componentResult instanceof LightScrollPane
            && ((LightScrollPane) componentResult).scrollPane.getViewport().getView() instanceof JTextComponent
        ) {
            
            JTextComponent textarea = (JTextComponent) ((LightScrollPane) componentResult).scrollPane.getViewport().getView();
            this.saveToFile(textarea);
        }
    }
    
    private void saveToFile(JComponent textarea) {
        
        if (textarea == null) {
            return;
        }
        
        int stateSave = this.replaceFileChooser.showSaveDialog(MediatorHelper.frame());
        
        if (stateSave == JFileChooser.APPROVE_OPTION) {
            
            var folderSelectedFile = this.replaceFileChooser.getCurrentDirectory().toString();
            MediatorHelper.model().getMediatorUtils().getPreferencesUtil().set(folderSelectedFile);
            
            if (textarea instanceof JTextComponent) {
                this.saveTextToFile((JTextComponent) textarea);
            } else if (textarea instanceof JTable) {
                this.saveTableToFile((JTable) textarea);
            }
        }
    }

    private void saveTableToFile(JTable tableResults) {
        
        var fileSelected = this.replaceFileChooser.getSelectedFile();
        
        try (var fileWriterExcel = new FileWriter(fileSelected, StandardCharsets.UTF_8)) {
            
            var tableModel = tableResults.getModel();
            
            for (var i = 2 ; i < tableModel.getColumnCount() ; i++) {
                fileWriterExcel.write(tableModel.getColumnName(i) + "\t");
            }
            
            fileWriterExcel.write("\n");
            
            for (var i = 0 ; i < tableModel.getRowCount() ; i++) {
                
                for (var j = 2 ; j < tableModel.getColumnCount() ; j++) {
                    
                    // Cell empty when string was too long to be injected (columnTooLong|cellEmpty|cellEmpty).
                    if (tableModel.getValueAt(i, j) == null) {
                        fileWriterExcel.write("\t");
                    } else {
                        
                        // Encode line break.
                        var line = tableModel.getValueAt(i, j).toString();
                        line = line
                            .replace("\n", "\\n")
                            .replace("\t", "\\t");
                        line = line + "\t";
                        fileWriterExcel.write(line);
                    }
                }
                
                fileWriterExcel.write("\n");
            }
        } catch (IOException e) {
            LOGGER.log(
                LogLevelUtil.CONSOLE_ERROR,
                String.format("Error writing to %s", fileSelected.getName()),
                e
            );
        }
    }

    private void saveTextToFile(JTextComponent textarea) {
        
        var fileSelected = this.replaceFileChooser.getSelectedFile();
        
        try (
            var fileWriter = new FileWriter(fileSelected, StandardCharsets.UTF_8);
            var fileOut = new BufferedWriter(fileWriter)
        ) {
            textarea.write(fileOut);
        } catch (IOException e) {
            LOGGER.log(
                LogLevelUtil.CONSOLE_ERROR,
                String.format("Error writing to %s", fileSelected.getName()),
                e
            );
        }
    }
}