此使用实现一个TableViewer展示数据同时点击双击行是弹出属性页面,进行修改。
package com.unutrip.plugin.dev.views; import java.util.List; import org.eclipse.jface.action.Action; import org.eclipse.jface.action.IMenuListener; import org.eclipse.jface.action.IMenuManager; import org.eclipse.jface.action.IToolBarManager; import org.eclipse.jface.action.MenuManager; import org.eclipse.jface.action.Separator; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.jface.viewers.ColumnWeightData; import org.eclipse.jface.viewers.DoubleClickEvent; import org.eclipse.jface.viewers.IDoubleClickListener; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.IStructuredContentProvider; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.viewers.ITableLabelProvider; import org.eclipse.jface.viewers.LabelProvider; import org.eclipse.jface.viewers.TableLayout; import org.eclipse.jface.viewers.TableViewer; import org.eclipse.jface.viewers.Viewer; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Menu; import org.eclipse.swt.widgets.Table; import org.eclipse.swt.widgets.TableColumn; import org.eclipse.ui.IActionBars; import org.eclipse.ui.ISharedImages; import org.eclipse.ui.IWorkbenchActionConstants; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.internal.dialogs.PropertyDialog; import org.eclipse.ui.part.ViewPart; import com.unutrip.plugin.dev.initdata.InitData; import com.unutrip.plugin.dev.model.Person; import com.unutrip.plugin.dev.model.PersonCompartor; /** * 关于一个TableViewer的使用View * @author longgangbai * */ public class PersonView extends ViewPart { private TableViewer viewer; private Action action1; private Action action2; private Action doubleClickAction; /* * The content provider class is responsible for * providing objects to the view. It can wrap * existing objects in adapters or simply return * objects as-is. These objects may be sensitive * to the current input of the view, or ignore * it and always show the same content * (like Task List, for example). */ class ViewContentProvider implements IStructuredContentProvider { public void inputChanged(Viewer v, Object oldInput, Object newInput) { } public void dispose() { } public Object[] getElements(Object parent) { if(parent instanceof List){ return ((List)parent).toArray(); }else{ return new Object[0]; } } } class ViewLabelProvider extends LabelProvider implements ITableLabelProvider { public String getColumnText(Object obj, int index) { String value=""; if(obj instanceof Person ) { Person p=(Person)obj; switch(index){ case 0 : value=p.getStuNo(); break; case 1 : value=p.getStuName(); break; case 2 : value=p.getStuAge(); break; case 3 : value=p.getStuAddr(); break; case 4 : value=p.getStuSex(); break; } } return value; } public Image getColumnImage(Object obj, int index) { if(obj instanceof Person) { if(index==0) { return getImage(obj); } } return null; } public Image getImage(Object obj) { return PlatformUI.getWorkbench(). getSharedImages().getImage(ISharedImages.IMG_OBJ_ELEMENT); } } /** * The constructor. */ public PersonView() { } /** * This is a callback that will allow us * to create the viewer and initialize it. */ public void createPartControl(Composite parent) { createTableViewer(parent); makeActions(); hookContextMenu(); hookDoubleClickAction(); contributeToActionBars(); } private void createTableViewer(Composite parent) { viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL|SWT.FULL_SELECTION|SWT.BORDER); Table table=viewer.getTable(); facadeTableViewer(table); TableLayout layout=new TableLayout(); table.setLayout(layout); layout.addColumnData(new ColumnWeightData(50)); viewer.setContentProvider(new ViewContentProvider()); viewer.setLabelProvider(new ViewLabelProvider()); viewer.setSorter(new PersonCompartor()); viewer.setInput(InitData.init()); } private void facadeTableViewer(Table table) { table.setLinesVisible(true); table.setHeaderVisible(true); final TableColumn newColumnTableColumn = new TableColumn(table, SWT.NONE); newColumnTableColumn.setWidth(39); newColumnTableColumn.setText("编号"); newColumnTableColumn.addSelectionListener(new SelectionAdapter(){ boolean asc = true; public void widgetSelected(SelectionEvent e){ viewer.setSorter(asc?PersonCompartor.ID_ASC:PersonCompartor.ID_DESC); asc = !asc; } }); final TableColumn newColumnTableColumn_1 = new TableColumn(table, SWT.NONE); newColumnTableColumn_1.setWidth(85); newColumnTableColumn_1.setText("姓名"); newColumnTableColumn_1.addSelectionListener(new SelectionAdapter(){ boolean asc = true; public void widgetSelected(SelectionEvent e){ viewer.setSorter(asc?PersonCompartor.NAME_ASC:PersonCompartor.NAME_DESC); asc = !asc; } }); final TableColumn newColumnTableColumn_3 = new TableColumn(table, SWT.NONE); newColumnTableColumn_3.setWidth(43); newColumnTableColumn_3.setText("年龄"); newColumnTableColumn_3.addSelectionListener(new SelectionAdapter(){ boolean asc = true; public void widgetSelected(SelectionEvent e){ viewer.setSorter(asc?PersonCompartor.AGE_ASC:PersonCompartor.AGE_DESC); asc = !asc; } }); final TableColumn newColumnTableColumn_4 = new TableColumn(table, SWT.NONE); newColumnTableColumn_4.setWidth(126); newColumnTableColumn_4.setText("地址"); final TableColumn newColumnTableColumn_2 = new TableColumn(table, SWT.NONE); newColumnTableColumn_2.setWidth(41); newColumnTableColumn_2.setText("性别"); } private void hookContextMenu() { MenuManager menuMgr = new MenuManager("#PopupMenu"); menuMgr.setRemoveAllWhenShown(true); menuMgr.addMenuListener(new IMenuListener() { public void menuAboutToShow(IMenuManager manager) { PersonView.this.fillContextMenu(manager); } }); Menu menu = menuMgr.createContextMenu(viewer.getControl()); viewer.getControl().setMenu(menu); getSite().registerContextMenu(menuMgr, viewer); } private void contributeToActionBars() { IActionBars bars = getViewSite().getActionBars(); fillLocalPullDown(bars.getMenuManager()); fillLocalToolBar(bars.getToolBarManager()); } private void fillLocalPullDown(IMenuManager manager) { manager.add(action1); manager.add(new Separator()); manager.add(action2); } private void fillContextMenu(IMenuManager manager) { manager.add(action1); manager.add(action2); // Other plug-ins can contribute there actions here manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS)); } private void fillLocalToolBar(IToolBarManager manager) { manager.add(action1); manager.add(action2); } private void makeActions() { action1 = new Action() { public void run() { showMessage("Action 1 executed"); } }; action1.setText("Action 1"); action1.setToolTipText("Action 1 tooltip"); action1.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages(). getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK)); action2 = new Action() { public void run() { showMessage("Action 2 executed"); } }; action2.setText("Action 2"); action2.setToolTipText("Action 2 tooltip"); action2.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages(). getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK)); doubleClickAction = new Action() { @SuppressWarnings("restriction") public void run() { //获取一个选中的对象 ISelection selection = viewer.getSelection(); Object obj = ((IStructuredSelection)selection).getFirstElement(); //打开属性页面 PropertyDialog dialog = PropertyDialog.createDialogOn(getSite().getShell(), "com.unutrip.plugin.dev.properties.SamplePropertyPage", obj); dialog.open(); } }; } private void hookDoubleClickAction() { viewer.addDoubleClickListener(new IDoubleClickListener() { public void doubleClick(DoubleClickEvent event) { doubleClickAction.run(); } }); } private void showMessage(String message) { MessageDialog.openInformation( viewer.getControl().getShell(), "Sample View", message); } /** * Passing the focus request to the viewer's control. */ public void setFocus() { viewer.getControl().setFocus(); } }
package com.unutrip.plugin.dev.model; import java.util.Comparator; import org.eclipse.jface.viewers.ViewerSorter; public class PersonCompartor extends ViewerSorter implements Comparator<Person> { private static final int ID = 1; private static final int NAME = 2; private static final int SEX = 3; private static final int AGE = 4; private static final int CREATE_DATE = 5; private int index=0; private int sortType=1; public static final PersonCompartor ID_ASC = new PersonCompartor(ID); public static final PersonCompartor ID_DESC = new PersonCompartor(-ID); public static final PersonCompartor NAME_ASC = new PersonCompartor(NAME); public static final PersonCompartor NAME_DESC = new PersonCompartor(-NAME); public static final PersonCompartor SEX_ASC = new PersonCompartor(SEX); public static final PersonCompartor SEX_DESC = new PersonCompartor(-SEX); public static final PersonCompartor AGE_ASC = new PersonCompartor(AGE); public static final PersonCompartor AGE_DESC = new PersonCompartor(-AGE); public static final PersonCompartor CREATE_DATE_ASC = new PersonCompartor(CREATE_DATE); public static final PersonCompartor CREATE_DATE_DESC = new PersonCompartor(-CREATE_DATE); public PersonCompartor(int inxdex,int sortType) { this.index=inxdex; this.sortType=sortType; } public PersonCompartor() { } public PersonCompartor(int inxdex) { this.index=inxdex; } public int compare(Person o1, Person o2) { int result=0; switch(index) { case 0: result= o1.getStuNo().compareTo(o2.getStuNo()); case 1: result= o1.getStuName().compareTo(o2.getStuName()); case 2: result= o1.getStuAge().compareTo(o2.getStuAge()); case 3: result= o1.getStuAddr().compareTo(o2.getStuAddr()); case 4: result= o1.getStuSex().compareTo(o2.getStuSex()); } if(sortType<0) { return -result; } return result; } }