Altova StyleVision 2023 Professional Edition

The code listing below shows how menu items can be created. Each StyleVisionCommand object gets a corresponding MenuItem object, with the ActionCommand set to the ID of the command. The actions generated by all menu items are handled by the same function, which can perform specific handlings (like reinterpreting the closing mechanism) or can delegate the execution to the StyleVisionControl object by calling its exec method. The menuMap object that is filled during menu creation is used later (see section UI Update Event Handling).

 

 

01

02       // Load the file menu when the button is pressed

03       btnMenu.addActionListener( new ActionListener() {

04         public void actionPerformed(ActionEvent e) {

05           try {

06             // Create the menubar that will be attached to the frame

07             MenuBar mb = new MenuBar();

08             // Load the main menu's first item - the File menu

09             StyleVisionCommand xmlSpyMenu = styleVisionControl.getMainMenu().getSubCommands().getItem( 0 );

10             // Create Java menu items from the Commands objects

11             Menu fileMenu = new Menu();

12             handlerObject.fillMenu( fileMenu, xmlSpyMenu.getSubCommands() );

13             fileMenu.setLabel( xmlSpyMenu.getLabel().replace( "&", "" ) );

14             mb.add( fileMenu );

15             frame.setMenuBar( mb );

16             frame.validate();

17           } catch (AutomationException e1) {

18             e1.printStackTrace();

19           }

20           // Disable the button when the action has been performed

21           ((AbstractButton) e.getSource()).setEnabled( false );

22         }

23       } ) ;

24   /**

25    * Populates a menu with the commands and submenus contained in a StyleVisionCommands object

26    */

27   public void fillMenu(Menu newMenu, StyleVisionCommands styleVisionMenu) throws AutomationException

28   {

29     // For each command/submenu in the xmlSpyMenu

30     for ( int i = 0 ; i < styleVisionMenu.getCount() ; ++i  )

31     {

32       StyleVisionCommand styleVisionCommand = styleVisionMenu.getItem( i );

33       if ( styleVisionCommand.getIsSeparator() )

34         newMenu.addSeparator();

35       else

36       {

37         StyleVisionCommands subCommands = styleVisionCommand.getSubCommands();

38         // Is it a command (leaf), or a submenu?

39         if ( subCommands.isNull() || subCommands.getCount() == 0 )

40         {

41           // Command -> add it to the menu, set its ActionCommand to its ID and store it in the menuMap

42           MenuItem mi = new MenuItem( styleVisionCommand.getLabel().replace( "&", "" ) );

43           mi.setActionCommand( "" + styleVisionCommand.getID() );

44           mi.addActionListener( this );

45           newMenu.add( mi );

46           menuMap.put( styleVisionCommand.getID(), mi );

47         }

48         else

49         {

50           // Submenu -> create submenu and repeat recursively

51           Menu newSubMenu = new Menu();

52           fillMenu( newSubMenu, subCommands );

53           newSubMenu.setLabel( styleVisionCommand.getLabel().replace( "&", "" ) );

54           newMenu.add( newSubMenu );

55         }

56       }

57     }

58   }

59     /**

60    * Action handler for the menu items

61    * Called when the user selects a menu item; the item's action command corresponds to the command table for XMLSpy

62    */

63   public void actionPerformed( ActionEvent e )

64   {

65     try

66     {

67       int iCmd = Integer.parseInt( e.getActionCommand() );

68       // Handle explicitly the Close commands

69       switch ( iCmd )

70       {

71         case 57602:       // Close

72         case 34050:       // Close All

73           StyleVisionContainer.initStyleVisionDocument();

74           break;

75         default:

76           StyleVisionContainer.styleVisionControl.exec( iCmd );

77           break;

78       }

79     }

80     catch ( Exception ex )

81     {

82       ex.printStackTrace();

83     }

84

85   }

© 2017-2023 Altova GmbH