Altova XMLSpy 2024 Enterprise Edition

The code listing below shows how menu items can be created. Each XMLSpyCommand 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 XMLSpyControl 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  // Load the file menu when the button is pressed

02       btnMenu.addActionListener( new ActionListener() {

03         public void actionPerformed(ActionEvent e) {

04           try {

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

06             MenuBar mb = new MenuBar();

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

08             XMLSpyCommand xmlSpyMenu = xmlSpyControl.getMainMenu().getSubCommands().getItem( 0 );

09             // Create Java menu items from the Commands objects

10             Menu fileMenu = new Menu();

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

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

13             mb.add( fileMenu );

14             frame.setMenuBar( mb );

15             frame.validate();

16           } catch (AutomationException e1) {

17             e1.printStackTrace();

18           }

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

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

21         }

22       } ) ;

23 /** * Populates a menu with the commands and submenus contained in an XMLSpyCommands object  */

24       public void fillMenu(Menu newMenu, XMLSpyCommands xmlSpyMenu) throws AutomationException

25   {

26     // For each command/submenu in the xmlSpyMenu

27     for ( int i = 0 ; i < xmlSpyMenu.getCount() ; ++i  )

28     {

29       XMLSpyCommand xmlSpyCommand = xmlSpyMenu.getItem( i );

30       if ( xmlSpyCommand.getIsSeparator() )

31         newMenu.addSeparator();

32       else

33       {

34         XMLSpyCommands subCommands = xmlSpyCommand.getSubCommands();

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

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

37         {

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

39           MenuItem mi = new MenuItem( xmlSpyCommand.getLabel().replace( "&", "" ) );

40           mi.setActionCommand( "" + xmlSpyCommand.getID() );

41           mi.addActionListener( this );

42           newMenu.add( mi );

43           menuMap.put( xmlSpyCommand.getID(), mi );

44         }

45         else

46         {

47           // Submenu -> create submenu and repeat recursively

48           Menu newSubMenu = new Menu();

49           fillMenu( newSubMenu, subCommands );

50           newSubMenu.setLabel( xmlSpyCommand.getLabel().replace( "&", "" ) );

51           newMenu.add( newSubMenu );

52         }

53       }

54     }

55   }

56

57    /**

58    * Action handler for the menu items

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

60    */

61   public void actionPerformed( ActionEvent e )

62   {

63     try

64     {

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

66       // Handle explicitly the Close commands

67       switch ( iCmd )

68       {

69         case 57602:       // Close

70         case 34050:       // Close All

71           XMLSpyContainer.initXmlSpyDocument();

72           break;

73         default:

74           XMLSpyContainer.xmlSpyControl.exec( iCmd );

75           break;

76       }

77     }

78     catch ( Exception ex )

79     {

80       ex.printStackTrace();

81     }

82

83   }

 

© 2017-2023 Altova GmbH