Merge with trank @ 137446
[official-gcc.git] / libjava / classpath / tools / gnu / classpath / tools / appletviewer / StandaloneAppletWindow.java
blobb92dc50f5eb13cbc7f2e713390dec2d70f910dc6
1 /* StandaloneAppletWindow.java -- an applet frame
2 Copyright (C) 2003, 2004, 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package gnu.classpath.tools.appletviewer;
40 import java.applet.Applet;
41 import java.applet.AppletContext;
42 import java.awt.BorderLayout;
43 import java.awt.Dimension;
44 import java.awt.Frame;
45 import java.awt.Insets;
46 import java.awt.Label;
47 import java.awt.Menu;
48 import java.awt.MenuBar;
49 import java.awt.MenuItem;
50 import java.awt.event.ActionEvent;
51 import java.awt.event.ActionListener;
52 import java.awt.event.ComponentEvent;
53 import java.awt.event.ComponentListener;
54 import java.awt.event.ContainerEvent;
55 import java.awt.event.ContainerListener;
56 import java.awt.event.HierarchyBoundsListener;
57 import java.awt.event.HierarchyEvent;
58 import java.awt.event.HierarchyListener;
59 import java.awt.event.InputMethodEvent;
60 import java.awt.event.InputMethodListener;
61 import java.awt.event.MouseEvent;
62 import java.awt.event.MouseListener;
63 import java.awt.event.MouseMotionListener;
64 import java.awt.event.WindowAdapter;
65 import java.awt.event.WindowEvent;
66 import java.util.HashMap;
67 import java.util.List;
69 class StandaloneAppletWindow
70 extends Frame
71 implements ActionListener, ContainerListener, ComponentListener,
72 MouseListener, MouseMotionListener, InputMethodListener, HierarchyListener,
73 HierarchyBoundsListener
76 // This class implements various listeners because the author of an applet
77 // may attach listeners to it, unaware of the applet's parent (this class).
78 // So, we must pass all listener events on this plugin applet window to the
79 // actual applet.
81 private static int testWindowCount;
82 private static HashMap contexts = new HashMap();
83 private Applet applet;
84 private Label status = new Label();
86 private MenuItem restartItem;
87 private MenuItem reloadItem;
88 private MenuItem cancelItem;
89 private MenuItem saveItem;
90 private MenuItem startItem;
91 private MenuItem cloneItem;
92 private MenuItem tagItem;
93 private MenuItem infoItem;
94 private MenuItem editItem;
95 private MenuItem encodingItem;
96 private MenuItem printItem;
97 private MenuItem propertiesItem;
98 private MenuItem closeItem;
99 private MenuItem quitItem;
101 StandaloneAppletWindow(AppletTag tag, List appletWindows)
103 appletWindows.add(this);
104 applet = Main.createApplet(tag);
106 if (contexts.get(tag.codebase) == null)
107 contexts.put(tag.codebase, new StandaloneAppletContext(appletWindows));
109 setLayout(new BorderLayout());
110 add(applet, BorderLayout.CENTER);
111 add(status, BorderLayout.SOUTH);
113 addWindowListener(new WindowAdapter()
115 public void windowClosing(WindowEvent event)
117 applet.stop();
118 StandaloneAppletWindow.this.hide();
119 System.exit(0);
123 addContainerListener(this);
124 addComponentListener(this);
125 addMouseListener(this);
126 addMouseMotionListener(this);
127 addInputMethodListener(this);
128 addHierarchyListener(this);
129 addHierarchyBoundsListener(this);
131 restartItem = new MenuItem(Messages.getString("StandaloneAppletWindow.MenuRestart"));
132 restartItem.setEnabled(false);
133 restartItem.addActionListener(this);
134 reloadItem = new MenuItem(Messages.getString("StandaloneAppletWindow.MenuReload"));
135 reloadItem.setEnabled(false);
136 reloadItem.addActionListener(this);
137 cancelItem = new MenuItem(Messages.getString("StandaloneAppletWindow.MenuCancel"));
138 cancelItem.setEnabled(false);
139 cancelItem.addActionListener(this);
140 saveItem = new MenuItem(Messages.getString("StandaloneAppletWindow.MenuSave"));
141 saveItem.setEnabled(false);
142 saveItem.addActionListener(this);
143 startItem = new MenuItem(Messages.getString("StandaloneAppletWindow.MenuStart"));
144 startItem.setEnabled(false);
145 startItem.addActionListener(this);
146 cloneItem = new MenuItem(Messages.getString("StandaloneAppletWindow.MenuClone"));
147 cloneItem.setEnabled(false);
148 cloneItem.addActionListener(this);
149 closeItem = new MenuItem(Messages.getString("StandaloneAppletWindow.MenuClose"));
150 closeItem.setEnabled(false);
151 closeItem.addActionListener(this);
152 tagItem =
153 new MenuItem(Messages.getString("StandaloneAppletWindow.MenuTag"));
154 tagItem.setEnabled(false);
155 tagItem.addActionListener(this);
156 infoItem =
157 new MenuItem(Messages.getString("StandaloneAppletWindow.MenuInfo"));
158 infoItem.setEnabled(false);
159 infoItem.addActionListener(this);
160 editItem =
161 new MenuItem(Messages.getString("StandaloneAppletWindow.MenuEdit"));
162 editItem.setEnabled(false);
163 editItem.addActionListener(this);
164 editItem.setEnabled(false);
165 encodingItem =
166 new MenuItem(Messages.getString("StandaloneAppletWindow.MenuEncoding"));
167 encodingItem.setEnabled(false);
168 encodingItem.addActionListener(this);
169 printItem =
170 new MenuItem(Messages.getString("StandaloneAppletWindow.MenuPrint"));
171 printItem.setEnabled(false);
172 printItem.addActionListener(this);
173 propertiesItem =
174 new MenuItem(Messages.getString("StandaloneAppletWindow.MenuProperties"));
175 propertiesItem.setEnabled(false);
176 propertiesItem.addActionListener(this);
177 quitItem = new MenuItem(Messages.getString("StandaloneAppletWindow.MenuQuit"));
178 quitItem.addActionListener(this);
180 MenuBar menuBar = new MenuBar();
181 Menu menuApplet = new Menu(Messages.getString("StandaloneAppletWindow.MenuTitle"));
182 menuBar.add(menuApplet);
183 menuApplet.add(restartItem);
184 menuApplet.add(reloadItem);
185 menuApplet.add(cancelItem);
186 menuApplet.add(saveItem);
187 menuApplet.add(startItem);
188 menuApplet.add(cloneItem);
189 menuApplet.addSeparator();
190 menuApplet.add(tagItem);
191 menuApplet.add(infoItem);
192 menuApplet.add(editItem);
193 menuApplet.add(encodingItem);
194 menuApplet.addSeparator();
195 menuApplet.add(printItem);
196 menuApplet.addSeparator();
197 menuApplet.add(propertiesItem);
198 menuApplet.addSeparator();
199 menuApplet.add(closeItem);
200 menuApplet.add(quitItem);
201 setMenuBar(menuBar);
202 setTitle(Messages.getString("StandaloneAppletWindow.WindowTitle")
203 + " " + tag.code);
205 AppletContext context = (AppletContext) contexts.get(tag.codebase);
206 ((StandaloneAppletContext) context).addApplet(applet);
208 applet.setStub(new CommonAppletStub(tag, context, applet));
210 // Create the frame's peer. Otherwise getPreferredSize will read
211 // its insets as 0.
212 addNotify();
213 Insets i = getInsets();
214 Dimension size = tag.getSize();
215 setSize(i.left + size.width + i.right,
216 i.top + size.height + status.getPreferredSize().height
217 + i.bottom);
218 applet.setSize(size);
220 // Initialize the applet before showing this window so that the
221 // applet doesn't receive events before it has been initialized.
222 applet.init();
223 applet.start();
224 setVisible(true);
227 private void closeWindow()
229 applet.stop();
230 StandaloneAppletViewer.appletWindows.remove(this);
231 StandaloneAppletWindow.this.hide();
234 public void actionPerformed(ActionEvent e)
236 if (e.getSource() == quitItem)
238 closeWindow();
239 System.exit(0);
241 else if (e.getSource() == closeItem)
243 // Close current window.
244 closeWindow();
246 // Exit if there are other windows left.
247 if (StandaloneAppletViewer.appletWindows.isEmpty())
248 System.exit(0);
252 void showStatus(String status)
254 this.status.setText(status);
258 ///////////////////////////////////
259 /// ContainerListener Methods /////
260 ///////////////////////////////////
263 * This method is called when a component is added to the container.
265 * @param event the <code>ContainerEvent</code> indicating component
266 * addition
268 public void componentAdded(ContainerEvent event)
270 if (applet != null)
272 ContainerListener[] l = applet.getContainerListeners();
273 for (int i = 0; i < l.length; i++)
274 l[i].componentAdded(event);
279 * This method is called when a component is removed from the container.
281 * @param event the <code>ContainerEvent</code> indicating component removal
283 public void componentRemoved(ContainerEvent event)
285 if (applet != null)
287 ContainerListener[] l = applet.getContainerListeners();
288 for (int i = 0; i < l.length; i++)
289 l[i].componentRemoved(event);
293 ///////////////////////////////////
294 /// ComponentListener Methods /////
295 ///////////////////////////////////
298 * This method is called when the component is resized.
300 * @param event the <code>ComponentEvent</code> indicating the resize
302 public void componentResized(ComponentEvent event)
304 if (applet != null)
306 ComponentListener[] l = applet.getComponentListeners();
307 for (int i = 0; i < l.length; i++)
308 l[i].componentResized(event);
313 * This method is called when the component is moved.
315 * @param event the <code>ComponentEvent</code> indicating the move
317 public void componentMoved(ComponentEvent event)
319 if (applet != null)
321 ComponentListener[] l = applet.getComponentListeners();
322 for (int i = 0; i < l.length; i++)
323 l[i].componentMoved(event);
328 * This method is called when the component is made visible.
330 * @param event the <code>ComponentEvent</code> indicating the visibility
332 public void componentShown(ComponentEvent event)
334 if (applet != null)
336 ComponentListener[] l = applet.getComponentListeners();
337 for (int i = 0; i < l.length; i++)
338 l[i].componentShown(event);
343 * This method is called when the component is hidden.
345 * @param event the <code>ComponentEvent</code> indicating the visibility
347 public void componentHidden(ComponentEvent event)
349 if (applet != null)
351 ComponentListener[] l = applet.getComponentListeners();
352 for (int i = 0; i < l.length; i++)
353 l[i].componentHidden(event);
357 ///////////////////////////////////
358 ////// MouseListener Methods //////
359 ///////////////////////////////////
362 * This method is called when the mouse is clicked (pressed and released
363 * in short succession) on a component.
365 * @param event the <code>MouseEvent</code> indicating the click
367 public void mouseClicked(MouseEvent event)
369 if (applet != null)
371 MouseListener[] l = applet.getMouseListeners();
372 for (int i = 0; i < l.length; i++)
373 l[i].mouseClicked(event);
378 * This method is called when the mouse is pressed over a component.
380 * @param event the <code>MouseEvent</code> for the press
382 public void mousePressed(MouseEvent event)
384 if (applet != null)
386 MouseListener[] l = applet.getMouseListeners();
387 for (int i = 0; i < l.length; i++)
388 l[i].mousePressed(event);
393 * This method is called when the mouse is released over a component.
395 * @param event the <code>MouseEvent</code> for the release
397 public void mouseReleased(MouseEvent event)
399 if (applet != null)
401 MouseListener[] l = applet.getMouseListeners();
402 for (int i = 0; i < l.length; i++)
403 l[i].mouseReleased(event);
408 * This method is called when the mouse enters a component.
410 * @param event the <code>MouseEvent</code> for the entry
412 public void mouseEntered(MouseEvent event)
414 if (applet != null)
416 MouseListener[] l = applet.getMouseListeners();
417 for (int i = 0; i < l.length; i++)
418 l[i].mouseEntered(event);
422 /**
423 * This method is called when the mouse exits a component.
425 * @param event the <code>MouseEvent</code> for the exit
427 public void mouseExited(MouseEvent event)
429 if (applet != null)
431 MouseListener[] l = applet.getMouseListeners();
432 for (int i = 0; i < l.length; i++)
433 l[i].mouseExited(event);
437 ///////////////////////////////////
438 /// MouseMotionListener Methods ///
439 ///////////////////////////////////
442 * This method is called when the mouse is moved over a component
443 * while a button has been pressed.
445 * @param event the <code>MouseEvent</code> indicating the motion
447 public void mouseDragged(MouseEvent event)
449 if (applet != null)
451 MouseMotionListener[] l = applet.getMouseMotionListeners();
452 for (int i = 0; i < l.length; i++)
453 l[i].mouseDragged(event);
458 * This method is called when the mouse is moved over a component
459 * while no button is pressed.
461 * @param event the <code>MouseEvent</code> indicating the motion
463 public void mouseMoved(MouseEvent event)
465 if (applet != null)
467 MouseMotionListener[] l = applet.getMouseMotionListeners();
468 for (int i = 0; i < l.length; i++)
469 l[i].mouseMoved(event);
473 ///////////////////////////////////
474 /// InputMethodListener Methods ///
475 ///////////////////////////////////
478 * This method is called when the text is changed.
480 * @param event the <code>InputMethodEvent</code> indicating the text change
482 public void inputMethodTextChanged(InputMethodEvent event)
484 if (applet != null)
486 InputMethodListener[] l = applet.getInputMethodListeners();
487 for (int i = 0; i < l.length; i++)
488 l[i].inputMethodTextChanged(event);
493 * This method is called when the cursor position within the text is changed.
495 * @param event the <code>InputMethodEvent</code> indicating the change
497 public void caretPositionChanged(InputMethodEvent event)
499 if (applet != null)
501 InputMethodListener[] l = applet.getInputMethodListeners();
502 for (int i = 0; i < l.length; i++)
503 l[i].caretPositionChanged(event);
507 ///////////////////////////////////
508 //// HierarchyListener Methods ////
509 ///////////////////////////////////
512 * Called when the hierarchy of this component changes. Use
513 * <code>getChangeFlags()</code> on the event to see what exactly changed.
515 * @param event the event describing the change
517 public void hierarchyChanged(HierarchyEvent event)
519 if (applet != null)
521 HierarchyListener[] l = applet.getHierarchyListeners();
522 for (int i = 0; i < l.length; i++)
523 l[i].hierarchyChanged(event);
527 /////////////////////////////////////////
528 //// HierarchyBoundsListener Methods ////
529 /////////////////////////////////////////
532 * Called when an ancestor component of the source is moved.
534 * @param e the event describing the ancestor's motion
536 public void ancestorMoved(HierarchyEvent e)
538 if (applet != null)
540 HierarchyBoundsListener[] l = applet.getHierarchyBoundsListeners();
541 for (int i = 0; i < l.length; i++)
542 l[i].ancestorMoved(e);
547 * Called when an ancestor component is resized.
549 * @param e the event describing the ancestor's resizing
551 public void ancestorResized(HierarchyEvent e)
553 if (applet != null)
555 HierarchyBoundsListener[] l = applet.getHierarchyBoundsListeners();
556 for (int i = 0; i < l.length; i++)
557 l[i].ancestorResized(e);