package org.autoplot; import java.awt.Color; import java.awt.Component; import java.awt.Dimension; import java.awt.Toolkit; import java.awt.Window; import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.UnsupportedFlavorException; import java.awt.event.ActionEvent; import java.awt.event.ItemEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.lang.reflect.InvocationTargetException; import java.net.URI; import java.net.URISyntaxException; import java.text.ParseException; import java.util.ArrayList; import java.util.Arrays; 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 java.util.prefs.Preferences; import java.util.regex.Pattern; import javax.swing.AbstractAction; import javax.swing.BorderFactory; import javax.swing.BoxLayout; import javax.swing.ComboBoxModel; import javax.swing.DefaultComboBoxModel; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JComboBox; import javax.swing.JDialog; import javax.swing.JFileChooser; import javax.swing.JLabel; import javax.swing.JMenuBar; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.SwingUtilities; import javax.swing.border.Border; import javax.swing.filechooser.FileNameExtensionFilter; import org.autoplot.bookmarks.Bookmark; import org.autoplot.datasource.DataSetSelector; import org.autoplot.jythonsupport.JythonRefactory; import org.das2.components.DasProgressPanel; import org.das2.datum.DatumRange; import org.das2.datum.DatumRangeUtil; import org.das2.util.LoggerManager; import org.das2.util.monitor.NullProgressMonitor; import org.das2.util.monitor.ProgressMonitor; import org.python.util.InteractiveInterpreter; import org.autoplot.dom.Application; import org.autoplot.datasource.DataSetURI; import org.autoplot.datasource.URISplit; import org.autoplot.jythonsupport.JythonUtil.Param; import org.autoplot.jythonsupport.ui.ParametersFormPanel; import org.autoplot.jythonsupport.ui.Util; import org.das2.datum.Units; import org.das2.qds.DataSetUtil; import org.das2.qds.QDataSet; import org.das2.qds.ops.Ops; import org.das2.util.FileUtil; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; /** * Tool for running batches, generating inputs for jython scripts. * @see https://sourceforge.net/p/autoplot/feature-requests/545/ * @author jbf */ public class BatchMaster extends javax.swing.JPanel { private static final Logger logger= LoggerManager.getLogger("jython.batchmaster"); private Application dom; private Object state; private static final String STATE_READY= "ready"; private static final String STATE_LOADING= "loading"; private JSONObject results=null; private JSONObject resultsPending=null; private File resultsFile=null; private JLabel[] param1JLabels= null; public static final int HTML_LINE_LIMIT = 50; private ProgressMonitor monitor=null; // non-null when process is going. /** * Creates new form BatchMaster * @param dom */ public BatchMaster( final Application dom ) { initComponents(); generateButton1.setEnabled(false); generateButton2.setEnabled(false); generateMenuItem1.setEnabled(false); generateMenuItem2.setEnabled(false); this.dom= dom; this.state= STATE_READY; /** * register the browse trigger to the same action, because we always browse. */ dataSetSelector1.registerBrowseTrigger( "(.*)\\.jy(\\?.*)?", new AbstractAction( "Review Script" ) { @Override public void actionPerformed( ActionEvent ev ) { org.das2.util.LoggerManager.logGuiEvent(ev); state= STATE_LOADING; String s= dataSetSelector1.getValue(); Map args; try { URISplit split= URISplit.parse(s); //bug 1408--note runScript doesn't account for changes made to the GUI. args= URISplit.parseParams(split.params); Map env= new HashMap<>(); env.put( "dom", dom ); env.put( "PWD", split.path ); File scriptFile= DataSetURI.getFile(s,new NullProgressMonitor()); if ( JOptionPane.OK_OPTION==JythonUtil.showScriptDialog( BatchMaster.this, env, scriptFile, args, enabled, split.resourceUri ) ) { split.params= URISplit.formatParams(args); dataSetSelector1.setValue( URISplit.format( split ) ); } } catch ( IOException ex ) { throw new RuntimeException(ex); } finally { state= STATE_READY; } } }); dataSetSelector1.registerActionTrigger( "(.*)\\.jy(\\?.*)?", new AbstractAction( "Review Script" ) { @Override public void actionPerformed( ActionEvent ev ) { org.das2.util.LoggerManager.logGuiEvent(ev); doPlayButton(); } }); dataSetSelector1.setPromptText("Enter the name of a Jython script"); //dataSetSelector1.setRecent( Collections.singletonList() ); List recentUris= new ArrayList<>(20); recentUris.add( "http://autoplot.org/data/script/examples/parameters.jy" ); if ( dom.getController()!=null ) { // support testing. Pattern p= Pattern.compile(".*\\.jy(\\?.*)?"); Map recentJy= dom.getController().getApplicationModel().getRecent(p,20); recentJy.entrySet().forEach((recentItem) -> { recentUris.add( recentItem.getKey() ); }); } dataSetSelector1.setRecent( recentUris ); param1ScrollPane.getVerticalScrollBar().setUnitIncrement(param1ScrollPane.getFont().getSize()); param2ScrollPane.getVerticalScrollBar().setUnitIncrement(param2ScrollPane.getFont().getSize()); } /** * do the stuff to do when the play button is pressed. */ private void doPlayButton() { state= STATE_LOADING; try { String scriptName= dataSetSelector1.getValue(); URISplit split= URISplit.parse(scriptName); if ( !split.file.endsWith(".jy") ) { JOptionPane.showMessageDialog( BatchMaster.this, "script must end in .jy: "+scriptName ); return; } pwd= split.path; //Map params= URISplit.parseParams(split.params); //TODO: support these. Map env= new HashMap<>(); DasProgressPanel monitor= DasProgressPanel.createFramed( SwingUtilities.getWindowAncestor(BatchMaster.this), "download script"); File scriptFile= DataSetURI.getFile( split.file, monitor ); String script= readScript( scriptFile ); env.put( "dom", dom ); env.put( "PWD", split.path ); Map parms= Util.getParams( env, script, URISplit.parseParams(split.params), new NullProgressMonitor() ); String[] items= new String[parms.size()+1]; int i=0; items[0]=""; for ( Entry p: parms.entrySet() ) { items[i+1]= p.getKey(); i=i+1; } ComboBoxModel m1= new DefaultComboBoxModel(Arrays.copyOfRange(items,1,items.length)); param1NameCB.setModel(m1); generateButton1.setEnabled( items.length>1 ); generateMenuItem1.setEnabled( items.length>1 ); ComboBoxModel m2= new DefaultComboBoxModel(items); param2NameCB.setModel(m2); param1Values.setText(""); param2Values.setText(""); switchToEditableList(); messageLabel.setText("Load up those parameters and hit Go!"); param1ScrollPane.getViewport().setView(param1Values); } catch (IOException ex) { Logger.getLogger(BatchMaster.class.getName()).log(Level.SEVERE, null, ex); } finally { state= STATE_READY; } } /** * get the menu bar, which is typically added to the JDialog which will * contain this component. * * @return the menu bar. */ public JMenuBar getMenuBar() { return menuBar; } /** * 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() { bindingGroup = new org.jdesktop.beansbinding.BindingGroup(); jScrollPane2 = new javax.swing.JScrollPane(); jList2 = new javax.swing.JList<>(); jPopupMenu1 = new javax.swing.JPopupMenu(); generateMenuItem1 = new javax.swing.JMenuItem(); loadUriMenuItem = new javax.swing.JMenuItem(); loadFromFileMI = new javax.swing.JMenuItem(); pasteMenuItem = new javax.swing.JMenuItem(); timeRangesPanel = new javax.swing.JPanel(); jLabel2 = new javax.swing.JLabel(); timeRangeComboBox = new javax.swing.JComboBox<>(); timeFormatComboBox = new javax.swing.JComboBox<>(); jLabel3 = new javax.swing.JLabel(); jPopupMenu2 = new javax.swing.JPopupMenu(); generateMenuItem2 = new javax.swing.JMenuItem(); loadFromFileMI2 = new javax.swing.JMenuItem(); pasteMenuItem2 = new javax.swing.JMenuItem(); menuBar = new javax.swing.JMenuBar(); fileMenu = new javax.swing.JMenu(); OpenMenuItem = new javax.swing.JMenuItem(); SaveAsMenuItem = new javax.swing.JMenuItem(); exportResultsMenuItem = new javax.swing.JMenuItem(); helpMenu = new javax.swing.JMenu(); showHelpMenuItem = new javax.swing.JMenuItem(); jPanel1 = new javax.swing.JPanel(); goButton = new javax.swing.JButton(); param1ScrollPane = new javax.swing.JScrollPane(); param1Values = new javax.swing.JTextArea(); param2ScrollPane = new javax.swing.JScrollPane(); param2Values = new javax.swing.JTextArea(); dataSetSelector1 = new org.autoplot.datasource.DataSetSelector(); messageLabel = new javax.swing.JLabel(); jLabel1 = new javax.swing.JLabel(); param1NameCB = new javax.swing.JComboBox<>(); param2NameCB = new javax.swing.JComboBox<>(); cancelButton = new javax.swing.JButton(); generateButton1 = new javax.swing.JButton(); generateButton2 = new javax.swing.JButton(); writeCheckBox = new javax.swing.JCheckBox(); writeFilenameCB = new javax.swing.JComboBox<>(); progressPanel = new javax.swing.JPanel(); editParamsButton = new javax.swing.JButton(); jList2.setModel(new javax.swing.AbstractListModel() { String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" }; public int getSize() { return strings.length; } public String getElementAt(int i) { return strings[i]; } }); jScrollPane2.setViewportView(jList2); generateMenuItem1.setText("Generate..."); generateMenuItem1.setToolTipText("Generate items for list"); generateMenuItem1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { generateMenuItem1ActionPerformed(evt); } }); jPopupMenu1.add(generateMenuItem1); loadUriMenuItem.setText("Load Events File..."); loadUriMenuItem.setToolTipText("Load a list of time ranges from an events file."); loadUriMenuItem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { loadUriMenuItemActionPerformed(evt); } }); jPopupMenu1.add(loadUriMenuItem); loadFromFileMI.setText("Load from File..."); loadFromFileMI.setToolTipText("Load lines from file into this text area"); loadFromFileMI.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { loadFromFileMIActionPerformed(evt); } }); jPopupMenu1.add(loadFromFileMI); pasteMenuItem.setText("Paste"); pasteMenuItem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { pasteMenuItemActionPerformed(evt); } }); jPopupMenu1.add(pasteMenuItem); jLabel2.setText("Time Range:"); timeRangeComboBox.setEditable(true); timeRangeComboBox.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Jun 2000", "2000", "2000-01-01/03-01", "2000-2016" })); timeFormatComboBox.setEditable(true); timeFormatComboBox.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "$Y-$m-$d", "$Y", "$Y-$(m,span=3)", "$Y-$m", "$Y_$j", "$Y-$m-$dT$H/PT1H", "$Y-$m-$dT$H$M/PT1M", "$Y-$m-$dT$H$M$S/PT1S", " " })); jLabel3.setText("Time Format:"); javax.swing.GroupLayout timeRangesPanelLayout = new javax.swing.GroupLayout(timeRangesPanel); timeRangesPanel.setLayout(timeRangesPanelLayout); timeRangesPanelLayout.setHorizontalGroup( timeRangesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(timeRangesPanelLayout.createSequentialGroup() .addGroup(timeRangesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(timeRangeComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(timeRangesPanelLayout.createSequentialGroup() .addGroup(timeRangesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel3) .addComponent(jLabel2)) .addGap(0, 0, Short.MAX_VALUE)) .addComponent(timeFormatComboBox, javax.swing.GroupLayout.Alignment.TRAILING, 0, 220, Short.MAX_VALUE)) .addContainerGap()) ); timeRangesPanelLayout.setVerticalGroup( timeRangesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(timeRangesPanelLayout.createSequentialGroup() .addComponent(jLabel2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(timeRangeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jLabel3) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(timeFormatComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); generateMenuItem2.setText("Generate..."); generateMenuItem2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { generateMenuItem2ActionPerformed(evt); } }); jPopupMenu2.add(generateMenuItem2); loadFromFileMI2.setText("Load from File"); loadFromFileMI2.setToolTipText("Load lines from file into this text area"); loadFromFileMI2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { loadFromFileMI2ActionPerformed(evt); } }); jPopupMenu2.add(loadFromFileMI2); pasteMenuItem2.setText("Paste"); pasteMenuItem2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { pasteMenuItem2ActionPerformed(evt); } }); jPopupMenu2.add(pasteMenuItem2); fileMenu.setText("File"); OpenMenuItem.setText("Open batch file..."); OpenMenuItem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { OpenMenuItemActionPerformed(evt); } }); fileMenu.add(OpenMenuItem); SaveAsMenuItem.setText("Save Batch File As..."); SaveAsMenuItem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { SaveAsMenuItemActionPerformed(evt); } }); fileMenu.add(SaveAsMenuItem); exportResultsMenuItem.setText("Export Results..."); exportResultsMenuItem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { exportResultsMenuItemActionPerformed(evt); } }); fileMenu.add(exportResultsMenuItem); menuBar.add(fileMenu); helpMenu.setText("Help"); showHelpMenuItem.setText("Show Help Manual in Browser"); showHelpMenuItem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { showHelpMenuItemActionPerformed(evt); } }); helpMenu.add(showHelpMenuItem); menuBar.add(helpMenu); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 100, Short.MAX_VALUE) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 100, Short.MAX_VALUE) ); goButton.setText("Go!"); goButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { goButtonActionPerformed(evt); } }); param1Values.setColumns(20); param1Values.setRows(5); param1Values.addMouseListener(new java.awt.event.MouseAdapter() { public void mousePressed(java.awt.event.MouseEvent evt) { param1ValuesMousePressed(evt); } public void mouseReleased(java.awt.event.MouseEvent evt) { param1ValuesMouseReleased(evt); } public void mouseClicked(java.awt.event.MouseEvent evt) { param1ValuesMouseClicked(evt); } }); param1ScrollPane.setViewportView(param1Values); param2Values.setColumns(20); param2Values.setRows(5); param2Values.addMouseListener(new java.awt.event.MouseAdapter() { public void mousePressed(java.awt.event.MouseEvent evt) { param2ValuesMousePressed(evt); } public void mouseReleased(java.awt.event.MouseEvent evt) { param2ValuesMouseReleased(evt); } public void mouseClicked(java.awt.event.MouseEvent evt) { param2ValuesMouseClicked(evt); } }); param2ScrollPane.setViewportView(param2Values); messageLabel.setText("Load up those parameters and hit Go!"); jLabel1.setText("This tool generates inputs for scripts, running through a series of inputs. First load the script with the green \"play\" button, then specify the parameter name and values to assign, and optionally a second parameter. Each value of the second parameter is run for each value of the first. Use the inspect button to set values for any other parameters. Right-click within the values areas to generate values."); jLabel1.setVerticalAlignment(javax.swing.SwingConstants.TOP); param1NameCB.setEditable(true); param1NameCB.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { " " })); param1NameCB.addItemListener(new java.awt.event.ItemListener() { public void itemStateChanged(java.awt.event.ItemEvent evt) { param1NameCBItemStateChanged(evt); } }); param2NameCB.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { " " })); param2NameCB.addItemListener(new java.awt.event.ItemListener() { public void itemStateChanged(java.awt.event.ItemEvent evt) { param2NameCBItemStateChanged(evt); } }); cancelButton.setText("Cancel"); cancelButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cancelButtonActionPerformed(evt); } }); generateButton1.setText("Generate..."); generateButton1.setToolTipText("Generate items for list"); generateButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { generateButton1ActionPerformed(evt); } }); generateButton2.setText("Generate..."); generateButton2.setToolTipText("Generate items for list"); generateButton2.setEnabled(false); generateButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { generateButton2ActionPerformed(evt); } }); writeCheckBox.setText("Write:"); writeCheckBox.setToolTipText("After each iteration, write the file, where $x is replaced"); writeFilenameCB.setEditable(true); writeFilenameCB.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "/tmp/ap/$x.png", "/tmp/ap/$x_$x.png", "/tmp/ap/$x.pdf", "/tmp/ap/$x_$x.pdf", " " })); org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, writeCheckBox, org.jdesktop.beansbinding.ELProperty.create("${selected}"), writeFilenameCB, org.jdesktop.beansbinding.BeanProperty.create("enabled")); bindingGroup.addBinding(binding); javax.swing.GroupLayout progressPanelLayout = new javax.swing.GroupLayout(progressPanel); progressPanel.setLayout(progressPanelLayout); progressPanelLayout.setHorizontalGroup( progressPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 497, Short.MAX_VALUE) ); progressPanelLayout.setVerticalGroup( progressPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 51, Short.MAX_VALUE) ); editParamsButton.setText("Edit Parameter Values"); editParamsButton.setEnabled(false); editParamsButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { editParamsButtonActionPerformed(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() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(progressPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addComponent(cancelButton) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(goButton, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(editParamsButton, javax.swing.GroupLayout.Alignment.TRAILING))) .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE) .addComponent(dataSetSelector1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(param1NameCB, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(generateButton1)) .addComponent(param1ScrollPane)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(param2ScrollPane) .addGroup(layout.createSequentialGroup() .addComponent(param2NameCB, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(generateButton2)))) .addGroup(layout.createSequentialGroup() .addGap(0, 434, Short.MAX_VALUE) .addComponent(writeCheckBox) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(writeFilenameCB, javax.swing.GroupLayout.PREFERRED_SIZE, 238, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addComponent(messageLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 389, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, Short.MAX_VALUE))) .addContainerGap()) ); layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {generateButton1, generateButton2}); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(dataSetSelector1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(param1NameCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(param2NameCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(generateButton1) .addComponent(generateButton2)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(param1ScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 280, Short.MAX_VALUE) .addComponent(param2ScrollPane)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(writeCheckBox, javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(writeFilenameCB, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(22, 22, 22) .addComponent(editParamsButton) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(goButton) .addComponent(cancelButton))) .addGroup(layout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(messageLabel) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(progressPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap()) ); bindingGroup.bind(); }// //GEN-END:initComponents private void goButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_goButtonActionPerformed if ( !goButton.isEnabled() ) { return; } goButton.setEnabled(false); Runnable run= () -> { try { String scriptName= dataSetSelector1.getValue(); dom.getController().getApplicationModel().addRecent(scriptName); doIt(); } catch (IOException ex) { messageLabel.setText(ex.getMessage()); } }; new Thread(run,"runBatch").start(); }//GEN-LAST:event_goButtonActionPerformed private void param1ValuesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_param1ValuesMouseClicked if ( evt.isPopupTrigger() ) { jPopupMenu1.show( evt.getComponent(), evt.getX(), evt.getY() ); } }//GEN-LAST:event_param1ValuesMouseClicked private void param1ValuesMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_param1ValuesMousePressed if ( evt.isPopupTrigger() ) { jPopupMenu1.show( evt.getComponent(), evt.getX(), evt.getY() ); } }//GEN-LAST:event_param1ValuesMousePressed private void param1ValuesMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_param1ValuesMouseReleased if ( evt.isPopupTrigger() ) { jPopupMenu1.show( evt.getComponent(), evt.getX(), evt.getY() ); } }//GEN-LAST:event_param1ValuesMouseReleased private void generateMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_generateMenuItem1ActionPerformed doGenerate( param1NameCB, param1Values ); }//GEN-LAST:event_generateMenuItem1ActionPerformed private void param2ValuesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_param2ValuesMouseClicked if ( evt.isPopupTrigger() ) { jPopupMenu2.show( evt.getComponent(), evt.getX(), evt.getY() ); } }//GEN-LAST:event_param2ValuesMouseClicked private void generateMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_generateMenuItem2ActionPerformed doGenerate( param2NameCB, param2Values ); }//GEN-LAST:event_generateMenuItem2ActionPerformed private void param2ValuesMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_param2ValuesMousePressed if ( evt.isPopupTrigger() ) { jPopupMenu2.show( evt.getComponent(), evt.getX(), evt.getY() ); } }//GEN-LAST:event_param2ValuesMousePressed private void param2ValuesMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_param2ValuesMouseReleased if ( evt.isPopupTrigger() ) { jPopupMenu2.show( evt.getComponent(), evt.getX(), evt.getY() ); } }//GEN-LAST:event_param2ValuesMouseReleased private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed Window w=SwingUtilities.getWindowAncestor(this); if ( ! ( w instanceof JDialog ) ) { logger.warning("untested code might leave hidden windows..."); } ProgressMonitor mon= this.monitor; if ( mon!=null ) { mon.cancel(); } w.setVisible(false); }//GEN-LAST:event_cancelButtonActionPerformed private void loadUriMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loadUriMenuItemActionPerformed DataSetSelector eventsDataSetSelector= new DataSetSelector(); List deft= new ArrayList<>(); deft.add( new Bookmark.Item("http://autoplot.org/autoplot/data/event/simpleEvent.txt") ); org.autoplot.bookmarks.Util.loadRecent( "eventsRecent", eventsDataSetSelector, deft ); if ( JOptionPane.OK_OPTION==AutoplotUtil.showConfirmDialog(this, eventsDataSetSelector, "Load Events", JOptionPane.OK_CANCEL_OPTION ) ) { try { QDataSet ds= org.autoplot.jythonsupport.Util.getDataSet(eventsDataSetSelector.getValue()); ds= Ops.createEvents(ds); Units tu= ((Units)((QDataSet)ds.property(QDataSet.BUNDLE_1)).property(QDataSet.UNITS,0)); StringBuilder ss= new StringBuilder(); for ( int i=0; i0; generateButton1.setEnabled( present ); generateMenuItem1.setEnabled( present ); param1ScrollPane.getViewport().setView(param1Values); param2ScrollPane.getViewport().setView(param2Values); messageLabel.setText("Load up those parameters and hit Go!"); } }//GEN-LAST:event_param1NameCBItemStateChanged private void param2NameCBItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_param2NameCBItemStateChanged if ( evt.getStateChange()==ItemEvent.SELECTED ) { boolean present= param2NameCB.getSelectedItem().toString().trim().length()>0; generateButton2.setEnabled( present ); generateMenuItem2.setEnabled( present ); } }//GEN-LAST:event_param2NameCBItemStateChanged private void OpenMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OpenMenuItemActionPerformed JFileChooser chooser= new JFileChooser(); chooser.setFileFilter( new FileNameExtensionFilter( "Batch Parameters", "batch") ); chooser.setDialogType( JFileChooser.OPEN_DIALOG ); Preferences prefs= Preferences.userNodeForPackage( BatchMaster.class ); String s= prefs.get("batch",null); if ( s!=null ) { chooser.setSelectedFile(new File(s)); } if ( JFileChooser.APPROVE_OPTION==chooser.showOpenDialog( this ) ) { final File f= chooser.getSelectedFile(); prefs.put("batch", f.toString() ); Runnable run= () -> { try { loadBatchFile( f ); } catch (IOException|JSONException ex) { JOptionPane.showMessageDialog( BatchMaster.this, "Unable to open file. "+ex.getMessage() ); } }; new Thread(run).start(); } }//GEN-LAST:event_OpenMenuItemActionPerformed private void SaveAsMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SaveAsMenuItemActionPerformed JFileChooser chooser= new JFileChooser(); chooser.setFileFilter( new FileNameExtensionFilter( "Batch Parameters", "batch") ); chooser.setDialogType( JFileChooser.OPEN_DIALOG ); Preferences prefs= Preferences.userNodeForPackage( BatchMaster.class ); String s= prefs.get("batch",null); if ( s!=null ) { chooser.setSelectedFile(new File(s)); } if ( JFileChooser.APPROVE_OPTION==chooser.showSaveDialog( this ) ) { File ff= chooser.getSelectedFile(); if ( !ff.getName().endsWith(".batch") ) { ff= new File( ff.getAbsolutePath()+".batch"); } final File f= ff; prefs.put("batch", f.toString() ); Runnable run= () -> { try { saveFile( f ); } catch (IOException|JSONException ex) { JOptionPane.showMessageDialog( BatchMaster.this, "Unable to save file. "+ex.getMessage() ); } }; new Thread(run).start(); } }//GEN-LAST:event_SaveAsMenuItemActionPerformed private void exportResultsMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exportResultsMenuItemActionPerformed JFileChooser chooser= new JFileChooser(); chooser.setFileFilter( new FileNameExtensionFilter( "CSV Files", "csv") ); chooser.setDialogType( JFileChooser.OPEN_DIALOG ); Preferences prefs= Preferences.userNodeForPackage( BatchMaster.class ); String s= prefs.get("export",null); if ( s!=null ) { chooser.setSelectedFile(new File(s)); } if ( JFileChooser.APPROVE_OPTION==chooser.showSaveDialog( this ) ) { File ff= chooser.getSelectedFile(); if ( !(ff.getName().endsWith(".csv")||ff.getName().endsWith(".json")) ) { ff= new File( ff.getAbsolutePath()+".csv"); } final File f= ff; resultsFile= f; if ( results==null ) { String msg= "Output will be written to "+f+".pending and moved after the run."; JOptionPane.showMessageDialog( BatchMaster.this, msg ); return; } prefs.put("export", f.toString() ); Runnable run= () -> { try { exportResults( f ); JOptionPane.showMessageDialog( BatchMaster.this, "data saved to "+f ); } catch (IOException ex) { JOptionPane.showMessageDialog( BatchMaster.this, "Unable to save file. "+ex.getMessage() ); } catch (JSONException ex) { JOptionPane.showMessageDialog( BatchMaster.this, "Unable to save file because of JSON exception "+ex.getMessage() ); } }; new Thread(run).start(); } }//GEN-LAST:event_exportResultsMenuItemActionPerformed private void showHelpMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showHelpMenuItemActionPerformed AutoplotUtil.openBrowser("http://autoplot.org/batch"); }//GEN-LAST:event_showHelpMenuItemActionPerformed private void loadFromFileMIActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loadFromFileMIActionPerformed doLoadFromFile(param1Values); }//GEN-LAST:event_loadFromFileMIActionPerformed private void loadFromFileMI2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loadFromFileMI2ActionPerformed doLoadFromFile(param2Values); }//GEN-LAST:event_loadFromFileMI2ActionPerformed private void pasteMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pasteMenuItemActionPerformed try { String pasteMe= (String) Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor); param1Values.setText(pasteMe); } catch (UnsupportedFlavorException | IOException ex) { logger.log(Level.SEVERE, null, ex); } }//GEN-LAST:event_pasteMenuItemActionPerformed private void pasteMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pasteMenuItem2ActionPerformed try { String pasteMe= (String) Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor); param2Values.setText(pasteMe); } catch (UnsupportedFlavorException | IOException ex) { logger.log(Level.SEVERE, null, ex); } }//GEN-LAST:event_pasteMenuItem2ActionPerformed private void editParamsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editParamsButtonActionPerformed switchToEditableList(); editParamsButton.setEnabled(false); }//GEN-LAST:event_editParamsButtonActionPerformed private void doLoadFromFile( JTextArea paramValues ) { JFileChooser chooser= new JFileChooser(); chooser.setFileFilter( new FileNameExtensionFilter( "Text Files", "txt") ); chooser.setDialogType( JFileChooser.OPEN_DIALOG ); Preferences prefs= Preferences.userNodeForPackage( BatchMaster.class ); String s= prefs.get("textfile",null); if ( s!=null ) { chooser.setSelectedFile(new File(s)); } if ( JFileChooser.APPROVE_OPTION==chooser.showOpenDialog( this ) ) { readFromFile(chooser,paramValues); prefs.put("textfile",chooser.getSelectedFile().toString()); } } private void readFromFile(JFileChooser chooser, final JTextArea paramValues ) { final StringBuilder b= new StringBuilder(); try { try ( BufferedReader read= new BufferedReader( new FileReader(chooser.getSelectedFile()) ) ) { String l= read.readLine(); while ( l!=null ) { if ( l.trim().length()>0 ) { b.append(l).append("\n"); } l= read.readLine(); } } } catch ( IOException ex ) { logger.log( Level.WARNING, null, ex ); } Runnable run= () -> { paramValues.setText(b.toString()); }; SwingUtilities.invokeLater(run); } private void exportResults( File f ) throws IOException, JSONException { if ( results==null ) { return; } if ( f.getName().endsWith(".json") ) { String sresults= results.toString(3); FileUtil.writeStringToFile( f, sresults ); } else { exportResultsPendingCSV( f, results, results.getJSONArray("results"), 0 ); } } private void loadBatchFile( File f ) throws IOException, JSONException { if ( SwingUtilities.isEventDispatchThread() ) throw new IllegalArgumentException("don't call from event thread"); String src= FileUtil.readFileToString(f); JSONObject jo= new JSONObject(src); final Map params= new HashMap(); String scriptName1= jo.getString("script"); scriptName1= scriptName1.replaceAll("\\%\\{PWD\\}",f.getParentFile().getCanonicalPath() ); final String scriptName= scriptName1; params.put( "script", scriptName ); Runnable run= () -> { BatchMaster.this.dataSetSelector1.setValue(scriptName); doPlayButton(); }; try { SwingUtilities.invokeAndWait(run); } catch (InterruptedException | InvocationTargetException ex) { logger.log(Level.SEVERE, null, ex); } params.put( "param1", jo.getString("param1")); params.put( "param2", jo.getString("param2")); params.put( "param1Values", jo.getString("param1Values")); params.put( "param2Values", jo.getString("param2Values")); run= () -> { BatchMaster.this.param1NameCB.setSelectedItem(params.get("param1")); BatchMaster.this.param2NameCB.setSelectedItem(params.get("param2")); BatchMaster.this.param1Values.setText(params.get("param1Values")); BatchMaster.this.param2Values.setText(params.get("param2Values")); }; try { SwingUtilities.invokeAndWait(run); } catch (InterruptedException | InvocationTargetException ex) { Logger.getLogger(BatchMaster.class.getName()).log(Level.SEVERE, null, ex); } } private void saveFile( File f ) throws IOException, JSONException { JSONObject jo= new JSONObject(); jo.put( "script", this.dataSetSelector1.getValue() ); jo.put( "param1", this.param1NameCB.getSelectedItem().toString() ); jo.put( "param2", this.param2NameCB.getSelectedItem().toString() ); jo.put( "param1Values", this.param1Values.getText() ); jo.put( "param2Values", this.param2Values.getText() ); String src= jo.toString(4); FileUtil.writeStringToFile(f,src); } private void doGenerate( JComboBox cb, JTextArea ta ) { if ( cb.getSelectedItem()==null ) return; String p= cb.getSelectedItem().toString(); p= p.trim(); if ( p.length()>0 ) { try { org.autoplot.jythonsupport.JythonUtil.Param pd= getParamDescription( p ); if ( pd==null ) return; // shouldn't happen String[] ss=null; // will be generated values if ( pd.type=='T' ) { try { if ( AutoplotUtil.showConfirmDialog( this, timeRangesPanel, "Generate Time Ranges", JOptionPane.OK_CANCEL_OPTION )==JOptionPane.OK_OPTION ) { ss= ScriptContext.generateTimeRanges( timeFormatComboBox.getSelectedItem().toString(), timeRangeComboBox.getSelectedItem().toString() ); } } catch (ParseException ex) { Logger.getLogger(BatchMaster.class.getName()).log(Level.SEVERE, null, ex); } } else if ( pd.enums!=null ) { final JPanel panel= new JPanel(); panel.setLayout( new BoxLayout( panel, BoxLayout.Y_AXIS ) ); String label= pd.label; if ( pd.doc!=null ) label= ""+label+", "+pd.doc+""; panel.add( new JLabel( label ) ); for ( int i=0; i theList= new ArrayList<>(); for ( Component c: panel.getComponents() ) { if ( c instanceof JCheckBox ) { if ( ( (JCheckBox) c).isSelected() ) { theList.add(((JCheckBox)c).getText()); } } } ss= theList.toArray( new String[theList.size()] ); } } else if ( pd.type=='F' ) { JPanel panel= new JPanel(); panel.setLayout( new BoxLayout( panel, BoxLayout.Y_AXIS ) ); String label= pd.label; if ( pd.doc!=null ) label= ""+label+", "+pd.doc+""; panel.add( new JLabel( label ) ); JTextField min= new JTextField( "" ); JTextField max= new JTextField( "" ); JTextField step= new JTextField( "" ); boolean isInt; min.setText( String.valueOf( pd.deft ) ); if ( pd.deft instanceof Integer ) { max.setText( String.valueOf( ((Integer)pd.deft) + 4 ) ); step.setText( "1" ); isInt= true; } else { max.setText( String.valueOf( ((Number)pd.deft).doubleValue() + 10. ) ); step.setText( "0.1" ); isInt= false; } panel.add( new JLabel( "Minimum: " ) ); panel.add( min ); panel.add( new JLabel( "Maximum: " ) ); panel.add( max ); panel.add( new JLabel( "Step Size: " ) ); panel.add( step ); while ( AutoplotUtil.showConfirmDialog( this, panel, "Select range", JOptionPane.OK_CANCEL_OPTION )==JOptionPane.OK_OPTION ) { List theList= new ArrayList<>(); double dmin= Double.parseDouble(min.getText()); double dmax= Double.parseDouble(max.getText()); double dstep= Double.parseDouble(step.getText()); if ( dstep<=0 ) continue; if ( dmax0 ) { int i= lastItem.lastIndexOf('\n'); lastItem= lastItem.substring(i+1); URISplit split= URISplit.parse(lastItem); if ( split.path!=null && split.path.startsWith("file:") ) { f= new File( split.path.substring(5) ); } } JFileChooser cf= new JFileChooser(); if ( f!=null ) cf.setCurrentDirectory(f); cf.setMultiSelectionEnabled(true); if ( cf.showOpenDialog(this)==JFileChooser.APPROVE_OPTION ) { File[] ff= cf.getSelectedFiles(); ss= new String[ff.length]; for ( int i=0; i0 ) { int i= lastItem.lastIndexOf('\n'); lastItem= lastItem.substring(i+1); URISplit split= URISplit.parse(lastItem); if ( split.path!=null && split.path.startsWith("file:") ) { f= new File( split.path.substring(5) ); } } JFileChooser cf= new JFileChooser(); if ( f!=null ) cf.setCurrentDirectory(f); cf.setMultiSelectionEnabled(true); if ( cf.showOpenDialog(this)==JFileChooser.APPROVE_OPTION ) { File[] ff= cf.getSelectedFiles(); ss= new String[ff.length]; for ( int i=0; i params= URISplit.parseParams(split.params); //TODO: support these. Map env= new HashMap<>(); //DasProgressPanel monitor= DasProgressPanel.createFramed( SwingUtilities.getWindowAncestor(BatchMaster.this), "download script"); File scriptFile= DataSetURI.getFile( split.file, new NullProgressMonitor() ); String script= readScript( scriptFile ); env.put("dom",this.dom); env.put("PWD",pwd); Map parms= Util.getParams( env, script, params, new NullProgressMonitor() ); Param p= parms.get(name); return p; } /** * TODO: this is not complete! * @param interp * @param paramDescription * @param paramName * @param f1 * @throws IOException */ private void setParam( InteractiveInterpreter interp, org.autoplot.jythonsupport.JythonUtil.Param paramDescription, String paramName, String f1 ) throws IOException { if ( paramDescription==null ) { throw new IllegalArgumentException("expected to see parameter description!"); } switch (paramDescription.type) { case 'U': case 'R': URI uri; try { URISplit split= URISplit.parse(f1); if ( split.path==null ) { uri= new URI( pwd + f1 ); } else { uri= new URI(f1); } } catch ( URISyntaxException ex ) { throw new IOException(ex); } interp.set("_apuri", uri ); interp.exec("autoplot2017.params[\'"+paramName+"\']=_apuri"); // JythonRefactory okay break; case 'L': interp.exec("autoplot2017.params[\'"+paramName+"\']=URL(\'"+f1+"\')"); // JythonRefactory okay break; case 'A': interp.exec("autoplot2017.params[\'"+paramName+"\']=\'"+f1+"\'");// JythonRefactory okay break; case 'T': try { DatumRange timeRange= DatumRangeUtil.parseTimeRange(f1); interp.set("_apdr", timeRange ); interp.exec("autoplot2017.params[\'"+paramName+"\']=_apdr");// JythonRefactory okay } catch (ParseException ex) { Logger.getLogger(BatchMaster.class.getName()).log(Level.SEVERE, null, ex); } break; default: interp.exec("autoplot2017.params[\'"+paramName+"\']="+f1);// JythonRefactory okay break; } } private String pwd; /** * read the script into a string * @param f * @return * @throws FileNotFoundException * @throws IOException */ private String readScript( File f ) throws FileNotFoundException, IOException { StringBuilder build= new StringBuilder(); BufferedReader r; r = new BufferedReader( new FileReader(f) ); try { String line= r.readLine(); while ( line!=null ) { build.append(line).append("\n"); line= r.readLine(); } } finally { r.close(); } return build.toString(); } /** * write the current canvas to a file. * @param f1 * @param f2 * @return the name of the file used. * @throws IOException */ private String doWrite( String f1, String f2 ) throws IOException { f1= f1.replaceAll("/", "_"); f2= f2.replaceAll("/", "_"); f1= f1.replaceAll(" ", "_"); f2= f2.replaceAll(" ", "_"); f1= f1.replaceAll(":", "_"); // times f2= f2.replaceAll(":", "_"); if ( writeCheckBox.isSelected() ) { String template= writeFilenameCB.getSelectedItem().toString(); String[] ss= template.split("\\$x",-2); StringBuilder f= new StringBuilder(ss[0]); if ( ss.length>1 ) { f.append(f1).append(ss[1]); } if ( ss.length>2 ) { f.append(f2.trim()).append(ss[2]); } for ( int i=3; i-1 ) { param1JLabels[irec].setBackground(Color.GRAY); param1JLabels[irec].setBorder(BorderFactory.createLineBorder(Color.black)); } } private JPanel switchListToIconLabels( List jobs1, String[] ff1 ) { JPanel p= new JPanel(); p.setLayout( new BoxLayout(p,BoxLayout.Y_AXIS) ); for ( String f: ff1 ) { JLabel l= new JLabel(f); l.setIcon(queued); p.add( l ); jobs1.add(l); } JScrollPane scrollp= new JScrollPane(p); scrollp.getVerticalScrollBar().setUnitIncrement( scrollp.getFont().getSize()); scrollp.setPreferredSize( new Dimension(640,640)); scrollp.setMaximumSize( new Dimension(640,640)); messageLabel.setText("Running jobs, mouse over to view tooltip containing standard output."); return p; } /** * make an HTML rendering of the text, possibly truncating it to 50 lines. * @param txt * @return */ private static String htmlize( String txt ) { String[] ss= txt.split("\n"); StringBuilder sb= new StringBuilder("\n"); int iline=0; for ( String s: ss ) { sb.append( s ); sb.append( "
\n"); iline++; if ( iline>HTML_LINE_LIMIT ) { sb.append("&npsp; (").append(ss.length-HTML_LINE_LIMIT).append(" more lines...)
"); break; } } sb.append(""); return sb.toString(); } /** * make an HTML rendering of the text, possibly truncating it to 50 lines. * @param txt * @return */ private static String htmlize( String txt, String stderr ) { StringBuilder sb= new StringBuilder("\n"); int iline=0; if ( txt.trim().length()>0 ) { String[] ss= txt.split("\n"); for ( String s: ss ) { sb.append( s ); sb.append( "
\n"); iline++; if ( iline>HTML_LINE_LIMIT ) { sb.append("&npsp; (").append(ss.length-HTML_LINE_LIMIT).append(" more lines...)
"); break; } } if ( stderr.trim().length()>0 ) { sb.append("stderr:
"); } } if ( stderr.trim().length()>0 ) { String[] ss= stderr.split("\n"); for ( String s: ss ) { sb.append( s ); sb.append( "
\n"); iline++; if ( iline>HTML_LINE_LIMIT ) { sb.append("&npsp; (").append(ss.length-HTML_LINE_LIMIT).append(" more lines...)
"); break; } } } sb.append(""); return sb.toString(); } /** * if the parameter name contains a split character then return the names. * This is just so we can experiment with the feature. * @param param * @return */ private static String[] maybeSplit( String param ) { if ( param.contains("|") ) { return param.split( "\\|", -2 ); } else if ( param.contains(",") ) { return param.split( ",", -2 ); } else if ( param.contains(";") ) { return param.split( ";", -2 ); } else { return null; } } /** * run the batch process. The * @throws IOException */ public void doIt() throws IOException { final DasProgressPanel monitor= DasProgressPanel.createComponent( "" ); progressPanel.add( monitor.getComponent() ); this.monitor= monitor; final List jobs1= new ArrayList<>(); final List jobs2= new ArrayList<>(); editParamsButton.setEnabled(true); { String[] ff1= param1Values.getText().split("\n"); JPanel p= switchListToIconLabels( jobs1, ff1 ); for ( int i=0; i thisRow= new ArrayList<>(); try { JSONObject jo= BatchMaster.this.results; JSONArray ja= jo.getJSONArray("results"); for ( int j=0; j0 ) { jobs2.get(i).setIcon(prob); } else { jobs2.get(i).setIcon(okay); } if ( jobs2.get(i).getIcon()==okay ) { jobs2.get(i).setToolTipText( htmlize(runResults.getString("stdout")) ); } else { jobs2.get(i).setToolTipText( htmlize(runResults.getString("stdout"),runResults.getString("result"))); } } } else { logger.fine("Nothing to do."); } } catch (JSONException ex) { Logger.getLogger(BatchMaster.class.getName()).log(Level.SEVERE, null, ex); } } } }); } param1JLabels= jobs1.toArray( new JLabel[jobs1.size()] ); param1ScrollPane.getViewport().setView(p); } if ( param2Values.getText().trim().length()>0 ) { String[] ff1= param2Values.getText().split("\n"); JPanel p= switchListToIconLabels( jobs2, ff1 ); param2ScrollPane.getViewport().setView(p); } try { String scriptName= dataSetSelector1.getValue(); URISplit split= URISplit.parse(scriptName); pwd= split.path; if ( !split.file.endsWith(".jy") ) { JOptionPane.showMessageDialog( this, "script must end in .jy: "+scriptName ); return; } String[] ff1= param1Values.getText().split("\n"); monitor.setTaskSize(ff1.length); monitor.started(); Map params= URISplit.parseParams(split.params); Map env= new HashMap<>(); env.put("dom",this.dom); env.put("PWD",pwd); File scriptFile= DataSetURI.getFile( split.file, monitor.getSubtaskMonitor("download script") ); String script= readScript( scriptFile ); Map parms= Util.getParams( env, script, params, new NullProgressMonitor() ); InteractiveInterpreter interp = JythonUtil.createInterpreter( true, false ); interp.exec(JythonRefactory.fixImports("import autoplot2017")); ParametersFormPanel pfp= new org.autoplot.jythonsupport.ui.ParametersFormPanel(); pfp.doVariables( env, scriptFile, params, null ); params.entrySet().forEach((ent) -> { try { pfp.getFormData().implement( interp, ent.getKey(), ent.getValue() ); } catch (ParseException ex) { logger.log(Level.SEVERE, null, ex); } }); if ( writeCheckBox.isSelected() ) { String template= writeFilenameCB.getSelectedItem().toString(); if ( !( template.endsWith(".pdf") || template.endsWith(".png") ) ) { AutoplotUtil.showConfirmDialog( this, "write template must end in .pdf or .png", "Write Template Error", JOptionPane.OK_OPTION ); return; } } JSONObject jo= new JSONObject(); JSONArray ja= new JSONArray(); jo.put( "results", ja ); String param1= param1NameCB.getSelectedItem()!=null ? param1NameCB.getSelectedItem().toString().trim() : ""; String param2= param2NameCB.getSelectedItem()!=null ? param2NameCB.getSelectedItem().toString().trim() : ""; JSONArray paramsJson= new JSONArray(); paramsJson.put(0,param1); if ( param2.length()>0 ) { paramsJson.put(1,param2); } jo.put("params", paramsJson ); monitor.setTaskSize( ff1.length ); int icount=0; int i1=0; int exportResultsWritten=0; for ( String f1 : ff1 ) { JSONObject runResults= new JSONObject(); try { if ( monitor.isCancelled() ) { break; } monitor.setProgressMessage(f1); monitor.setTaskProgress(monitor.getTaskProgress()+1); if ( f1.trim().length()==0 ) { i1++; continue; } jobs1.get(i1).setIcon(working); //interp.set( "monitor", monitor.getSubtaskMonitor(f1) ); interp.set( "monitor", new NullProgressMonitor() { @Override public boolean isCancelled() { return monitor.isCancelled(); } }); // subtask would reset indeterminate. interp.set( "dom", this.dom ); interp.set( "PWD", split.path ); String paramName= param1NameCB.getSelectedItem().toString(); String[] paramNames= maybeSplit( paramName ); if ( paramNames!=null ) { char splitc= paramName.charAt(paramNames[0].length()); String[] paramValues= f1.trim().split("\\"+splitc); for ( int j= 0; j jList2; private javax.swing.JPanel jPanel1; private javax.swing.JPopupMenu jPopupMenu1; private javax.swing.JPopupMenu jPopupMenu2; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JMenuItem loadFromFileMI; private javax.swing.JMenuItem loadFromFileMI2; private javax.swing.JMenuItem loadUriMenuItem; private javax.swing.JMenuBar menuBar; private javax.swing.JLabel messageLabel; private javax.swing.JComboBox param1NameCB; private javax.swing.JScrollPane param1ScrollPane; private javax.swing.JTextArea param1Values; private javax.swing.JComboBox param2NameCB; private javax.swing.JScrollPane param2ScrollPane; private javax.swing.JTextArea param2Values; private javax.swing.JMenuItem pasteMenuItem; private javax.swing.JMenuItem pasteMenuItem2; private javax.swing.JPanel progressPanel; private javax.swing.JMenuItem showHelpMenuItem; private javax.swing.JComboBox timeFormatComboBox; private javax.swing.JComboBox timeRangeComboBox; private javax.swing.JPanel timeRangesPanel; private javax.swing.JCheckBox writeCheckBox; private javax.swing.JComboBox writeFilenameCB; private org.jdesktop.beansbinding.BindingGroup bindingGroup; // End of variables declaration//GEN-END:variables private static void exportResultsPendingJSON( File pendingFile, JSONObject results, JSONArray resultsArray, int recordsWrittenAlready ) throws FileNotFoundException, IOException, JSONException { } private static void exportResultsPendingCSV( File pendingFile, JSONObject results, JSONArray resultsArray, int recordsWrittenAlready ) throws FileNotFoundException, IOException { boolean header= recordsWrittenAlready==0; try (PrintWriter out = new PrintWriter( new FileWriter( pendingFile, true ) ) ) { if ( resultsArray.length()==0 ) { logger.warning("no records in results"); return; } JSONObject jo= resultsArray.getJSONObject(0); boolean hasOutputFile= jo.has("writeFile"); JSONArray params= results.getJSONArray("params"); StringBuilder record; if ( header ) { record= new StringBuilder(); record.append("jobNumber"); for ( int j=0; j