33 34 35 36
} }
在构造函数中保存我们工作台窗口的引用,在run方法中执行功能,是不是很简单?在这里,我们用到了一个对话框类cn.blogjava.youxia.rcp_start.FirstDialog,这个类从
org.eclipse.swt.widgets.Dialog类继承,熟悉swt的朋友一定不会陌生。我建议大家可以使用Designer插件,这个插件对swt/jface提供非常好的可视化支持,在这个对话框中,我们只简单的添加了两个按钮。 FirstDialog.java源文件如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
protected Shell shell;
private int result;
public FirstDialog(Shell parent, int style) { super (parent, style);
package cn.blogjava.youxia.rcp_start;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Dialog; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell;
public class FirstDialog extends Dialog {
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
}
public FirstDialog(Shell parent) { this (parent, SWT.NONE); }
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.APPLIC
ATION_MODAL); 40 41 42 43 44 45 46 47 48
shell.setSize( 150 , 70 ); shell.setText( \ 第一个对话框 \ );
final Button okButton = new Button(shell, SWT.NONE); okButton.addSelectionListener( new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { result = 1 ; shell.dispose(); }
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
} );
okButton.setText( \ OK \ );
okButton.setBounds( 10 , 10 , 48 , 22 );
final Button cancelButton = new Button(shell, SWT.NONE); cancelButton.addSelectionListener( new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { result = 2 ; shell.dispose(); } } );
cancelButton.setText( \ Cancel \ );
cancelButton.setBounds( 89 , 10 , 48 , 22 ); } }
上面所讲的,只是添加菜单和工具栏的第一种方法,这种方法把构建菜单的工作以静态代码的方式加入到了ApplicationActionBarAdvisor类中,如果需要修改用户界面,则需要修改代码并重新编译。
添加菜单项的第二种方法就要简单得多,而且修改起来也方便,还可以对菜单项实现更加灵活的控制,但是,需要对Eclipse的插件基础有比较好的了解。那这第二种方法就是通过扩展actionSets扩展点来添加菜单。
对扩展点的扩展,可以通过编辑plugin.xml文件了实现,比如我们添加的第二个菜单项,其配置文件如下: 1 2
< extension
id =\
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
name =\我的菜单扩展\
point =\ > < actionSet
description =\第一个扩展\ id =\ label =\ visible =\rue\ > < action
class =\ icon =\ id =\ label =\第二个菜单项\
menubarPath =\menu/additions\ style =\
toolbarPath =\ tooltip =\第二个菜单项的按钮\ /> actionSet > extension >
其实Eclipse为我们提供了很好的可视化plugin.xml的编辑器,如下图,我们可以对菜单的外观进行和行为进行灵活的控制:
相关推荐: