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

Last change on this file was 12, checked in by lpino, 18 years ago
  • Initial commit
File size: 17.7 KB
Line 
1/*
2 * SWT007_01.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
18import java.util.ArrayList;
19import java.util.Iterator;
20
21/**
22 * This testcase allows to create Buttons with all possible styles (except
23 * alignment styles that are not currently suppored by SWT for OS/2) and
24 * lay them out in different ways. It also tests SWT.FocusIn.FocusOut,
25 * SWT.Selection and SWT.Activate/Deactivate/Iconify/Deiconify event delivery,
26 * as well as focus traversal using the keyboard in a standard way (TAB,
27 * arrows, mnemonics).
28 *
29 * To check that the testcase succeeds, a careful comparison with SWT for
30 * Win32 should be done.
31 */
32
33public class SWT007_01 extends SWTTestCase {
34
35static {
36 STEP = "007";
37 TEST = "01";
38 DESC = "Buttons & Focus Traversal";
39}
40
41public static void main (String [] args) {
42 go (new SWT007_01 ());
43}
44
45int page = 1;
46final static int LAST_PAGE = 1;
47
48Image background = null;
49
50Button next, prev;
51
52boolean drawCheckers = false;
53boolean isFillLayout = false;
54boolean isFlat = false;
55boolean isEnabled = true;
56boolean isVisible = true;
57boolean isColorFgnd = false;
58boolean isColorBgnd = false;
59String isImage = "t";
60
61Color clrForeground = null;
62Color clrBackground = null;
63Image imgBitmap, imgIcon32x32, imgIcon48x48;
64
65boolean isCustomFont = false;
66Font fntCustom;
67
68ArrayList buttons = new ArrayList();
69
70final static FillLayout fillLayout = new FillLayout (SWT.VERTICAL);
71final static RowLayout rowLayout = new RowLayout (SWT.VERTICAL);
72static {
73 rowLayout.marginLeft = rowLayout.marginRight =
74 rowLayout.marginTop = rowLayout.marginBottom = 0;
75 rowLayout.spacing = 4;
76}
77
78Canvas canvas;
79
80final static String imageDir = System.getProperty ("user.dir") +
81 "/tests/SWT/images/";
82
83class MyFocusListener implements FocusListener {
84 public void focusGained (FocusEvent e) {
85 System.out.println("FocusIn: " + e.widget);
86 }
87 public void focusLost (FocusEvent e) {
88 System.out.println("FocusOut: " + e.widget);
89 }
90}
91
92class MySelectionListener implements SelectionListener {
93 public void widgetDefaultSelected (SelectionEvent e) {
94 System.out.println("DefaultSelected: " + e.widget + ", (" + e.item +"), [" +
95 e.x + "," + e.y + ";" + e.width + "," + e.height + "], " +
96 "selected=" + ((Button)e.widget).getSelection());
97 }
98 public void widgetSelected (SelectionEvent e) {
99 System.out.println("Selected: " + e.widget + ", (" + e.item +"), [" +
100 e.x + "," + e.y + ";" + e.width + "," + e.height + "], " +
101 "selected=" + ((Button)e.widget).getSelection());
102 }
103}
104
105class MyShellListener implements ShellListener {
106 public void shellActivated (ShellEvent e) {
107 System.out.println("shellActivated: " + e.widget);
108 }
109 public void shellClosed (ShellEvent e) {
110 System.out.println("shellClosed: " + e.widget);
111 }
112 public void shellDeactivated (ShellEvent e) {
113 System.out.println("shellDeactivated: " + e.widget);
114 }
115 public void shellDeiconified (ShellEvent e) {
116 System.out.println("shellDeiconified: " + e.widget);
117 }
118 public void shellIconified (ShellEvent e) {
119 System.out.println("shellIconified: " + e.widget);
120 }
121}
122
123//@@TODO (dmik): seems to work well, disable for cleaner output
124//class MyTraverseListener implements TraverseListener {
125// public void keyTraversed (TraverseEvent e) {
126// System.out.println("traversed: " + e.widget + ", detail=" + e.detail);
127// }
128//}
129
130Shell createTopShell (Display display) {
131 return new Shell (display,
132 SWT.SHELL_TRIM | SWT.NO_REDRAW_RESIZE); // | SWT.NO_BACKGROUND); // | SWT.NO_MERGE_PAINTS);
133}
134
135void createButtons (Composite composite, boolean isFlat) {
136 Iterator it = buttons.iterator();
137 while (it.hasNext()) ((Control)it.next()).dispose();
138 buttons.clear();
139
140 int flat = isFlat ? SWT.FLAT : 0;
141 Button b;
142 b = new Button (composite, SWT.PUSH | flat);
143 b.setText ("pus&h");
144 buttons.add (b);
145 b = new Button (composite, SWT.TOGGLE | flat);
146 b.setText ("to&ggle");
147 buttons.add (b);
148 b = new Button (composite, SWT.CHECK | flat);
149 b.setText ("check 1");
150 buttons.add (b);
151 b = new Button (composite, SWT.CHECK | flat);
152 b.setText ("check 2");
153 buttons.add (b);
154 b = new Button (composite, SWT.RADIO | flat);
155 b.setText ("radio 1");
156 buttons.add (b);
157 b = new Button (composite, SWT.RADIO | flat);
158 b.setText ("radio 2");
159 buttons.add (b);
160 b = new Button (composite, SWT.RADIO | flat);
161 b.setText ("radio 3");
162 buttons.add (b);
163 b = new Button (composite, SWT.ARROW | SWT.UP | flat);
164 b.setText ("arrow up");
165 buttons.add (b);
166 b = new Button (composite, SWT.ARROW | SWT.DOWN | flat);
167 b.setText ("arrow down");
168 buttons.add (b);
169 b = new Button (composite, SWT.ARROW | SWT.LEFT | flat);
170 b.setText ("arrow left");
171 buttons.add (b);
172 b = new Button (composite, SWT.ARROW | SWT.RIGHT | flat);
173 b.setText ("arrow right");
174 buttons.add (b);
175
176 it = buttons.iterator();
177 while (it.hasNext()) {
178 b = ((Button)it.next());
179 b.setEnabled (isEnabled);
180 b.setVisible (isVisible);
181 if (isColorFgnd) b.setForeground (clrForeground);
182 if (isColorBgnd) b.setBackground (clrBackground);
183 if (isImage.equals ("b")) b.setImage (imgBitmap);
184 else if (isImage.equals ("i")) b.setImage (imgIcon32x32);
185 else if (isImage.equals ("li")) b.setImage (imgIcon48x48);
186 if (isCustomFont) b.setFont (fntCustom);
187 b.addFocusListener (new MyFocusListener());
188 b.addSelectionListener (new MySelectionListener());
189//@@TODO (dmik): seems to work well, disable for cleaner output
190// b.addTraverseListener (new MyTraverseListener());
191 }
192}
193
194void initComponents () {
195 imgBitmap = new Image (display, imageDir + "img01.24.bmp"); //"icon48_full.bmp");
196// imgBitmap = new Image (display, imageDir + "img01.1.tr-b.gif");
197 imgIcon32x32 = new Image (display, imageDir + "test.ico");
198// imgIcon48x48 = new Image (display, imageDir + "test_20x20.ico");
199 ImageData[] iconData = new ImageLoader().load (imageDir + "eclipse.ico");
200 imgIcon48x48 = new Image (display, iconData[iconData.length - 1]);
201
202 fntCustom = new Font (display, "Arial", 24, SWT.NORMAL);
203
204 clrForeground = display.getSystemColor (SWT.COLOR_RED);
205 clrBackground = display.getSystemColor (SWT.COLOR_GREEN);
206
207 shell.addDisposeListener (new DisposeListener () {
208 public void widgetDisposed (DisposeEvent e) {
209 if (background != null) background.dispose();
210 imgBitmap.dispose();
211 imgIcon32x32.dispose();
212 imgIcon48x48.dispose();
213 fntCustom.dispose();
214 }
215 });
216
217 shell.addFocusListener (new MyFocusListener());
218 shell.addShellListener (new MyShellListener());
219
220 canvas = new Canvas (shell, SWT.NO_BACKGROUND);
221 canvas.addPaintListener(new PaintListener () {
222 public void paintControl(PaintEvent event) {
223 GC gc = event.gc;
224
225 Rectangle r = new Rectangle (event.x, event.y, event.width, event.height);
226 gc.setClipping (r);
227
228 drawGrid (gc,
229 (r.x / 20) * 20, (r.y / 20) * 20,
230 r.width + 20, r.height + 20, 10);
231
232 switch (page) {
233 case 1: {
234 break;
235 }
236 }
237 }
238 });
239
240 createButtons (canvas, isFlat);
241 canvas.setLayout (isFillLayout ? (Layout) fillLayout : (Layout) rowLayout);
242
243 // -------------------------------
244
245 Composite bottom = new Composite (shell, 0);
246
247 Composite group1 = new Composite (bottom, 0); //SWT.NO_BACKGROUND);
248 group1.setLayout (new FillLayout (SWT.VERTICAL));
249
250 prev = new Button (group1, SWT.PUSH);
251 prev.setText ("< &Previous");
252 prev.setEnabled (page > 1);
253 prev.addSelectionListener (new SelectionAdapter () {
254 public void widgetSelected (SelectionEvent e) {
255 if (page > 1) {
256 page --;
257 updateTitle();
258 }
259 if (page == 1) prev.setEnabled (false);
260 if (page == LAST_PAGE - 1) next.setEnabled (true);
261 }
262 });
263 next = new Button (group1, SWT.PUSH);
264 next.setText ("&Next >");
265 next.setEnabled (LAST_PAGE > page);
266 next.addSelectionListener (new SelectionAdapter () {
267 public void widgetSelected (SelectionEvent e) {
268 if (page < LAST_PAGE) {
269 page ++;
270 updateTitle();
271 }
272 if (page == LAST_PAGE) next.setEnabled (false);
273 if (page == 2) prev.setEnabled (true);
274 }
275 });
276 updateTitle();
277
278 Composite group2 = new Composite (bottom, 0); //, SWT.NO_BACKGROUND);
279 group2.setLayout (new RowLayout (SWT.VERTICAL));
280
281 Button checks = new Button (group2, SWT.CHECK);
282 checks.setText ("&Checkers");
283 checks.setSelection (drawCheckers);
284 checks.addSelectionListener (new SelectionAdapter() {
285 public void widgetSelected (SelectionEvent e) {
286 drawCheckers = ((Button)e.widget).getSelection();
287 canvas.redraw();
288 }
289 });
290 Button layout = new Button (group2, SWT.CHECK);
291 layout.setText ("Fill &Layout");
292 layout.setSelection (isFillLayout);
293 layout.addSelectionListener (new SelectionAdapter() {
294 public void widgetSelected (SelectionEvent e) {
295 isFillLayout = ((Button)e.widget).getSelection();
296 canvas.setLayout (isFillLayout ? (Layout) fillLayout : (Layout) rowLayout);
297 canvas.layout();
298 }
299 });
300 Button flat = new Button (group2, SWT.CHECK);
301 flat.setText ("&Flat");
302 flat.setSelection (isFlat);
303 flat.addSelectionListener (new SelectionAdapter() {
304 public void widgetSelected (SelectionEvent e) {
305 isFlat = ((Button)e.widget).getSelection();
306 createButtons (canvas, isFlat);
307 canvas.layout();
308 }
309 });
310 Button colorCanvas = new Button (group2, SWT.CHECK);
311 colorCanvas.setText ("C&olor Canvas");
312 colorCanvas.addSelectionListener (new SelectionAdapter() {
313 public void widgetSelected (SelectionEvent e) {
314 if (((Button)e.widget).getSelection())
315 canvas.setBackground (display.getSystemColor (SWT.COLOR_CYAN));
316 else
317 canvas.setBackground (null);
318 }
319 });
320 Button colorFgnd = new Button (group2, SWT.CHECK);
321 colorFgnd.setText ("Colo&r Foreground");
322 colorFgnd.setSelection (isColorFgnd);
323 colorFgnd.addSelectionListener (new SelectionAdapter() {
324 public void widgetSelected (SelectionEvent e) {
325 isColorFgnd = ((Button)e.widget).getSelection();
326 Color color = isColorFgnd ? clrForeground : null;
327 Iterator it = buttons.iterator();
328 while (it.hasNext()) ((Button)it.next()).setForeground (color);
329 }
330 });
331 Button colorBgnd = new Button (group2, SWT.CHECK);
332 colorBgnd.setText ("Color &Background");
333 colorFgnd.setSelection (isColorBgnd);
334 colorBgnd.addSelectionListener (new SelectionAdapter() {
335 public void widgetSelected (SelectionEvent e) {
336 isColorBgnd = ((Button)e.widget).getSelection();
337 Color color = isColorBgnd ? clrBackground : null;
338 Iterator it = buttons.iterator();
339 while (it.hasNext()) ((Button)it.next()).setBackground (color);
340 }
341 });
342 Button enabled = new Button (group2, SWT.CHECK);
343 enabled.setText ("&Enabled");
344 enabled.setSelection (isEnabled);
345 enabled.addSelectionListener (new SelectionAdapter() {
346 public void widgetSelected (SelectionEvent e) {
347 isEnabled = ((Button)e.widget).getSelection();
348 Iterator it = buttons.iterator();
349 while (it.hasNext()) ((Control)it.next()).setEnabled (isEnabled);
350 }
351 });
352 Button visible = new Button (group2, SWT.CHECK);
353 visible.setText ("&Visible");
354 visible.setSelection (isVisible);
355 visible.addSelectionListener (new SelectionAdapter() {
356 public void widgetSelected (SelectionEvent e) {
357 isVisible = ((Button)e.widget).getSelection();
358 Iterator it = buttons.iterator();
359 while (it.hasNext()) ((Control)it.next()).setVisible (isVisible);
360 }
361 });
362 Button customFont = new Button (group2, SWT.CHECK);
363 customFont.setText ("Cu&stom Font");
364 customFont.setSelection (false);
365 customFont.addSelectionListener (new SelectionAdapter() {
366 public void widgetSelected (SelectionEvent e) {
367 isCustomFont = ((Button)e.widget).getSelection();
368 Iterator it = buttons.iterator();
369 while (it.hasNext())
370 ((Control)it.next()).setFont (isCustomFont ? fntCustom : null);
371 canvas.layout();
372 }
373 });
374 SelectionListener imgSelAdapter = new SelectionAdapter() {
375 public void widgetSelected (SelectionEvent e) {
376 Button b = (Button) e.widget;
377 if (b.getSelection()) {
378 Image i = null;
379 isImage = (String) b.getData();
380 if (isImage.equals ("b")) i = imgBitmap;
381 else if (isImage.equals ("i")) i = imgIcon32x32;
382 else if (isImage.equals ("li")) i = imgIcon48x48;
383 Iterator it = buttons.iterator();
384 while (it.hasNext()) ((Button)it.next()).setImage (i);
385 }
386 canvas.layout();
387 }
388 };
389// Composite imageGroup = new Composite (group2, 0);
390 Composite imageGroup = group2;
391 Button b;
392 b = new Button (imageGroup, SWT.RADIO);
393 b.setText ("&Text"); b.setData (new String("t")); b.setSelection (true);
394 b.addSelectionListener (imgSelAdapter);
395 b = new Button (imageGroup, SWT.RADIO);
396 b.setText ("B&itmap"); b.setData (new String("b"));
397 b.addSelectionListener (imgSelAdapter);
398 b = new Button (imageGroup, SWT.RADIO);
399 b.setText ("Icon &32x32"); b.setData (new String("i"));
400 b.addSelectionListener (imgSelAdapter);
401 b = new Button (imageGroup, SWT.RADIO);
402 b.setText ("Icon &48x48"); b.setData (new String("li"));
403 b.addSelectionListener (imgSelAdapter);
404 imageGroup.setLayout (new FillLayout (SWT.VERTICAL));
405
406 Button checkAll = new Button (group2, SWT.PUSH);
407 checkAll.setText ("Check &All");
408 checkAll.addSelectionListener (new SelectionAdapter() {
409 public void widgetSelected (SelectionEvent e) {
410 Iterator it = buttons.iterator();
411 while (it.hasNext()) ((Button)it.next()).setSelection (true);
412 }
413 });
414 Button unCheckAll = new Button (group2, SWT.PUSH);
415 unCheckAll.setText ("&Uncheck All");
416 unCheckAll.addSelectionListener (new SelectionAdapter() {
417 public void widgetSelected (SelectionEvent e) {
418 Iterator it = buttons.iterator();
419 while (it.hasNext()) ((Button)it.next()).setSelection (false);
420 }
421 });
422
423 FormData fdata = new FormData();
424 fdata.left = new FormAttachment (0, 0);
425 fdata.right = new FormAttachment (100, 0);
426 fdata.bottom = new FormAttachment (100, 0);
427 group1.setLayoutData (fdata);
428 fdata = new FormData();
429 fdata.top = new FormAttachment (0, 0);
430 group2.setLayoutData (fdata);
431 FormLayout formLayout = new FormLayout ();
432 formLayout.marginWidth = 0;
433 formLayout.marginHeight = 0;
434 bottom.setLayout (formLayout);
435
436 GridData gdata = new GridData (
437 GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL |
438 GridData.VERTICAL_ALIGN_FILL | GridData.GRAB_VERTICAL);
439 canvas.setLayoutData (gdata);
440 gdata = new GridData (
441 GridData.HORIZONTAL_ALIGN_FILL |
442 GridData.VERTICAL_ALIGN_FILL);
443 bottom.setLayoutData (gdata);
444
445 GridLayout gridLayout = new GridLayout (2, false);
446 shell.setLayout (gridLayout);
447
448 Rectangle dr = display.getBounds ();
449 //Rectangle sr = new Rectangle (0, 0, 640, 300);
450 Rectangle sr = shell.getBounds();
451 sr.x = (dr.width-sr.width)/2;
452 sr.y = (dr.height-sr.height)/2;
453 shell.setBounds (sr);
454// shell.setMaximized (true);
455}
456
457void updateTitle () {
458 setTitle ("page #" + page + " of " + LAST_PAGE);
459 shell.redraw ();
460}
461
462void drawGrid (GC gc, int x0, int y0, int width, int height, int step) {
463 if (drawCheckers) {
464 if (background == null) {
465 int w = 20, h = 20;
466 Display display = Display.getDefault();
467 background = new Image (display, w, h);
468 GC bgc = new GC (background);
469 bgc.setBackground (display.getSystemColor (SWT.COLOR_WHITE));
470 bgc.fillRectangle(0, 0, 10, 10);
471 bgc.fillRectangle(10, 10, 10, 10);
472 bgc.setBackground (display.getSystemColor (SWT.COLOR_GRAY));
473 bgc.fillRectangle(10, 0, 10, 10);
474 bgc.fillRectangle(0, 10, 10, 10);
475 bgc.dispose();
476 }
477 Rectangle r = background.getBounds();
478 for (int y = 0; y <= height; y += r.height)
479 for (int x = 0; x <= width; x += r.width)
480 gc.drawImage (background, x0 + x, y0 + y);
481 } else {
482 gc.fillRectangle (gc.getClipping());
483 }
484}
485
486}
487
Note: See TracBrowser for help on using the repository browser.