最近在客戶端軟件開發(fā)過程中,,使用到了Eclipse
RCP的若干技術(shù),有種相見很晚的感覺,。盡管自己在日常開發(fā)中也使用了SWT中的若干技術(shù),,在運(yùn)用了Theme之后看起來也讓人眼前一亮,但是比起RCP
就相當(dāng)于閉門造車了,,例如下面兩幅圖片,,就是SWT技術(shù)和RCP技術(shù)開發(fā)出的不同界面,后者看起來是不是很炫呢,,用流行的話來講,,就是使用戶的體驗(yàn)更
rich了,呵呵,。 (一)下載最新的Eclipse RCP開發(fā)環(huán)境 (二)新建并運(yùn)行Eclipse RCP項(xiàng)目
(四)創(chuàng)建視圖 1<extension
2 point="org.eclipse.ui.views"> 3 <view 4 class="hellorcp.NavigationViewPart" 5 id="hellorcp.navigationview" 6 name="navigationview"> 7 </view> 8 </extension> 通過以上步驟,,我們得到了一個(gè)視圖類,,現(xiàn)在我們就可以回到eclipse開發(fā)環(huán)境中,點(diǎn)擊package explorer中的NavigationViewPart文件,, package hellorcp;
public class Person { private String name; private String sex; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } } PersonModel.class package hellorcp;
import java.util.ArrayList; public class PersonModel { private ArrayList<Person> list = new ArrayList<Person>(); public PersonModel() { // 硬編碼,,初始化數(shù)據(jù)示例 Person p1 = new Person(); p1.setName("毛毛"); p1.setSex("男"); list.add(p1); Person p2 = new Person(); p2.setName("皮皮"); p2.setSex("女"); list.add(p2); } public ArrayList<Person> elements() { return list; } } (2)LabelProvider package hellorcp;
import org.eclipse.jface.viewers.ILabelProvider; import org.eclipse.jface.viewers.ILabelProviderListener; import org.eclipse.jface.viewers.LabelProvider; import org.eclipse.jface.viewers.LabelProviderChangedEvent; import org.eclipse.swt.graphics.Image; public class NavigationViewLabelProvider implements ILabelProvider { public Image getImage(Object element) { // TODO Auto-generated method stub return null; } public String getText(Object element) { // TODO Auto-generated method stub return ((Person)element).getName(); } public void addListener(ILabelProviderListener listener) { // TODO Auto-generated method stub } public void dispose() { // TODO Auto-generated method stub } public boolean isLabelProperty(Object element, String property) { // TODO Auto-generated method stub return false; } public void removeListener(ILabelProviderListener listener) { // TODO Auto-generated method stub } public void labelProviderChanged(LabelProviderChangedEvent event) { // TODO Auto-generated method stub } } (3)content provider 1package hellorcp;
2 3import org.eclipse.jface.viewers.ILabelProviderListener; 4import org.eclipse.jface.viewers.IStructuredContentProvider; 5import org.eclipse.jface.viewers.ListViewer; 6import org.eclipse.jface.viewers.Viewer; 7 8public class NavigationListViewContentProvider implements IStructuredContentProvider{ 9 PersonModel input; 10 ListViewer viewer; 11 12 public Object[] getElements(Object inputElement) { 13 // TODO Auto-generated method stub 14 return input.elements().toArray(); 15 } 16 17 public String getText(Object element) { 18 return ((Person) element).getName(); 19 } 20 21 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 22 viewer = (ListViewer) viewer; 23 input = (PersonModel) newInput; 24 25 } 26 27 public void addListener(ILabelProviderListener listener) { 28 // TODO 自動(dòng)生成方法存根 29 30 } 31 32 public void add(Person p) { 33 viewer.add(p); 34 35 } 36 37 public void remove(Person p) { 38 viewer.remove(p); 39 40 } 41 42 public void dispose() { 43 // TODO Auto-generated method stub 44 45 } 46} 47 (4)以下navigation view部分的代碼 1package hellorcp;
2 3import org.eclipse.jface.action.IToolBarManager; 4import org.eclipse.jface.viewers.IStructuredContentProvider; 5import org.eclipse.jface.viewers.ListViewer; 6import org.eclipse.swt.SWT; 7import org.eclipse.swt.widgets.Composite; 8import org.eclipse.ui.part.ViewPart; 9import hellorcp.NavigationViewLabelProvider; 10 11public class NavigationViewPart extends ViewPart { 12 public NavigationViewPart() { 13 // TODO Auto-generated constructor stub 14 } 15 16 @Override 17 public void createPartControl(Composite parent) { 18 final ListViewer listViewer = new ListViewer(parent, SWT.BORDER); 19 listViewer.setLabelProvider(new NavigationViewLabelProvider()); 20 listViewer.setContentProvider(new NavigationListViewContentProvider()); listViewer.setInput(new PersonModel()); 21 initializeToolBar(); 22 // TODO Auto-generated method stub 23 24 } 25 26 @Override 27 public void setFocus() { 28 // TODO Auto-generated method stub 29 30 } 31 private void initializeToolBar() { 32 IToolBarManager toolBarManager = getViewSite().getActionBars().getToolBarManager(); 33 } 34 35} 36 (5)最后,,我們將創(chuàng)建的navigationview添加到perspective透視圖中 1package hellorcp;
2 3import org.eclipse.jface.action.IToolBarManager; 4import org.eclipse.jface.viewers.IStructuredContentProvider; 5import org.eclipse.jface.viewers.ListViewer; 6import org.eclipse.swt.SWT; 7import org.eclipse.swt.widgets.Composite; 8import org.eclipse.ui.part.ViewPart; 9import hellorcp.NavigationViewLabelProvider; 10 11public class NavigationViewPart extends ViewPart { 12 public NavigationViewPart() { 13 // TODO Auto-generated constructor stub 14 } 15 16 @Override 17 public void createPartControl(Composite parent) { 18 final ListViewer listViewer = new ListViewer(parent, SWT.BORDER); 19 listViewer.setLabelProvider(new NavigationViewLabelProvider()); 20 listViewer.setContentProvider(new NavigationListViewContentProvider()); 21 listViewer.setInput(new PersonModel()); 22 initializeToolBar(); 23 // TODO Auto-generated method stub 24 25 } 26 27 @Override 28 public void setFocus() { 29 // TODO Auto-generated method stub 30 31 } 32 private void initializeToolBar() { 33 IToolBarManager toolBarManager = getViewSite().getActionBars().getToolBarManager(); 34 } 35 36} 37 至此,我們已經(jīng)創(chuàng)建了一個(gè)可以瀏覽人員信息的navigation
view,,運(yùn)行一下我們創(chuàng)建的視圖,,可以得到如下效果,注意運(yùn)行前需要將eclipse文件夾中以runtime...開頭的文件夾刪除,,這是
eclipse上次運(yùn)行時(shí)創(chuàng)建的臨時(shí)文件,,如果不刪除,,就看不到修改之后的效果: 1package hellorcp;
2 3import java.beans.PropertyChangeEvent; 4import java.util.AbstractList; 5import java.util.ArrayList; 6import java.util.Iterator; 7 8import org.eclipse.core.runtime.Preferences.IPropertyChangeListener; 9import org.eclipse.jface.action.IToolBarManager; 10import org.eclipse.jface.viewers.ISelection; 11import org.eclipse.jface.viewers.IStructuredSelection; 12import org.eclipse.jface.viewers.ISelectionChangedListener; 13import org.eclipse.jface.viewers.SelectionChangedEvent; 14import org.eclipse.jface.viewers.TableViewer; 15import org.eclipse.swt.SWT; 16import org.eclipse.swt.events.SelectionAdapter; 17import org.eclipse.swt.widgets.Composite; 18import org.eclipse.swt.widgets.Group; 19import org.eclipse.swt.widgets.Label; 20import org.eclipse.swt.widgets.Table; 21import org.eclipse.swt.widgets.Text; 22import org.eclipse.ui.ISelectionListener; 23import org.eclipse.ui.IWorkbenchPart; 24import org.eclipse.ui.part.ViewPart; 25 26public class DetailView extends ViewPart { 27 28 private Table table; 29 private Text text_1; 30 private Text text; 31 @SuppressWarnings("unchecked") 32 ArrayList myListeners; 33 private ISelection selection; 34 public DetailView() { 35 super(); 36 // TODO Auto-generated constructor stub 37 } 38 39 @Override 40 public void createPartControl(Composite parent) { 41 final Composite composite = new Composite(parent, SWT.NONE); 42 43 final Group group = new Group(composite, SWT.NONE); 44 group.setBounds(0, 0, 494, 62); 45 46 final Label label = new Label(group, SWT.NONE); 47 label.setText("姓名"); 48 label.setBounds(10, 32, 24, 12); 49 50 text = new Text(group, SWT.BORDER); 51 text.setBounds(40, 29, 80, 15); 52 53 final Label label_1 = new Label(group, SWT.NONE); 54 label_1.setText("性別"); 55 label_1.setBounds(172, 32, 30, 12); 56 57 text_1 = new Text(group, SWT.BORDER); 58 text_1.setBounds(208, 29, 80, 15); 59 60 final TableViewer tableViewer = new TableViewer(composite, SWT.BORDER); 61 table = tableViewer.getTable(); 62 table.setLinesVisible(true); 63 table.setHeaderVisible(true); 64 table.setBounds(0, 68, 494, 270); 65 initializeToolBar(); 66 } 67 68 @Override 69 public void setFocus() { 70 // TODO Auto-generated method stub 71 72 } 73 private void initializeToolBar() { 74 IToolBarManager toolBarManager = getViewSite().getActionBars().getToolBarManager(); 75 } 76 77} 78 (二)添加過detailview視圖的perspective透視類: 1package hellorcp.intro;
以上步驟完成之后,,我們第一階段的工作就完畢了,。讓我們回顧一下,采取上述一系列步驟后,,我們創(chuàng)建了一個(gè)簡(jiǎn)單的RCP應(yīng)用,,具備兩個(gè)視圖
navigation view和detail
view,前者以列表的形式顯示人員信息,,當(dāng)用戶點(diǎn)擊列表上某一人員信息元素時(shí),,后者能夠顯示出該人員的詳細(xì)信息,就這么簡(jiǎn)單,,但是別忘了我們只完成了顯
示部分,,而最重要、也是相對(duì)較為復(fù)雜的視圖間消息傳遞部分,,我們將放在后續(xù)部分繼續(xù)講解,。2 3import org.eclipse.ui.IPageLayout; 4import org.eclipse.ui.IPerspectiveFactory; 5 6public class Perspective implements IPerspectiveFactory { 7 8 public void createInitialLayout(IPageLayout layout) { 9 String editorArea = layout.getEditorArea(); 10 layout.addView("hellorcp.navigationview", IPageLayout.LEFT, 0.15f, editorArea); 11 layout.addView("hellorcp.detailview", IPageLayout.LEFT, 1f, editorArea); 12 } 13} 以下是我們完成的視圖,檢視一下,,第一個(gè)RCP程序從自己手中誕生了,,是不是有種成功的喜悅呢? |
|