import org.eclipse.jface.action.Action;import org.eclipse.ui.IWorkbenchWindow;import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;public class AboutAction extends Action implements IWorkbenchAction {private IWorkbenchWindow workbenchWindow;public AboutAction(IWorkbenchWindow window){if(window==null){throw new IllegalArgumentException();}this.workbenchWindow=window;}public void run(){if(workbenchWindow !=null){AboutDialog ad=new AboutDialog(workbenchWindow.getShell());ad.open();}}@Overridepublic void dispose() {}}
在上面的run方法中我用到了一個AboutDialog,這個類是一個表單類,具體實現為:
import org.eclipse.swt.SWT;import org.eclipse.swt.widgets.Dialog;import org.eclipse.swt.widgets.Display;import org.eclipse.swt.widgets.Label;import org.eclipse.swt.widgets.Shell;public class AboutDialog extends Dialog{protected Shell shell;private int result;public AboutDialog(Shell parent) {this(parent,SWT.NONE);}public AboutDialog(Shell parent,int style){super(parent,style);}public int open(){createContents();shell.open();shell.layout();Display display=getParent().getDisplay();while(!shell.isDisposed()){if(!display.readAndDispatch())display.sleep();}return result;}protected void createContents(){shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);shell.setSize(400, 150);shell.setText("關於 ");Label label2 =new Label(shell,SWT.NONE);label2.setText("自訂菜單");label2.setBounds(70, 50, 300,20);}}
下面就是和前面部落格講解的那樣註冊Action和實現FillMenuBar。
public class ApplicationActionBarAdvisor extends ActionBarAdvisor {private IWorkbenchAction aboutAction;public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) {super(configurer);}protected void makeActions(IWorkbenchWindow window) {aboutAction = new AboutAction(window);aboutAction.setId("test.about");aboutAction.setText("關於");register(aboutAction);}protected void fillMenuBar(IMenuManager menuBar) {menuBar.add(createHelpMenu());}private MenuManager createHelpMenu() {MenuManager menu = new MenuManager("About");menu.add(aboutAction);return menu;}}
實現這些代碼之後我們就可以運行了。之後就會出現一下的表單:
這樣自訂菜單就完成了。