<commit_before>/***
 * Copyright (c) 2008, <NAME>.
 * All rights reserved.
 *
 * The OBDA-API is licensed under the terms of the Lesser General Public
 * License v.3 (see OBDAAPI_LICENSE.txt for details). The components of this
 * work include:
 * 
 * a) The OBDA-API developed by the author and licensed under the LGPL; and, 
 * b) third-party components licensed under terms that may be different from 
 *   those of the LGPL.  Information about such licenses can be found in the 
 *   file named OBDAAPI_3DPARTY-LICENSES.txt.
 */
package it.unibz.krdb.obda.gui.swing.panel;

import it.unibz.krdb.obda.gui.swing.IconLoader;
import it.unibz.krdb.obda.gui.swing.OBDADataQueryAction;
import it.unibz.krdb.obda.gui.swing.OBDASaveQueryResultToFileAction;
import it.unibz.krdb.obda.gui.swing.treemodel.IncrementalResultSetTableModel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.ToolTipManager;
import javax.swing.table.TableModel;

public class ResultViewTablePanel extends javax.swing.JPanel {

	private static final long serialVersionUID = -8494558136315031084L;

	private OBDADataQueryAction countAllTuplesAction;
	private OBDADataQueryAction countAllTuplesActionEQL;
	private QueryInterfacePanel querypanel;
	private OBDASaveQueryResultToFileAction saveToFileAction;
	
	/** 
	 * Creates new form ResultViewTablePanel 
	 */
	public ResultViewTablePanel(QueryInterfacePanel panel) {
		querypanel = panel;
		initComponents();
		addPopUpMenu();
	}

	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        scrQueryResult = new javax.swing.JScrollPane();
        tblQueryResult = new javax.swing.JTable();
        pnlCommandButton = new javax.swing.JPanel();
        pnlComment = new javax.swing.JPanel();
        lblHint = new javax.swing.JLabel();
        lblComment = new javax.swing.JLabel();
        cmdExportResult = new javax.swing.JButton();

        setMinimumSize(new java.awt.Dimension(400, 480));
        setLayout(new java.awt.BorderLayout(0, 5));

        tblQueryResult.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Results"
            }
        ));
        scrQueryResult.setViewportView(tblQueryResult);

        add(scrQueryResult, java.awt.BorderLayout.CENTER);

        pnlCommandButton.setMinimumSize(new java.awt.Dimension(500, 32));
        pnlCommandButton.setPreferredSize(new java.awt.Dimension(500, 32));
        pnlCommandButton.setLayout(new java.awt.BorderLayout(0, 5));

        pnlComment.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT, 7, 5));

        lblHint.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblHint.setText("Hint:");
        pnlComment.add(lblHint);

        lblComment.setText("--");
        pnlComment.add(lblComment);

        pnlCommandButton.add(pnlComment, java.awt.BorderLayout.CENTER);

        cmdExportResult.setIcon(IconLoader.getImageIcon("images/export.png"));
        cmdExportResult.setText("Export to CSV...");
        cmdExportResult.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        cmdExportResult.setContentAreaFilled(false);
        cmdExportResult.setFocusable(false);
        cmdExportResult.setIconTextGap(5);
        cmdExportResult.setMaximumSize(new java.awt.Dimension(125, 25));
        cmdExportResult.setMinimumSize(new java.awt.Dimension(125, 25));
        cmdExportResult.setPreferredSize(new java.awt.Dimension(125, 20));
        cmdExportResult.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdExportResultActionPerformed(evt);
            }
        });
        pnlCommandButton.add(cmdExportResult, java.awt.BorderLayout.EAST);

        add(pnlCommandButton, java.awt.BorderLayout.SOUTH);
    }// </editor-fold>//GEN-END:initComponents


	private void cmdExportResultActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_buttonSaveResultsActionPerformed
		JFileChooser fileChooser = new JFileChooser(System.getProperty("user.dir"));
		fileChooser.setDialogTitle("Export to...");
		int response = fileChooser.showSaveDialog(this);
		if (response == JFileChooser.APPROVE_OPTION) {
			File targetFile = fileChooser.getSelectedFile();
			final String fileLocation = targetFile.getPath();
			if (canWrite(targetFile)) {
				Thread thread = new Thread() {
					public void run() {						
						getOBDASaveQueryToFileAction().run(fileLocation);
					}
				};
				thread.start();
			}
		}
	}// GEN-LAST:event_buttonSaveResultsActionPerformed

	/**
	 * A utility method to check if the result should be written to the target file. 
	 * Return true if the target file doesn't exist yet or the user allows overwriting.
	 */
	private boolean canWrite(File outputFile) {
		boolean fileIsValid = false;
		if (outputFile.exists()) {
			int result = JOptionPane.showConfirmDialog(
					this, "File exists, overwrite?",
					"Warning", JOptionPane.YES_NO_CANCEL_OPTION);
			switch (result) {
			case JOptionPane.YES_OPTION:
				fileIsValid = true;
				break;
			default:
				fileIsValid = false;
			}
		} else {
			fileIsValid = true;
		}
		return fileIsValid;
	}
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cmdExportResult;
    private javax.swing.JLabel lblComment;
    private javax.swing.JLabel lblHint;
    private javax.swing.JPanel pnlCommandButton;
    private javax.swing.JPanel pnlComment;
    private javax.swing.JScrollPane scrQueryResult;
    private javax.swing.JTable tblQueryResult;
    // End of variables declaration//GEN-END:variables
	
	public void setTableModel(final TableModel newmodel) {
		Runnable updateModel = new Runnable() {
			@Override
			public void run() {
				tblQueryResult.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
				ToolTipManager.sharedInstance().unregisterComponent(tblQueryResult);
				ToolTipManager.sharedInstance().unregisterComponent(tblQueryResult.getTableHeader());

				TableModel oldmodel = tblQueryResult.getModel();
				if (oldmodel != null) {
					oldmodel.removeTableModelListener(tblQueryResult);
					if (oldmodel instanceof IncrementalResultSetTableModel) {
						IncrementalResultSetTableModel incm = (IncrementalResultSetTableModel) oldmodel;
						incm.close();
					}
				}
				tblQueryResult.setModel(newmodel);

				addNotify();

				tblQueryResult.invalidate();
				tblQueryResult.repaint();
			}
		};
		SwingUtilities.invokeLater(updateModel);
		
		// Write a hint in the comment panel for user information
		writeHintMessage();
	}
	
	private void writeHintMessage() {
		String msg = "--";
		if (querypanel.isFetchAllSelect()) {
			msg = "Continue scrolling down to retrieve more results.";
		}
		lblComment.setText(msg);
	}<commit_msg>Minor revision to the message string.<commit_after>/***
 * Copyright (c) 2008, <NAME>.
 * All rights reserved.
 *
 * The OBDA-API is licensed under the terms of the Lesser General Public
 * License v.3 (see OBDAAPI_LICENSE.txt for details). The components of this
 * work include:
 * 
 * a) The OBDA-API developed by the author and licensed under the LGPL; and, 
 * b) third-party components licensed under terms that may be different from 
 *   those of the LGPL.  Information about such licenses can be found in the 
 *   file named OBDAAPI_3DPARTY-LICENSES.txt.
 */
package it.unibz.krdb.obda.gui.swing.panel;

import it.unibz.krdb.obda.gui.swing.IconLoader;
import it.unibz.krdb.obda.gui.swing.OBDADataQueryAction;
import it.unibz.krdb.obda.gui.swing.OBDASaveQueryResultToFileAction;
import it.unibz.krdb.obda.gui.swing.treemodel.IncrementalResultSetTableModel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.ToolTipManager;
import javax.swing.table.TableModel;

public class ResultViewTablePanel extends javax.swing.JPanel {

	private static final long serialVersionUID = -8494558136315031084L;

	private OBDADataQueryAction countAllTuplesAction;
	private OBDADataQueryAction countAllTuplesActionEQL;
	private QueryInterfacePanel querypanel;
	private OBDASaveQueryResultToFileAction saveToFileAction;
	
	/** 
	 * Creates new form ResultViewTablePanel 
	 */
	public ResultViewTablePanel(QueryInterfacePanel panel) {
		querypanel = panel;
		initComponents();
		addPopUpMenu();
	}

	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        scrQueryResult = new javax.swing.JScrollPane();
        tblQueryResult = new javax.swing.JTable();
        pnlCommandButton = new javax.swing.JPanel();
        pnlComment = new javax.swing.JPanel();
        lblHint = new javax.swing.JLabel();
        lblComment = new javax.swing.JLabel();
        cmdExportResult = new javax.swing.JButton();

        setMinimumSize(new java.awt.Dimension(400, 480));
        setLayout(new java.awt.BorderLayout(0, 5));

        tblQueryResult.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Results"
            }
        ));
        scrQueryResult.setViewportView(tblQueryResult);

        add(scrQueryResult, java.awt.BorderLayout.CENTER);

        pnlCommandButton.setMinimumSize(new java.awt.Dimension(500, 32));
        pnlCommandButton.setPreferredSize(new java.awt.Dimension(500, 32));
        pnlCommandButton.setLayout(new java.awt.BorderLayout(0, 5));

        pnlComment.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT, 7, 5));

        lblHint.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblHint.setText("Hint:");
        pnlComment.add(lblHint);

        lblComment.setText("--");
        pnlComment.add(lblComment);

        pnlCommandButton.add(pnlComment, java.awt.BorderLayout.CENTER);

        cmdExportResult.setIcon(IconLoader.getImageIcon("images/export.png"));
        cmdExportResult.setText("Export to CSV...");
        cmdExportResult.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        cmdExportResult.setContentAreaFilled(false);
        cmdExportResult.setFocusable(false);
        cmdExportResult.setIconTextGap(5);
        cmdExportResult.setMaximumSize(new java.awt.Dimension(125, 25));
        cmdExportResult.setMinimumSize(new java.awt.Dimension(125, 25));
        cmdExportResult.setPreferredSize(new java.awt.Dimension(125, 20));
        cmdExportResult.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdExportResultActionPerformed(evt);
            }
        });
        pnlCommandButton.add(cmdExportResult, java.awt.BorderLayout.EAST);

        add(pnlCommandButton, java.awt.BorderLayout.SOUTH);
    }// </editor-fold>//GEN-END:initComponents


	private void cmdExportResultActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_buttonSaveResultsActionPerformed
		JFileChooser fileChooser = new JFileChooser(System.getProperty("user.dir"));
		fileChooser.setDialogTitle("Export to...");
		int response = fileChooser.showSaveDialog(this);
		if (response == JFileChooser.APPROVE_OPTION) {
			File targetFile = fileChooser.getSelectedFile();
			final String fileLocation = targetFile.getPath();
			if (canWrite(targetFile)) {
				Thread thread = new Thread() {
					public void run() {						
						getOBDASaveQueryToFileAction().run(fileLocation);
					}
				};
				thread.start();
			}
		}
	}// GEN-LAST:event_buttonSaveResultsActionPerformed

	/**
	 * A utility method to check if the result should be written to the target file. 
	 * Return true if the target file doesn't exist yet or the user allows overwriting.
	 */
	private boolean canWrite(File outputFile) {
		boolean fileIsValid = false;
		if (outputFile.exists()) {
			int result = JOptionPane.showConfirmDialog(
					this, "File exists, overwrite?",
					"Warning", JOptionPane.YES_NO_CANCEL_OPTION);
			switch (result) {
			case JOptionPane.YES_OPTION:
				fileIsValid = true;
				break;
			default:
				fileIsValid = false;
			}
		} else {
			fileIsValid = true;
		}
		return fileIsValid;
	}
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cmdExportResult;
    private javax.swing.JLabel lblComment;
    private javax.swing.JLabel lblHint;
    private javax.swing.JPanel pnlCommandButton;
    private javax.swing.JPanel pnlComment;
    private javax.swing.JScrollPane scrQueryResult;
    private javax.swing.JTable tblQueryResult;
    // End of variables declaration//GEN-END:variables
	
	public void setTableModel(final TableModel newmodel) {
		Runnable updateModel = new Runnable() {
			@Override
			public void run() {
				tblQueryResult.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
				ToolTipManager.sharedInstance().unregisterComponent(tblQueryResult);
				ToolTipManager.sharedInstance().unregisterComponent(tblQueryResult.getTableHeader());

				TableModel oldmodel = tblQueryResult.getModel();
				if (oldmodel != null) {
					oldmodel.removeTableModelListener(tblQueryResult);
					if (oldmodel instanceof IncrementalResultSetTableModel) {
						IncrementalResultSetTableModel incm = (IncrementalResultSetTableModel) oldmodel;
						incm.close();
					}
				}
				tblQueryResult.setModel(newmodel);

				addNotify();

				tblQueryResult.invalidate();
				tblQueryResult.repaint();
			}
		};
		SwingUtilities.invokeLater(updateModel);
		
		// Write a hint in the comment panel for user information
		writeHintMessage();
	}
	
	private void writeHintMessage() {
		String msg = "--";
		if (querypanel.isFetchAllSelect()) {
			msg = "Try to continue scrolling down the table to retrieve more results.";
		}
		lblComment.setText(msg);
	}
{'fraction_non_alphanumeric': 0.10373902826733016, 'fraction_numerical': 0.009122952519178934, 'mean_word_length': 3.7179654385392893, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5111891', 'n_tokens_mistral': 4774, 'n_tokens_neox': 4289, 'n_words': 869}