*CommonNavigator没有默认加入WorkingSet功能,但是提供了WorkingSetFilterActionGroup,它包含了所有需要的功能. 如下是代码示例,在导航器中加入工作集...
*CommonNavigator没有默认加入WorkingSet功能,但是提供了WorkingSetFilterActionGroup,它包含了所有需要的功能.
如下是代码示例,在导航器中加入工作集的基本过程
*添加WorkingSetFilterActionGroup到导航器,参考org.eclipse.ui.views.navigator.MainActionGroup
添加WorkingSetFilterActionGroup /* * Copyright(C) 2011 Agree Tech, All rights reserved. * * Created on 2011-2-18 by dzh */ package cn.com.agree.ide.common.navigator.view; import org.eclipse.jface.action.IMenuManager; import org.eclipse.jface.util.IPropertyChangeListener; import org.eclipse.jface.util.PropertyChangeEvent; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.IWorkingSet; import org.eclipse.ui.actions.ActionContext; import org.eclipse.ui.actions.WorkingSetFilterActionGroup; import org.eclipse.ui.internal.navigator.CommonNavigatorActionGroup; import org.eclipse.ui.navigator.CommonNavigator; import org.eclipse.ui.navigator.CommonViewer; import org.eclipse.ui.navigator.LinkHelperService; public class AgreeIDECommonNavigatorActionGroup extends CommonNavigatorActionGroup { private WorkingSetFilterActionGroup workingSetGroup; public AgreeIDECommonNavigatorActionGroup(CommonNavigator aNavigator, CommonViewer aViewer, LinkHelperService linkHelperService) { super(aNavigator, aViewer, linkHelperService); makeSubGroups(aNavigator); } private void makeSubGroups(final CommonNavigator navigator){ IPropertyChangeListener workingSetUpdater = new IPropertyChangeListener() { public void propertyChange(PropertyChangeEvent event) { String property = event.getProperty(); if (WorkingSetFilterActionGroup.CHANGE_WORKING_SET .equals(property)) { Object newValue = event.getNewValue(); if (newValue instanceof IWorkingSet) { String workingSetName =((IWorkingSet) newValue).getName(); navigator.setWorkingSetLabel(workingSetName); //refresh } else{ navigator.setWorkingSetLabel(null); } } } }; Shell shell =navigator.getCommonViewer().getControl().getShell(); workingSetGroup =new WorkingSetFilterActionGroup(shell, workingSetUpdater); //WorkingSetFilterActionGroup } public void setContext(ActionContext context) { super.setContext(context); workingSetGroup.setContext(context); } protected void fillViewMenu(IMenuManager menu) { super.fillViewMenu(menu); workingSetGroup.fillContextMenu(menu); } public void dispose() { super.dispose(); workingSetGroup.dispose(); //dispose } }
*刷新Tree
CommonNavigator.setWorkingSetLabel(String label)@Override public void setWorkingSetLabel(String label) { super.setWorkingSetLabel(label); this.workingSetLabel =label; getCommonViewer().refresh(); //refresh } @Override public String getWorkingSetLabel() { if(workingSetLabel!=null) return workingSetLabel; return super.getWorkingSetLabel(); }
*ContentProvider中配置Resource是否隐藏,改动getElements().
updateWorkingSet(element) public Object[] getElements(Object element) { if (CategoryControl.getInstance().getCurrentValue()) { categoryProjectsMap.clear(); Collection<CategoryModel> categoryModels = categories.values(); processProjects(categoryModels); return categoryModels.toArray(new CategoryModel[0]); } else { updateWorkingSet(element); //working set for (ITreeConstructorProvider provider : constructors) { if (provider.isAcceptable(element)) { Object[] elements = provider.getContentProvider() .getElements(element); return elements == null ? new Object[0] : elements; } } } return super.getElements(element); } /** * * <DL> * <DT>标题.</DT> * <p> * <DD>详细介绍</DD> * setHidden * </DL> * <p><B> * 例子: * </B><p><blockquote><pre> * 例子内容 * </pre></blockquote><p> * @param element */ private void updateWorkingSet(Object element){ if(element!=null && element instanceof IWorkspaceRoot){ try { IResource[] children =((IWorkspaceRoot) element).members(IContainer.INCLUDE_HIDDEN); List<IAdaptable> workingSetConent =getWorkingSetContent(); for(IResource child:children){ if(child.isHidden()) child.setHidden(false); } if(workingSetConent!=null){ for(IResource child:children){ if(!workingSetConent.contains(child)) child.setHidden(true); } } } catch (CoreException e) { e.printStackTrace(); } } } /** * * <DL> * <DT>标题.</DT> * <p> * <DD>详细介绍</DD> * 获取当前workingSet的内容 * </DL> * <p><B> * 例子: * </B><p><blockquote><pre> * 例子内容 * </pre></blockquote><p> * @return null表示没有选择工作集,所有内容可见 */ private List<IAdaptable> getWorkingSetContent(){ IWorkbenchPart part =null; try{ part =PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart(); }catch(NullPointerException exception){ return null; } if(part!=null && part instanceof CommonNavigator){ IWorkingSet workingSet =PlatformUI.getWorkbench() .getWorkingSetManager().getWorkingSet(((CommonNavigator)part).getWorkingSetLabel()); if(workingSet!=null){ return Arrays.asList(workingSet.getElements()); } } return null; }