Back to index

lightning-sunbird  0.9+nobinonly
TrivialApplet.java
Go to the documentation of this file.
00001 /*
00002        Trivial applet that displays a string - 4/96 PNL
00003 */
00004 
00005 import java.awt.*;
00006 import java.awt.event.*;
00007 import java.applet.Applet;
00008 import java.net.URL;
00009 import java.net.MalformedURLException;
00010 
00011 import netscape.javascript.JSObject;
00012 
00013 class AboutBox extends Frame {
00014        AboutBox(Menu aboutMenu, ActionListener[] actionListeners) {
00015               super("About Applet");
00016               
00017               addWindowListener(
00018                      new WindowAdapter() {
00019                             public void windowClosing(WindowEvent e) {
00020                                    dispose();
00021                             }
00022                      });
00023               
00024               Button okButton = new Button("OK");
00025               okButton.addActionListener(
00026                      new ActionListener() {
00027                             public void actionPerformed(ActionEvent e) {
00028                                    dispose();
00029                             }
00030                      });
00031               
00032               // Annoying use of flow layout managers.
00033               Panel labelPanel = new Panel();
00034               Panel buttonPanel = new Panel();
00035               Panel fieldPanel = new Panel();
00036 
00037               labelPanel.add(new Label("This applet's about box..."));
00038               buttonPanel.add(okButton);
00039 
00040               fieldPanel.add(new TextField(20));
00041               fieldPanel.add(new TextField(20));
00042 
00043               add(labelPanel, "North");
00044               add(buttonPanel, "Center");
00045               add(fieldPanel, "South");
00046               
00047               // test menu bar stuff.
00048               MenuBar menuBar = new MenuBar();
00049               aboutMenu = (Menu) cloneMenu(aboutMenu);
00050               for (int i = 0; i < actionListeners.length; i++)
00051                      aboutMenu.getItem(i).addActionListener(actionListeners[i]);
00052               menuBar.add(aboutMenu);
00053               setMenuBar(menuBar);
00054 
00055               pack();
00056               show();
00057        }
00058        
00059        private MenuItem cloneMenu(MenuItem oldItem) {
00060               if (oldItem instanceof Menu) {
00061                      Menu oldMenu = (Menu) oldItem;
00062                      Menu newMenu = new Menu(oldMenu.getLabel());
00063                      int count = oldMenu.getItemCount();
00064                      for (int i = 0; i < count; i++) {
00065                             newMenu.add(cloneMenu(oldMenu.getItem(i)));
00066                      }
00067                      return newMenu;
00068               } else {
00069                      MenuItem newItem = new MenuItem(oldItem.getLabel());
00070                      return newItem;
00071               }
00072        }
00073 }
00074 
00075 class ExceptionThread extends Thread {
00076        ExceptionThread() {
00077               start();
00078        }
00079        
00080        public void run() {
00081               throw new Error("this is an error!");
00082        }
00083 }
00084 
00085 public class TrivialApplet extends Applet {
00086        public Button goButton;
00087        public Button aboutButton;
00088        public TextField urlField;
00089        public PopupMenu contextMenu;
00090        public Menu aboutMenu;
00091        public ActionListener[] actionListeners;
00092        private static int appletCount;
00093 
00094        public void init() {
00095               ++appletCount;
00096        
00097               goButton = new Button("Go");
00098               aboutButton = new Button("About");
00099               
00100               String urlText = getParameter("URL");
00101               if (urlText == null)
00102                      urlText = "http://www.apple.com";
00103               
00104               urlField = new TextField(urlText);
00105               
00106               ActionListener goListener =
00107                      new ActionListener() {
00108                             public void actionPerformed(ActionEvent e) {
00109                                    try {
00110                                           URL location = new URL(urlField.getText());
00111                                           System.out.println("going to URL: " + location);
00112                                           JSObject window = JSObject.getWindow(TrivialApplet.this);
00113                                           if (window != null) {
00114                                                  //window.eval("alert('going to location " + location + "');");
00115                                                  window.eval("println('" + location + "')");
00116                                           }
00117                                           getAppletContext().showDocument(location, "_new");
00118                                    } catch (MalformedURLException mfue) {
00119                                    }
00120                             }
00121                      };
00122               
00123               ActionListener aboutListener =
00124                      new ActionListener() {
00125                             public void actionPerformed(ActionEvent e) {
00126                                    new AboutBox(aboutMenu, actionListeners);
00127                             }
00128                      };
00129               
00130               goButton.addActionListener(goListener);
00131               aboutButton.addActionListener(aboutListener);
00132                      
00133               add(goButton);
00134               add(aboutButton);
00135               add(urlField);
00136               
00137               // Try a pop-up menu, and a menu in the menubar.
00138               contextMenu = new PopupMenu();
00139               aboutMenu = new Menu("About");
00140               
00141               contextMenu.add(newItem("About", aboutListener));
00142               aboutMenu.add(newItem("About", aboutListener));
00143               
00144               contextMenu.add(newItem("Go", goListener));
00145               aboutMenu.add(newItem("Go", goListener));
00146               
00147               ActionListener errorListener = new ActionListener() {
00148                             public void actionPerformed(ActionEvent e) {
00149                                    new ExceptionThread();
00150                             }
00151                      };
00152               contextMenu.add(newItem("Error", errorListener));
00153               aboutMenu.add(newItem("Error", errorListener));
00154               
00155               ActionListener[] listeners = { aboutListener, goListener, errorListener };
00156               actionListeners = listeners;
00157               
00158               add(contextMenu);
00159 
00160               // add a mouse listener that causes the pop-up to appear appropriately.
00161               MouseListener mouseListener = new MouseAdapter() {
00162                      public void mousePressed(MouseEvent e) {
00163                             if (e.isPopupTrigger()) {
00164                                    e.consume();
00165                                    contextMenu.show(TrivialApplet.this, e.getX(), e.getY());
00166                             }
00167                      }
00168               };
00169               
00170               addMouseListener(mouseListener);
00171        }
00172        
00173        private MenuItem newItem(String title, ActionListener listener) {
00174               MenuItem item = new MenuItem(title);
00175               item.addActionListener(listener);
00176               return item;
00177        }
00178        
00179        private Frame getFrame() {
00180               Component p = this;
00181               while (p != null && !(p instanceof Frame))
00182                      p = getParent();
00183               return (Frame)p;
00184        }
00185        
00186        // public void paint( Graphics g ) {}
00187 
00188     public boolean mouseEnter(Event evt, int x, int y) {
00189        showStatus("Welcome!");
00190               return true;
00191     }
00192 
00193     public boolean mouseExit(Event evt, int x, int y) {
00194        showStatus("See you later!");
00195               return true;
00196     }
00197     
00198        public void print(String message) {
00199               JSObject window = JSObject.getWindow(this);
00200               Object[] args = { message };
00201               window.call("println", args);
00202     }
00203     
00204     public int getAppletCount() {
00205        return appletCount;
00206     }
00207 }