/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.metrapp.visual;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
import org.metrapp.dataModel.DataFrame;
import org.metrapp.dataModel.StringDataFrame;
import org.metrapp.io.Loader;
import org.metrapp.sys.Controller;
import rcaller.RCaller;
import rcaller.RCode;
/**
*
* @author Ilija
*/
public class DataEntry extends javax.swing.JPanel {
String filePath = "";
/**
* Creates new form DataEntry
*/
public DataEntry() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
browseButton = new javax.swing.JButton();
jLabel2 = new javax.swing.JLabel();
selectedFilePath = new javax.swing.JTextField();
loadDataButton = new javax.swing.JButton();
jButtonOnlySetPath = new javax.swing.JButton();
jLabel1.setText("Chose file...");
browseButton.setText("Browse");
browseButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
browseButtonActionPerformed(evt);
}
});
jLabel2.setText("Selected file");
selectedFilePath.setText("No file selected...");
selectedFilePath.setEnabled(false);
loadDataButton.setText("Load data");
loadDataButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
loadDataButtonActionPerformed(evt);
}
});
jButtonOnlySetPath.setText("Only set path");
jButtonOnlySetPath.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonOnlySetPathActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(27, 27, 27)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 145, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jButtonOnlySetPath)
.addGap(19, 19, 19)))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(selectedFilePath, javax.swing.GroupLayout.DEFAULT_SIZE, 214, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(loadDataButton, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(browseButton))
.addGap(0, 0, Short.MAX_VALUE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(34, 34, 34)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(browseButton))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(selectedFilePath, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(loadDataButton)
.addComponent(jButtonOnlySetPath))
.addContainerGap(176, Short.MAX_VALUE))
);
}// </editor-fold>//GEN-END:initComponents
private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseButtonActionPerformed
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(".csv files", "csv");
chooser.setFileFilter(filter);
int returnVal = chooser.showDialog(null, "Choose file");
if (returnVal == JFileChooser.APPROVE_OPTION) {
String path = chooser.getSelectedFile().getName();
System.out.println("You chose to open this file: " + path);
if (!path.equals("")) {
selectedFilePath.setText(chooser.getSelectedFile().getAbsolutePath().replace("\\", "/"));
} else if (path == null || path.equals("")) {
JOptionPane.showMessageDialog(this, "Please provide path to your file.", "Error", JOptionPane.ERROR_MESSAGE);
}
}
}//GEN-LAST:event_browseButtonActionPerformed
private void loadDataButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loadDataButtonActionPerformed
if (selectedFilePath.getText() == null || selectedFilePath.getText().isEmpty() || selectedFilePath.getText().equals("") || selectedFilePath.getText().equals("No file selected...")) {
JOptionPane.showMessageDialog(this, "Please provide path to your file.", "Error", JOptionPane.ERROR_MESSAGE);
} else {
RCaller caller = new RCaller();
RCode code = new RCode();
Controller.getInstance().initializeBasic(caller, code);
//old
// Loader loader = new Loader();
// loader.loadFile(selectedFilePath.getText(), caller, code);
Controller.getInstance().setPath(selectedFilePath.getText());
String rCodeForOpeningFile = "dataset2 = read.csv(\"" + selectedFilePath.getText() + "\", header=TRUE, sep=\",\")";
Controller.getInstance().addRCode(rCodeForOpeningFile + "\n");
Controller.getInstance().executeRCode(1, "", "");
// File xmlDocument = caller.getParser().getXMLFile();
//
// DataFrame df = new StringDataFrame();
// df.createDataFrameAndFillItWithData(xmlDocument);
// Controller.getInstance().updateDataFrame(df);
//
// JOptionPane.showMessageDialog(this, "File is loaded.");
}
}//GEN-LAST:event_loadDataButtonActionPerformed
private void jButtonOnlySetPathActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOnlySetPathActionPerformed
if (selectedFilePath.getText() == null || selectedFilePath.getText().isEmpty() || selectedFilePath.getText().equals("") || selectedFilePath.getText().equals("No file selected...")) {
JOptionPane.showMessageDialog(this, "Please provide path to your file.", "Error", JOptionPane.ERROR_MESSAGE);
} else {
Controller.getInstance().setPath(selectedFilePath.getText());
JOptionPane.showMessageDialog(this, "Your path is set.", "Information", JOptionPane.INFORMATION_MESSAGE);
String rCodeForOpeningFile = "dataset2 = read.csv(\"" + selectedFilePath.getText() + "\", header=TRUE, sep=\",\")";
Controller.getInstance().addRCode(rCodeForOpeningFile + "\n");
}
}//GEN-LAST:event_jButtonOnlySetPathActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton browseButton;
private javax.swing.JButton jButtonOnlySetPath;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JButton loadDataButton;
private javax.swing.JTextField selectedFilePath;
// End of variables declaration//GEN-END:variables
public String returnPath() {
return filePath;
}
}