:http://blog.csdn.net/meteors1113/archive/2009/06/06/4245475.aspx 1. package com.newautovideo.impclient.app; 2. import java.util.ArrayList; 3. import java.util.HashMap; 4. import java.util.List; 5. import java.util.Map; 6. import org.eclipse.core.runtime.IConfigurationElement; 7. import org.eclipse.swt.graphics.Point; 8. import org.eclipse.swt.widgets.Menu; 9. import org.eclipse.swt.widgets.MenuItem; 10. import org.eclipse.ui.PlatformUI; 11. import org.eclipse.ui.application.ActionBarAdvisor; 12. import org.eclipse.ui.application.IActionBarConfigurer; 13. import org.eclipse.ui.application.IWorkbenchWindowConfigurer; 14. import org.eclipse.ui.application.WorkbenchWindowAdvisor; 15. import org.eclipse.ui.internal.WorkbenchPlugin; 16. import org.eclipse.ui.internal.registry.ActionSetRegistry; 17. import org.eclipse.ui.internal.registry.IActionSetDescriptor; 18. /** 19. * @author zhanggang 20. * 21. */ 22. public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor { 23. private Map elements = new HashMap();// 存放着action的配置信息,以action中的label作为key 24. private List alist = new ArrayList();// 禁用的ActionId,仿服务端返回 25. public ApplicationWorkbenchWindowAdvisor( 26. IWorkbenchWindowConfigurer configurer) { 27. super(configurer); 28. } 29. public ActionBarAdvisor createActionBarAdvisor( 30. IActionBarConfigurer configurer) { 31. return new ApplicationActionBarAdvisor(configurer); 32. } 33. @SuppressWarnings("restriction") 34. public void preWindowOpen() { 35. IWorkbenchWindowConfigurer configurer = getWindowConfigurer(); 36. configurer.setInitialSize(new Point(400, 300)); 37. configurer.setShowCoolBar(false); 38. configurer.setShowStatusLine(false); 39. configurer.setTitle("Hello RCP"); 40. // 好象只能通过actionset来设置action是否可不可见了,暂时对action无法作呀,真哭 41. ActionSetRegistry reg = WorkbenchPlugin.getDefault() 42. .getActionSetRegistry(); 43. IActionSetDescriptor[] sets = reg.getActionSets(); 44. for (IActionSetDescriptor a : sets) {// 这里取到actionSet,并取到actionSet中的所有action配置,这里还取不到action对象的,需要在postwindow后才能取到 45. IConfigurationElement element = a.getConfigurationElement(); 46. IConfigurationElement[] es = element.getChildren(); 47. // a.setInitiallyVisible(false);//设置actionSet的属性 48. for (IConfigurationElement e : es) { 49. if (!e.getName().equals("action"))// 记住一定是要action 50. continue; 51. elements.put(e.getAttribute("label"), e);// 存放所有action的配置 52. } 53. } 54. } 55. @SuppressWarnings( { "restriction", "unchecked" }) 56. public void postWindowOpen() {// 这里可以设置action为不可用,但不能隐藏 57. super.postWindowOpen(); 58. MenuItem[] items = PlatformUI.getWorkbench().getActiveWorkbenchWindow() 59. .getShell().getMenuBar().getItems(); 60. alist.add("com.gzhnag.SampleActionTest3");// 有一个禁用权限 61. initMenu(items); 62. PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell() 63. .layout(); 64. } 65. /** 66. * 菜单项 67. * 68. * @param items 69. * @return 70. */ 71. private boolean initMenu(MenuItem[] items) { 72. String menuId; 73. boolean flag = false;// 此项菜单的父菜单是否显视标志,默认为不可显视,即要dispose(),但若有一个菜单,则为true 74. for (MenuItem item : items) { 75. Menu menu = item.getMenu(); 76. if (menu == null) 77. continue; 78. MenuItem[] ims = menu.getItems(); 79. int y = 0; 80. for (MenuItem mi : ims) { 81. boolean tmpFlag = false; 82. if (mi.getText().equals("")) { 83. continue; 84. } 85. if (mi.getMenu() != null && mi.getMenu().getItemCount() > 0) { 86. tmpFlag = initMenu(new MenuItem[] { mi });// 递归处理菜单,mi本身就是一个MenuItem 87. if (!tmpFlag) { 88. ims[y--].dispose();// 让分隔线也禁用 89. mi.dispose(); 90. continue; 91. } 92. } 93. menuId = getMenuId(mi.getText());// 有下级菜单时是不会取不到id的 94. if (menuId == null) 95. continue; 96. if (alist.contains(menuId)) {// 若禁用权限里包括了些ID则禁用 97. mi.dispose(); 98. } else { 99. flag = !flag;// 设为true即显神曲菜单 100. } 101. y++; 102. } 103. } 104. return flag; 105. } 106. /** 107. * @param name 108. * action中的label,注意有快捷方式,有快捷方式会以/t分隔 109. * @return 110. */ 111. private String getMenuId(String name) { 112. String lable = name.split(" ")[0]; 113. if (elements.get(lable) == null) 114. return null; 115. return elements.get(lable).getAttribute("id");// 快捷方式与label是以/t相隔的 116. }
RCP中权限与菜单的操作例子关键字: rcp中权限与菜单的操作例子 from
最新推荐文章于 2016-07-01 13:41:58 发布