home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 July / Chip_1998-07_cd.bin / zkuste / JBuilder / BDK / Win / bdk_sep97.exe / _SETUP.1 / MakeAppletDialog.java < prev    next >
Encoding:
Java Source  |  1997-09-17  |  7.0 KB  |  274 lines

  1.  
  2. /**
  3.  * Pop up a MakeApplet dialog
  4.  */
  5.  
  6. package sun.beanbox;
  7.  
  8. import java.io.*;
  9. import java.awt.*;
  10. import java.awt.event.*;
  11.  
  12. public class MakeAppletDialog extends Dialog implements Runnable {
  13.  
  14.     private TextField appletNameField;
  15.     private TextField appletJarField;
  16.     private boolean dismissed = false;
  17.     private Frame frame;
  18.     private BeanBox beanbox;
  19.  
  20.     private static String defaultJarName
  21.         = "tmp"+File.separator+"myApplet"+File.separator+"myApplet.jar";
  22.     private static String defaultAppletName
  23.         = beanNameFromJarName(defaultJarName);
  24.  
  25.     public MakeAppletDialog(Frame frame, BeanBox bb) {
  26.     super(frame, "Make an Applet", false);
  27.         new WindowCloser(this);
  28.     this.frame = frame;
  29.     this.beanbox = bb;
  30.  
  31.     GridBagLayout gb = new GridBagLayout();
  32.     this.setLayout(gb);
  33.  
  34.     GridBagConstraints middle = new GridBagConstraints();
  35.     middle.anchor = GridBagConstraints.WEST;
  36.     GridBagConstraints last = new GridBagConstraints();
  37.     last.gridwidth = GridBagConstraints.REMAINDER;
  38.     last.anchor = GridBagConstraints.WEST;
  39.     GridBagConstraints only = new GridBagConstraints();
  40.     only.gridwidth = GridBagConstraints.REMAINDER;
  41.  
  42.     Label l, ll;
  43.     TextField f;
  44.     Button b;
  45.  
  46.     l = new Label("Select a JAR file where to package an Applet.");
  47.     gb.setConstraints(l, last);
  48.     add(l);
  49.  
  50.     l = new Label("Jar File: ");
  51.     appletJarField = f = new TextField(defaultJarName, 30);
  52.     b = new Button("Choose JAR File...");
  53.     gb.setConstraints(l, middle);
  54.     gb.setConstraints(f, middle);
  55.     gb.setConstraints(b, last);
  56.     b.addActionListener(new ChooseAdaptor());
  57.     add(l);
  58.     add(f);
  59.     add(b);
  60.  
  61.     l = new Label("Applet Class: ");
  62.     appletNameField = f = new TextField(defaultAppletName, 30);
  63.     gb.setConstraints(l, middle);
  64.     gb.setConstraints(f, last);
  65.     add(l);
  66.     add(f);
  67.     
  68.     Button bOK, bCancel, bHelp;
  69.  
  70.     Panel p = new Panel();
  71.     bOK= new Button("OK");
  72.     bCancel = new Button("Cancel");
  73.     bHelp = new Button("Help");
  74.     p.add(bOK);
  75.     p.add(bCancel);
  76.     p.add(bHelp);
  77.     gb.setConstraints(p, only);
  78.     add(p);
  79.  
  80.     bCancel.addActionListener(new CancelAdaptor());
  81.     bOK.addActionListener(new OKAdaptor());
  82.     bHelp.addActionListener(new HelpAdaptor());
  83.  
  84.         int x = frame.getLocation().x + 30;
  85.         int y = frame.getLocation().y + 100;
  86.     pack();
  87.     Dimension d = getPreferredSize();
  88.         setBounds(x, y, d.width, d.height);
  89.         show();
  90.     }
  91.  
  92.     void chooseFile() {
  93.     FileDialog fd = new FileDialog(frame, "Choose JAR File",
  94.                        FileDialog.SAVE);
  95.  
  96.     File file = new File(defaultJarName);
  97.     File dir = new File(file.getParent());
  98.  
  99.     if (! dir.exists()) {
  100.         dir.mkdirs();
  101.     }
  102.  
  103.     fd.setDirectory(dir.getPath());
  104.     fd.setFile(file.getName());
  105.     fd.setFilenameFilter(new FileExtension("jar"));
  106.  
  107.     fd.show();
  108.  
  109.     String fname = fd.getFile();
  110.     if (fname == null) {
  111.         return;
  112.     }
  113.     if (! fname.endsWith(".jar")) {
  114.         new ErrorDialog(frame, "JAR file should end in .jar");
  115.         return;
  116.     }
  117.  
  118.     File f = new File(fd.getDirectory(), fname);
  119.     String s = f.getPath();
  120.  
  121.     appletJarField.setText(s);
  122.     appletNameField.setText(beanNameFromJarName(s));
  123.     }
  124.  
  125.     /**
  126.      * Auxiliary method so user most likely needs not type the BeanName
  127.      */
  128.     private static String capitalize(String name) {
  129.     if (name == null || name.length() == 0) {
  130.         return name;
  131.     }
  132.     char chars[] = name.toCharArray();
  133.     chars[0] = Character.toUpperCase(chars[0]);
  134.     return new String(chars);
  135.     }
  136.  
  137.     private static String beanNameFromJarName(String fname) {
  138.     if (! fname.endsWith(".jar")) {
  139.         return null;
  140.     }
  141.     int i = fname.lastIndexOf(File.separator);
  142.     String name = fname.substring(i+1, fname.length()-4);
  143.     return capitalize(name);
  144.     }
  145.  
  146.     /**
  147.      * validation of input
  148.      */
  149.  
  150.     private boolean validateInput() {
  151.     String s = appletJarField.getText();
  152.     if (! s.endsWith(".jar")) {
  153.         return false;
  154.     }
  155.     // other tests would go here
  156.     return true;
  157.     }
  158.  
  159.     /**
  160.      * The applet name selected by user,
  161.      * or null if cancelled
  162.      */
  163.     private String getAppletName() {
  164.     return appletNameField.getText();
  165.     }
  166.  
  167.     /**
  168.      * The directory selected by user,
  169.      */
  170.     private String getAppletDirectory() {
  171.     File f = new File(appletJarField.getText());
  172.     return f.getParent();
  173.     }
  174.  
  175.     /**
  176.      * The name of the desired JAR file.  This is relative to AppletDirectory
  177.      */
  178.     private String getJarName() {
  179.     File f = new File(appletJarField.getText());
  180.     String s = f.getName();
  181.     return s.substring(0, s.length()-4);
  182.     }
  183.  
  184.     // Adaptors;
  185.     // one could also discriminate on evt.getSource()
  186.     
  187.     class ChooseAdaptor implements ActionListener {
  188.     public void actionPerformed(ActionEvent evt) {
  189.         chooseFile();
  190.     }
  191.     }
  192.     class CancelAdaptor implements ActionListener {
  193.     public void actionPerformed(ActionEvent evt) {
  194.         dismissed = true;
  195.         dispose();
  196.     }
  197.     }
  198.     class OKAdaptor implements ActionListener {
  199.     public void actionPerformed(ActionEvent evt) {
  200.         if (! validateInput() ) {
  201.         new ErrorDialog(frame, "invalid JAR filename");
  202.         return;
  203.         }
  204.         Thread th = new Thread(MakeAppletDialog.this);
  205.         th.start();
  206.     }
  207.     }
  208.  
  209.     /**
  210.      * Update the status message
  211.      */
  212.     void updateMessage(String message) {
  213.     // System.err.println(message);
  214.     status2.setText(message);
  215.     repaint();
  216.     }
  217.     private Label status2;
  218.  
  219.     /**
  220.      * Run is called in a separate thread to actually complete the
  221.      * generation of the applet data, including the compilation
  222.      */
  223.     public void run() {
  224.     removeAll();
  225.     setLayout(null);
  226.     Label status = new Label("Generating and compiling "+getAppletName()+" Files");
  227.  
  228.     add(status);
  229.     status.setBounds(20, getSize().height/2 - 10, getSize().width-30, 15);
  230.     status2 = new Label();
  231.     add(status2);
  232.     status2.setBounds(20, getSize().height/2 + 10, getSize().width-30, 15);
  233.     updateMessage("Start...");
  234.     repaint();
  235.     
  236.     AppletGenerator.generate(frame, beanbox, this,
  237.                  getAppletDirectory(),
  238.                  getAppletName(),
  239.                  getJarName());
  240.     dispose();
  241.     }
  242.     class HelpAdaptor implements ActionListener {
  243.     public void actionPerformed(ActionEvent evt) {
  244.         new MakeAppletHelpDialog(frame);
  245.     }
  246.     }
  247. }
  248.  
  249. class MakeAppletHelpDialog extends MessageDialog {
  250.     private final static String message
  251.         = "To generate an Applet, you must specify a JAR file\n"
  252.         + "with a name that ends with \".jar\", as in MyApplet.jar.\n"
  253.         + "\n"
  254.         + "The default name of the Applet is inferred from the name\n"
  255.         + "of the JAR file, MyApplet in the example above\n"
  256.         + "\n"
  257.         + "This operation will generate a JAR file (MyApplet.jar), a readme\n"
  258.         + "file (MyApplet_readme), and an HTML file (MyApplet.html) that can\n"
  259.         + "be used to test the Applet in a JDK1.1-compliant browser.\n"
  260.         + "Additional files will be generated into a subdirectory with the name\n"
  261.         + "MyApplet_files.\n" 
  262.         + "\n"
  263.         + "The applet will work with appletviewer and HotJava but it will not\n"
  264.         + "work in most other browsers yet, as it requires full JDK1.1 support."
  265.         + "\n"
  266.         + "For additional details check the generated readme file.\n"
  267.         + "\n";
  268.  
  269.  
  270.     public MakeAppletHelpDialog(Frame frame) {
  271.     super(frame, "Help for MakeApplet", message, true);
  272.     }
  273. }
  274.