/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package org.autoplot.jythonsupport; import java.awt.BorderLayout; import java.awt.Toolkit; import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.ClipboardOwner; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.Transferable; import java.awt.datatransfer.UnsupportedFlavorException; import java.awt.event.ActionEvent; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.AbstractAction; import javax.swing.Action; import javax.swing.JOptionPane; import javax.swing.JScrollPane; import javax.swing.KeyStroke; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; import jsyntaxpane.DefaultSyntaxKit; import org.autoplot.jythonsupport.ui.EditorTextPane; import org.autoplot.jythonsupport.ui.RefactorRenameVariable; /** * * @author jbf */ public class ClipboardEditorPanel extends javax.swing.JPanel { EditorTextPane edit; /** * Creates new form ClipboardEditorPanel */ public ClipboardEditorPanel() { initComponents(); edit= new EditorTextPane(); DefaultSyntaxKit.initKit(); edit.setContentType("text/python"); edit.getDocument().addDocumentListener( new DocumentListener() { @Override public void insertUpdate(DocumentEvent e) { copyClipButton.setEnabled(true); } @Override public void removeUpdate(DocumentEvent e) { copyClipButton.setEnabled(true); } @Override public void changedUpdate(DocumentEvent e) { copyClipButton.setEnabled(true); } }); JScrollPane pane= new JScrollPane(edit); editorPanel.add( pane, BorderLayout.CENTER ); editorPanel.revalidate(); Action renameAction= new AbstractAction("Rename Variable") { @Override public void actionPerformed(ActionEvent e) { String script= edit.getText(); try { StaticCodeAnalysis.showUsage( script,"x" ); } catch ( Exception ex ) { JOptionPane.showMessageDialog( editorPanel, "Script must be Jython syntax without errors"); return; } RefactorRenameVariable rrv= new RefactorRenameVariable(edit); if ( JOptionPane.OK_OPTION== JOptionPane.showConfirmDialog( editorPanel, rrv, "Rename Variable", JOptionPane.OK_CANCEL_OPTION ) ) { rrv.refactorVarRename( ); } } }; renameVariableButton.setAction(renameAction); //edit.getKeymap().addActionForKeyStroke( // KeyStroke.getKeyStroke( 'r', java.awt.event.InputEvent.SHIFT_DOWN_MASK ), renameAction ); } /** * set the text in the editor. * @param text */ public void setText( String text ) { edit.setText(text); copyClipButton.setEnabled(true); } /** * return the text in the editor * @return */ public String getText() { return edit.getText(); } public void setTextFromClipboard() { try { Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); String text= clipboard.getContents(null).getTransferData( DataFlavor.stringFlavor ).toString(); setText(text); copyClipButton.setEnabled(false); } catch (UnsupportedFlavorException ex) { Logger.getLogger(ClipboardEditorPanel.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(ClipboardEditorPanel.class.getName()).log(Level.SEVERE, null, 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() { editorPanel = new javax.swing.JPanel(); copyClipButton = new javax.swing.JButton(); renameVariableButton = new javax.swing.JButton(); editorPanel.setLayout(new java.awt.BorderLayout()); copyClipButton.setText("Copy back into Clipboard"); copyClipButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { copyClipButtonActionPerformed(evt); } }); renameVariableButton.setText("Rename Variable..."); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(editorPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup() .addContainerGap(15, Short.MAX_VALUE) .addComponent(renameVariableButton) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(copyClipButton)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(editorPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 269, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(copyClipButton) .addComponent(renameVariableButton))) ); }// //GEN-END:initComponents private void copyClipButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_copyClipButtonActionPerformed Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); StringSelection selection = new StringSelection(edit.getText()); clipboard.setContents(selection, null); copyClipButton.setEnabled(false); }//GEN-LAST:event_copyClipButtonActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton copyClipButton; private javax.swing.JPanel editorPanel; private javax.swing.JButton renameVariableButton; // End of variables declaration//GEN-END:variables }