1 19 20 package org.apache.excalibur.instrument.client; 21 22 import java.awt.event.ActionEvent ; 23 import java.awt.event.ActionListener ; 24 25 import javax.swing.AbstractAction ; 26 import javax.swing.Action ; 27 import javax.swing.JCheckBoxMenuItem ; 28 import javax.swing.JInternalFrame ; 29 import javax.swing.JMenu ; 30 import javax.swing.JMenuBar ; 31 import javax.swing.JMenuItem ; 32 import javax.swing.event.MenuEvent ; 33 import javax.swing.event.MenuListener ; 34 35 41 public class MenuBar 42 extends JMenuBar 43 { 44 protected InstrumentClientFrame m_frame; 45 46 boolean m_showUnconfigured = true; 47 48 private JMenu m_menuFile; 49 50 private JMenu m_menuInstrumentManagers; 51 52 private JMenu m_menuOptions; 53 private JCheckBoxMenuItem m_menuItemShowUnconfigured; 54 55 private JMenu m_menuWindow; 56 57 60 MenuBar( InstrumentClientFrame frame ) 61 { 62 m_frame = frame; 63 64 add( buildFileMenu() ); 65 add( buildInstrumentManagerMenu() ); 66 add( buildOptionsMenu() ); 67 add( buildWindowMenu() ); 68 } 69 70 73 private JMenu buildFileMenu() 74 { 75 m_menuFile = (JMenu )new LargeMenu( "File" ); 76 m_menuFile.setMnemonic( 'F' ); 77 78 79 Action newAction = new AbstractAction ( "New" ) 81 { 82 public void actionPerformed( ActionEvent event ) 83 { 84 m_frame.fileNew(); 85 } 86 }; 87 JMenuItem newItem = new JMenuItem ( newAction ); 88 newItem.setMnemonic( 'N' ); 89 m_menuFile.add( newItem ); 90 91 92 Action openAction = new AbstractAction ( "Open ..." ) 94 { 95 public void actionPerformed( ActionEvent event ) 96 { 97 m_frame.fileOpen(); 98 } 99 }; 100 JMenuItem open = new JMenuItem ( openAction ); 101 open.setMnemonic( 'O' ); 102 m_menuFile.add( open ); 103 104 105 m_menuFile.addSeparator(); 107 108 109 Action saveAction = new AbstractAction ( "Save" ) 111 { 112 public void actionPerformed( ActionEvent event ) 113 { 114 m_frame.fileSave(); 115 } 116 }; 117 JMenuItem save = new JMenuItem ( saveAction ); 118 save.setMnemonic( 'S' ); 119 m_menuFile.add( save ); 120 121 122 Action saveAsAction = new AbstractAction ( "Save As ..." ) 124 { 125 public void actionPerformed( ActionEvent event ) 126 { 127 m_frame.fileSaveAs(); 128 } 129 }; 130 JMenuItem saveAs = new JMenuItem ( saveAsAction ); 131 saveAs.setMnemonic( 'A' ); 132 m_menuFile.add( saveAs ); 133 134 135 m_menuFile.addSeparator(); 137 138 139 Action exitAction = new AbstractAction ( "Exit" ) 141 { 142 public void actionPerformed( ActionEvent event ) 143 { 144 m_frame.fileExit(); 145 } 146 }; 147 JMenuItem exit = new JMenuItem ( exitAction ); 148 exit.setMnemonic( 'X' ); 149 m_menuFile.add( exit ); 150 151 return m_menuFile; 152 } 153 154 private JMenu buildInstrumentManagerMenu() 155 { 156 m_menuInstrumentManagers = new LargeMenu( "Instrument Managers" ); 157 m_menuInstrumentManagers.setMnemonic( 'I' ); 158 159 m_menuInstrumentManagers.addMenuListener( new MenuListener () 160 { 161 public void menuSelected( MenuEvent event ) 162 { 163 rebuildInstrumentManagersMenu(); 164 } 165 166 public void menuDeselected( MenuEvent event ) 167 { 168 } 169 170 public void menuCanceled( MenuEvent event ) 171 { 172 } 173 } ); 174 175 return m_menuInstrumentManagers; 176 } 177 178 private void rebuildInstrumentManagersMenu() 179 { 180 m_menuInstrumentManagers.removeAll(); 181 182 Action connectAction = new AbstractAction ( "Connect to Instrument Manager..." ) 184 { 185 public void actionPerformed( ActionEvent event ) 186 { 187 m_frame.showConnectDialog(); 189 } 190 }; 191 192 JMenuItem connectItem = new JMenuItem ( connectAction ); 193 connectItem.setMnemonic( 'C' ); 194 m_menuInstrumentManagers.add( connectItem ); 195 196 InstrumentManagerConnection[] connections = m_frame.getConnections(); 198 if ( connections.length > 0 ) 199 { 200 m_menuInstrumentManagers.addSeparator(); 201 202 for ( int i = 0; i < connections.length; i++ ) 203 { 204 InstrumentManagerConnection connection = connections[i]; 205 206 Action action = new AbstractAction ( connection.getTitle() ) 207 { 208 public void actionPerformed( ActionEvent event ) 209 { 210 } 211 }; 212 action.putValue( "InstrumentManagerConnection", connection ); 213 214 JMenu menu = new LargeMenu( action ); 215 216 menu.addMenuListener( new MenuListener () 218 { 219 public void menuSelected( MenuEvent event ) 220 { 221 JMenu menu = (JMenu )event.getSource(); 222 Action action = menu.getAction(); 223 224 rebuildInstrumentManagerMenu( 225 menu, (InstrumentManagerConnection)action.getValue( 226 "InstrumentManagerConnection" ) ); 227 } 228 229 public void menuDeselected( MenuEvent event ) 230 { 231 } 232 233 public void menuCanceled( MenuEvent event ) 234 { 235 } 236 } ); 237 238 m_menuInstrumentManagers.add( menu ); 239 } 240 } 241 } 242 243 private void rebuildInstrumentManagerMenu( final JMenu managerMenu, 244 final InstrumentManagerConnection connection ) 245 { 246 managerMenu.removeAll(); 247 248 boolean showAll = m_showUnconfigured; 249 250 Action deleteAction = new AbstractAction ( "Delete" ) 252 { 253 public void actionPerformed( ActionEvent event ) 254 { 255 JMenuItem item = (JMenuItem )event.getSource(); 256 Action action = item.getAction(); 257 258 InstrumentManagerConnection connection = 259 (InstrumentManagerConnection)action.getValue( "InstrumentManagerConnection" ); 260 261 connection.delete(); 262 } 263 }; 264 deleteAction.putValue( "InstrumentManagerConnection", connection ); 265 266 JMenuItem deleteItem = new JMenuItem ( deleteAction ); 267 deleteItem.setMnemonic( 'I' ); 268 managerMenu.add( deleteItem ); 269 270 InstrumentManagerData manager = connection.getInstrumentManager(); 272 273 if ( manager != null ) 274 { 275 managerMenu.addSeparator(); 276 277 InstrumentableData[] instrumentables = manager.getInstrumentables(); 278 279 for( int i = 0; i < instrumentables.length; i++ ) 280 { 281 InstrumentableData instrumentable = instrumentables[i]; 282 283 if( showAll || instrumentable.isConfigured() ) 284 { 285 String description = instrumentable.getDescription(); 286 287 Action action = new AbstractAction ( description ) 288 { 289 public void actionPerformed( ActionEvent event ) 290 { 291 } 292 }; 293 action.putValue( "InstrumentManagerConnection", connection ); 294 action.putValue( "InstrumentableData", instrumentable ); 295 296 JMenu menu = new LargeMenu( action ); 297 298 menu.addMenuListener( new MenuListener () 300 { 301 public void menuSelected( MenuEvent event ) 302 { 303 JMenu menu = (JMenu )event.getSource(); 304 Action action = menu.getAction(); 305 306 rebuildInstrumentableMenu( 307 menu, 308 (InstrumentManagerConnection)action.getValue( 309 "InstrumentManagerConnection" ), 310 (InstrumentableData)action.getValue( 311 "InstrumentableData" ) ); 312 } 313 314 public void menuDeselected( MenuEvent event ) 315 { 316 } 317 318 public void menuCanceled( MenuEvent event ) 319 { 320 } 321 } ); 322 323 managerMenu.add( menu ); 324 } 325 } 326 } 327 } 328 329 private void rebuildInstrumentableMenu( final JMenu instrumentableMenu, 330 final InstrumentManagerConnection connection, 331 final InstrumentableData instrumentable ) 332 { 333 instrumentableMenu.removeAll(); 334 335 boolean showAll = m_showUnconfigured; 336 337 InstrumentableData[] children = instrumentable.getInstrumentables(); 339 340 for( int i = 0; i < children.length; i++ ) 341 { 342 InstrumentableData child = children[i]; 343 344 if( showAll || child.isConfigured() ) 345 { 346 String description = child.getDescription(); 347 348 Action action = new AbstractAction ( description ) 349 { 350 public void actionPerformed( ActionEvent event ) 351 { 352 } 353 }; 354 action.putValue( "InstrumentManagerConnection", connection ); 355 action.putValue( "InstrumentableData", child ); 356 357 JMenu menu = new LargeMenu( action ); 358 359 menu.addMenuListener( new MenuListener () 361 { 362 public void menuSelected( MenuEvent event ) 363 { 364 JMenu menu = (JMenu )event.getSource(); 365 Action action = menu.getAction(); 366 367 rebuildInstrumentableMenu( 368 menu, 369 (InstrumentManagerConnection)action.getValue( 370 "InstrumentManagerConnection" ), 371 (InstrumentableData)action.getValue( 372 "InstrumentableData" ) ); 373 } 374 375 public void menuDeselected( MenuEvent event ) 376 { 377 } 378 379 public void menuCanceled( MenuEvent event ) 380 { 381 } 382 } ); 383 384 instrumentableMenu.add( menu ); 385 } 386 } 387 388 InstrumentData[] instruments = instrumentable.getInstruments(); 390 391 for( int i = 0; i < instruments.length; i++ ) 392 { 393 InstrumentData instrument = instruments[ i ]; 394 395 if( showAll || instrument.isConfigured() ) 396 { 397 String description = instrument.getDescription(); 398 399 Action action = new AbstractAction ( description ) 400 { 401 public void actionPerformed( ActionEvent event ) 402 { 403 } 404 }; 405 action.putValue( "InstrumentManagerConnection", connection ); 406 action.putValue( "InstrumentableData", instrumentable ); 407 action.putValue( "InstrumentData", instrument ); 408 409 JMenu menu = new LargeMenu( action ); 410 411 menu.addMenuListener( new MenuListener () 413 { 414 public void menuSelected( MenuEvent event ) 415 { 416 JMenu menu = (JMenu )event.getSource(); 417 Action action = menu.getAction(); 418 419 rebuildInstrumentMenu( 420 menu, 421 (InstrumentManagerConnection)action.getValue( 422 "InstrumentManagerConnection" ), 423 (InstrumentableData)action.getValue( 424 "InstrumentableData" ), 425 (InstrumentData)action.getValue( 426 "InstrumentData" ) ); 427 } 428 429 public void menuDeselected( MenuEvent event ) 430 { 431 } 432 433 public void menuCanceled( MenuEvent event ) 434 { 435 } 436 } ); 437 438 instrumentableMenu.add( menu ); 439 } 440 } 441 } 442 443 private void rebuildInstrumentMenu( final JMenu instrumentMenu, 444 final InstrumentManagerConnection connection, 445 final InstrumentableData instrumentable, 446 final InstrumentData instrument ) 447 { 448 instrumentMenu.removeAll(); 449 450 boolean showAll = m_showUnconfigured; 451 452 Action createAction = new AbstractAction ( "Create Sample..." ) 454 { 455 public void actionPerformed( ActionEvent event ) 456 { 457 connection.showCreateSampleDialog( instrument ); 458 } 459 }; 460 createAction.putValue( "InstrumentManagerConnection", connection ); 461 createAction.putValue( "InstrumentableData", instrumentable ); 462 createAction.putValue( "InstrumentData", instrument ); 463 464 JMenuItem createItem = new JMenuItem ( createAction ); 465 createItem.setMnemonic( 'C' ); 466 instrumentMenu.add( createItem ); 467 468 InstrumentSampleData[] samples = instrument.getInstrumentSamples(); 469 470 if ( samples.length > 0 ) 471 { 472 instrumentMenu.addSeparator(); 473 474 for( int i = 0; i < samples.length; i++ ) 475 { 476 final InstrumentSampleData sample = samples[ i ]; 477 478 if( showAll || sample.isConfigured() ) 479 { 480 String description = sample.getDescription(); 481 482 Action action = new AbstractAction ( description ) 483 { 484 public void actionPerformed( ActionEvent event ) 485 { 486 connection.viewSample( sample.getName() ); 487 } 488 }; 489 action.putValue( "InstrumentManagerConnection", connection ); 490 action.putValue( "InstrumentSampleData", sample ); 491 492 JMenuItem item = new JMenuItem ( action ); 493 494 instrumentMenu.add( item ); 495 } 496 } 497 } 498 } 499 500 private JMenu buildOptionsMenu() 501 { 502 m_menuOptions = new LargeMenu( "Options" ); 503 m_menuOptions.setMnemonic( 'O' ); 504 505 m_menuOptions.addMenuListener( new MenuListener () 506 { 507 public void menuSelected( MenuEvent event ) 508 { 509 rebuildOptionsMenu(); 510 } 511 512 public void menuDeselected( MenuEvent event ) 513 { 514 } 515 516 public void menuCanceled( MenuEvent event ) 517 { 518 } 519 } ); 520 521 527 528 return m_menuOptions; 529 } 530 531 protected void rebuildOptionsMenu() 532 { 533 m_menuOptions.removeAll(); 534 535 final JCheckBoxMenuItem antialias = 536 new JCheckBoxMenuItem ( "Antialias", m_frame.isAntialias() ); 537 antialias.setMnemonic( 'a' ); 538 antialias.addActionListener( new ActionListener () 539 { 540 public void actionPerformed( ActionEvent event ) 541 { 542 m_frame.setAntialias( antialias.getState() ); 543 } 544 } ); 545 m_menuOptions.add( antialias ); 546 } 547 548 private JMenu buildWindowMenu() 549 { 550 m_menuWindow = new LargeMenu( "Window" ); 551 m_menuWindow.setMnemonic( 'W' ); 552 553 m_menuWindow.addMenuListener( new MenuListener () 554 { 555 public void menuSelected( MenuEvent event ) 556 { 557 rebuildWindowMenu(); 558 } 559 560 public void menuDeselected( MenuEvent event ) 561 { 562 } 563 564 public void menuCanceled( MenuEvent event ) 565 { 566 } 567 } ); 568 569 return m_menuWindow; 570 } 571 572 protected void rebuildWindowMenu() 573 { 574 m_menuWindow.removeAll(); 575 576 Action tileFramesAction = new AbstractAction ( "Tile frames" ) 578 { 579 public void actionPerformed( ActionEvent event ) 580 { 581 m_frame.tileFrames(); 582 } 583 }; 584 585 JMenuItem tileFrames = new JMenuItem ( tileFramesAction ); 586 tileFrames.setMnemonic( 't' ); 587 m_menuWindow.add( tileFrames ); 588 589 590 Action tileFramesVAction = new AbstractAction ( "Tile frames vertically" ) 592 { 593 public void actionPerformed( ActionEvent event ) 594 { 595 m_frame.tileFramesV(); 596 } 597 }; 598 599 JMenuItem tileFramesV = new JMenuItem ( tileFramesVAction ); 600 tileFramesV.setMnemonic( 'v' ); 601 m_menuWindow.add( tileFramesV ); 602 603 Action tileFramesHAction = new AbstractAction ( "Tile frames horizontally" ) 605 { 606 public void actionPerformed( ActionEvent event ) 607 { 608 m_frame.tileFramesH(); 609 } 610 }; 611 612 JMenuItem tileFramesH = new JMenuItem ( tileFramesHAction ); 613 tileFramesH.setMnemonic( 'h' ); 614 m_menuWindow.add( tileFramesH ); 615 616 Action closeAllAction = new AbstractAction ( "Close All" ) 618 { 619 public void actionPerformed( ActionEvent event ) 620 { 621 m_frame.closeAllFrames(); 622 } 623 }; 624 625 JMenuItem closeAll = new JMenuItem ( closeAllAction ); 626 closeAll.setMnemonic( 'o' ); 627 m_menuWindow.add( closeAll ); 628 629 630 JInternalFrame [] frames = m_frame.getDesktopPane().getAllFrames(); 632 633 if( frames.length > 0 ) 634 { 635 m_menuWindow.addSeparator(); 636 } 637 638 for( int i = 0; i < frames.length; i++ ) 639 { 640 String label = ( i + 1 ) + " " + frames[ i ].getTitle(); 641 Action action = new AbstractAction ( label ) 642 { 643 public void actionPerformed( ActionEvent event ) 644 { 645 JMenuItem menu = (JMenuItem )event.getSource(); 646 Action action = menu.getAction(); 647 648 JInternalFrame frame = (JInternalFrame )action.getValue( "frame" ); 649 try 650 { 651 if( frame.isIcon() ) 652 { 653 frame.setIcon( false ); 655 } 656 frame.setSelected( true ); 657 m_frame.getDesktopPane().moveToFront( frame ); 658 } 659 catch( java.beans.PropertyVetoException e ) 660 { 661 } 662 } 663 }; 664 action.putValue( "frame", frames[ i ] ); 665 666 JMenuItem item = new JMenuItem ( action ); 667 m_menuWindow.add( item ); 668 669 if( i < 10 ) 670 { 671 item.setMnemonic( (char)( '1' + i ) ); 672 } 673 } 674 } 675 } 676 | Popular Tags |