KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > RootPaneTest


1 /*
2  * $Id: RootPaneTest.java,v 1.1 2004/07/31 00:03:28 davidson1 Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.swing;
9
10 import java.util.Timer JavaDoc;
11 import java.util.TimerTask JavaDoc;
12
13 import java.awt.Component JavaDoc;
14 import java.awt.event.ActionEvent JavaDoc;
15 import java.awt.event.ActionListener JavaDoc;
16 import java.awt.event.MouseAdapter JavaDoc;
17 import java.awt.event.MouseEvent JavaDoc;
18 import java.awt.event.MouseListener JavaDoc;
19 import javax.swing.AbstractAction JavaDoc;
20 import javax.swing.Action JavaDoc;
21 import javax.swing.BorderFactory JavaDoc;
22 import javax.swing.JButton JavaDoc;
23 import javax.swing.JLabel JavaDoc;
24 import javax.swing.JMenu JavaDoc;
25 import javax.swing.JMenuBar JavaDoc;
26 import javax.swing.JPanel JavaDoc;
27 import javax.swing.JToolBar JavaDoc;
28
29 import org.jdesktop.swing.JXStatusBar;
30 import org.jdesktop.swing.JXFrame;
31 import org.jdesktop.swing.JXRootPane;
32 import org.jdesktop.swing.event.MessageListener;
33 import org.jdesktop.swing.event.MessageSource;
34 import org.jdesktop.swing.event.MessageSourceSupport;
35 import org.jdesktop.swing.event.ProgressListener;
36 import org.jdesktop.swing.event.ProgressSource;
37 import junit.framework.TestCase;
38
39 public class RootPaneTest extends TestCase {
40
41     private Action JavaDoc[] actions;
42     private TestComponent[] comps;
43
44     private static final int HEAD = 0;
45     private static final int BODY = 1;
46     private static final int ARMS = 2;
47     private static final int LEGS = 3;
48
49     protected void setUp() {
50     actions = new Action JavaDoc[4];
51     actions[0] = new TestAction("New", 'N', "Create a new item");
52     actions[1] = new TestAction("Open", 'O', "Opens an item");
53     actions[2] = new TestAction("Save", 'S', "Saves an item");
54     actions[3] = new TestAction("Exit", 'X', "Exits the application");
55
56     comps = new TestComponent[4];
57     comps[0] = new TestComponent("Head");
58     comps[1] = new TestComponent("Body");
59     comps[2] = new TestComponent("Arms");
60     comps[3] = new TestComponent("Legs");
61     }
62
63     protected void tearDown() {
64     for (int i = 0; i < actions.length; i++) {
65         actions[i] = null;
66         comps[i] = null;
67     }
68     actions = null;
69     comps = null;
70     }
71
72     /**
73      * Simple test to ensure that components are added/removed
74      * and registered/unregistered correctly.
75      */

76     public void testComponentRegistration() {
77     JXRootPane rootPane = new JXRootPane();
78     JXStatusBar statusBar = new JXStatusBar();
79     rootPane.setStatusBar(statusBar);
80
81     for (int i = 0; i < comps.length; i++) {
82         rootPane.addComponent(comps[i]);
83     }
84
85     Component JavaDoc[] cs = rootPane.getContentComponents();
86     assertTrue(cs.length == comps.length);
87
88     // Ensure that messages are passed to the
89
// status bar. The PERSISTENT message is sent to the
90
// trailing message location.
91
for (int i = 0; i < comps.length; i++) {
92         TestComponent comp = comps[i];
93         comp.sendMessage();
94         assertEquals(comp.getMessage(), statusBar.getTrailingMessage());
95     }
96
97     // Remove all components.
98
for (int i = 0; i < comps.length; i++) {
99         rootPane.removeComponent(comps[i]);
100     }
101     cs = rootPane.getContentComponents();
102     assertTrue(cs.length == 0);
103
104     // Ensure that the status bar has been unregistered.
105
statusBar.setTrailingMessage("");
106     for (int i = 0; i < comps.length; i++) {
107         TestComponent comp = comps[i];
108         comp.sendMessage();
109         assertEquals("", statusBar.getTrailingMessage());
110     }
111     }
112
113     /**
114      * Test to ensure that all MessageSources in a containment hierarchy
115      * are registered.
116      */

117     public void testAggregateContainerRegistration() {
118     // Create an aggregate.
119
comps[HEAD].add(comps[BODY]);
120     comps[BODY].add(comps[ARMS]);
121     comps[BODY].add(comps[LEGS]);
122
123     JXRootPane rootPane = new JXRootPane();
124     JXStatusBar statusBar = new JXStatusBar();
125     rootPane.setStatusBar(statusBar);
126
127     rootPane.addComponent(comps[HEAD]);
128
129     Component JavaDoc[] cs = rootPane.getContentComponents();
130     assertTrue(cs.length == 1);
131
132     // The status bar should get all messages send to all
133
// components.
134
for (int i = 0; i < comps.length; i++) {
135         TestComponent comp = comps[i];
136         comp.sendMessage();
137         assertEquals(comp.getMessage(), statusBar.getTrailingMessage());
138     }
139
140     // Remove the head. There shouldn't be any components.
141
rootPane.removeComponent(comps[HEAD]);
142     cs = rootPane.getContentComponents();
143     assertTrue(cs.length == 0);
144
145     // Ensure that the status bar has been unregistered.
146
statusBar.setTrailingMessage("");
147     for (int i = 0; i < comps.length; i++) {
148         TestComponent comp = comps[i];
149         comp.sendMessage();
150         assertEquals("", statusBar.getTrailingMessage());
151     }
152     }
153
154     /**
155      * A test to ensure that the status bar is correctly registered with
156      * existing components and will be unregistered when removed.
157      */

158     public void testStatusBar() {
159     JXRootPane rootPane = new JXRootPane();
160     for (int i = 0; i < comps.length; i++) {
161         rootPane.addComponent(comps[i]);
162     }
163
164     JXStatusBar statusBar = new JXStatusBar();
165     rootPane.setStatusBar(statusBar);
166
167     // The status bar should get all messages send to all
168
// components.
169
for (int i = 0; i < comps.length; i++) {
170         TestComponent comp = comps[i];
171         comp.sendMessage();
172         assertEquals(comp.getMessage(), statusBar.getTrailingMessage());
173     }
174
175     // Change the status bar. Reset the old status bar. It shouldn't get
176
// any messages.
177
rootPane.setStatusBar(new JXStatusBar());
178     assertNotSame(statusBar, rootPane.getStatusBar());
179
180     // Ensure that the status bar has been unregistered.
181
statusBar.setTrailingMessage("");
182     for (int i = 0; i < comps.length; i++) {
183         TestComponent comp = comps[i];
184         comp.sendMessage();
185         assertEquals("", statusBar.getTrailingMessage());
186     }
187     }
188
189     /**
190      * Given a root pane with a status bar, a toolbar with components
191      * should get mouse listeners added to the components when added.
192      */

193     public void testToolBar() {
194     JXRootPane rootPane = new JXRootPane();
195     rootPane.setStatusBar(new JXStatusBar());
196
197     JToolBar JavaDoc toolBar = new JToolBar JavaDoc();
198     for (int i = 0; i < actions.length; i++) {
199         toolBar.add(actions[i]);
200     }
201
202     // set the baseline number of mouse listeners
203
Component JavaDoc[] comps = toolBar.getComponents();
204     int[] original = new int[comps.length];
205     for (int i = 0; i < comps.length; i++) {
206         MouseListener JavaDoc[] listeners = comps[i].getMouseListeners();
207         original[i] = listeners.length;
208     }
209
210     // Add the toolbar and mouse listeners should be registered
211
rootPane.setToolBar(toolBar);
212
213     comps = toolBar.getComponents();
214     for (int i = 0; i < comps.length; i++) {
215         MouseListener JavaDoc[] listeners = comps[i].getMouseListeners();
216         assertEquals(original[i] + 1, listeners.length);
217     }
218
219     // the toolbar is replaces. MouseListeners should be unregistered
220
rootPane.setToolBar(new JToolBar JavaDoc());
221
222     comps = toolBar.getComponents();
223     for (int i = 0; i < comps.length; i++) {
224         MouseListener JavaDoc[] listeners = comps[i].getMouseListeners();
225         assertEquals(original[i], listeners.length);
226     }
227     }
228
229     public void testMenuBar() {
230     JXRootPane rootPane = new JXRootPane();
231     rootPane.setStatusBar(new JXStatusBar());
232
233     JMenuBar JavaDoc menuBar = new JMenuBar JavaDoc();
234     JMenu JavaDoc menu = new JMenu JavaDoc("File");
235
236     for (int i = 0; i < actions.length; i++) {
237         menu.add(actions[i]);
238     }
239     menuBar.add(menu);
240
241     // set the baseline number of mouse listeners
242
Component JavaDoc[] comps = menu.getComponents();
243     int[] original = new int[comps.length];
244     for (int i = 0; i < comps.length; i++) {
245         MouseListener JavaDoc[] listeners = comps[i].getMouseListeners();
246         original[i] = listeners.length;
247     }
248
249     // Add the toolbar and mouse listeners should be registered
250
rootPane.setJMenuBar(menuBar);
251
252     comps = menu.getComponents();
253     for (int i = 0; i < comps.length; i++) {
254         MouseListener JavaDoc[] listeners = comps[i].getMouseListeners();
255         assertEquals(original[i] + 1, listeners.length);
256     }
257
258     // the toolbar is replaces. MouseListeners should be unregistered
259
rootPane.setJMenuBar(new JMenuBar JavaDoc());
260
261     for (int i = 0; i < comps.length; i++) {
262         MouseListener JavaDoc[] listeners = comps[i].getMouseListeners();
263         original[i] = listeners.length;
264     }
265     }
266
267     public static void main(String JavaDoc[] args) {
268     Action JavaDoc[] actions = new Action JavaDoc[4];
269     actions[0] = new TestAction("New", 'N', "Create a new item");
270     actions[1] = new TestAction("Open", 'O', "Opens an item");
271     actions[2] = new TestAction("Save", 'S', "Saves an item");
272     actions[3] = new TestAction("Exit", 'X', "Exits the application");
273
274     JToolBar JavaDoc toolBar = new JToolBar JavaDoc();
275     JMenuBar JavaDoc menuBar = new JMenuBar JavaDoc();
276     JMenu JavaDoc menu = new JMenu JavaDoc("File");
277
278     for (int i = 0; i < actions.length; i++) {
279         toolBar.add(actions[i]);
280         menu.add(actions[i]);
281     }
282     menuBar.add(menu);
283
284     JXRootPane rootPane = new JXRootPane();
285
286     Component JavaDoc[] comps = new Component JavaDoc[4];
287     comps[0] = new TestComponent("Head");
288     comps[1] = new TestComponent("Body");
289     comps[2] = new TestComponent("Arms");
290     comps[3] = new TestComponent("Legs");
291
292     rootPane.setStatusBar(new JXStatusBar());
293     rootPane.setToolBar(toolBar);
294     rootPane.setJMenuBar(menuBar);
295
296     for (int i = 0; i < comps.length; i++) {
297         rootPane.addComponent(comps[i]);
298     }
299     rootPane.addComponent(new ProgressComponent());
300
301     JXFrame frame = new JXFrame();
302     frame.setRootPane(rootPane);
303     frame.setVisible(true);
304     }
305
306     /**
307      * A simple action which can be used for creating components.
308      */

309     public static class TestAction extends AbstractAction JavaDoc {
310
311     public TestAction(String JavaDoc name, int mnemonic,
312               String JavaDoc description) {
313         super(name);
314         putValue(Action.MNEMONIC_KEY, new Integer JavaDoc(mnemonic));
315         putValue(Action.LONG_DESCRIPTION, description);
316     }
317     public void actionPerformed(ActionEvent JavaDoc evt) {}
318     }
319
320     /**
321      * A panel with some buttons to fire progress messages.
322      */

323     public static class ProgressComponent extends TestComponent
324     implements ActionListener JavaDoc {
325
326     private JButton JavaDoc start, stop, progress;
327
328     public ProgressComponent() {
329         super();
330     }
331
332     protected void initUI() {
333         setBorder(BorderFactory.createTitledBorder("Progress Messages"));
334         start = new JButton JavaDoc("Start");
335         start.addActionListener(this);
336
337         stop = new JButton JavaDoc("Stop");
338         stop.addActionListener(this);
339
340         progress = new JButton JavaDoc("Progress");
341         progress.addActionListener(this);
342
343         add(start);
344         add(stop);
345         add(progress);
346     }
347
348     public void actionPerformed(ActionEvent JavaDoc evt) {
349         Object JavaDoc source = evt.getSource();
350         if (source == start) {
351         support.fireProgressStarted(0,0);
352         } else if (source == stop) {
353         support.fireProgressEnded();
354         } else if (source == progress) {
355         doLongOperation();
356         }
357     }
358
359     /**
360      * Demonstrates how to use the progress bar feature.
361      */

362     private void doLongOperation() {
363         final int start = 0;
364         final int stop = 100;
365
366         Integer JavaDoc[] values = { new Integer JavaDoc(start), new Integer JavaDoc(stop) };
367
368         // Initialized the progress start.
369
support.fireProgressStarted(start, stop);
370
371         final Timer JavaDoc timer = new Timer JavaDoc();
372
373         TimerTask JavaDoc task = new TimerTask JavaDoc() {
374             int progress = start;
375             public void run() {
376             progress += 10;
377             if (progress > stop) {
378                 support.fireProgressEnded();
379                 timer.cancel();
380             } else {
381                 support.fireProgressIncremented(progress);
382             }
383             }
384         };
385         timer.schedule(task, 250L, 250L);
386
387
388     }
389
390     }
391
392
393     /**
394      * A test component which is a source of messages.
395      */

396     public static class TestComponent extends JPanel JavaDoc implements MessageSource,
397                                 ProgressSource {
398
399     protected MessageSourceSupport support;
400     private JLabel JavaDoc label;
401     private String JavaDoc text;
402
403     public TestComponent(String JavaDoc text) {
404         support = new MessageSourceSupport(this);
405         addMouseListener(new MouseHandler());
406         this.text = text;
407         initUI();
408     }
409
410     public TestComponent() {
411         this("TestComponent");
412     }
413
414     protected void initUI() {
415         label = new JLabel JavaDoc(text);
416         add(label);
417     }
418
419     public void addMessageListener(MessageListener l) {
420         support.addMessageListener(l);
421     }
422
423     public void removeMessageListener(MessageListener l) {
424         support.removeMessageListener(l);
425     }
426
427     public MessageListener[] getMessageListeners() {
428         return support.getMessageListeners();
429     }
430
431     public void addProgressListener(ProgressListener l) {
432         support.addProgressListener(l);
433     }
434
435     public void removeProgressListener(ProgressListener l) {
436         support.removeProgressListener(l);
437     }
438
439     public ProgressListener[] getProgressListeners() {
440         return support.getProgressListeners();
441     }
442
443     // Returns a message identifying this component.
444
public String JavaDoc getMessage() {
445         return "I'm a " + text;
446     }
447
448     // Sends a persistent message to the any listeners.
449
public void sendMessage() {
450         support.fireMessage(getMessage());
451     }
452
453     private class MouseHandler extends MouseAdapter JavaDoc {
454         public void mouseEntered(MouseEvent JavaDoc evt) {
455         sendMessage();
456         }
457     }
458     }
459
460 }
461
Popular Tags