/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package systole.view.preview;
import java.awt.BorderLayout;
import java.io.File;
import javax.swing.JFileChooser;
import org.jfree.chart.ChartPanel;
import systole.domain.signals.RawSignal;
import systole.domain.signals.Segment;
import systole.exceptions.ExceptionIO;
import systole.ioHandling.fileReaders.FileReaderTXT;
import systole.propertiesController.SettingsProperties;
import systole.view.charts.XYChart;
import systole.view.utils.ErrorDialog;
/**
*
* @author jmj
*/
public class PreviewController {
private File file = null;
private RawSignal rawSignal = null;
private JDialogPreview preview;
private FileReaderTXT reader;
private SettingsProperties properties;
private ChartPanel chart;
private boolean doNewAnalysis = false;
public PreviewController() {
super();
this.reader = new FileReaderTXT();
this.properties = new SettingsProperties();
}
private void generateRawSignal() {
if ((this.file != null) && (!this.file.getAbsolutePath().equals(this.preview.getjTxtFile().getText()))) {
this.rawSignal = null;
this.preview.getjTxtFile().setText("");
Segment segment = null;
try {
segment = this.reader.readFile(file);
if (segment != null) {
this.rawSignal = new RawSignal();
this.getRawSignal().setSegment(segment);
this.getRawSignal().setInverted(this.preview.getjChkInverted().isSelected());
this.preview.getjTxtFile().setText(this.file.getAbsolutePath());
} else {
ErrorDialog.showError(this.preview, "Archivo no válido");
}
} catch (ExceptionIO e) {
ErrorDialog.showError(this.preview, "Archivo no válido, " + e.getMessage());
}
}
}
private boolean open() {
JFileChooser chooser = new JFileChooser(this.properties.loadImportSignalPath());
if (chooser.showOpenDialog(this.preview) == JFileChooser.APPROVE_OPTION) {
this.file = chooser.getSelectedFile();
return true;
}
return false;
}
private void doPloit() {
if (this.getRawSignal() != null) {
this.preview.getjPnlCenter().removeAll();
this.preview.getjPnlCenter().setLayout(new BorderLayout());
Segment segment = (this.getRawSignal().isInverted() ? this.getRawSignal().getSegment().invert() : this.getRawSignal().getSegment());
XYChart ploter = new XYChart("Tren de pulsos", "Muestras", "Amplitud");
ploter.addSeries("pulsos", segment.toDoubleArray(), 1);
ploter.setShowLegend(false);
ploter.setShowTitle(false);
this.chart = ploter.plot();
this.preview.getjPnlCenter().add(this.chart, BorderLayout.CENTER);
} else {
this.preview.getjPnlCenter().removeAll();
}
this.preview.getjPnlCenter().revalidate();
}
public boolean plotFile() {
if (this.open()) {
this.generateRawSignal();
this.doPloit();
return true;
}
this.preview.getjPnlCenter().removeAll();
this.preview.getjPnlCenter().revalidate();
return false;
}
public void changeInvertedValue() {
if (this.getRawSignal() != null) {
if (this.getRawSignal().isInverted() != this.preview.getjChkInverted().isSelected()) {
this.getRawSignal().setInverted(this.preview.getjChkInverted().isSelected());
this.doPloit();
}
}
}
public void showForm(java.awt.Frame parent) {
this.preview = new JDialogPreview(parent, this);
this.preview.setLocationRelativeTo(parent);
if (!this.plotFile()) {
this.preview.dispose();
return;
}
this.preview.setVisible(true);
}
public void closeForm() {
this.preview.setVisible(false);
}
public boolean hasttoInitNewanalysis() {
return this.doNewAnalysis;
}
public void doNewAnalysis() {
this.preview.setVisible(false);
this.doNewAnalysis = true;
}
/**
* @return the rawSignal
*/
public RawSignal getRawSignal() {
return rawSignal;
}
/**
*
* @return file path
*/
public String getFilePath() {
return (this.file != null ? this.file.getAbsolutePath() : "");
}
}