/*
* Copyright (C) 2015 Vinu K.N
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.domainmath.gui.octave;
import com.google.common.collect.Maps;
import com.jediterm.pty.PtyMain;
import com.jediterm.terminal.TerminalStarter;
import com.jediterm.terminal.TtyConnector;
import com.jediterm.terminal.ui.JediTermWidget;
import com.jediterm.terminal.ui.TerminalSession;
import com.jediterm.terminal.ui.TerminalWidget;
import com.jediterm.terminal.ui.settings.DefaultSettingsProvider;
import com.pty4j.PtyProcess;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import java.nio.charset.Charset;
import java.util.Map;
import javax.swing.JFileChooser;
import org.domainmath.gui.Util.DomainMathFileFilter;
import org.domainmath.gui.workspace.AudioVarDialog;
/**
*
* @author Vinu K.N
*/
public class ExternalEngine extends javax.swing.JFrame {
private TerminalStarter terminalStarter;
private final JediTermWidget myTerminal;
private final String cmd;
private final TtyConnector createTtyConnector;
/**
* Creates new form NewJFrame
* @param cmd
*/
public ExternalEngine(String cmd) {
myTerminal = new JediTermWidget(new DefaultSettingsProvider());
this.cmd=cmd;
createTtyConnector = createTtyConnector();
myTerminal.setTtyConnector(createTtyConnector);
setIconImage(icon);
initComponents();
getContentPane().add("Center", myTerminal.getComponent());
pack();
openSession(myTerminal);
terminalStarter = new TerminalStarter(myTerminal.getCurrentSession().getTerminal(),createTtyConnector);
}
private void openSession(TerminalWidget terminal) {
if (terminal.canOpenSession()) {
openSession(terminal,createTtyConnector);
}
}
public void openSession(TerminalWidget terminal, TtyConnector ttyConnector) {
TerminalSession session = terminal.createTerminalSession(ttyConnector);
session.start();
}
public final TtyConnector createTtyConnector() {
try {
Map<String, String> envs = Maps.newHashMap(System.getenv());
envs.put("TERM", "xterm-256color");
String[] command = new String[]{cmd};
PtyProcess process = PtyProcess.exec(command, envs, null);
return new PtyMain.LoggingPtyProcessTtyConnector(process, Charset.forName("UTF-8"));
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
/**
* 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() {
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
importItem = new javax.swing.JMenuItem();
exportItem = new javax.swing.JMenuItem();
jSeparator1 = new javax.swing.JPopupMenu.Separator();
savePlotItem = new javax.swing.JMenuItem();
jSeparator2 = new javax.swing.JPopupMenu.Separator();
exitItem = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosed(java.awt.event.WindowEvent evt) {
formWindowClosed(evt);
}
});
jMenu1.setText("File");
importItem.setText("Import Workspace");
importItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
importItemActionPerformed(evt);
}
});
jMenu1.add(importItem);
exportItem.setText("Export Workspace");
exportItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
exportItemActionPerformed(evt);
}
});
jMenu1.add(exportItem);
jMenu1.add(jSeparator1);
savePlotItem.setText("Save Plot...");
savePlotItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
savePlotItemActionPerformed(evt);
}
});
jMenu1.add(savePlotItem);
jMenu1.add(jSeparator2);
exitItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.ALT_MASK));
exitItem.setText("Exit");
exitItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
exitItemActionPerformed(evt);
}
});
jMenu1.add(exitItem);
jMenuBar1.add(jMenu1);
setJMenuBar(jMenuBar1);
pack();
}// </editor-fold>//GEN-END:initComponents
public void execute(String s){
terminalStarter.sendString(s+"\n");
myTerminal.getCurrentSession().getTerminal().setTerminalOutput(terminalStarter);
}
private void exit(){
terminalStarter.sendString("exit"+"\n");
terminalStarter.close();
dispose();
}
private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
exit();
}//GEN-LAST:event_formWindowClosed
private void savePlotItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_savePlotItemActionPerformed
saveplot();
}//GEN-LAST:event_savePlotItemActionPerformed
private void exitItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitItemActionPerformed
exit();
}//GEN-LAST:event_exitItemActionPerformed
private void importItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_importItemActionPerformed
importData();
}//GEN-LAST:event_importItemActionPerformed
public void saveplot() {
JFileChooser fc = new JFileChooser();
fc.setCurrentDirectory(new File(System.getProperty("user.dir")+File.separator+"works"));
fc.setFileFilter(DomainMathFileFilter.SAVE_PLOT_FILE_FILTER);
fc.setAcceptAllFileFilterUsed(false);
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
fc.setMultiSelectionEnabled(false);
File file_plot;
String name;
int returnVal = fc.showSaveDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
System.err.println(fc.getFileFilter().getDescription());
file_plot = fc.getSelectedFile();
name =file_plot.getName();
execute("saveas(1,"+"'"+file_plot.getAbsolutePath()+"');");
}
}
private void exportItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exportItemActionPerformed
exportData();
}//GEN-LAST:event_exportItemActionPerformed
public void importData(){
JFileChooser fc = new JFileChooser();
fc.setCurrentDirectory(new File(System.getProperty("user.dir")+File.separator+"works"));
fc.setAcceptAllFileFilterUsed(false);
fc.setFileFilter(DomainMathFileFilter.ZIP_FILE_FILTER);
fc.setFileFilter(DomainMathFileFilter.FHDF5_FILE_FILTER);
fc.setFileFilter(DomainMathFileFilter.HDF5_FILE_FILTER);
fc.setFileFilter(DomainMathFileFilter.FLOAT_BINARY_FILE_FILTER);
fc.setFileFilter(DomainMathFileFilter.BINARY_FILE_FILTER);
fc.setFileFilter(DomainMathFileFilter.IMAGES_FILE_FILTER);
fc.setFileFilter(DomainMathFileFilter.DCM_FILE_FILTER);
fc.setFileFilter(DomainMathFileFilter.ASCII_FILE_FILTER);
fc.setFileFilter(DomainMathFileFilter.CSV_FILE_FILTER);
fc.setFileFilter(DomainMathFileFilter.AUDIO_FILE_FILTER);
fc.setFileFilter(DomainMathFileFilter.FIS_FILE_FILTER);
fc.setFileFilter(DomainMathFileFilter.MATLAB_FILE_FILTER);
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
fc.setMultiSelectionEnabled(false);
File file;
String name;
String ext;
int returnVal = fc.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
System.err.println(fc.getFileFilter().getDescription());
file = fc.getSelectedFile();
name =file.getName();
if(name.endsWith(".mat")) {
load(file,"-mat");
}else if(name.endsWith(".hdf5")) {
load(file,"-hdf5");
}else if(name.endsWith(".fhdf5")) {
load(file,"-float-hdf5");
}else if(name.endsWith(".txt")) {
load(file,"-ascii");
}else if(name.endsWith(".bin")) {
load(file,"-binary");
}else if(name.endsWith(".fbin")) {
load(file,"-float-binary");
}else if(name.endsWith(".zip")) {
load(file,"-zip");
}else if(name.endsWith(".fis")) {
loadPackage("fuzzy-logic-toolkit");
execute("readfis('"+file.getAbsolutePath()+"');");
}else if(name.endsWith(".csv")) {
loadCSV(file);
}
else if(name.endsWith(".bmp") ||
name.endsWith(".gif") ||
name.endsWith(".jpg") ||
name.endsWith(".jpeg") ||
name.endsWith(".pbm") ||
name.endsWith(".pcx") ||
name.endsWith(".pgm") ||
name.endsWith(".png") ||
name.endsWith(".pnm") ||
name.endsWith(".ppm") ||
name.endsWith(".ras") ||
name.endsWith(".tif") ||
name.endsWith(".tiff") ||
name.endsWith(".xwd") ) {
loadImage(file.getAbsolutePath());
}
// "lin", "raw", "au", "mu", "snd", "wav","riff"
else if(name.endsWith(".lin")) {
loadAudio1(file.getParent()+File.separator+name.substring(0, name.indexOf(".")),"lin");
}else if(name.endsWith(".raw")) {
loadAudio1(file.getParent()+File.separator+name.substring(0, name.indexOf(".")),"raw");
}else if(name.endsWith(".au")) {
loadAudio1(file.getParent()+File.separator+name.substring(0, name.indexOf(".")),"au");
}else if(name.endsWith(".mu")) {
loadAudio1(file.getParent()+File.separator+name.substring(0, name.indexOf(".")),"mu");
}
else if(name.endsWith(".snd")) {
loadAudio1(file.getAbsolutePath(),"snd");
}
else if(name.endsWith(".dcm")) {
loadDCM(file.getAbsolutePath());
}
else if(name.endsWith(".wav") ||
name.endsWith(".riff"))
{
loadAudio2(file.getAbsolutePath());
}
}
}
public void loadPackage(String pkg) {
execute("pkg load "+pkg);
}
public void loadCSV(File file) {
execute("csvread('"+file.getAbsolutePath()+"');");
}
public void loadDCM(String path) {
loadPackage("dicom");
execute("im=dicomread('"+path+"');");
}
public void loadAudio1(String filename,String ext) {
String txt ="loadaudio('"+filename+"','"+ext+"',8)";
execute(txt);
}
public void loadAudio2(String filename) {
String txt ="waveread('"+filename+"');";
AudioVarDialog audioVarDialog = new AudioVarDialog(this,true,txt);
audioVarDialog.setLocationRelativeTo(null);
audioVarDialog.setVisible(true);
}
public void loadImage(String filename) {
String txt ="[image,map,Ra]=imread('"+filename+"');";
execute(txt);
}
public void load(File file,String opt) {
execute("load('"+opt+"','"+file.getAbsolutePath()+"');");
System.out.println(file.getAbsolutePath());
}
private void saveAll(File file,String opt) {
execute("save('"+opt+"','"+file.getAbsolutePath()+"');");
System.out.println(file.getAbsolutePath());
}
public void exportData(){
JFileChooser fc = new JFileChooser();
fc.setCurrentDirectory(new File(System.getProperty("user.dir")+File.separator+"works"));
fc.setAcceptAllFileFilterUsed(false);
fc.setFileFilter(DomainMathFileFilter.ZIP_FILE_FILTER);
fc.setFileFilter(DomainMathFileFilter.FHDF5_FILE_FILTER);
fc.setFileFilter(DomainMathFileFilter.HDF5_FILE_FILTER);
fc.setFileFilter(DomainMathFileFilter.FLOAT_BINARY_FILE_FILTER);
fc.setFileFilter(DomainMathFileFilter.BINARY_FILE_FILTER);
fc.setFileFilter(DomainMathFileFilter.ASCII_FILE_FILTER);
fc.setFileFilter(DomainMathFileFilter.MATLAB_FILE_FILTER);
//fc.setFileFilter(this.matlabFileFilter);
// fc.setFileFilter(filter2);
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
fc.setMultiSelectionEnabled(false);
File file;
String name;
int returnVal = fc.showSaveDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
System.err.println(fc.getFileFilter().getDescription());
file = fc.getSelectedFile();
name =file.getName();
if(name.endsWith(".mat")) {
saveAll(file,"-mat");
}else if(name.endsWith(".hdf5")) {
saveAll(file,"-hdf5");
}else if(name.endsWith(".fhdf5")) {
saveAll(file,"-float-hdf5");
}else if(name.endsWith(".txt")) {
saveAll(file,"-ascii");
}else if(name.endsWith(".bin")) {
saveAll(file,"-binary");
}else if(name.endsWith(".fbin")) {
saveAll(file,"-float-binary");
}else if(name.endsWith(".zip")) {
saveAll(file,"-zip");
}
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(() -> {
new ExternalEngine("cmd").setVisible(true);
});
}
public java.net.URL imgURL = getClass().getResource("/org/domainmath/gui/resources/DomainMath.png");
public Image icon = Toolkit.getDefaultToolkit().getImage(imgURL);
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JMenuItem exitItem;
private javax.swing.JMenuItem exportItem;
private javax.swing.JMenuItem importItem;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JPopupMenu.Separator jSeparator1;
private javax.swing.JPopupMenu.Separator jSeparator2;
private javax.swing.JMenuItem savePlotItem;
// End of variables declaration//GEN-END:variables
}