Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / plugin / oji / MRJ / testing / TrivialApplet / TrivialApplet.java
blobe3e57de63314333605cd04a02dc452866282bac7
1 /*
2 Trivial applet that displays a string - 4/96 PNL
3 */
5 import java.awt.*;
6 import java.awt.event.*;
7 import java.applet.Applet;
8 import java.net.URL;
9 import java.net.MalformedURLException;
11 import netscape.javascript.JSObject;
13 class AboutBox extends Frame {
14 AboutBox(Menu aboutMenu, ActionListener[] actionListeners) {
15 super("About Applet");
17 addWindowListener(
18 new WindowAdapter() {
19 public void windowClosing(WindowEvent e) {
20 dispose();
22 });
24 Button okButton = new Button("OK");
25 okButton.addActionListener(
26 new ActionListener() {
27 public void actionPerformed(ActionEvent e) {
28 dispose();
30 });
32 // Annoying use of flow layout managers.
33 Panel labelPanel = new Panel();
34 Panel buttonPanel = new Panel();
35 Panel fieldPanel = new Panel();
37 labelPanel.add(new Label("This applet's about box..."));
38 buttonPanel.add(okButton);
40 fieldPanel.add(new TextField(20));
41 fieldPanel.add(new TextField(20));
43 add(labelPanel, "North");
44 add(buttonPanel, "Center");
45 add(fieldPanel, "South");
47 // test menu bar stuff.
48 MenuBar menuBar = new MenuBar();
49 aboutMenu = (Menu) cloneMenu(aboutMenu);
50 for (int i = 0; i < actionListeners.length; i++)
51 aboutMenu.getItem(i).addActionListener(actionListeners[i]);
52 menuBar.add(aboutMenu);
53 setMenuBar(menuBar);
55 pack();
56 show();
59 private MenuItem cloneMenu(MenuItem oldItem) {
60 if (oldItem instanceof Menu) {
61 Menu oldMenu = (Menu) oldItem;
62 Menu newMenu = new Menu(oldMenu.getLabel());
63 int count = oldMenu.getItemCount();
64 for (int i = 0; i < count; i++) {
65 newMenu.add(cloneMenu(oldMenu.getItem(i)));
67 return newMenu;
68 } else {
69 MenuItem newItem = new MenuItem(oldItem.getLabel());
70 return newItem;
75 class ExceptionThread extends Thread {
76 ExceptionThread() {
77 start();
80 public void run() {
81 throw new Error("this is an error!");
85 public class TrivialApplet extends Applet {
86 public Button goButton;
87 public Button aboutButton;
88 public TextField urlField;
89 public PopupMenu contextMenu;
90 public Menu aboutMenu;
91 public ActionListener[] actionListeners;
92 private static int appletCount;
94 public void init() {
95 ++appletCount;
97 goButton = new Button("Go");
98 aboutButton = new Button("About");
100 String urlText = getParameter("URL");
101 if (urlText == null)
102 urlText = "http://www.apple.com";
104 urlField = new TextField(urlText);
106 ActionListener goListener =
107 new ActionListener() {
108 public void actionPerformed(ActionEvent e) {
109 try {
110 URL location = new URL(urlField.getText());
111 System.out.println("going to URL: " + location);
112 JSObject window = JSObject.getWindow(TrivialApplet.this);
113 if (window != null) {
114 //window.eval("alert('going to location " + location + "');");
115 window.eval("println('" + location + "')");
117 getAppletContext().showDocument(location, "_new");
118 } catch (MalformedURLException mfue) {
123 ActionListener aboutListener =
124 new ActionListener() {
125 public void actionPerformed(ActionEvent e) {
126 new AboutBox(aboutMenu, actionListeners);
130 goButton.addActionListener(goListener);
131 aboutButton.addActionListener(aboutListener);
133 add(goButton);
134 add(aboutButton);
135 add(urlField);
137 // Try a pop-up menu, and a menu in the menubar.
138 contextMenu = new PopupMenu();
139 aboutMenu = new Menu("About");
141 contextMenu.add(newItem("About", aboutListener));
142 aboutMenu.add(newItem("About", aboutListener));
144 contextMenu.add(newItem("Go", goListener));
145 aboutMenu.add(newItem("Go", goListener));
147 ActionListener errorListener = new ActionListener() {
148 public void actionPerformed(ActionEvent e) {
149 new ExceptionThread();
152 contextMenu.add(newItem("Error", errorListener));
153 aboutMenu.add(newItem("Error", errorListener));
155 ActionListener[] listeners = { aboutListener, goListener, errorListener };
156 actionListeners = listeners;
158 add(contextMenu);
160 // add a mouse listener that causes the pop-up to appear appropriately.
161 MouseListener mouseListener = new MouseAdapter() {
162 public void mousePressed(MouseEvent e) {
163 if (e.isPopupTrigger()) {
164 e.consume();
165 contextMenu.show(TrivialApplet.this, e.getX(), e.getY());
170 addMouseListener(mouseListener);
173 private MenuItem newItem(String title, ActionListener listener) {
174 MenuItem item = new MenuItem(title);
175 item.addActionListener(listener);
176 return item;
179 private Frame getFrame() {
180 Component p = this;
181 while (p != null && !(p instanceof Frame))
182 p = getParent();
183 return (Frame)p;
186 // public void paint( Graphics g ) {}
188 public boolean mouseEnter(Event evt, int x, int y) {
189 showStatus("Welcome!");
190 return true;
193 public boolean mouseExit(Event evt, int x, int y) {
194 showStatus("See you later!");
195 return true;
198 public void print(String message) {
199 JSObject window = JSObject.getWindow(this);
200 Object[] args = { message };
201 window.call("println", args);
204 public int getAppletCount() {
205 return appletCount;