Altova XMLSpy 2024 Enterprise Edition

Im unten gezeigten Code sehen Sie, wie Menüeinträge erstellt werden können. Jedes XMLSpyCommand Objekt holt ein entsprechendes MenuItem Objekt, wobei der ActionCommand auf die ID des Befehls gesetzt wird. Die von allen Menüeinträgen generierten Aktionen werden von derselben Funktion gehandelt. Diese kann bestimmte Handlings (wie z.B Neuinterpretieren der Schließfunktion) durchführen oder die Ausführung durch Aufruf seiner exec-Methode an das XMLSpyControl Objekt delegieren. Das menuMap Objekt, das bei der Menüerstellung befüllt wird, wird später verwendet (siehe Abschnitt Behandlung von UI Update Events).

 

 

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 in 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