package org.autoplot.jythonsupport; import java.awt.Component; import java.awt.Toolkit; import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.ClipboardOwner; import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.Transferable; import java.io.IOException; import javax.swing.JDialog; import javax.swing.JEditorPane; import javax.swing.JOptionPane; import jsyntaxpane.DefaultSyntaxKit; /** * Convenient tool for converting Java code found on-line into Jython * code which can be tinkered with. * @author jbf */ public class JavaJythonConverter extends javax.swing.JPanel { public JavaJythonConverter( JEditorPane editor ) { this(editor, DIR_JAVA_TO_JYTHON); } /** * Creates new form JavaJythonConverter * @param editor editor from which preferences come * @param direction */ public JavaJythonConverter( JEditorPane editor, int direction ) { initComponents(); this.direction= direction; DefaultSyntaxKit.initKit(); javaEditorPane.setContentType("text/java"); DefaultSyntaxKit.initKit(); jythonEditorPane.setContentType("text/python"); if ( direction==DIR_JAVA_TO_JYTHON ) { } else { Component _1= jSplitPane1.getLeftComponent(); Component _2= jSplitPane1.getRightComponent(); jSplitPane1.setLeftComponent(_2); jSplitPane1.setRightComponent(_1); jSplitPane1.revalidate(); jButton1.setText("Convert Jython to Java"); jButton2.setText("Copy Java to Clipboard"); } if ( editor!=null ) { javaEditorPane.setBackground( editor.getBackground() ); javaEditorPane.setForeground( editor.getForeground() ); javaEditorPane.setFont(editor.getFont()); jythonEditorPane.setBackground( editor.getBackground() ); jythonEditorPane.setForeground( editor.getForeground() ); jythonEditorPane.setFont(editor.getFont()); } } public static final int DIR_JAVA_TO_JYTHON = 1; public static final int DIR_JYTHON_TO_JAVA = 2; private int direction; /** * 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() { jPanel1 = new javax.swing.JPanel(); jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); jPanel2 = new javax.swing.JPanel(); jSplitPane1 = new javax.swing.JSplitPane(); jPanel3 = new javax.swing.JPanel(); jLabel1 = new javax.swing.JLabel(); jScrollPane1 = new javax.swing.JScrollPane(); javaEditorPane = new javax.swing.JEditorPane(); jPanel4 = new javax.swing.JPanel(); jScrollPane2 = new javax.swing.JScrollPane(); jythonEditorPane = new javax.swing.JEditorPane(); jLabel2 = new javax.swing.JLabel(); jButton1.setText("Convert Java to Jython"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jButton2.setText("Copy Jython to Clipboard"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() .addContainerGap(382, Short.MAX_VALUE) .addComponent(jButton1) .addGap(280, 280, 280) .addComponent(jButton2)) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton1) .addComponent(jButton2)) ); jSplitPane1.setDividerLocation(400); jSplitPane1.setResizeWeight(0.5); jLabel1.setText("Java Code"); jScrollPane1.setViewportView(javaEditorPane); javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3); jPanel3.setLayout(jPanel3Layout); jPanel3Layout.setHorizontalGroup( jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 456, Short.MAX_VALUE) .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); jPanel3Layout.setVerticalGroup( jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup() .addGap(31, 31, 31) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 641, Short.MAX_VALUE)) .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createSequentialGroup() .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 648, Short.MAX_VALUE))) ); jSplitPane1.setLeftComponent(jPanel3); jScrollPane2.setViewportView(jythonEditorPane); jLabel2.setText("Jython Code"); javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4); jPanel4.setLayout(jPanel4Layout); jPanel4Layout.setHorizontalGroup( jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel4Layout.createSequentialGroup() .addComponent(jLabel2) .addGap(0, 508, Short.MAX_VALUE)) .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 593, Short.MAX_VALUE)) ); jPanel4Layout.setVerticalGroup( jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel4Layout.createSequentialGroup() .addComponent(jLabel2) .addGap(0, 657, Short.MAX_VALUE)) .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup() .addGap(29, 29, 29) .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 643, Short.MAX_VALUE))) ); jSplitPane1.setRightComponent(jPanel4); javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); jPanel2.setLayout(jPanel2Layout); jPanel2Layout.setHorizontalGroup( jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jSplitPane1, javax.swing.GroupLayout.Alignment.TRAILING) ); jPanel2Layout.setVerticalGroup( jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jSplitPane1, javax.swing.GroupLayout.Alignment.TRAILING) ); 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) .addGroup(layout.createSequentialGroup() .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, Short.MAX_VALUE)) .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addContainerGap()) ); }// //GEN-END:initComponents private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed if ( direction==DIR_JAVA_TO_JYTHON ) { String java= javaEditorPane.getText(); String jython= JythonToJavaConverter.convertReverse(java); jythonEditorPane.setText(jython); } else if ( direction==DIR_JYTHON_TO_JAVA ) { try { String jython= jythonEditorPane.getText(); String java= JythonToJavaConverter.convert(jython); javaEditorPane.setText(java); } catch (Exception ex) { ex.printStackTrace(); JOptionPane.showMessageDialog(this,ex.toString()); } } }//GEN-LAST:event_jButton1ActionPerformed private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); StringSelection ss= new StringSelection( jythonEditorPane.getText() ); clipboard.setContents( ss, new ClipboardOwner() { @Override public void lostOwnership(Clipboard clipboard, Transferable contents) { } }); }//GEN-LAST:event_jButton2ActionPerformed public static void main( String[] args ) throws IOException { JavaJythonConverter cc= new JavaJythonConverter(null); String src= " this.args= new LinkedHashMap<>(args);\n" + " String s= args.get(\"start\");\n" + " if ( s==null ) return \"periodic field needs start\";\n" + " start= TimeUtil.isoTimeToArray(s);\n" + " julday= TimeUtil.julianDay( start[0], start[1], start[2] );\n" + " start[0]= 0;\n" + " start[1]= 0;\n" + " start[2]= 0;\n" + " s= args.get(\"offset\");\n" + " if ( s==null ) return \"periodic field needs offset\";\n" + " offset= Integer.parseInt( s );\n" + " s= args.get(\"period\");\n" + " if ( s==null ) return \"periodic field needs period\";\n" + " if ( !s.startsWith(\"P\") ) {\n" + " if ( s.endsWith(\"D\") ) {\n" + " throw new IllegalArgumentException(\"periodic unit for day is d, not D\");\n" + " } if ( s.endsWith(\"d\") ) {\n" + " s= \"P\"+s.toUpperCase(); // TODO: this only supports d,H,M,S\n" + " } else {\n" + " s= \"PT\" + s.toUpperCase(); \n" + " }\n" + " }"; cc.javaEditorPane.setText(src); JDialog dia= new JDialog(); dia.setContentPane( cc ); dia.pack(); dia.setVisible(true); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; private javax.swing.JPanel jPanel3; private javax.swing.JPanel jPanel4; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JSplitPane jSplitPane1; private javax.swing.JEditorPane javaEditorPane; private javax.swing.JEditorPane jythonEditorPane; // End of variables declaration//GEN-END:variables public void setJavaSource(String doThis) { javaEditorPane.setText(doThis); } public void setPythonSource(String doThis) { jythonEditorPane.setText(doThis); } }