1 7 8 package org.jdesktop.swing; 9 10 import java.util.Timer ; 11 import java.util.TimerTask ; 12 13 import java.awt.Component ; 14 import java.awt.event.ActionEvent ; 15 import java.awt.event.ActionListener ; 16 import java.awt.event.MouseAdapter ; 17 import java.awt.event.MouseEvent ; 18 import java.awt.event.MouseListener ; 19 import javax.swing.AbstractAction ; 20 import javax.swing.Action ; 21 import javax.swing.BorderFactory ; 22 import javax.swing.JButton ; 23 import javax.swing.JLabel ; 24 import javax.swing.JMenu ; 25 import javax.swing.JMenuBar ; 26 import javax.swing.JPanel ; 27 import javax.swing.JToolBar ; 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 [] 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 [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 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 [] cs = rootPane.getContentComponents(); 86 assertTrue(cs.length == comps.length); 87 88 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 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 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 117 public void testAggregateContainerRegistration() { 118 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 [] cs = rootPane.getContentComponents(); 130 assertTrue(cs.length == 1); 131 132 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 rootPane.removeComponent(comps[HEAD]); 142 cs = rootPane.getContentComponents(); 143 assertTrue(cs.length == 0); 144 145 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 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 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 rootPane.setStatusBar(new JXStatusBar()); 178 assertNotSame(statusBar, rootPane.getStatusBar()); 179 180 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 193 public void testToolBar() { 194 JXRootPane rootPane = new JXRootPane(); 195 rootPane.setStatusBar(new JXStatusBar()); 196 197 JToolBar toolBar = new JToolBar (); 198 for (int i = 0; i < actions.length; i++) { 199 toolBar.add(actions[i]); 200 } 201 202 Component [] comps = toolBar.getComponents(); 204 int[] original = new int[comps.length]; 205 for (int i = 0; i < comps.length; i++) { 206 MouseListener [] listeners = comps[i].getMouseListeners(); 207 original[i] = listeners.length; 208 } 209 210 rootPane.setToolBar(toolBar); 212 213 comps = toolBar.getComponents(); 214 for (int i = 0; i < comps.length; i++) { 215 MouseListener [] listeners = comps[i].getMouseListeners(); 216 assertEquals(original[i] + 1, listeners.length); 217 } 218 219 rootPane.setToolBar(new JToolBar ()); 221 222 comps = toolBar.getComponents(); 223 for (int i = 0; i < comps.length; i++) { 224 MouseListener [] 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 menuBar = new JMenuBar (); 234 JMenu menu = new JMenu ("File"); 235 236 for (int i = 0; i < actions.length; i++) { 237 menu.add(actions[i]); 238 } 239 menuBar.add(menu); 240 241 Component [] comps = menu.getComponents(); 243 int[] original = new int[comps.length]; 244 for (int i = 0; i < comps.length; i++) { 245 MouseListener [] listeners = comps[i].getMouseListeners(); 246 original[i] = listeners.length; 247 } 248 249 rootPane.setJMenuBar(menuBar); 251 252 comps = menu.getComponents(); 253 for (int i = 0; i < comps.length; i++) { 254 MouseListener [] listeners = comps[i].getMouseListeners(); 255 assertEquals(original[i] + 1, listeners.length); 256 } 257 258 rootPane.setJMenuBar(new JMenuBar ()); 260 261 for (int i = 0; i < comps.length; i++) { 262 MouseListener [] listeners = comps[i].getMouseListeners(); 263 original[i] = listeners.length; 264 } 265 } 266 267 public static void main(String [] args) { 268 Action [] actions = new Action [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 toolBar = new JToolBar (); 275 JMenuBar menuBar = new JMenuBar (); 276 JMenu menu = new JMenu ("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 [] comps = new Component [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 309 public static class TestAction extends AbstractAction { 310 311 public TestAction(String name, int mnemonic, 312 String description) { 313 super(name); 314 putValue(Action.MNEMONIC_KEY, new Integer (mnemonic)); 315 putValue(Action.LONG_DESCRIPTION, description); 316 } 317 public void actionPerformed(ActionEvent evt) {} 318 } 319 320 323 public static class ProgressComponent extends TestComponent 324 implements ActionListener { 325 326 private JButton 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 ("Start"); 335 start.addActionListener(this); 336 337 stop = new JButton ("Stop"); 338 stop.addActionListener(this); 339 340 progress = new JButton ("Progress"); 341 progress.addActionListener(this); 342 343 add(start); 344 add(stop); 345 add(progress); 346 } 347 348 public void actionPerformed(ActionEvent evt) { 349 Object 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 362 private void doLongOperation() { 363 final int start = 0; 364 final int stop = 100; 365 366 Integer [] values = { new Integer (start), new Integer (stop) }; 367 368 support.fireProgressStarted(start, stop); 370 371 final Timer timer = new Timer (); 372 373 TimerTask task = new TimerTask () { 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 396 public static class TestComponent extends JPanel implements MessageSource, 397 ProgressSource { 398 399 protected MessageSourceSupport support; 400 private JLabel label; 401 private String text; 402 403 public TestComponent(String 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 (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 public String getMessage() { 445 return "I'm a " + text; 446 } 447 448 public void sendMessage() { 450 support.fireMessage(getMessage()); 451 } 452 453 private class MouseHandler extends MouseAdapter { 454 public void mouseEntered(MouseEvent evt) { 455 sendMessage(); 456 } 457 } 458 } 459 460 } 461 | Popular Tags |