package nxm.sys.libg;

import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;

import nxm.sys.intr.Generate;
import nxm.sys.lib.Shell;
import nxm.sys.lib.Table;

/** Generate JNLP
 *
 *  @author  J. Czechowski IV
 */
@SuppressWarnings("serial")
public class MJGenerateJnlp extends javax.swing.JFrame {
  /** The generator to call when done. */ private final Generate generator;

  /** Creates new form MJGenerateJnlp */
  public MJGenerateJnlp() {
    this(null);
  }

  /** Creates new form MJGenerateJnlp
   *  @param table configuration table */
  public MJGenerateJnlp(Table table) {
    this(null, null, null, table);
  }

  /** Creates new form MJGenerateJnlp
   *  @param generator the generator to call when done
   *  @param fname     file name
   *  @param tname     template name
   *  @param table     configuration table
   */
  public MJGenerateJnlp(Generate generator, String fname, String tname, Table table) {
    initComponents();

    this.generator = generator;

    if(table == null) { table = new Table(); }
    if(fname == null) { fname = ""; }
    if(tname == null) { tname = ""; }

    fileName.setText(fname);
    templateName.setText(tname);
    command.setText(table.getS("COMMAND", "SD360 1e9"));
    optionTrees.setText(table.getS("OPTS", "DSP|SYS"));


    String nmrootUrl   = "";
    String hrefUrl     = table.getS("JNLPHREF", "http://"+getHostName()+"/nmroot/htdocs/demo.jnlp");
    String codebaseUrl = table.getS("CODEBASE", "http://"+getHostName()+"/nmroot/jars");

    if(codebaseUrl.endsWith("/jars")) {
      String root = codebaseUrl.substring(0, codebaseUrl.length()-5);

      if(hrefUrl.startsWith(root + "/htdocs")) {
        nmrootUrl = root;
      }
    }

    nmroot.setText(nmrootUrl.trim());
    codebase.setText(nmrootUrl.trim());
    jnlpHref.setText(codebaseUrl.trim());

    saveTemplate.setSelected(false);
    useNativeLibs.setSelected(table.getState("USENATIVE", false));

    // Set based on above values
    filesRelative.setSelected(nmroot.getText().length() == 0);
    update();
  }

  /** Create a Jnlp file
   *  @param generator generator to call when done
   *  @param fname     file name
   *  @param tname     template name
   *  @param table     configuration table
   */
  public static void createJnlpFile(final Generate generator, final String fname,
                                    final String tname, final Table table) {
    final MJGenerateJnlp frame = new MJGenerateJnlp(generator, fname, tname, table);
    java.awt.EventQueue.invokeLater(new Runnable() {
      public void run() {
        frame.pack();
        frame.setVisible(true);
      }
    });
  }


  private static String getHostName() {
    return Shell.getSharedMidasContext().getResults().getS("ENV.HOSTNAME");
  }

  /** test by creating visible frame */
  public static void test() {
    MJGenerateJnlp frame = new MJGenerateJnlp();
    frame.pack();
    frame.setVisible(true);
  }

  private void update() {
    templateName.setEnabled(saveTemplate.isSelected());
    browseTemplateName.setEnabled(saveTemplate.isSelected());

    if(filesRelative.isSelected()) {
      nmroot.setEnabled(true);
      jnlpHref.setEnabled(false);
      codebase.setEnabled(false);

      if(nmroot.getText().trim().length() == 0) {
        nmroot.setText("http://"+getHostName()+"/nmroot");
      }

      jnlpHref.setText(nmroot.getText()+"/htdocs/"+new File(fileName.getText().trim()).getName());
      codebase.setText(nmroot.getText()+"/jars");

    } else {
      nmroot.setEnabled(false);
      jnlpHref.setEnabled(true);
      codebase.setEnabled(true);

      nmroot.setText("");
    }
  }

  /** 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.
   */
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {
    java.awt.GridBagConstraints gridBagConstraints;

    serverConfigType = new javax.swing.ButtonGroup();
    jPanel1 = new javax.swing.JPanel();
    jLabel5 = new javax.swing.JLabel();
    optionTrees = new javax.swing.JTextField();
    useNativeLibs = new javax.swing.JCheckBox();
    jLabel2 = new javax.swing.JLabel();
    jLabel7 = new javax.swing.JLabel();
    command = new javax.swing.JTextField();
    jPanel2 = new javax.swing.JPanel();
    filesRelative = new javax.swing.JRadioButton();
    jLabel3 = new javax.swing.JLabel();
    nmroot = new javax.swing.JTextField();
    jnlpHref = new javax.swing.JTextField();
    jLabel6 = new javax.swing.JLabel();
    jLabel4 = new javax.swing.JLabel();
    codebase = new javax.swing.JTextField();
    filesArbitrary = new javax.swing.JRadioButton();
    jPanel3 = new javax.swing.JPanel();
    jLabel1 = new javax.swing.JLabel();
    saveTemplate = new javax.swing.JCheckBox();
    fileName = new javax.swing.JTextField();
    browseFileName = new javax.swing.JButton();
    templateName = new javax.swing.JTextField();
    browseTemplateName = new javax.swing.JButton();
    okButton = new javax.swing.JButton();
    cancelButton = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
    setTitle("JNLP Config Wizard");
    getContentPane().setLayout(new java.awt.GridBagLayout());

    jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Application Config"));
    jPanel1.setLayout(new java.awt.GridBagLayout());

    jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
    jLabel5.setText("OptionTrees=");
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
    gridBagConstraints.weightx = 1.0;
    gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
    jPanel1.add(jLabel5, gridBagConstraints);

    optionTrees.setToolTipText("The required option trees (specified same as OPT.PATH).");
    optionTrees.setPreferredSize(new java.awt.Dimension(50, 19));
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
    gridBagConstraints.weightx = 5.0;
    gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
    jPanel1.add(optionTrees, gridBagConstraints);

    useNativeLibs.setToolTipText("Select this to enable the use of native libraries (when available).");
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 2;
    gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
    gridBagConstraints.weightx = 5.0;
    gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
    jPanel1.add(useNativeLibs, gridBagConstraints);

    jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
    jLabel2.setText("UseNativeLibs=");
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 2;
    gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
    gridBagConstraints.weightx = 1.0;
    gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
    jPanel1.add(jLabel2, gridBagConstraints);

    jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
    jLabel7.setText("Command=");
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
    gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
    jPanel1.add(jLabel7, gridBagConstraints);

    command.setToolTipText("The command to run (e.g. \"SD360 1e9\").");
    command.setPreferredSize(new java.awt.Dimension(50, 19));
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
    gridBagConstraints.weightx = 5.0;
    gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
    jPanel1.add(command, gridBagConstraints);

    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.gridwidth = 2;
    gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
    gridBagConstraints.ipadx = 10;
    gridBagConstraints.ipady = 10;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
    gridBagConstraints.weightx = 1.0;
    gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
    getContentPane().add(jPanel1, gridBagConstraints);

    jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Web Server Config"));
    jPanel2.setLayout(new java.awt.GridBagLayout());

    serverConfigType.add(filesRelative);
    filesRelative.setSelected(true);
    filesRelative.setText("Files in default location (relative to $NMROOT)");
    filesRelative.setToolTipText("JNLP file in $NMROOT/htdocs, Jars in $NMROOT/jars");
    filesRelative.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        filesRelativeActionPerformed(evt);
      }
    });
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 0;
    gridBagConstraints.gridwidth = 3;
    gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
    gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
    jPanel2.add(filesRelative, gridBagConstraints);

    jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
    jLabel3.setText("NMROOT=");
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
    gridBagConstraints.weightx = 1.0;
    gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
    jPanel2.add(jLabel3, gridBagConstraints);

    nmroot.setToolTipText("$NMROOT as seen via the network (e.g. \"http://myserver/nmroot\")");
    nmroot.setPreferredSize(new java.awt.Dimension(50, 19));
    nmroot.addCaretListener(new javax.swing.event.CaretListener() {
      public void caretUpdate(javax.swing.event.CaretEvent evt) {
        nmrootCaretUpdate(evt);
      }
    });
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 2;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
    gridBagConstraints.weightx = 5.0;
    gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
    jPanel2.add(nmroot, gridBagConstraints);

    jnlpHref.setToolTipText("The location of the JNLP file as seen via the network");
    jnlpHref.setPreferredSize(new java.awt.Dimension(50, 19));
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 2;
    gridBagConstraints.gridy = 3;
    gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
    gridBagConstraints.weightx = 5.0;
    gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
    jPanel2.add(jnlpHref, gridBagConstraints);

    jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
    jLabel6.setText("CODEBASE=");
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 4;
    gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
    gridBagConstraints.weightx = 1.0;
    gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
    jPanel2.add(jLabel6, gridBagConstraints);

    jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
    jLabel4.setText("HREF=");
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 3;
    gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
    gridBagConstraints.weightx = 1.0;
    gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
    jPanel2.add(jLabel4, gridBagConstraints);

    codebase.setToolTipText("The directory holding the JAR files as seen via the network.");
    codebase.setPreferredSize(new java.awt.Dimension(50, 19));
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 2;
    gridBagConstraints.gridy = 4;
    gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
    gridBagConstraints.weightx = 5.0;
    gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
    jPanel2.add(codebase, gridBagConstraints);

    serverConfigType.add(filesArbitrary);
    filesArbitrary.setText("Files in arbitrary location");
    filesArbitrary.setToolTipText("Must set HREF and CODEBASE manually");
    filesArbitrary.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        filesArbitraryActionPerformed(evt);
      }
    });
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 2;
    gridBagConstraints.gridwidth = 3;
    gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
    gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
    jPanel2.add(filesArbitrary, gridBagConstraints);

    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 2;
    gridBagConstraints.gridwidth = 2;
    gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
    gridBagConstraints.ipadx = 15;
    gridBagConstraints.ipady = 10;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
    gridBagConstraints.weightx = 1.0;
    gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
    getContentPane().add(jPanel2, gridBagConstraints);

    jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "JNLP File Properties"));
    jPanel3.setLayout(new java.awt.GridBagLayout());

    jLabel1.setText("File Name");
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
    gridBagConstraints.weightx = 1.0;
    gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
    jPanel3.add(jLabel1, gridBagConstraints);

    saveTemplate.setText("Save Template");
    saveTemplate.setToolTipText("Indicates if a template should be saved.");
    saveTemplate.setPreferredSize(new java.awt.Dimension(115, 19));
    saveTemplate.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        saveTemplateActionPerformed(evt);
      }
    });
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
    gridBagConstraints.weightx = 1.0;
    gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
    jPanel3.add(saveTemplate, gridBagConstraints);

    fileName.setToolTipText("The file name of the JNLP file.");
    fileName.setPreferredSize(new java.awt.Dimension(50, 19));
    fileName.addCaretListener(new javax.swing.event.CaretListener() {
      public void caretUpdate(javax.swing.event.CaretEvent evt) {
        fileNameCaretUpdate(evt);
      }
    });
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
    gridBagConstraints.weightx = 10.0;
    gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
    jPanel3.add(fileName, gridBagConstraints);

    browseFileName.setText("Browse");
    browseFileName.setPreferredSize(new java.awt.Dimension(89, 19));
    browseFileName.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        browseFileNameActionPerformed(evt);
      }
    });
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
    gridBagConstraints.weightx = 1.0;
    gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
    jPanel3.add(browseFileName, gridBagConstraints);

    templateName.setToolTipText("The file name of the template that can be used to recreate the JNLP file in the future.");
    templateName.setPreferredSize(new java.awt.Dimension(50, 19));
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
    gridBagConstraints.weightx = 10.0;
    gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
    jPanel3.add(templateName, gridBagConstraints);

    browseTemplateName.setText("Browse");
    browseTemplateName.setPreferredSize(new java.awt.Dimension(89, 19));
    browseTemplateName.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        browseTemplateNameActionPerformed(evt);
      }
    });
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 2;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
    gridBagConstraints.weightx = 1.0;
    gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
    jPanel3.add(browseTemplateName, gridBagConstraints);

    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 0;
    gridBagConstraints.gridwidth = 2;
    gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
    gridBagConstraints.ipadx = 10;
    gridBagConstraints.ipady = 10;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
    gridBagConstraints.weightx = 1.0;
    gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
    getContentPane().add(jPanel3, gridBagConstraints);

    okButton.setText("OK");
    okButton.setPreferredSize(new java.awt.Dimension(75, 25));
    okButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        okButtonActionPerformed(evt);
      }
    });
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 3;
    gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
    gridBagConstraints.weightx = 1.0;
    gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
    getContentPane().add(okButton, gridBagConstraints);

    cancelButton.setText("Cancel");
    cancelButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        cancelButtonActionPerformed(evt);
      }
    });
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 3;
    gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
    gridBagConstraints.weightx = 1.0;
    gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
    getContentPane().add(cancelButton, gridBagConstraints);

    pack();
  }// </editor-fold>//GEN-END:initComponents

  private void fileNameCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_fileNameCaretUpdate
    update();
  }//GEN-LAST:event_fileNameCaretUpdate

  private void nmrootCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_nmrootCaretUpdate
    update();
  }//GEN-LAST:event_nmrootCaretUpdate

  private void filesArbitraryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_filesArbitraryActionPerformed
    update();
  }//GEN-LAST:event_filesArbitraryActionPerformed

  private void filesRelativeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_filesRelativeActionPerformed
    update();
  }//GEN-LAST:event_filesRelativeActionPerformed

  private void saveTemplateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveTemplateActionPerformed
    templateName.setEnabled(saveTemplate.isSelected());
    browseTemplateName.setEnabled(saveTemplate.isSelected());
  }//GEN-LAST:event_saveTemplateActionPerformed

  private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
    update();

    String fname = fileName.getText().trim();
    String tname = (saveTemplate.isSelected())? templateName.getText().trim(): null;

    if((new File(fname)).exists()) {
      int status = JOptionPane.showConfirmDialog(this,
                                                 "File "+fname+" exists and will be overwritten.",
                                                 "File Exists", JOptionPane.OK_CANCEL_OPTION,
                                                 JOptionPane.WARNING_MESSAGE);
      if(status != JOptionPane.OK_OPTION) {
        return; /* ABORT */
      }
    }
    if((tname != null) && (new File(tname)).exists()) {
      int status = JOptionPane.showConfirmDialog(this,
                                                 "File "+tname+" exists and will be overwritten.",
                                                 "File Exists", JOptionPane.OK_CANCEL_OPTION,
                                                 JOptionPane.WARNING_MESSAGE);
      if(status != JOptionPane.OK_OPTION) {
        return; /* ABORT */
      }
    }

    Table table = new Table();
    table.put("COMMAND",   command.getText().trim());
    table.put("OPTS",      optionTrees.getText().trim());
    table.put("USENATIVE", (useNativeLibs.isSelected())? "TRUE": "FALSE");
    table.put("JNLPHREF",  jnlpHref.getText().trim());
    table.put("CODEBASE",  codebase.getText().trim());

    if(generator != null) {
      generator.writeJnlpFile(fname, tname, table);

    } else {
      System.out.println("fname="+fname+"  tname="+tname+"  table="+table);
    }

    setVisible(false);
    dispose();
  }//GEN-LAST:event_okButtonActionPerformed

  private void browseFileNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseFileNameActionPerformed
    JFileChooser chooser = new JFileChooser();
    chooser.setFileFilter(makeFileFilter("JNLP Files", ".jnlp"));

    int returnVal = chooser.showOpenDialog(this);
    if(returnVal == JFileChooser.APPROVE_OPTION) {
       fileName.setText(chooser.getSelectedFile().toString());
    }
  }//GEN-LAST:event_browseFileNameActionPerformed

  /** Creates a file filter that accepts files ending in a given extension.

      @param name The name of the filter (e.g. "JNLP Files").
      @param ext  The file name extension (e.g. ".jnlp").

      @return A new file filter.
   */
  private static FileFilter makeFileFilter(final String name, final String ext) {
    return new FileFilter() {
      public boolean accept(File f)   { return f.isDirectory() || f.getName().endsWith(ext); }
      public String  getDescription() { return name; }
    };
  }


  private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
    update();

    if(generator != null) {
      generator.writeJnlpFile(null, null, null);

    } else {
      System.out.println("fname="+null+"  tname="+null+"  table="+null);
    }

    setVisible(false);
    dispose();
  }//GEN-LAST:event_cancelButtonActionPerformed

  private void browseTemplateNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseTemplateNameActionPerformed
    JFileChooser chooser = new JFileChooser();
    chooser.setFileFilter(makeFileFilter("NeXtMidas Table Files", ".tbl"));

    int returnVal = chooser.showOpenDialog(this);
    if(returnVal == JFileChooser.APPROVE_OPTION) {
       templateName.setText(chooser.getSelectedFile().toString());
    }
  }//GEN-LAST:event_browseTemplateNameActionPerformed

  // Variables declaration - do not modify//GEN-BEGIN:variables
  /** browse file name button */        private javax.swing.JButton browseFileName;
  /** browse template name button */    private javax.swing.JButton browseTemplateName;
  /** cancel button */                  private javax.swing.JButton cancelButton;
  /** codebase */                       private javax.swing.JTextField codebase;
  /** command */                        private javax.swing.JTextField command;
  /** filename */                       private javax.swing.JTextField fileName;
  /** files arbitrary button */         private javax.swing.JRadioButton filesArbitrary;
  /** files relative button */          private javax.swing.JRadioButton filesRelative;
  /** File Name label */                private javax.swing.JLabel jLabel1;
  /** UseNativeLibs= label */           private javax.swing.JLabel jLabel2;
  /** NMROOT= label */                  private javax.swing.JLabel jLabel3;
  /** HREF= label */                    private javax.swing.JLabel jLabel4;
  /** OptionTrees= label */             private javax.swing.JLabel jLabel5;
  /** CODEBASE= label */                private javax.swing.JLabel jLabel6;
  /** Command= label */                 private javax.swing.JLabel jLabel7;
  /** 1st panel - cmd, opttrees, etc */ private javax.swing.JPanel jPanel1;
  /** 2nd panel - jar, file location */ private javax.swing.JPanel jPanel2;
  /** 3rd panel - template, etc. */     private javax.swing.JPanel jPanel3;
  /** JNLP HREF */                      private javax.swing.JTextField jnlpHref;
  /** NMROOT */                         private javax.swing.JTextField nmroot;
  /** ok button */                      private javax.swing.JButton okButton;
  /** option trees */                   private javax.swing.JTextField optionTrees;
  /** save template checkbox */         private javax.swing.JCheckBox saveTemplate;
  /** server configuration type */      private javax.swing.ButtonGroup serverConfigType;
  /** template name */                  private javax.swing.JTextField templateName;
  /** use native lbs checkbox */        private javax.swing.JCheckBox useNativeLibs;
  // End of variables declaration//GEN-END:variables

}
