/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package org.autoplot; import java.io.File; import javax.swing.JFileChooser; import javax.swing.filechooser.FileFilter; /** * * @author jbf */ public class ReplaceFilePanel extends javax.swing.JPanel { /** * Creates new form ReplaceFilePanel */ public ReplaceFilePanel() { initComponents(); } public void setCurrentFile( String file ) { this.jLabel2.setText(file); this.jTextField1.setText(file); checkLocal(); } public String getSelectedFile() { return this.jTextField1.getText(); } /** * 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() { jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); jTextField1 = new javax.swing.JTextField(); jButton1 = new javax.swing.JButton(); jLabel1.setText("Replace references to the file:"); jLabel2.setText("jLabel2"); jLabel3.setText("With the new file name:"); jTextField1.setText("jTextField1"); jTextField1.addKeyListener(new java.awt.event.KeyAdapter() { public void keyTyped(java.awt.event.KeyEvent evt) { jTextField1KeyTyped(evt); } }); jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/autoplot/file.png"))); // NOI18N jButton1.setToolTipText("Pick file"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 470, Short.MAX_VALUE) .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup() .addComponent(jTextField1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButton1))) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel2) .addGap(18, 18, 18) .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton1)) .addGap(0, 174, Short.MAX_VALUE)) ); }// //GEN-END:initComponents private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed String sfile= this.jLabel2.getText(); if ( sfile.startsWith("file://") ) { sfile= sfile.substring(7); } else if ( sfile.startsWith("file:") ) { sfile= sfile.substring(5); } int iext= sfile.lastIndexOf('.'); final String ext; if ( iext!=-1 ) { ext= sfile.substring(iext).toLowerCase(); } else { ext= null; } File current= new File( sfile ); if ( !current.exists() ) { current= current.getParentFile(); } JFileChooser choose= new JFileChooser( current ); if ( ext!=null ) { choose.addChoosableFileFilter( new FileFilter() { @Override public boolean accept(File f) { String sf= f.getName(); return sf.toLowerCase().endsWith(ext); } @Override public String getDescription() { return ext + " files"; } }); } if (choose.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) { this.jTextField1.setText( "file://"+ choose.getSelectedFile().getPath() ); } }//GEN-LAST:event_jButton1ActionPerformed private void checkLocal() { String s= this.jTextField1.getText(); if ( s.startsWith("file:" ) || s.startsWith("/") || s.startsWith("\\") || ( s.length()>2 && s.charAt(1)==':') ) { this.jButton1.setEnabled(true); } else { this.jButton1.setEnabled(false); } } private void jTextField1KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyTyped checkLocal(); }//GEN-LAST:event_jTextField1KeyTyped // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButton1; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JTextField jTextField1; // End of variables declaration//GEN-END:variables }