首先申明下,本文为笔者学习《Eclipse插件开发学习笔记》的笔记,并加入笔者自己的理解和归纳总结。
扩展点【org.eclipse.ui.views】。
在【Extensions】中添加【org.eclipse.ui.views】。
在【org.eclipse.ui.views】中添加【category】和【view】。
【category】代表类别,【view】中的【class】,必须选择ViewPart
的子类。
显示视图
在【Window】菜单中,选择【Show View】->【Other…】,选择【DemoViewPart】就可打开视图。
在OpenViewAction
中打开视图
public class OpenViewAction implements IWorkbenchWindowActionDelegate {
private IWorkbenchWindow mWindow;
@Override
public void run(IAction action) {
IWorkbenchPage page = mWindow.getActivePage();
if (page != null) {
try {
page.showView("com.plugin.blog.demo.view.DemoViewPart");
} catch (PartInitException e) {
}
}
}
@Override
public void selectionChanged(IAction action, ISelection selection) {
}
@Override
public void dispose() {
}
@Override
public void init(IWorkbenchWindow window) {
this.mWindow = window;
}
}
新建的视图必须继承org.eclipse.ui.part.ViewPart
,并完成方法createPartControl()
和setFocus()
。
DemoViewPart
类包含一个列表,显示着所有用户的姓名和类别。
public class DemoViewPart extends ViewPart {
private TableViewer mTableViewer;
private Table mTable;
private UserManager mManager = UserManager.getInstance();
@Override
public void createPartControl(Composite parent) {
mTableViewer = new TableViewer(parent,
SWT.BORDER|SWT.FULL_SELECTION|SWT.MULTI);
mTableViewer.setContentProvider(new ListContentProvider());
mTableViewer.setLabelProvider(new TableLabelProvider());
mTable = mTableViewer.getTable();
TableColumn nameColumn = new TableColumn(mTable, SWT.NONE);
nameColumn.setText("name");
nameColumn.setWidth(120);
TableColumn categoryColumn = new TableColumn(mTable, SWT.NONE);
categoryColumn.setWidth(80);
categoryColumn.setText("category");
mTable.setHeaderVisible(true);
mTable.setLinesVisible(true);
mTableViewer.setInput(mManager);
}
@Override
public void setFocus() {
mTable.setFocus();
}
}
TableLabelProvider
类
public class TableLabelProvider implements ITableLabelProvider {
@Override
public void addListener(ILabelProviderListener listener) {
}
@Override
public void dispose() {
}
@Override
public boolean isLabelProperty(Object element, String property) {
return false;
}
@Override
public void removeListener(ILabelProviderListener listener) {
}
@Override
public Image getColumnImage(Object element, int columnIndex) {
return null;
}
@Override
public String getColumnText(Object element, int columnIndex) {
if (element instanceof User) {
User user = (User) element;
if (columnIndex == 0) {
return user.getName();
} else if (columnIndex == 1) {
return user.getCategory();
}
}
return null;
}
}
ListContentProvider
类
public class ListContentProvider implements IStructuredContentProvider,
PropertyChangeListener {
private Viewer viewer;
@Override
public Object[] getElements(Object inputElement) {
if (inputElement instanceof UserManager) {
return ((UserManager) inputElement).getElements();
}
return new Object[0];
}
@Override
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
this.viewer = viewer;
if (oldInput instanceof UserManager) {
((UserManager) oldInput).removePropertyChangeListener(this);
}
if (newInput instanceof UserManager) {
((UserManager) newInput).addPropertyChangeListener(this);
}
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (UserManager.PROPERTY_ADD.equals(evt.getPropertyName())) {
viewer.refresh();
} else if (UserManager.PROPERTY_REMOVE.equals(evt.getPropertyName())) {
viewer.refresh();
} else if (UserManager.PROPERTY_MODIFY.equals(evt.getPropertyName())) {
viewer.refresh();
}
}
}
User
类,存储用户的姓名和类别。
public class User {
public final static String[] CATEGORY_ARRAY =
new String[]{"朋友", "同事", "同学", "亲属"};
private String mName;
private String mCategory;
public User(String name, String category) {
this.mName = name;
this.mCategory = category;
}
public String getName() {
return mName;
}
public void setName(String name) {
this.mName = name;
UserManager.getInstance().modifyObject(this);
}
public String getCategory() {
return mCategory;
}
public void setCategory(String category) {
this.mCategory = category;
UserManager.getInstance().modifyObject(this);
}
}
UserManager
类,存放User
数据类,是个单例类。
public class UserManager {
public final static String PROPERTY_ADD = "add";
public final static String PROPERTY_REMOVE = "remove";
public final static String PROPERTY_MODIFY = "modify";
private List mData = new ArrayList();
private PropertyChangeSupport mSupport = new PropertyChangeSupport(this);
private UserManager() {
// 显示数据,后面删除
mData.add(new User("Mike", User.CATEGORY_ARRAY[1]));
mData.add(new User("Tom", User.CATEGORY_ARRAY[2]));
}
public static UserManager getInstance() {
return UserManagerHolder.INSTANCE;
}
public void addObject(User data) {
if (mData.add(data)) {
mSupport.firePropertyChange(PROPERTY_ADD, null, data);
}
}
public void removeObject(User data) {
if (mData.remove(data)) {
mSupport.firePropertyChange(PROPERTY_REMOVE, data, null);
}
}
public void modifyObject(Object data) {
mSupport.firePropertyChange(PROPERTY_MODIFY, null, data);
}
public Object[] getElements() {
return mData.toArray();
}
public int getLength() {
return mData.size();
}
public void addPropertyChangeListener(PropertyChangeListener listener) {
mSupport.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
mSupport.removePropertyChangeListener(listener);
}
private static class UserManagerHolder {
private static UserManager INSTANCE = new UserManager();
}
}
在属性视图显示被选中的内容,并且修改。
在DemoViewPart
的createPartControl()
方法中添加
getViewSite().setSelectionProvider(mTableViewer);
由于属性视图只能识别IPropertySource
接口数据,需要User
数据类实现接口IAdaptable
,返回IPropertySource
类型数据。IPropertySource
需要添加plugins\org.eclipse.ui.views
依赖。
在User
类中实现getAdapter()
方法
public class User implements IAdaptable {
@Override
public IPropertySource getAdapter(Class adapter) {
if (adapter == IPropertySource.class) {
return new UserPropertySource(this);
}
return null;
}
}
UserPropertySource
类
public class UserPropertySource implements IPropertySource {
public final static String PROPERTY_NAME
= "com.plugin.blog.demo.PROPERTY_NAME";
public final static String PROPERTY_CATEGORY
= "com.plugin.blog.demo.PROPERTY_CATEGORY";
private User mUser;
public UserPropertySource(User user) {
mUser = user;
}
@Override
public Object getEditableValue() {
return mUser;
}
@Override
public IPropertyDescriptor[] getPropertyDescriptors() {
TextPropertyDescriptor namePropertyDescriptor =
new TextPropertyDescriptor(PROPERTY_NAME, "姓名");
ComboBoxPropertyDescriptor agePropertyDescriptor =
new ComboBoxPropertyDescriptor(PROPERTY_CATEGORY,
"类别", User.CATEGORY_ARRAY);
agePropertyDescriptor.setLabelProvider(new LabelProvider() {
@Override
public String getText(Object element) {
return User.CATEGORY_ARRAY[(Integer) element];
}
});
return new IPropertyDescriptor[] {
namePropertyDescriptor, agePropertyDescriptor };
}
@Override
public Object getPropertyValue(Object id) {
if (PROPERTY_NAME.equals(id)) {
return mUser.getName();
} else if (PROPERTY_CATEGORY.equals(id)) {
for (int index = 0; index < User.CATEGORY_ARRAY.length; index++) {
String category = User.CATEGORY_ARRAY[index];
if (category.equals(mUser.getCategory())) {
return index;
}
}
}
return null;
}
@Override
public boolean isPropertySet(Object id) {
if (PROPERTY_NAME.equals(id) || PROPERTY_CATEGORY.equals(id)) {
return true;
}
return false;
}
@Override
public void resetPropertyValue(Object id) {
}
@Override
public void setPropertyValue(Object id, Object value) {
if (PROPERTY_NAME.equals(id)) {
mUser.setName(value.toString());
} else if (PROPERTY_CATEGORY.equals(id)) {
int index = (Integer) value;
if (index >= 0 && index < User.CATEGORY_ARRAY.length) {
mUser.setCategory(User.CATEGORY_ARRAY[index]);
}
}
}
}
在DemoViewPart
中,覆盖init()
和saveState()
方法,用于存储和读取数据。
@Override
public void init(IViewSite site, IMemento memento) throws PartInitException {
super.init(site, memento);
mManager.init(memento);
}
@Override
public void saveState(IMemento memento) {
super.saveState(memento);
mManager.saveState(memento);
}
UserManager
类,添加init()
和saveState()
方法。mInitialized
变量避免重复读取。
public class UserManager {
private boolean mInitialized;
public void init(IMemento memento) {
if (mInitialized) {
return;
}
IMemento[] children = memento.getChildren("UserManager");
if (children != null) {
for (IMemento child : children) {
addObject(new User(child.getString("name"), child.getString("category")));
}
}
mInitialized = true;
}
public void saveState(IMemento memento) {
for (User user : mData) {
IMemento child = memento.createChild("UserManager");
child.putString("name", user.getName());
child.putString("category", user.getCategory());
}
}
}
把DemoViewPart
列表中选择行数据显示在状态栏上。
mTableViewer.addSelectionChangedListener(new ISelectionChangedListener() {
@Override
public void selectionChanged(SelectionChangedEvent event) {
IStructuredSelection selection = (IStructuredSelection) event.getSelection();
if (!selection.isEmpty()) {
User user = (User) selection.getFirstElement();
getViewSite().getActionBars().getStatusLineManager().setMessage(
"name = " + user.getName() + " category = " + user.getCategory());
}
}
});
相关文章
Java Plug-in(一) 创建插件项目
Java Plug-in(二) 菜单
Java Plug-in(三) 视图
Java Plug-in(四) 视图菜单
Java Plug-in(五) 编辑器
Java Plug-in(六) 编辑器菜单
Java Plug-in(七) 透视图
Java Plug-in(八) 向导页
Java Plug-in(九) 首选项
Java Plug-in(十) 自定义扩展点
Java Plug-in(十一) 创建RCP项目