Altova XMLSpy 2024 Professional Edition

下方代码片段展示了如何创建菜单项。每个XMLSpy Command对象都有一个对应的MenuItem对象,其ActionCommand设为该命令的ID。所有的菜单项产生的操作都是由同一个函数处理的,该函数可以执行特定的处理(类似重新解释关闭机制),或可以通过调用其exec方法将执行委托给XMLSpyControl对象。在菜单创建期间填充的menuMap对象将在稍后使用(参见UI更新事件处理)。

 

 

01  // 释放按钮时加载文件菜单

02       btnMenu.addActionListener( new ActionListener() {

03         public void actionPerformed(ActionEvent e) {

04           try {

05             // 创建将附加到框架的菜单栏

06             MenuBar mb = new MenuBar();

07             // 加载主菜单的第一个项 - 文件菜单

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

09             // 从Command对象创建Java菜单项

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           // 操作执行后禁用该按钮

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

21         }

22       } ) ;

23 /** * 用XMLSpyCommands对象中包含的命令和子菜单填充菜单 */

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

25   {

26     // 遍历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         // 它是一个命令(叶),还是一个子菜单?

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

37         {

38           // 如果是命令 -> 添加到菜单,将ActionCommand设为其ID并存储在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           // 如果是子菜单 -> 创建子菜单并以递归方式重复

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    * 菜单项的操作处理器

59    * 用户选择一个菜单项时被调用,该项的操作命令对应于XMLSpy的命令表

60    */

61   public void actionPerformed( ActionEvent e )

62   {

63     try

64     {

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

66       // 显式处理关闭命令

67       switch ( iCmd )

68       {

69         case 57602:       // 关闭

70         case 34050:       // 全部关闭

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