﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-超越自我-文章分类-SWT/JFace/Draw2d/Swing技术</title><link>http://www.blogjava.net/jame-liu/category/3915.html</link><description>一切皆有可能</description><language>zh-cn</language><lastBuildDate>Thu, 01 Mar 2007 18:26:31 GMT</lastBuildDate><pubDate>Thu, 01 Mar 2007 18:26:31 GMT</pubDate><ttl>60</ttl><item><title>创建TableViewer</title><link>http://www.blogjava.net/jame-liu/articles/17789.html</link><dc:creator>jame</dc:creator><author>jame</author><pubDate>Wed, 02 Nov 2005 05:16:00 GMT</pubDate><guid>http://www.blogjava.net/jame-liu/articles/17789.html</guid><wfw:comment>http://www.blogjava.net/jame-liu/comments/17789.html</wfw:comment><comments>http://www.blogjava.net/jame-liu/articles/17789.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.blogjava.net/jame-liu/comments/commentRss/17789.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jame-liu/services/trackbacks/17789.html</trackback:ping><description><![CDATA[<P><IMG height=124 alt=checkbox.JPG src="http://www.blogjava.net/images/blogjava_net/jame-liu/upload/checkbox.JPG" width=526 border=0><BR>1.tableviewer初始化<BR>TableViewer viewer = new TableViewer(chooseModuleGroup,SWT.FULL_SELECTION|SWT.BORDER);<BR>&nbsp;&nbsp;&nbsp;Table table=viewer.getTable();<BR>&nbsp;&nbsp;&nbsp;GridData griddata = new GridData(GridData.FILL_BOTH);<BR>&nbsp;&nbsp;&nbsp;griddata.horizontalSpan = 2;<BR>&nbsp;&nbsp;&nbsp;griddata.verticalSpan = 2;<BR>&nbsp;&nbsp;&nbsp;griddata.heightHint=100;<BR>&nbsp;&nbsp;&nbsp;table.setLayoutData(griddata);<BR>&nbsp;&nbsp;&nbsp;TableLayout layout = new TableLayout();<BR>&nbsp;&nbsp;&nbsp;layout.addColumnData(new ColumnWeightData(120, true));<BR>&nbsp;&nbsp;&nbsp;layout.addColumnData(new ColumnWeightData(120, true));<BR>&nbsp;&nbsp;&nbsp;TableColumn column1 = new TableColumn(table,SWT.CENTER);<BR>&nbsp;&nbsp;&nbsp;column1.setText("choose");<BR>&nbsp;&nbsp;&nbsp;TableColumn column2 = new TableColumn(table,SWT.CENTER);<BR>&nbsp;&nbsp;&nbsp;column2.setText("module");<BR>&nbsp;&nbsp;&nbsp;viewer.setColumnProperties(new String[]{"choose","module"});<BR>&nbsp;&nbsp;&nbsp;table.setLayout(layout);<BR>&nbsp;&nbsp;&nbsp;table.setLinesVisible(true);<BR>&nbsp;&nbsp;&nbsp;table.setHeaderVisible(true);<BR>&nbsp;&nbsp;&nbsp;viewer.setCellEditors(new CellEditor[] {new CheckboxCellEditor(table) ,new TextCellEditor(table)});<BR>&nbsp;&nbsp;&nbsp;viewer.setLabelProvider(new CheckBoxLabelProvider());<BR>&nbsp;&nbsp;&nbsp;viewer.setContentProvider(new CheckBoxContentProvider());<BR>&nbsp;&nbsp;&nbsp;viewer.setCellModifier(new ModuleSelectionModifier());<BR>&nbsp;&nbsp;&nbsp;viewer.setInput(selectionList);<BR>&nbsp;&nbsp;&nbsp;selectionList.collectSelectedModules();<BR>2.创建ICellModifier<BR>class ModuleSelectionModifier implements ICellModifier{</P>
<P>&nbsp;&nbsp;&nbsp;public boolean canModify(Object element, String property) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;if(property.equals("choose")){<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return true;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;return false;<BR>&nbsp;&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;&nbsp;public Object getValue(Object element, String property) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;Object result=null;<BR>&nbsp;&nbsp;&nbsp;&nbsp;MichelleSelectedModule module=(MichelleSelectedModule)element;<BR>&nbsp;&nbsp;&nbsp;&nbsp;if(property.equals("choose")){<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return new Boolean(module.getSelected());<BR>&nbsp;&nbsp;&nbsp;&nbsp;}else if(property.equals("module")){<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return module.getPlugin();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }else{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return result;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;&nbsp;public void modify(Object element, String property, Object value) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;MichelleSelectedModule module=(MichelleSelectedModule)((TableItem) element).getData();<BR>&nbsp;&nbsp;&nbsp;&nbsp;if(property.equals("choose")){<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;module.setSelected(((Boolean)value).booleanValue());<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;selectionList.moduleChanged(module);<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;}<BR>&nbsp;}<BR>3.创建modellist 为tableviewer提供添加 删除 更新等model的方法<BR>private ModuleSelectionList selectionList=new ModuleSelectionList();<BR>&nbsp;class CheckBoxContentProvider implements IStructuredContentProvider,IModuleListViewer{<BR>&nbsp;&nbsp;public Object[] getElements(Object inputElement) {<BR>&nbsp;&nbsp;&nbsp;return selectionList.getElements();<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;public void dispose() {<BR>&nbsp;&nbsp;&nbsp;selectionList.removeChangeListener(this);<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;public void addModule(MichelleSelectedModule module) {<BR>&nbsp;&nbsp;&nbsp;viewer.add(module);<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;public void removeModule(MichelleSelectedModule module) {<BR>&nbsp;&nbsp;&nbsp;viewer.remove(module);<BR>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;<BR>&nbsp;&nbsp;public void updateModule(MichelleSelectedModule module) {<BR>&nbsp;&nbsp;&nbsp;viewer.update(module, null);&nbsp;<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {<BR>&nbsp;&nbsp;&nbsp;if (newInput != null)<BR>&nbsp;&nbsp;&nbsp;&nbsp;((ModuleSelectionList) newInput).addChangeListener(this);<BR>&nbsp;&nbsp;&nbsp;if (oldInput != null)<BR>&nbsp;&nbsp;&nbsp;&nbsp;((ModuleSelectionList) oldInput).removeChangeListener(this);<BR>&nbsp;&nbsp;}<BR>&nbsp;}<BR>4.创建LabelProvider&nbsp; (这个LabelProvider 在第一列提供checkbox选项)<BR>class CheckBoxLabelProvider extends LabelProvider implements ITableLabelProvider{<BR>&nbsp;&nbsp;private Image getImageByBoolean(boolean selected) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return selected?checkImage.createImage():unCheckImage.createImage();<BR>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;public Image getColumnImage(Object element, int columnIndex) {<BR>&nbsp;&nbsp;&nbsp;return (columnIndex == 0) ?&nbsp;&nbsp; // COMPLETED_COLUMN?<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getImageByBoolean(((MichelleSelectedModule) element).getSelected()) :<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;null;<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;public String getColumnText(Object element, int columnIndex) {<BR>&nbsp;&nbsp;&nbsp;String result = "";<BR>&nbsp;&nbsp;&nbsp;if(element==null)return result;<BR>&nbsp;&nbsp;&nbsp;MichelleSelectedModule module=(MichelleSelectedModule)element;<BR>&nbsp;&nbsp;&nbsp;switch (columnIndex) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;case 0:&nbsp; // COMPLETED_COLUMN<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<BR>&nbsp;&nbsp;&nbsp;&nbsp;case 1 :<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result =module.getPlugin();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<BR>&nbsp;&nbsp;&nbsp;&nbsp;default :<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break; &nbsp;<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;return result;<BR>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;<BR>&nbsp;}</P><img src ="http://www.blogjava.net/jame-liu/aggbug/17789.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jame-liu/" target="_blank">jame</a> 2005-11-02 13:16 <a href="http://www.blogjava.net/jame-liu/articles/17789.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>