package org.autoplot.datasource.jython;
import java.awt.Dimension;
import java.awt.Window;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.text.BadLocationException;
import org.das2.jythoncompletion.nbadapt.Utilities;
import org.das2.util.filesystem.FileSystem;
import org.das2.util.monitor.NullProgressMonitor;
import org.das2.util.monitor.ProgressMonitor;
import org.python.core.PyException;
import org.autoplot.datasource.DataSetURI;
import org.autoplot.datasource.DataSourceEditorPanel;
import org.autoplot.datasource.FileSystemUtil;
import org.autoplot.datasource.LogNames;
import org.autoplot.datasource.URISplit;
import org.autoplot.jythonsupport.JythonUtil;
import org.autoplot.jythonsupport.JythonUtil.ScriptDescriptor;
import org.autoplot.jythonsupport.ui.EditorAnnotationsSupport;
import org.autoplot.jythonsupport.ui.ParametersFormPanel;
import org.autoplot.jythonsupport.ui.ParametersFormPanel.FormData;
import org.autoplot.jythonsupport.ui.ScriptPanelSupport;
import org.das2.util.FileUtil;
/**
* Editor for vap+jyds uris. These URIs offer a couple of challenges,
* such as the mode where the resourceURI is the name of the data file and
* the script argument is used. Also, we identify types and offer decent
* GUI elements to control the types.
* @author jbf
*/
public class JythonEditorPanel extends javax.swing.JPanel implements DataSourceEditorPanel {
private static final Logger logger= Logger.getLogger( LogNames.APDSS_JYDS );
ScriptPanelSupport support;
String suri;
File file;
URI resourceUri;
boolean hasVariables= false;
ParametersFormPanel parametersFormPanel;
/** Creates new form JythonEditorPanel */
public JythonEditorPanel() {
initComponents();
tearoffTabbedPane1.hideMouseAdapter();
support= new ScriptPanelSupport(textArea);
support.addCaretLabel(caretPositionLabel);
support.addFileLabel(fileNameLabel);
support.setReadOnly();
scriptScrollPane.getVerticalScrollBar().setUnitIncrement(scriptScrollPane.getFont().getSize());
paramsScrollPane.getVerticalScrollBar().setUnitIncrement(scriptScrollPane.getFont().getSize());
textArea.addMouseListener( new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
try {
int offs= textArea.viewToModel(e.getPoint());
int[] ii= Utilities.getIdentifierBlock( textArea, offs ) ;
if ( ii!=null ) {
String id= textArea.getDocument().getText(ii[0],ii[1]-ii[0]);
int idx= lookupResultVariableIndex(id);
if ( idx!=-1 ) variableComboBox.setSelectedIndex( idx );
}
} catch (BadLocationException ex) {
logger.log(Level.SEVERE, ex.getMessage(), ex);
}
}
});
}
/** 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")
// //GEN-BEGIN:initComponents
private void initComponents() {
variableComboBox = new javax.swing.JComboBox();
jLabel1 = new javax.swing.JLabel();
tearoffTabbedPane1 = new org.das2.components.TearoffTabbedPane();
scriptPanel = new javax.swing.JPanel();
caretPositionLabel = new javax.swing.JLabel();
fileNameLabel = new javax.swing.JLabel();
scriptScrollPane = new javax.swing.JScrollPane();
jPanel1 = new javax.swing.JPanel();
textArea = new org.autoplot.jythonsupport.ui.EditorTextPane();
paramsScrollPane = new javax.swing.JScrollPane();
paramsPanel = new javax.swing.JPanel();
jLabel2 = new javax.swing.JLabel();
setName("jythonDataSourceEditorPanel"); // NOI18N
variableComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "(running script)" }));
jLabel1.setText("Select variable:");
jLabel1.setToolTipText("The dataset pointed to by the URI");
caretPositionLabel.setText("1,1");
fileNameLabel.setMinimumSize(new java.awt.Dimension(200, 16));
jPanel1.setLayout(new java.awt.BorderLayout());
jPanel1.add(textArea, java.awt.BorderLayout.CENTER);
scriptScrollPane.setViewportView(jPanel1);
org.jdesktop.layout.GroupLayout scriptPanelLayout = new org.jdesktop.layout.GroupLayout(scriptPanel);
scriptPanel.setLayout(scriptPanelLayout);
scriptPanelLayout.setHorizontalGroup(
scriptPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(org.jdesktop.layout.GroupLayout.TRAILING, scriptPanelLayout.createSequentialGroup()
.add(fileNameLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(caretPositionLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 56, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(scriptScrollPane)
);
scriptPanelLayout.setVerticalGroup(
scriptPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(org.jdesktop.layout.GroupLayout.TRAILING, scriptPanelLayout.createSequentialGroup()
.add(scriptScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 208, Short.MAX_VALUE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(scriptPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
.add(fileNameLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 20, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(caretPositionLabel)))
);
tearoffTabbedPane1.addTab("script", scriptPanel);
paramsPanel.setLayout(new javax.swing.BoxLayout(paramsPanel, javax.swing.BoxLayout.Y_AXIS));
paramsScrollPane.setViewportView(paramsPanel);
tearoffTabbedPane1.addTab("params", paramsScrollPane);
jLabel2.setText("Select from the variables calculated by the script, 'data' or 'result' is used by default:");
jLabel2.setMinimumSize(new java.awt.Dimension(200, 17));
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(jLabel1)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(variableComboBox, 0, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.add(tearoffTabbedPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
.add(jLabel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
.add(tearoffTabbedPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(8, 8, 8)
.add(jLabel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(variableComboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(jLabel1)))
);
}// //GEN-END:initComponents
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel caretPositionLabel;
protected javax.swing.JLabel fileNameLabel;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel paramsPanel;
private javax.swing.JScrollPane paramsScrollPane;
private javax.swing.JPanel scriptPanel;
private javax.swing.JScrollPane scriptScrollPane;
private org.das2.components.TearoffTabbedPane tearoffTabbedPane1;
private org.autoplot.jythonsupport.ui.EditorTextPane textArea;
private javax.swing.JComboBox variableComboBox;
// End of variables declaration//GEN-END:variables
@Override
public JPanel getPanel() {
return this;
}
/**
* TODO: params is object?
* @param parms
* @return
*/
private boolean isBoolean( List