package org.autoplot.jythonsupport; import java.io.IOException; import java.util.HashMap; import java.util.logging.Level; import java.util.logging.Logger; import org.autoplot.jythonsupport.JythonUtil.ScriptDescriptor; /** * * @author jbf */ public class ScriptDocumentationPanel extends javax.swing.JPanel { /** * Creates new form ScriptDocumentationPanel */ public ScriptDocumentationPanel() { 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") // //GEN-BEGIN:initComponents private void initComponents() { titleTextField = new org.autoplot.datasource.ui.PromptTextField(); jScrollPane1 = new javax.swing.JScrollPane(); descriptionTextField = new org.autoplot.datasource.ui.PromptTextArea(); jLabel1 = new javax.swing.JLabel(); labelTextField = new org.autoplot.datasource.ui.PromptTextField(); jLabel2 = new javax.swing.JLabel(); titleTextField.setText("promptTextField2"); titleTextField.setFont(titleTextField.getFont().deriveFont(titleTextField.getFont().getStyle() | java.awt.Font.BOLD, titleTextField.getFont().getSize()+2)); titleTextField.setPromptText("script title"); descriptionTextField.setColumns(20); descriptionTextField.setRows(5); descriptionTextField.setPromptText("script description"); jScrollPane1.setViewportView(descriptionTextField); jLabel1.setText("Label to use when adding to menu:"); labelTextField.setText("promptTextField3"); labelTextField.setPromptText("script label"); jLabel2.setText("Autoplot scripts have several functions they can call to set the title and description of the script. These are used for branding and instructions, and set the label used when the script is added to a menu."); jLabel2.setVerticalAlignment(javax.swing.SwingConstants.TOP); 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, false) .addComponent(titleTextField, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jScrollPane1) .addGroup(layout.createSequentialGroup() .addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(labelTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 286, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGap(0, 0, Short.MAX_VALUE)) .addGroup(layout.createSequentialGroup() .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(titleTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jScrollPane1, 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(jLabel1) .addComponent(labelTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(89, Short.MAX_VALUE)) ); }// //GEN-END:initComponents // Variables declaration - do not modify//GEN-BEGIN:variables private org.autoplot.datasource.ui.PromptTextArea descriptionTextField; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JScrollPane jScrollPane1; private org.autoplot.datasource.ui.PromptTextField labelTextField; private org.autoplot.datasource.ui.PromptTextField titleTextField; // End of variables declaration//GEN-END:variables public void initialize(String src) { try { ScriptDescriptor sd= JythonUtil.describeScript( src, new HashMap<>() ); this.titleTextField.setText( sd.getTitle() ); this.descriptionTextField.setText( sd.getDescription() ); this.labelTextField.setText( sd.getLabel() ); } catch (IOException ex) { Logger.getLogger(ScriptDocumentationPanel.class.getName()).log(Level.SEVERE, null, ex); } } /** * this is a placeholder for some improved functionality. The ideal implementation * will remove the old setters. * @param text the old script. * @return the new script. */ public String implement(String text) { StringBuilder b= new StringBuilder(); String[] ss= text.split("\n"); boolean handledTitle= this.titleTextField.getText().trim().length()==0; boolean handledLabel= this.labelTextField.getText().trim().length()==0; boolean handledDescription= this.descriptionTextField.getText().trim().length()==0; String ignoreUntilTriple=null; for ( String s: ss ) { if ( ignoreUntilTriple!=null ) { if ( s.contains(ignoreUntilTriple) ) { ignoreUntilTriple= null; } continue; } if ( s.startsWith("setScriptTitle") ) { if ( this.titleTextField.getText().trim().length()>0 ) { b.append("setScriptTitle('").append(this.titleTextField.getText()).append("')\n"); handledTitle= true; } } else if ( s.startsWith("setScriptLabel") ) { if ( this.labelTextField.getText().trim().length()>0 ) { b.append("setScriptLabel('").append(this.labelTextField.getText()).append("')\n"); handledLabel= true; } } else if ( s.startsWith("setScriptDescription") ) { if ( this.descriptionTextField.getText().trim().length()>0 ) { b.append("setScriptDescription('''").append(this.descriptionTextField.getText()).append("''')\n"); handledDescription= true; } if ( s.charAt(21)=='\'' && s.charAt(22)=='\'') { ignoreUntilTriple="'''"; } else if ( s.charAt(21)=='"' && s.charAt(22)=='"' ) { ignoreUntilTriple="\"\"\""; } if ( s.indexOf(ignoreUntilTriple,24)>-1 ) { // setScriptDescription('''my description''') ignoreUntilTriple=null; } } else { b.append(s).append("\n"); } } text= b.toString(); b= new StringBuilder(); if ( !handledTitle ) { b.append("setScriptTitle('").append(this.titleTextField.getText()).append("')\n"); } if ( !handledLabel ) { b.append("setScriptLabel('").append(this.labelTextField.getText()).append("')\n"); } if ( !handledDescription ) { b.append("setScriptDescription('''").append(this.descriptionTextField.getText()).append("''')\n"); } b.append( text ); return b.toString(); } }