source: trunk/tests/SWT/java/SWT004_02.java

Last change on this file was 12, checked in by lpino, 18 years ago
  • Initial commit
File size: 7.2 KB
Line 
1/*
2 * SWT004_02.java
3 */
4
5/*
6 * Copyright (c) 2002, 2004 EclipseOS2 Team.
7 * This file is made available under the terms of the Common Public License v1.0
8 * which accompanies this distribution, and is available at
9 * http://www.eclipse.org/legal/cpl-v10.html
10 */
11
12import org.eclipse.swt.*;
13import org.eclipse.swt.widgets.*;
14import org.eclipse.swt.events.*;
15import org.eclipse.swt.graphics.*;
16import org.eclipse.swt.layout.*;
17
18/**
19 * This example tests the basics of layout management and some button
20 * functionality.
21 *
22 * The Next button is used to go through sub-objectives. The Check Me! button
23 * can be used to disable size/move logging.
24 */
25
26public class SWT004_02 extends SWTTestCase {
27
28static {
29 STEP= "004";
30 TEST = "02";
31 DESC = "Buttons";
32}
33
34boolean sizeMoveLogging = true;
35
36public static void main (String [] args) {
37 go (new SWT004_02 ());
38}
39
40Shell createTopShell (Display display) {
41
42 final Shell shell = new Shell (display); //, SWT.SHELL_TRIM | SWT.NO_REDRAW_RESIZE);
43 final Color bg = new Color (display, 254, 254, 180);
44
45 shell.addDisposeListener (new DisposeListener () {
46 public void widgetDisposed (DisposeEvent e) {
47 bg.dispose ();
48 }
49 });
50
51 shell.addShellListener (new ShellAdapter () {
52 public void shellIconified (ShellEvent e) {
53 say ("Shell Iconified: " + e.widget);
54 say (" minimized = " + shell.getMinimized ());
55 say (" maximized = " + shell.getMaximized ());
56 }
57 public void shellDeiconified (ShellEvent e) {
58 say ("Shell Deiconified: " + e.widget);
59 say (" minimized = " + shell.getMinimized ());
60 say (" maximized = " + shell.getMaximized ());
61 }
62 });
63
64 ControlListener cl = new ControlAdapter () {
65 public void controlMoved (ControlEvent e) {
66 if (!sizeMoveLogging) return;
67 say ("controlMoved: " + e.widget +
68 ", bounds = " + ((Control)e.widget).getBounds ());
69 }
70 public void controlResized (ControlEvent e) {
71 if (!sizeMoveLogging) return;
72 say ("controlResized: " + e.widget +
73 (e.widget instanceof Shell ?
74 ", client = " + ((Shell)e.widget).getClientArea () :
75 ", bounds = " + ((Control)e.widget).getBounds ()));
76 }
77 };
78
79 SelectionListener sl = new SelectionAdapter () {
80 public void widgetSelected (SelectionEvent e) {
81 say ("controlSelected: " + e.widget +
82 ", time = " + Integer.toHexString (e.time) +
83 ", detail = " + Integer.toHexString (e.detail) +
84 ", item = " + e.item +
85 ", sel = " + (
86 ((e.widget instanceof Button) ?
87 ((Button)e.widget).getSelection () ?
88 "yes" : "no" : "[NA]")
89 )
90 );
91 }
92 };
93
94 final PaintListener pl = new PaintListener () {
95 public void paintControl(PaintEvent e) {
96 GC gc = e.gc;
97 Rectangle r = null;
98 if (e.widget instanceof Shell)
99 r = ((Shell)e.widget).getClientArea();
100 else if (e.widget instanceof Control)
101 r = ((Control)e.widget).getBounds();
102 if (r != null)
103 drawGrid (gc, 0, 0, r.width, r.height, 20);
104 }
105 };
106
107 final Button push = new Button (shell, SWT.PUSH);
108 push.addControlListener (cl);
109 push.addSelectionListener (sl);
110 push.setText ("Push me!");
111 push.pack();
112
113 final Button check = new Button (shell, SWT.CHECK);
114 check.addControlListener (cl);
115 check.addSelectionListener (sl);
116 check.setText ("Check me!");
117 check.pack();
118 check.setLocation (40, 40);
119 check.addSelectionListener (new SelectionAdapter () {
120 public void widgetSelected (SelectionEvent e) {
121 sizeMoveLogging = !((Button)e.widget).getSelection ();
122 if (sizeMoveLogging)
123 say ("Size/move logging enabled");
124 else
125 say ("Size/move logging disabled");
126 }
127 });
128
129 final Button next = new Button (shell, SWT.PUSH);
130 next.addControlListener (cl);
131 next.addSelectionListener (sl);
132 next.setText ("Next >");
133 next.setBackground (bg);
134 next.setBounds (100, 100, 120, 40);
135 next.addSelectionListener (new SelectionAdapter () {
136 int timesPressed = 0;
137 public void widgetSelected (SelectionEvent e) {
138 switch (++timesPressed) {
139 case 1:
140 saySubObjective ("\nAdd shell painter");
141 shell.addPaintListener (pl);
142 shell.setBackground (bg);
143 push.setBackground (bg);
144 check.setBackground (bg);
145 break;
146 case 2:
147 saySubObjective ("\nResize and move 'Next' button");
148 next.setBounds (50, 10, 200, 40);
149 break;
150 case 3:
151 saySubObjective ("\nSet FillLayout into the shell and restore its background color");
152 shell.setBackground (null);
153 shell.setLayout (new FillLayout (SWT.VERTICAL));
154 shell.pack();
155 break;
156 case 4:
157 saySubObjective ("\nSet push button painter");
158 push.addPaintListener (pl);
159 push.redraw ();
160 break;
161 case 5:
162 saySubObjective ("\nSet RowLayout into the shell");
163 push.removePaintListener (pl);
164 //push.redraw ();
165 RowLayout rowLayout = new RowLayout();
166 rowLayout.wrap = false;
167 rowLayout.pack = false;
168 rowLayout.justify = true;
169 rowLayout.type = SWT.VERTICAL;
170 rowLayout.marginLeft = 5;
171 rowLayout.marginTop = 5;
172 rowLayout.marginRight = 5;
173 rowLayout.marginBottom = 5;
174 rowLayout.spacing = 0;
175 shell.setLayout(rowLayout);
176 shell.pack();
177
178 next.setText ("Finish");
179 break;
180 default:
181 shell.close ();
182 }
183 }
184 });
185
186 shell.addControlListener (cl);
187
188 say ("shell.getBorderWidth () = " + shell.getBorderWidth ());
189 say ("button.getBorderWidth () = " + push.getBorderWidth ());
190
191 Rectangle dr = display.getBounds ();
192 Rectangle sr = new Rectangle (0, 0, 512, 300); //shell.getBounds ();
193 sr.x = (dr.width-sr.width)/2;
194 sr.y = (dr.height-sr.height)/2;
195 shell.setBounds (sr);
196
197 return shell;
198}
199
200void drawGrid (GC gc, int x0, int y0, int width, int height, int step)
201{
202 int i;
203 for (i = 0; i <= width/step; i++)
204 gc.drawLine (x0+i*step, y0, x0+i*step, y0+height);
205 for (i = 0; i <= height/step; i++)
206 gc.drawLine (x0, y0+i*step, x0+width, y0+i*step);
207}
208
209}
Note: See TracBrowser for help on using the repository browser.