Package net.megabyte.nosaf

Source Code of net.megabyte.nosaf.ConverterDialog

/*
* Copyright (c) 2012 Uwe Pachler
* Distributed under the LGPL license.
*/
package net.megabyte.nosaf;

import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.xml.transform.TransformerException;

/**
*
* @author count
*/
public class ConverterDialog extends javax.swing.JDialog {

  /**
   * Creates new form ConverterDialog
   */
  public ConverterDialog(java.awt.Frame parent, boolean modal) {
    super(parent, modal);
    initComponents();
   
    DocumentListener dl = new DocumentListener() {
      public void insertUpdate(DocumentEvent arg0) {
        updateUIStateLater();
      }
      public void removeUpdate(DocumentEvent arg0) {
        updateUIStateLater();
      }
      public void changedUpdate(DocumentEvent arg0) {
        updateUIStateLater();
      }
    };
   
    ChangeListener cl = new ChangeListener() {
      public void stateChanged(ChangeEvent arg0) {
        updateUIStateLater();
      }
    };
   
    resourcePathTextField.getDocument().addDocumentListener(dl);
    sourcePathTextField.getDocument().addDocumentListener(dl);
    sameAsSourcePathCheckBox.addChangeListener(cl);
   
    updateUIState();
  }
 
  private void updateUIStateLater() {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        updateUIState();
      }
    });
  }
  private void updateUIState() {
    resourcePathTextField.setEnabled(!sameAsSourcePathCheckBox.isSelected());
    if(sameAsSourcePathCheckBox.isSelected()) {
      String text = sourcePathTextField.getText();
      if(!resourcePathTextField.getText().equals(text))
        resourcePathTextField.setText(text);
    }
   
    String errorMessage = null;
    File resourcePath = null;
    File sourcePath = null;
    if(!resourcePathTextField.getText().trim().isEmpty())
      resourcePath = new File(resourcePathTextField.getText());
    if(!sourcePathTextField.getText().trim().isEmpty())
      sourcePath = new File(sourcePathTextField.getText());
   
    boolean pathsValid = false;
    if(sourcePath == null || resourcePath == null)
      errorMessage = "valid paths must be given to proceed";
    else if(!sourcePath.exists())
      errorMessage = "source path does not exist";
    else if(!sourcePath.isDirectory())
      errorMessage = "the selected source path is not a directory";
    else if(!resourcePath.exists())
      errorMessage = "resource path does not exist";
    else if(!resourcePath.isDirectory())
      errorMessage = "the selected resource path is not a directory";
    else
      pathsValid = true;
   
    if(errorMessage != null)
      errorLabel.setText(errorMessage);
    errorLabel.setVisible(errorMessage != null);
   
    convertButton.setEnabled(pathsValid);
  }
 
  private void browseDirectoryForTextField(JTextField textField) {
    JFileChooser chooser = new JFileChooser(textField.getText());
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    if(JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) {
      textField.setText(chooser.getSelectedFile().toString());
      updateUIState();
   
  }
 
  /**
   * 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")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        sourcePathTextField = new javax.swing.JTextField();
        browseSourcePathButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        convertButton = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        resourcePathTextField = new javax.swing.JTextField();
        browseResourcePathButton = new javax.swing.JButton();
        sameAsSourcePathCheckBox = new javax.swing.JCheckBox();
        jTextPane1 = new javax.swing.JTextPane();
        errorLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("NetBeans SAF Forms Converter");
        setLocationByPlatform(true);

        sourcePathTextField.setToolTipText("Enter the path to your sources here. This is the actual source root; if your classes live in the package com.mycompany.stuff, which are in /Users/Me/myproj/src/com/mycompany/stuff, pass /Users/Me/myproj/src in here.");

        browseSourcePathButton.setText("...");
        browseSourcePathButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                browseSourcePathButtonActionPerformed(evt);
            }
        });

        jLabel1.setLabelFor(sourcePathTextField);
        jLabel1.setText("Source Path:");

        convertButton.setText("Convert");
        convertButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                convertButtonActionPerformed(evt);
            }
        });

        jLabel2.setText("Resource Base Path:");

        resourcePathTextField.setToolTipText("This is the root path where your resources live. In non-Maven projects, resources are typically saved in the same path as the Java files. \nOn Maven projects however, there is a separate base directory for resources, which needs to be entered in this field.");

        browseResourcePathButton.setText("...");
        browseResourcePathButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                browseResourcePathButtonActionPerformed(evt);
            }
        });

        sameAsSourcePathCheckBox.setSelected(true);
        sameAsSourcePathCheckBox.setText("Same as Source Path");

        jTextPane1.setEditable(false);
        jTextPane1.setText("This tool allows you to convert forms in a NetBeans Java Swing Application Framework project to plain Swing forms, so they will open again in NetBeans 7.1 and later.\nIt is important that you BACKUP ALL YOUR WORK before proceeding. The dialog files will be REPLACED by the conversion.\n\nOriginal tool written by rcasha@megabyte.net\nUser Interface written by Uwe Pachler\n");
        jTextPane1.setOpaque(false);

        errorLabel.setForeground(new java.awt.Color(255, 0, 0));
        errorLabel.setText("error message here");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(sourcePathTextField)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(browseSourcePathButton))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(errorLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(convertButton))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(sameAsSourcePathCheckBox, javax.swing.GroupLayout.DEFAULT_SIZE, 399, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(resourcePathTextField)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(browseResourcePathButton))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTextPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 202, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(sourcePathTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(browseSourcePathButton))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(sameAsSourcePathCheckBox)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(45, 45, 45)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(resourcePathTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(browseResourcePathButton))))
                .addGap(49, 49, 49)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(convertButton)
                    .addComponent(errorLabel))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
 
  private void convertButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_convertButtonActionPerformed
   
   
    new Thread(){
      public void run() {
        final ConverterProgressDialog cpd = new ConverterProgressDialog(ConverterDialog.this);
        SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            cpd.setVisible(true);
          }
        });
       
        String errorTitle = null;
        String errorMessage = null;
        try {
          Converter converter = new Converter(new File(sourcePathTextField.getText()));
          converter.convert();
        } catch (TransformerException ex) {
          errorTitle = "XSL Transform failed";
          errorMessage = ex.getMessage();
          Logger.getLogger(ConverterDialog.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
          errorTitle = "Error occurred";
          errorMessage = ex.getMessage();
          Logger.getLogger(ConverterDialog.class.getName()).log(Level.SEVERE, null, ex);
        }
        final String title = errorTitle==null ? "Success" : errorTitle;
        final String msg = errorTitle==null ? "The conversion finished without errors. Check each Dialog and ensure everything is in order." : errorMessage;
        SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            cpd.setVisible(false);
            cpd.dispose();
            JOptionPane.showMessageDialog(ConverterDialog.this, msg, title, JOptionPane.ERROR_MESSAGE);
          }
        });
      }
    }.start();

  }//GEN-LAST:event_convertButtonActionPerformed

  private void browseSourcePathButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseSourcePathButtonActionPerformed
    browseDirectoryForTextField(sourcePathTextField);
  }//GEN-LAST:event_browseSourcePathButtonActionPerformed

  private void browseResourcePathButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseResourcePathButtonActionPerformed
    browseDirectoryForTextField(resourcePathTextField);
  }//GEN-LAST:event_browseResourcePathButtonActionPerformed

  /**
   * @param args the command line arguments
   */
  public static void main(String args[]) {
    /*
     * Set the Nimbus look and feel
     */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
     * If Nimbus (introduced in Java SE 6) is not available, stay with the
     * default look and feel. For details see
     * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
     */
    try {
      for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
        if ("Nimbus".equals(info.getName())) {
          javax.swing.UIManager.setLookAndFeel(info.getClassName());
          break;
        }
      }
    } catch (ClassNotFoundException ex) {
      java.util.logging.Logger.getLogger(ConverterDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
      java.util.logging.Logger.getLogger(ConverterDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
      java.util.logging.Logger.getLogger(ConverterDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
      java.util.logging.Logger.getLogger(ConverterDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /*
     * Create and display the dialog
     */
    java.awt.EventQueue.invokeLater(new Runnable() {

      public void run() {
        ConverterDialog dialog = new ConverterDialog(new javax.swing.JFrame(), true);
        dialog.addWindowListener(new java.awt.event.WindowAdapter() {

          @Override
          public void windowClosing(java.awt.event.WindowEvent e) {
            System.exit(0);
          }
        });
        dialog.setVisible(true);
      }
    });
  }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton browseResourcePathButton;
    private javax.swing.JButton browseSourcePathButton;
    private javax.swing.JButton convertButton;
    private javax.swing.JLabel errorLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextPane jTextPane1;
    private javax.swing.JTextField resourcePathTextField;
    private javax.swing.JCheckBox sameAsSourcePathCheckBox;
    private javax.swing.JTextField sourcePathTextField;
    // End of variables declaration//GEN-END:variables
}
TOP

Related Classes of net.megabyte.nosaf.ConverterDialog

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.