好在二的基础上,继续,这个时候我们须要增加一个Viewer.在这里我须要说一下,在eclipse的开发中用到的很多
的是Viewer(视图)这个概念,而不像Vb等开发中经常用到的window(窗口),并不是说Rcp中没有窗口,而是使用
频率较低,所以分别说一下如何添加窗口和Viewer
一,添加一个对话框窗口:添加一个类如下,然後在须要显视的地方调用一下open()方法
不一定非要继承自Dialog,这里为了渐少一些代码,而且我使用中也多继承自Dialog
package hellorcp;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class HelloDialog extends Dialog {
protected Object result;
protected Shell shell;
public HelloDialog(Shell parent, int style) {
super(parent, style);
}
public HelloDialog(Shell parent) {
this(parent, SWT.NONE);
}
public Object 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(500, 375);
shell.setText("SWT Dialog");
//
}
}
二,添加一个viewer,首先建立一个viewer,下边是Designer(一个很好用的插件)自动生成的一个viewer,
也就是一个Viewer的大概结构
package hellorcp;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;
public class HelloView extends ViewPart {
public static final String ID = "hellorcp.HelloView"; //$NON-NLS-1$
public void createPartControl(Composite parent) {
Composite container = new Composite(parent, SWT.NONE);
//
createActions();
initializeToolBar();
initializeMenu();
}
private void createActions() {
// Create the actions
}
private void initializeToolBar() {
IToolBarManager toolbarManager = getViewSite().getActionBars()
.getToolBarManager();
}
private void initializeMenu() {
IMenuManager menuManager = getViewSite().getActionBars()
.getMenuManager();
}
public void setFocus() {
// Set the focus
}
}
显视这个viewer,每个viewer须要加载到perspective上才能显视,所以在Perspective.java中加入如下代码
public void createInitialLayout(IPageLayout layout) {
layout.setEditorAreaVisible(false);//不显视edit窗口
String editorArea = layout.getEditorArea();
//下边两句的不同是,一个显视的是单页窗口,一个显视的是多页窗口
layout.addStandaloneView(HelloViewer.ID,false, IPageLayout.LEFT, 0.25f, editorArea);
layout.addView(HelloViewer.ID, IPageLayout.RIGHT, 0.75f, editorArea);
}
三,在viewer或者dialog上添加控件,如果装有Designer可以直接拖放,如果没有编程实现也可以
大部份添加到下边这样的函数中
viewer:
public void createPartControl(Composite parent) {
Composite container = new Composite(parent, SWT.NONE);
//添加一个button
final Button delBtn = new Button(container, SWT.NONE);
delBtn.setText("删除");
delBtn.setBounds(10, 83, 44, 22);
addListener2DelBtn(delBtn);
createActions();
initializeToolBar();
initializeMenu();
}
dialog:
protected void createContents() {
shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
shell.setSize(500, 375);
shell.setText("SWT Dialog");
}
四,响应事件,SWT的事件响应和Swing是一样的,添加listener
delBtn.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
//加入你响应事件要做的事情
}
});
五,布局
布局方面swt没有什么新的地方,发个简单使用布局的例子,参考吧.另外布局还有很多搭配,但不是本文的
重点,暂时带过
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
public class TableDemo {
/**
* @param args
*/
public static void main(String[] args) {
Display dispMain = new Display ();
final Shell shellMain = new Shell (dispMain, SWT.TITLE | SWT.MIN | SWT.BORDER);
shellMain.setText("Table Demo");
FormLayout formLayout = new FormLayout();
formLayout.marginLeft = 10;
formLayout.marginRight = 10;
formLayout.marginTop = 10;
formLayout.marginBottom = 10;
formLayout.spacing = 10;
shellMain.setLayout(formLayout);
shellMain.setSize(800, 600);
Point size = shellMain.getSize();
Rectangle rect = dispMain.getBounds();
shellMain.setLocation(rect.x + (rect.width-size.x)/2, rect.y + (rect.height-size.y)/2);
Table demoTable = (Table)createContents(shellMain);
FormData formData = new FormData();
formData.left = new FormAttachment(0, 0);
formData.top = new FormAttachment(0, 0);
formData.right = new FormAttachment(100, 0);
formData.bottom = new FormAttachment(100, -34);
demoTable.setLayoutData(formData);
Button btnClose = new Button(shellMain, SWT.PUSH | SWT.FLAT);
btnClose.setText("close");
formData = new FormData();
formData.right = new FormAttachment(100, 0);
formData.top = new FormAttachment(demoTable, 0);
formData.width = 100;
formData.bottom = new FormAttachment(100, 0);
btnClose.setLayoutData(formData);
btnClose.addSelectionListener(
new SelectionListener() {
public void widgetDefaultSelected(SelectionEvent e) {
this.widgetSelected(e);
}
public void widgetSelected(SelectionEvent e) {
shellMain.close();
}
}
);
shellMain.open ();
while (!shellMain.isDisposed ()) {
if (!dispMain.readAndDispatch ()) {
dispMain.sleep ();
}
}
dispMain.dispose ();
dispMain = null;
}
/**
*
* @param shellMain
* @return
*/
private static Table createContents(Shell shellMain) {
Table table = new Table(shellMain, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
table.setHeaderVisible(true);
table.setLinesVisible(true);
table.setFont(new Font(shellMain.getDisplay(), "Arial", 11, SWT.BOLD));
TableColumn colNumber = new TableColumn(table, SWT.NONE);
TableColumn colMedName = new TableColumn(table, SWT.CENTER);
TableColumn colPrice = new TableColumn(table, SWT.CENTER);
TableColumn colUnit = new TableColumn(table, SWT.CENTER);
TableColumn colCount = new TableColumn(table, SWT.CENTER);
colNumber.setWidth(25);
colMedName.setWidth(150);
colMedName.setText("Medicine Name");
colPrice.setWidth(150);
colPrice.setText("Medicine Price");
colUnit.setWidth(150);
colUnit.setText("Medicine Unit");
colCount.setWidth(150);
colCount.setText("Medicine Count");
for(int i=0; i<10; i++) {
TableItem item = new TableItem(table, SWT.NONE);
item.setText(new String[]{i+1+"","4/2","4/3","4/4","4/5","4/6","4/7","4/8","4/9"});
}
return table;
}
}
五,加入 右键 ,双击
加入两个listener
//右键
private void hookContextMenu() {
MenuManager menuMgr = new MenuManager("#PopupMenu"); //$NON-NLS-1$
menuMgr.setRemoveAllWhenShown(true);
menuMgr.addMenuListener(new IMenuListener() {
public void menuAboutToShow(IMenuManager manager) {
HelloView.this.fillContextMenu(manager);
}
});
Menu menu = menuMgr.createContextMenu(viewer.getControl());
viewer.getControl().setMenu(menu);
getSite().registerContextMenu(menuMgr, viewer);
}
private void fillContextMenu(IMenuManager manager) {
manager.add(addAction);
manager.add(modifyAction);
manager.add(deleteAction);
manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
}
//双击
private void hookDoubleClickAction() {
viewer.addDoubleClickListener(new IDoubleClickListener() {
public void doubleClick(DoubleClickEvent event) {
//doubleClickAction.run();
}
});
}
六,使用TableViewer
Jface中封装了talbeViewer TreeViewer等控件,能简单的实现很多功能,首先说说TableView
//SWT.FULL_SELECTION 可以选中一整行
//SWT.MULTI 可以选中多行
viewer = new TableViewer(wareListGroup, SWT.BORDER | SWT.FULL_SELECTION
| SWT.MULTI);
final Table table = viewer.getTable();
table.setHeaderVisible(true);//显视表头
table.setLinesVisible(true);//显视表格
//实现点击表头自动重新排序
final TableColumn num = new TableColumn(table, SWT.NONE);
num.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
resetSort(WareViewerSort.NUM);
//resetSort是自己实现的重新排序的函数,只须要把不通的ViewerSort重新设置给
TableViewer,并刷新
}
});
num.setAlignment(SWT.CENTER);
num.setWidth(50);
//这个地方使用了message,只要做过国际话的大概都能明白
num.setText(Messages.getString("HelloView.warenum")); //$NON-NLS-1$
final TableColumn name = new TableColumn(table, SWT.NONE);
name.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
resetSort(WareViewerSort.NAME);//同上
}
});
name.setWidth(80);
name.setText(Messages.getString("WareView.warename")); //$NON-NLS-1$
name.setAlignment(SWT.CENTER);
final TableColumn desc = new TableColumn(table, SWT.NONE);
desc.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
resetSort(WareViewerSort.DESC);
}
});
desc.setWidth(110);
desc.setText(Messages.getString("WareView.waredesc")); //$NON-NLS-1$
final TableColumn price = new TableColumn(table, SWT.NONE);
price.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
resetSort(WareViewerSort.PRICE);
}
});
price.setWidth(70);
price.setText(Messages.getString("WareView.wareprice")); //$NON-NLS-1$
price.setAlignment(SWT.RIGHT);
final TableColumn updDate = new TableColumn(table, SWT.NONE);
updDate.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
resetSort(WareViewerSort.UPDDATE);
}
});
updDate.setWidth(150);
updDate.setText(Messages.getString("WareView.wareupddate")); //$NON-NLS-1$
updDate.setAlignment(SWT.CENTER);
//一个TableViewer里边的数据变动主要取决于下边四句
viewer.setContentProvider(new WareContentProvider()); //表的显视
viewer.setLabelProvider(new WareLabelProvider()); //表的数据提供者
viewer.setInput(//真实的数据来源); //数据来源例如ArrayList等
viewer.setSorter(new WareViewerSort()); //排序
两个provider的实现类似下边的情况
class WareContentProvider implements IStructuredContentProvider {
public Object[] getElements(Object inputElement) {
if (inputElement instanceof Node) {
ArrayList list = new ArrayList();
makeWareList(((Node) inputElement), list);
return list.toArray();
}
if (inputElement instanceof List) {
return ((List) inputElement).toArray();
}
return null;
}
public void dispose() {
// TODO Auto-generated method stub
}
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
// TODO Auto-generated method stub
}
}
class WareLabelProvider extends LabelProvider implements
ITableLabelProvider {
public Image getColumnImage(Object element, int columnIndex) {
// TODO Auto-generated method stub
return null;
}
public String getColumnText(Object element, int columnIndex) {
if (element instanceof Ware) {
switch (columnIndex) {
case 0:
return ((Ware) element).getDisplayNum();
case 1:
return ((Ware) element).getDisplayName();
case 2:
return ((Ware) element).getDisplayDesc();
case 3:
return ((Ware) element).getDisplayPrice();
case 4:
return ((Ware) element).getDisplayUpdDate();
default:
break;
}
}
return null;
}
}