新建菜單裡面都是聲明過NewWizard的東東
不過既然已經聲明了NewWizard還要在ActionSet或者PopupMenus裡面再聲明一遍不是很痛苦?
其實可以如下做法
先聲明一個PullDown的菜單
<action
class=\
hoverIcon=\
id=\
label=\
menubarPath=\
style=\
toolbarPath=\
</action>
如果這個菜單用Coolbar
public class NewAction implements IWorkbenchWindowPulldownDelegate
private IWorkbench workbench;
@SuppressWarnings(\
private IWorkbenchWindow window;
private Menu parent = null;
private Menu inMenu = null;
private Menu inToolbar = null;
private IStructuredSelection selection;
/** *//**
* (non
*
* @see org
*/
public void dispose()
if (inMenu != null)
inMenu
inMenu = null;
} [Page]
this
this
}
/** *//**
* (non
*
* @see org
*/
public void init(IWorkbenchWindow window)
this
this
}
/** *//**
* (non
*
* @see org
*/
public void run(IAction action)
}
/** *//**
* (non
*
* @see org
* org
*/
public void selectionChanged(IAction action
this
if (parent != null)
// 找到新建菜單項
// 重新創建其子菜單
MenuItem[] items = parent
if (items != null)
MenuItem parentItem = null;
for (int index =
MenuItem item = items[index];
Object data = item
if (data instanceof ActionSetContributionItem)
IContributionItem innerItem = ((ActionSetContributionItem) data)
if (innerItem
parentItem = item;
break;
}
}
}
if (parentItem != null)
Menu menu = getMenu(parent);
parentItem
parentItem
}
}
}
}
/** *//**
* (non
*
* @see org
*/
public Menu getMenu(Control parent)
if (inToolbar != null)
inToolbar
inToolbar = new Menu(parent);
return createMenu(inToolbar);
}
/** *//**
* (non
*
* @see org
*/
public Menu getMenu(Menu parent)
if (inMenu != null)
inMenu
this
inMenu = new Menu(parent);
return createMenu(inMenu);
}
/** *//**
* @param parent
* @return
*/
private Menu createMenu(Menu menu)
boolean hasMenu = false;
IWizardRegistry registry = workbench
IWizardCategory rootCategory = registry
IWizardCategory[] categories = rootCategory
if (categories != null)
reverseArray(categories);
for (IWizardCategory category : categories)
hasMenu = createMenuItems(menu
}
}
if (hasMenu)
new MenuItem(menu
MenuItem mnuOther = new MenuItem(menu
mnuOther
mnuOther
/** *//**
* (non
*
* @see org
*/
@Override
public void widgetSelected(SelectionEvent e)
NewWizardDialog dialog = new NewWizardDialog(workbench
dialog
}
}); [Page]
return menu;
}
/** *//**
* 創建菜單
*
* @param menu
* @param category
* @param hasMenu 是否在之前已經有菜單
* @return 是否成功創建了菜單
*/
private boolean createMenuItems(Menu menu
boolean create = false;
IWizardDescriptor[] wizards = category
if (wizards != null)
reverseArray(wizards);
List<IWizardDescriptor> shown = new ArrayList<IWizardDescriptor>();
for (IWizardDescriptor wizard : wizards)
IConfigurationElement element = (IConfigurationElement) wizard
IConfigurationElement[] children = element
if (children != null && children
IConfigurationElement enablement = children[
ActionExpression exp = new ActionExpression(enablement); [Page]
if (!exp
continue;
}
}
shown
}
if (shown
create = true;
if (hasMenu)
new MenuItem(menu
}
for (IWizardDescriptor wizard : shown)
MenuItem item = new MenuItem(menu
item
if (wizard
item
item
item
}
}
}
return create;
}
/** *//**
* 反轉數組
*
* @param array
*/
private void reverseArray(Object[] array)
if (array != null)
int count = array
int last = count
for (int index =
Object temp = array[index];
array[index] = array[last
array[last] = temp;
}
}
}
/** *//**
* (non
*
* @see org
*/
public void widgetDefaultSelected(SelectionEvent e)
// do nothing
}
/** *//**
* (non
*
* @see org
*/ [Page]
public void widgetSelected(SelectionEvent e)
MenuItem item = (MenuItem) e
IWizardDescriptor desc = (IWizardDescriptor) item
try
IWorkbenchWizard wizard = desc
wizard
WizardDialog dialog = new WizardDialog(workbench
wizard);
dialog
} catch (CoreException ex)
// TODO 處理異常
}
}
}
這裡有幾個重點
第一個是IWorkbenchWindowPulldownDelegate
第二個就是getMenu(Menu)方法只會被調用一次
第三就是selectionChanged的操作
/** *//**
* (non
*
* @see org
* org
*/
public void selectionChanged(IAction action
this
if (parent != null)
// 找到新建菜單項
// 重新創建其子菜單
MenuItem[] items = parent
if (items != null)
MenuItem parentItem = null;
for (int index =
MenuItem item = items[index];
Object data = item
if (data instanceof ActionSetContributionItem)
IContributionItem innerItem = ((ActionSetContributionItem) data)
if (innerItem
parentItem = item;
break;
} [Page]
}
}
if (parentItem != null)
Menu menu = getMenu(parent);
parentItem
parentItem
}
}
}
}
parent是在getMenu(menu)中記錄下來的
沒有子菜單的很簡單
說完這個
實際上JDT就是這麼做的
至於PopupMenus
還有這一段
IConfigurationElement element = (IConfigurationElement) wizard
IConfigurationElement[] children = element [Page]
if (children != null && children
IConfigurationElement enablement = children[
ActionExpression exp = new ActionExpression(enablement);
if (!exp
continue;
}
}
Wizard裡面好像只能添加selection的限制
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28387.html