2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / gnu / java / awt / peer / gtk / TestAWT.java
blob48cdce1a1fe6878b0581120c71e97e2d62ab3651
1 /* TestAWT.java -- Tests the AWT like testgtk
2 Copyright (C) 1998, 1999, 2002 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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 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 import java.awt.List;
39 import java.util.*;
40 import java.awt.*;
41 import java.awt.event.*;
42 import java.awt.peer.*;
44 class TestAWT
46 public static void main(String args[])
48 if (args.length==0)
50 Properties prop = System.getProperties ();
51 prop.put ("awt.toolkit", "gnu.java.awt.peer.gtk.GtkToolkit");
53 MainWindow f = new MainWindow();
54 System.out.println(f.isDisplayable());
55 f.show();
56 System.out.println(f.isDisplayable());
60 interface SubWindow
62 public void init ();
65 class PrettyPanel extends Panel
67 Insets myInsets;
69 public PrettyPanel ()
71 myInsets = new Insets (10, 10, 10, 10);
73 public Insets getInsets ()
75 return myInsets;
79 abstract class PrettyFrame extends Frame
81 public PrettyFrame ()
83 ((BorderLayout) getLayout ()).setHgap (5);
84 ((BorderLayout) getLayout ()).setVgap (5);
87 // public Insets getInsets()
88 // {
89 // Insets oldInsets = super.getInsets ();
90 // return new Insets (oldInsets.top+10,
91 // oldInsets.left+10,
92 // oldInsets.bottom+10,
93 // oldInsets.right+10);
94 // }
97 abstract class SubFrame extends PrettyFrame implements SubWindow
99 boolean initted = false;
101 public void setVisible (boolean visible)
103 if (!initted && visible)
104 init();
105 super.setVisible (visible);
109 class MainWindow extends PrettyFrame implements ActionListener
111 Button closeButton;
113 Hashtable windows;
114 Vector buttons;
116 void addSubWindow (String name, SubWindow w)
118 Button b = new Button (name);
119 b.addActionListener (this);
121 buttons.addElement (b);
122 windows.put (b, w);
125 MainWindow ()
127 MenuBar mb = new MenuBar ();
128 Menu menu = new Menu ("File");
129 Menu submenu = new Menu ("Testing");
130 submenu.add (new CheckboxMenuItem ("Foobar"));
131 menu.add (submenu);
132 mb.add (menu);
134 setMenuBar (mb);
136 add (new Label ("Classpath v0.0.0"), "North");
138 closeButton = new Button ("Close");
139 closeButton.addActionListener (this);
140 closeButton.setFont (new Font ("Serif", Font.BOLD | Font.ITALIC, 18));
141 add (closeButton, "South");
143 windows = new Hashtable ();
144 buttons = new Vector ();
146 addSubWindow ("Buttons", new ButtonsWindow ());
147 addSubWindow ("Cursors", new CursorsWindow ());
148 addSubWindow ("Dialog", new DialogWindow (this));
149 addSubWindow ("File", new FileWindow (this));
150 addSubWindow ("Labels", new LabelWindow ());
151 addSubWindow ("List", new ListWindow ());
152 addSubWindow ("Radio Buttons", new RadioWindow ());
153 addSubWindow ("TextField", new TextFieldWindow ());
155 Panel sp = new Panel();
156 PrettyPanel p = new PrettyPanel();
157 p.setLayout (new GridLayout (windows.size(), 1));
159 for (Enumeration e = buttons.elements (); e.hasMoreElements (); )
161 p.add ((Button) e.nextElement ());
164 sp.add (p);
165 add (sp, "Center");
167 setSize (200, 86 + (windows.size ()*22));
168 setTitle ("TestAWT");
171 public void actionPerformed (ActionEvent evt)
173 Button source = (Button) evt.getSource ();
175 if (source==closeButton)
177 System.getProperties ().list (System.out);
178 dispose();
179 System.exit (0);
182 Window w = (Window) windows.get (source);
183 if (w.isVisible ())
184 w.dispose ();
185 else
187 w.setVisible (true);
188 w.show();
193 class ButtonsWindow extends SubFrame implements ActionListener
195 Button b[] = new Button [9];
197 public void init ()
199 initted = true;
200 Panel p = new Panel ();
201 p.setLayout (new GridLayout (0, 3, 5, 5));
203 for (int i=0; i<9; i++)
205 b[i]=new Button ("button" + (i+1));
206 b[i].addActionListener (this);
209 p.add (b[0]);
210 p.add (b[6]);
211 p.add (b[4]);
212 p.add (b[8]);
213 p.add (b[1]);
214 p.add (b[7]);
215 p.add (b[3]);
216 p.add (b[5]);
217 p.add (b[2]);
219 add (p, "North");
221 Button cb = new Button ("close");
222 cb.addActionListener(new ActionListener () {
223 public void actionPerformed (ActionEvent e) {
224 dispose();
227 add (cb, "South");
228 setTitle ("Buttons");
231 public void actionPerformed (ActionEvent evt)
233 Button source = (Button) evt.getSource ();
235 for (int i=0; i<9; i++)
237 if (source == b[i])
239 int i2=((i+1)==9)?0:(i+1);
240 if (b[i2].isVisible())
241 b[i2].setVisible(false);
242 else
243 b[i2].setVisible(true);
250 class DialogWindow extends Dialog implements SubWindow
252 Label text;
253 boolean initted = false;
255 public DialogWindow (Frame f)
257 super (f, true);
260 public void setVisible (boolean visible)
262 if (!initted && visible)
263 init();
264 super.setVisible (visible);
267 public void init ()
269 text = new Label ("Dialog Test");
270 text.setAlignment (Label.CENTER);
272 add (text, "North");
273 text.setVisible (false);
275 Panel p = new PrettyPanel();
277 Button cb = new Button ("OK");
278 cb.addActionListener(new ActionListener () {
279 public void actionPerformed (ActionEvent e)
281 dispose();
285 p.setLayout (new GridLayout (1, 2));
286 ((GridLayout) p.getLayout ()).setHgap (5);
287 ((GridLayout) p.getLayout ()).setVgap (5);
288 p.add (cb);
290 Button toggle = new Button ("Toggle");
291 p.add (toggle);
293 toggle.addActionListener(new ActionListener () {
294 public void actionPerformed (ActionEvent e)
296 if (text.isVisible ())
297 text.setVisible (false);
298 else
299 text.setVisible (true);
300 doLayout();
304 add (p, "South");
305 setTitle ("Dialog");
306 setSize (130, 70);
310 class CursorsWindow extends SubFrame implements ItemListener
312 Choice cursorChoice;
313 Canvas cursorCanvas;
315 public void init ()
317 cursorChoice = new Choice();
318 cursorChoice.add ("Default");
319 cursorChoice.add ("Crosshair");
320 cursorChoice.add ("Text");
321 cursorChoice.add ("Wait");
322 cursorChoice.add ("Southwest Resize");
323 cursorChoice.add ("Southeast Resize");
324 cursorChoice.add ("Northwest Resize");
325 cursorChoice.add ("Northeast Resize");
326 cursorChoice.add ("North Resize");
327 cursorChoice.add ("South Resize");
328 cursorChoice.add ("West Resize");
329 cursorChoice.add ("East Resize");
330 cursorChoice.add ("Hand");
331 cursorChoice.add ("Move");
333 cursorChoice.addItemListener(this);
335 add (cursorChoice, "North");
337 cursorCanvas = new Canvas ()
339 public void paint (Graphics g)
341 Dimension d = this.getSize();
342 g.setColor (Color.white);
343 g.fillRect (0, 0, d.width, d.height/2);
344 g.setColor (Color.black);
345 g.fillRect (0, d.height/2, d.width, d.height/2);
346 g.setColor (this.getBackground());
347 g.fillRect (d.width/3, d.height/3, d.width/3,
348 d.height/3);
352 cursorCanvas.setSize (80,80);
354 add (cursorCanvas, "Center");
356 Button cb = new Button ("Close");
357 cb.addActionListener(new ActionListener () {
358 public void actionPerformed (ActionEvent e) {
359 dispose();
363 add (cb, "South");
364 setTitle ("Cursors");
365 setSize (160, 180);
368 public void itemStateChanged (ItemEvent e)
370 cursorCanvas.setCursor (Cursor.getPredefinedCursor (cursorChoice.getSelectedIndex()));
374 class TextFieldWindow extends SubFrame implements ItemListener
376 Checkbox editable, visible, sensitive;
377 TextField text;
379 public void init ()
381 initted = true;
382 text = new TextField ("hello world");
383 add (text, "North");
385 Panel p = new Panel();
386 p.setLayout (new GridLayout (3, 1));
387 ((GridLayout) p.getLayout ()).setHgap (5);
388 ((GridLayout) p.getLayout ()).setVgap (5);
390 editable = new Checkbox("Editable", true);
391 p.add (editable);
392 editable.addItemListener (this);
394 visible = new Checkbox("Visible", true);
395 p.add (visible);
396 visible.addItemListener (this);
398 sensitive = new Checkbox("Sensitive", true);
399 p.add (sensitive);
400 sensitive.addItemListener (this);
402 add (p, "Center");
404 Button cb = new Button ("Close");
405 cb.addActionListener(new ActionListener () {
406 public void actionPerformed (ActionEvent e) {
407 dispose();
411 add (cb, "South");
412 setTitle ("TextField");
413 setSize (160, 180);
416 public void itemStateChanged (ItemEvent e)
418 boolean on=true;
420 if (e.getStateChange () == ItemEvent.DESELECTED)
421 on=false;
422 if (e.getSource() == editable)
423 text.setEditable (on);
424 if (e.getSource() == visible)
425 if (on)
426 text.setEchoChar ((char) 0);
427 else
428 text.setEchoChar ('*');
429 if (e.getSource() == sensitive)
430 text.setEnabled (on);
435 class FileWindow extends FileDialog implements SubWindow
437 boolean initted = false;
439 public FileWindow (MainWindow mw)
441 super (mw);
444 public void setVisible (boolean visible)
446 if (!initted && visible)
447 init();
448 super.setVisible (visible);
451 public void init()
453 initted = true;
457 class LabelWindow extends SubFrame
459 public void init ()
461 initted = true;
463 Panel p = new Panel();
464 p.setLayout (new GridLayout (3, 1));
465 ((GridLayout) p.getLayout ()).setHgap (5);
466 ((GridLayout) p.getLayout ()).setVgap (5);
468 p.add (new Label ("left justified label", Label.LEFT));
469 p.add (new Label ("center justified label", Label.CENTER));
470 p.add (new Label ("right justified label", Label.RIGHT));
472 add (p, "Center");
474 Button cb = new Button ("Close");
475 cb.addActionListener(new ActionListener () {
476 public void actionPerformed (ActionEvent e) {
477 dispose();
481 add (cb, "South");
482 setTitle ("Labels");
483 setSize (160, 180);
487 class ListWindow extends SubFrame
489 public void init ()
491 initted = true;
493 Panel p = new Panel ();
494 p.setLayout (new GridLayout (3, 1));
496 List l = new List (5, true);
497 for (int i = 0; i < 10; i++)
498 l.add ("added item " + i);
500 p.add (l);
502 add (p, "Center");
504 Button cb = new Button ("Close");
505 cb.addActionListener(new ActionListener () {
506 public void actionPerformed (ActionEvent e) {
507 dispose();
511 add (cb, "South");
512 setTitle ("List");
513 setSize (85, 167);
518 class RadioWindow extends SubFrame
520 public void init ()
522 initted = true;
524 Panel p = new Panel();
525 p.setLayout (new GridLayout (3, 1));
526 ((GridLayout) p.getLayout ()).setHgap (5);
527 ((GridLayout) p.getLayout ()).setVgap (5);
529 final CheckboxGroup cg = new CheckboxGroup();
530 final Checkbox[] boxes = new Checkbox[3];
531 for (int i = 0; i < 3; ++i)
533 boxes[i] = new Checkbox("button" + i, cg, i == 0);
534 p.add(boxes[i]);
537 add (p, "North");
539 p = new Panel();
540 p.setLayout (new GridLayout (1, 3));
541 ((GridLayout) p.getLayout ()).setHgap (5);
542 ((GridLayout) p.getLayout ()).setVgap (5);
544 for (int i = 0; i < 3; ++i)
546 final int val = i;
547 Button tweak = new Button ("Set " + i);
548 tweak.addActionListener(new ActionListener ()
550 public void actionPerformed (ActionEvent e)
552 cg.setSelectedCheckbox(boxes[val]);
555 p.add(tweak);
558 add (p, "Center");
560 Button cb = new Button ("Close");
561 cb.addActionListener(new ActionListener () {
562 public void actionPerformed (ActionEvent e) {
563 dispose();
567 add (cb, "South");
568 setTitle ("Radio Buttons");
569 setSize (85, 167);