在上一文中,,我們介紹了怎樣創(chuàng)建一個(gè)Eclipse RCP,,并創(chuàng)建用戶(hù)交互的視圖,但是并沒(méi)有提到如何實(shí)現(xiàn)視圖的交互,。在本文中,我們會(huì)介紹以?xún)煞N不同的方式實(shí)現(xiàn)Eclipse RCP View之間的交互,,并結(jié)合代碼詳細(xì)領(lǐng)略一下Eclipse是如何巧妙處理View之間交互的,。
首先介紹一下我們要達(dá)到的效果,,如下圖所示: 可以看到,,用戶(hù)點(diǎn)擊左邊視圖上的人員列表,該人員的詳細(xì)信息會(huì)顯示在右側(cè)的view視圖中,,這一步驟看起來(lái)簡(jiǎn)單,,但是要做的工作卻不少,下面我們就著手把前期獲得的RCP程序做成這個(gè)樣子,,讓View之間傳遞信息或者更復(fù)雜一些的對(duì)象,。 從Eclipse目前提供的手段看來(lái),我們可以使用下列方法實(shí)現(xiàn)視圖的交互: (1)選擇提供器 - 選擇監(jiān)聽(tīng)器(selection provider-selection listener)模式,,從而讓視圖對(duì)其他視圖中的選擇做出反應(yīng) (2)使用屬性改變監(jiān)聽(tīng)模式,,即Property Changed Listener模式,用于視圖中沒(méi)有可供點(diǎn)擊的UI模塊的情況下,。這類(lèi)監(jiān)聽(tīng)模式允許視圖將屬性改變事件 以下將詳細(xì)介紹這兩種模式的概念,、適用場(chǎng)景,并結(jié)合我們的Hello RCP分別實(shí)例說(shuō)明這兩種視圖交互模式的實(shí)現(xiàn),。 (一)選擇提供器-選擇監(jiān)聽(tīng)器(selection provider-selection listener)模式: 這是一種傳統(tǒng)的事件傳遞模式,,一般來(lái)說(shuō),只要視圖中的UI控件實(shí)現(xiàn)了ISelectionProvider接口,,即具備提供選擇事件的能力,,就可以使用這個(gè)模式將自身發(fā)生的選擇事件提供給其他實(shí)現(xiàn)了ISelectionListener的UI控件。例如我們?cè)贖ello RCP中實(shí)現(xiàn)的Navigation視圖,,里面采用了ListViewer這一控件,,而Jface中所有的Viewer對(duì)象都實(shí)現(xiàn)了ISelectionProvider接口,那么當(dāng)我們點(diǎn)擊navigation視圖list列表中的人員名單時(shí),,ListViewer列表就可以向其他能夠監(jiān)聽(tīng)該選擇事件的對(duì)象提供該選擇事件,,如果我們?cè)谑录邪恍┳址畔ⅲ敲雌渌麑?duì)象得到這個(gè)信息之后,,就可以進(jìn)行相應(yīng)顯示了,。 “Eclipse提供了所謂的Site,以及ISelectionService機(jī)制,,來(lái)處理試圖之間的簡(jiǎn)單的交互,。簡(jiǎn)單的說(shuō),ViewSite提供了一個(gè)交互的中心點(diǎn),,其它View向ViewSite提供選擇事件,,或者向其注冊(cè)監(jiān)聽(tīng)器,而事件的觸發(fā)與轉(zhuǎn)發(fā)則由ViewSite()來(lái)完成,?!?BR> 以上只是一些概念介紹,,對(duì)于我們來(lái)說(shuō),只需按照下列步驟進(jìn)行,,就可以將一個(gè)視圖變成一個(gè)提供Selection 事件的Selection Provider: (1)首先將視圖注冊(cè)成為Selection Provider,,即實(shí)現(xiàn)ISelectionProvider接口 public class NavigationView extends ViewPart implements ISelectionProvider 以下是ISelectionProvider接口的定義: 1public interface ISelectionProvider { (2)有了以上步驟還不夠,還需要將視圖中具體的Viewer上想要發(fā)生的事件,,注冊(cè)到這個(gè)Provider上,。2 3 public void addSelectionChangedListener(ISelectionChangedListener listener); 4 5 public ISelection getSelection(); 6 7 public void removeSelectionChangedListener( 8 9 ISelectionChangedListener listener); 10 11 public void setSelection(ISelection selection); 12 13 } 14 viewer.addSelectionChangedListener(new ISelectionChangedListener(){ 這段代碼就是把ListViewer上的Selection Change Event注冊(cè)到了Selection Provider上,由后者負(fù)責(zé)向外傳播public void selectionChanged(SelectionChangedEvent event) { ISelection selection2 = event.getSelection(); setSelection(selection2); } }); (3)一個(gè)Selection Provider想要其他部件監(jiān)聽(tīng)的話(huà),,還要向Site中控臺(tái)進(jìn)行注冊(cè),,要不然就相當(dāng)于開(kāi)會(huì)時(shí)找不到話(huà)筒講話(huà),大家聽(tīng)不見(jiàn) 1this.getSite().setSelectionProvider(this); 注意this對(duì)象指的是Viwe視圖,,它具有指向Site的引用,,通過(guò)getSite()方法獲得引用(4)最后,還要把講給聽(tīng)眾聽(tīng),,沒(méi)有聽(tīng)眾也是白講,,這里是在setSelection()方法里迭代每一個(gè)注冊(cè)了ISelectionListener的控件,找到它們,,把事件傳遞給這些聽(tīng)眾即可: public void setSelection(ISelection selection) { this.selection = selection; SelectionChangedEvent event2 = new SelectionChangedEvent(viewer, selection); for (Iterator i = myListeners.iterator(); i.hasNext();) { ISelectionChangedListener object = (ISelectionChangedListener) i.next(); object.selectionChanged(event2); } } 至此,,我們已經(jīng)實(shí)現(xiàn)了一個(gè)Selection Provider了,,對(duì)于事件監(jiān)聽(tīng)的另一端,,即Selection Listener,,則更為簡(jiǎn)單一些。只需要實(shí)現(xiàn)ISelectionListener接口,,并注冊(cè)在Site中: site.getPage().addSelectionListener(this); 然后實(shí)現(xiàn)public void selectionChanged(IWorkbenchPart part, ISelection selection) {}方法即可,。這樣,當(dāng)SelectionProvider中的選擇發(fā)生改變時(shí),,這個(gè)視圖中的selectionChanged()方法就會(huì)被調(diào)用,。 1public void selectionChanged(IWorkbenchPart part, ISelection selection) { 2 IStructuredSelection structuredSelection = (IStructuredSelection)selection; 3 Object obj = structuredSelection.getFirstElement(); 4 Person tempPerson = (Person)obj; 5 if(tempPerson != null) 6 text.setText(tempPerson.getName()); 7 text_1.setText(tempPerson.getSex()); 8 } 根據(jù)以上介紹的Selection Provider-Selection Listener模式,我們重新改造了一下navigation view視圖和detail view視圖: 1package hellorcp; 2 3import java.util.ArrayList; 4import java.util.Iterator; 5 6import org.eclipse.jface.action.IToolBarManager; 7import org.eclipse.jface.viewers.ISelection; 8import org.eclipse.jface.viewers.ISelectionChangedListener; 9import org.eclipse.jface.viewers.ISelectionProvider; 10import org.eclipse.jface.viewers.IStructuredContentProvider; 11import org.eclipse.jface.viewers.ListViewer; 12import org.eclipse.jface.viewers.SelectionChangedEvent; 13import org.eclipse.swt.SWT; 14import org.eclipse.swt.widgets.Composite; 15import org.eclipse.ui.part.ViewPart; 16import hellorcp.NavigationViewLabelProvider; 17 18public class NavigationViewPart extends ViewPart implements ISelectionProvider{ 19 20 private ISelection selection; 21 ArrayList myListeners = new ArrayList(); 22 ListViewer listViewer ; 23 24 public NavigationViewPart() { 25 // TODO Auto-generated constructor stub 26 } 27 28 @Override 29 public void createPartControl(Composite parent) { 30 listViewer = new ListViewer(parent, SWT.BORDER); 31 listViewer.setLabelProvider(new NavigationViewLabelProvider()); 32 listViewer.setContentProvider(new NavigationListViewContentProvider()); 33 listViewer.setInput(new PersonModel()); 34 initializeToolBar(); 35 listViewer.addSelectionChangedListener(new ISelectionChangedListener(){ 36 public void selectionChanged(SelectionChangedEvent event) { 37 ISelection selection2 = event.getSelection(); 38 setSelection(selection2); 39 } 40 }); 41 this.getSite().setSelectionProvider(this); 42 // TODO Auto-generated method stub 43 44 } 45 46 public ISelection getSelection() { 47 // TODO Auto-generated method stub 48 return null; 49 } 50 51 public void addSelectionChangedListener(ISelectionChangedListener listener) { 52 if(!myListeners.contains(listener)) 53 myListeners.add(listener); 54 } 55 56 public void removeSelectionChangedListener(ISelectionChangedListener listener) { 57 myListeners.remove(listener); 58 } 59 60 @Override 61 public void setFocus() { 62 // TODO Auto-generated method stub 63 64 } 65 private void initializeToolBar() { 66 IToolBarManager toolBarManager = getViewSite().getActionBars().getToolBarManager(); 67 } 68 public void setSelection(ISelection selection) { 69 this.selection = selection; 70 SelectionChangedEvent event2 = new SelectionChangedEvent(listViewer, selection); 71 for (Iterator i = myListeners.iterator(); i.hasNext();) { 72 ISelectionChangedListener object = (ISelectionChangedListener) i.next(); 73 object.selectionChanged(event2); 74 } 75 76 } 77 78} 79 1package hellorcp; 2 3import java.util.ArrayList; 4 5import org.eclipse.jface.viewers.ISelection; 6import org.eclipse.jface.viewers.IStructuredSelection; 7import org.eclipse.jface.viewers.TableViewer; 8import org.eclipse.swt.SWT; 9import org.eclipse.swt.widgets.Composite; 10import org.eclipse.swt.widgets.Group; 11import org.eclipse.swt.widgets.Label; 12import org.eclipse.swt.widgets.Table; 13import org.eclipse.swt.widgets.Text; 14import org.eclipse.ui.ISelectionListener; 15import org.eclipse.ui.IWorkbenchPart; 16import org.eclipse.ui.part.ViewPart; 17 18public class DetailView extends ViewPart implements ISelectionListener{ 19 20 private Table table; 21 private Text text_1; 22 private Text text; 23 @SuppressWarnings("unchecked") 24 ArrayList myListeners; 25 public DetailView() { 26 super(); 27 // TODO Auto-generated constructor stub 28 } 29 30 @Override 31 public void createPartControl(Composite parent) { 32 final Composite composite = new Composite(parent, SWT.NONE); 33 34 final Group group = new Group(composite, SWT.NONE); 35 group.setBounds(0, 0, 494, 62); 36 37 final Label label = new Label(group, SWT.NONE); 38 label.setText("姓名"); 39 label.setBounds(10, 32, 24, 12); 40 41 text = new Text(group, SWT.BORDER); 42 text.setBounds(40, 29, 80, 15); 43 44 final Label label_1 = new Label(group, SWT.NONE); 45 label_1.setText("性別"); 46 label_1.setBounds(172, 32, 30, 12); 47 48 text_1 = new Text(group, SWT.BORDER); 49 text_1.setBounds(208, 29, 80, 15); 50 51 final TableViewer tableViewer = new TableViewer(composite, SWT.BORDER); 52 table = tableViewer.getTable(); 53 table.setLinesVisible(true); 54 table.setHeaderVisible(true); 55 table.setBounds(0, 68, 494, 270); 56 initializeToolBar(); 57 this.getSite().getPage().addSelectionListener(this); 58 } 59 60 @Override 61 public void setFocus() { 62 // TODO Auto-generated method stub 63 64 } 65 private void initializeToolBar() { 66 } 67 68 public void selectionChanged(IWorkbenchPart part, ISelection selection) { 69 IStructuredSelection structuredSelection = (IStructuredSelection)selection; 70 Object obj = structuredSelection.getFirstElement(); 71 Person tempPerson = (Person)obj; 72 if(tempPerson != null) 73 text.setText(tempPerson.getName()); 74 text_1.setText(tempPerson.getSex()); 75 76 } 77 78} 79 將以上文件重新編譯,,再運(yùn)行我們的Hello RCP,,可以看到,點(diǎn)擊Navigation Views時(shí),,已經(jīng)可以將選擇的人員信息傳遞給detal view了,,我們的視圖真正動(dòng)了起來(lái): 其實(shí)對(duì)于本例來(lái)說(shuō),由于JFace Viewer已經(jīng)實(shí)現(xiàn)了ISelectionProvider 接口,,因此還有一種更簡(jiǎn)便的方法實(shí)現(xiàn)上述效果,,就是將navigation view中的list viewer直接注冊(cè)為Selection Provider ,這樣就可以省去實(shí)現(xiàn)ISelectionProvider接口的代碼了: 以下是NavigationViewPart.java的另外一種實(shí)現(xiàn)方式: 1package hellorcp; 對(duì)于detail view ,如果將將消費(fèi)者視圖作為監(jiān)聽(tīng)器注冊(cè)到特定的視圖部分,,就不用循環(huán)所有的Listener來(lái)通知了,,這樣可以節(jié)約更多的系統(tǒng)資源了,。2 3import java.util.ArrayList; 4import java.util.Iterator; 5 6import org.eclipse.jface.action.IToolBarManager; 7import org.eclipse.jface.viewers.ISelection; 8import org.eclipse.jface.viewers.ISelectionChangedListener; 9import org.eclipse.jface.viewers.ISelectionProvider; 10import org.eclipse.jface.viewers.IStructuredContentProvider; 11import org.eclipse.jface.viewers.ListViewer; 12import org.eclipse.jface.viewers.SelectionChangedEvent; 13import org.eclipse.swt.SWT; 14import org.eclipse.swt.widgets.Composite; 15import org.eclipse.ui.part.ViewPart; 16import hellorcp.NavigationViewLabelProvider; 17 18public class NavigationViewPart extends ViewPart{ 19 20 private ISelection selection; 21 ArrayList myListeners = new ArrayList(); 22 ListViewer listViewer ; 23 24 public NavigationViewPart() { 25 // TODO Auto-generated constructor stub 26 } 27 28 @Override 29 public void createPartControl(Composite parent) { 30 listViewer = new ListViewer(parent, SWT.BORDER); 31 listViewer.setLabelProvider(new NavigationViewLabelProvider()); 32 listViewer.setContentProvider(new NavigationListViewContentProvider()); 33 listViewer.setInput(new PersonModel()); 34 this.getSite().setSelectionProvider(listViewer); 35 // TODO Auto-generated method stub 36 37 } 38 @Override 39 public void setFocus() { 40 // TODO Auto-generated method stub 41 42 } 43 44} 45 this.getSite().getPage().addSelectionListener("hellorcp.navigationview",(ISelectionListener)this); 以下是DetailView的另外一種實(shí)現(xiàn): 1package hellorcp; 看起來(lái)后兩種實(shí)現(xiàn)是不是比前面的要輕便很多呢?2 3import java.util.ArrayList; 4 5import org.eclipse.jface.viewers.ISelection; 6import org.eclipse.jface.viewers.IStructuredSelection; 7import org.eclipse.jface.viewers.TableViewer; 8import org.eclipse.swt.SWT; 9import org.eclipse.swt.widgets.Composite; 10import org.eclipse.swt.widgets.Group; 11import org.eclipse.swt.widgets.Label; 12import org.eclipse.swt.widgets.Table; 13import org.eclipse.swt.widgets.Text; 14import org.eclipse.ui.ISelectionListener; 15import org.eclipse.ui.IWorkbenchPart; 16import org.eclipse.ui.part.ViewPart; 17 18public class DetailView extends ViewPart implements ISelectionListener{ 19 20 private Table table; 21 private Text text_1; 22 private Text text; 23 @SuppressWarnings("unchecked") 24 ArrayList myListeners; 25 public DetailView() { 26 super(); 27 // TODO Auto-generated constructor stub 28 } 29 30 @Override 31 public void createPartControl(Composite parent) { 32 final Composite composite = new Composite(parent, SWT.NONE); 33 34 final Group group = new Group(composite, SWT.NONE); 35 group.setBounds(0, 0, 494, 62); 36 37 final Label label = new Label(group, SWT.NONE); 38 label.setText("姓名"); 39 label.setBounds(10, 32, 24, 12); 40 41 text = new Text(group, SWT.BORDER); 42 text.setBounds(40, 29, 80, 15); 43 44 final Label label_1 = new Label(group, SWT.NONE); 45 label_1.setText("性別"); 46 label_1.setBounds(172, 32, 30, 12); 47 48 text_1 = new Text(group, SWT.BORDER); 49 text_1.setBounds(208, 29, 80, 15); 50 51 final TableViewer tableViewer = new TableViewer(composite, SWT.BORDER); 52 table = tableViewer.getTable(); 53 table.setLinesVisible(true); 54 table.setHeaderVisible(true); 55 table.setBounds(0, 68, 494, 270); 56 initializeToolBar(); 57 this.getSite().getPage().addSelectionListener("hellorcp.navigationview",(ISelectionListener)this); 58 59 } 60 61 @Override 62 public void setFocus() { 63 // TODO Auto-generated method stub 64 65 } 66 private void initializeToolBar() { 67 } 68 69 public void selectionChanged(IWorkbenchPart part, ISelection selection) { 70 IStructuredSelection structuredSelection = (IStructuredSelection)selection; 71 Object obj = structuredSelection.getFirstElement(); 72 Person tempPerson = (Person)obj; 73 if(tempPerson != null) 74 text.setText(tempPerson.getName()); 75 text_1.setText(tempPerson.getSex()); 76 77 } 78 79} 80 但是對(duì)于我們來(lái)講,,視圖間以Selection Provider 和Listener模式傳遞消息還存在很多局限: 1 視圖可能希望公布其他信息,,而不只是公布可視化選擇信息。公布的信息可能是根據(jù)選擇進(jìn)行某些后期處理的結(jié)果,。 2 視圖可能希望使用來(lái)自另一個(gè)插件的信息,,而這個(gè)插件可能根本沒(méi)有提供視圖(使用包含的 JFace 查看器),。在這種情況下,,使用基于 UI 選擇的鏈接是不可能的 對(duì)于以上問(wèn)題,通過(guò)自動(dòng)方式進(jìn)行就很困難了,,需要我們手工解決,,而為視圖注冊(cè)屬性監(jiān)聽(tīng)器而實(shí)現(xiàn)觀測(cè)其它視圖的屬性變化是個(gè)不錯(cuò)的替代模式。 (二)屬性改變監(jiān)聽(tīng)器模式: 屬性改變監(jiān)聽(tīng)器模式主要通過(guò)使用JFace中一個(gè)重要的接口org.eclipse.jface.util.IPropertyChangeListener來(lái)實(shí)現(xiàn),,通過(guò)注冊(cè)該監(jiān)聽(tīng)器,,以及使用IPropertyChangeEvent,可以達(dá)到事件傳遞的目的,。 與Selection provider-listener模式不同的是,,屬性改變監(jiān)聽(tīng)器可以定義到插件上,由插件本身提供注冊(cè)列表,,如下所示: 1ArrayList myListeners = new ArrayList(); 2 // A public method that allows listener registration 3 public void addPropertyChangeListener(IPropertyChangeListener listener) { 4 if(!myListeners.contains(listener)) 5 myListeners.add(listener); 6 } 7 // A public method that allows listener registration 8 public void removePropertyChangeListener(IPropertyChangeListener listener) { 9 myListeners.remove(listener); 10 } 當(dāng)插件想要把產(chǎn)生的事件通知到各個(gè)監(jiān)聽(tīng)器的時(shí)候,,就對(duì)這個(gè)注冊(cè)列表中每個(gè)Listener元素進(jìn)行迭代,逐一通知,,這樣每個(gè)Property Change Event就傳播到了各個(gè)Listener,,由后者進(jìn)行處理了,這個(gè)調(diào)用模式如下所示: 1public void initAndInvoke(ArrayList listeners, Object obj) { 2 // Post Invocation, inform listeners 3 for (Iterator<IPropertyChangeListener> iter = myListeners.iterator(); iter.hasNext();) { 4 IPropertyChangeListener element = (IPropertyChangeListener) iter.next(); 5 element.propertyChange(new PropertyChangeEvent(this, "HelloRcpEvent" , null , obj)); 6 7 } 8 } 總而言之,,要想實(shí)現(xiàn)這個(gè)模式,,我們必須自己新定義一個(gè)注冊(cè)并調(diào)用Property Change Event的類(lèi),我們把它叫做:PersonPlugin.java 那么接下來(lái)對(duì)于我們的navigation view中的list來(lái)說(shuō),,由于沒(méi)有采用provider模式,,那么就必須自己定義它的SelectionChangeListener了,我們可以在這個(gè)ListViewer的添加該Listener中實(shí)現(xiàn)我們的目的: 1package hellorcp; 2 3import java.util.ArrayList; 4import java.util.Iterator; 5 6import org.eclipse.jface.action.IToolBarManager; 7import org.eclipse.jface.util.IPropertyChangeListener; 8import org.eclipse.jface.viewers.ISelection; 9import org.eclipse.jface.viewers.ISelectionChangedListener; 10import org.eclipse.jface.viewers.ISelectionProvider; 11import org.eclipse.jface.viewers.IStructuredContentProvider; 12import org.eclipse.jface.viewers.IStructuredSelection; 13import org.eclipse.jface.viewers.ListViewer; 14import org.eclipse.jface.viewers.SelectionChangedEvent; 15import org.eclipse.swt.SWT; 16import org.eclipse.swt.widgets.Composite; 17import org.eclipse.ui.part.ViewPart; 18 19import hellorcp.Person; 20import hellorcp.PersonPlugin; 21import hellorcp.NavigationViewLabelProvider; 22 23public class NavigationViewPart extends ViewPart{ 24 25 private ISelection selection; 26 ArrayList myListeners = new ArrayList(); 27 ListViewer listViewer ; 28 29 public NavigationViewPart() { 30 // TODO Auto-generated constructor stub 31 } 32 33 @Override 34 public void createPartControl(Composite parent) { 35 listViewer = new ListViewer(parent, SWT.BORDER); 36 listViewer.setLabelProvider(new NavigationViewLabelProvider()); 37 listViewer.setContentProvider(new NavigationListViewContentProvider()); 38 listViewer.setInput(new PersonModel()); 39// this.getSite().setSelectionProvider(listViewer); 40 listViewer.addSelectionChangedListener(new ISelectionChangedListener(){ 41 public void selectionChanged(SelectionChangedEvent event) { 42 ISelection selection2 = event.getSelection(); 43 setSelection(selection2); 44 // Post Invocation, inform listeners 45 IStructuredSelection structuredSelection = (IStructuredSelection)selection; 46 Object obj = structuredSelection.getFirstElement(); 47 Person tempPerson = (Person)obj; 48 PersonPlugin.getInstance().initAndInvoke(myListeners, tempPerson); 49 } 50 }); 51 // TODO Auto-generated method stub 52 53 } 54 55 public ISelection getSelection() { 56 // TODO Auto-generated method stub 57 return null; 58 } 59 60 // A public method that allows listener registration 61 public void addPropertyChangeListener(IPropertyChangeListener listener) { 62 if(!myListeners.contains(listener)) 63 myListeners.add(listener); 64 } 65 66 // A public method that allows listener registration 67 public void removePropertyChangeListener(IPropertyChangeListener listener) { 68 myListeners.remove(listener); 69 } 70 71 @Override 72 public void setFocus() { 73 // TODO Auto-generated method stub 74 75 } 76 public void setSelection(ISelection selection) { 77 this.selection = selection; 78 } 79} 80 注意我們?yōu)閘istviewer添加了Selection Change Listener 方法,,并通知到了PersonPlugin,。 接著,在detail view中,,我們只用實(shí)現(xiàn)IPropertyChangeListener即可,,以下是detail view的代碼: 1package hellorcp; 采用屬性改變監(jiān)聽(tīng)模式能夠更加靈活地在插件之間、插件的各個(gè)view之間傳遞信息,,突破了傳遞信息必須與UI相關(guān)的局限,,而且還可以異步傳遞信息,,這些信息可以是插件后臺(tái)JOB定期運(yùn)行獲得的信息,以及定期從數(shù)據(jù)庫(kù)中獲得的信息等等,。不難看到,,屬性改變監(jiān)聽(tīng)器擴(kuò)展了插件之間,視圖之間,,前臺(tái)與后臺(tái)之間的消息傳遞場(chǎng)景,,是我們開(kāi)發(fā)Eclipse RCP應(yīng)用更好的選擇。2 3import java.util.ArrayList; 4 5import org.eclipse.jface.util.IPropertyChangeListener; 6import org.eclipse.jface.viewers.ISelection; 7import org.eclipse.jface.viewers.IStructuredSelection; 8import org.eclipse.jface.viewers.TableViewer; 9import org.eclipse.swt.SWT; 10import org.eclipse.swt.widgets.Composite; 11import org.eclipse.swt.widgets.Group; 12import org.eclipse.swt.widgets.Label; 13import org.eclipse.swt.widgets.Table; 14import org.eclipse.swt.widgets.Text; 15import org.eclipse.ui.ISelectionListener; 16import org.eclipse.ui.IWorkbenchPart; 17import org.eclipse.ui.part.ViewPart; 18 19import hellorcp.PersonPlugin; 20 21import hellorcp.Person; 22 23public class DetailView extends ViewPart implements IPropertyChangeListener{ 24 25 private Table table; 26 private Text text_1; 27 private Text text; 28 @SuppressWarnings("unchecked") 29 ArrayList myListeners; 30 public DetailView() { 31 super(); 32 // TODO Auto-generated constructor stub 33 } 34 35 @Override 36 public void createPartControl(Composite parent) { 37 final Composite composite = new Composite(parent, SWT.NONE); 38 39 final Group group = new Group(composite, SWT.NONE); 40 group.setBounds(0, 0, 494, 62); 41 42 final Label label = new Label(group, SWT.NONE); 43 label.setText("姓名"); 44 label.setBounds(10, 32, 24, 12); 45 46 text = new Text(group, SWT.BORDER); 47 text.setBounds(40, 29, 80, 15); 48 49 final Label label_1 = new Label(group, SWT.NONE); 50 label_1.setText("性別"); 51 label_1.setBounds(172, 32, 30, 12); 52 53 text_1 = new Text(group, SWT.BORDER); 54 text_1.setBounds(208, 29, 80, 15); 55 56 final TableViewer tableViewer = new TableViewer(composite, SWT.BORDER); 57 table = tableViewer.getTable(); 58 table.setLinesVisible(true); 59 table.setHeaderVisible(true); 60 table.setBounds(0, 68, 494, 270); 61 initializeToolBar(); 62 //注冊(cè)事件 63 PersonPlugin.getInstance().addPropertyChangeListener(this); 64 65 } 66 67 @Override 68 public void setFocus() { 69 // TODO Auto-generated method stub 70 71 } 72 private void initializeToolBar() { 73 } 74 75// public void selectionChanged(IWorkbenchPart part, ISelection selection) { 76// IStructuredSelection structuredSelection = (IStructuredSelection)selection; 77// Object obj = structuredSelection.getFirstElement(); 78// Person tempPerson = (Person)obj; 79// if(tempPerson != null) 80// text.setText(tempPerson.getName()); 81// text_1.setText(tempPerson.getSex()); 82// 83// } 84 public void propertyChange(org.eclipse.jface.util.PropertyChangeEvent event) { 85 if( event.getProperty().equals("HelloRcpEvent")) { 86 Object val = event.getNewValue(); 87 Person tempPerson = (Person)val; 88 if(tempPerson != null){ 89 text.setText(tempPerson.getName()); 90 text_1.setText(tempPerson.getSex()); 91 } 92 } 93 94 } 95 96} 97 |
|
來(lái)自: qiujingLib > 《EclipseRCP》