下面列出了怎么用org.eclipse.ui.navigator.PipelinedShapeModification的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Converts the shape modification to use Java elements.
*
*
* @param modification
* the shape modification to convert
* @return returns true if the conversion took place
*/
private boolean convertToJavaElements(PipelinedShapeModification modification) {
Object parent = modification.getParent();
// As of 3.3, we no longer re-parent additions to IProject.
if (parent instanceof IContainer) {
IJavaElement element = JavaCore.create((IContainer) parent);
if (element != null && element.exists()) {
// we don't convert the root
if( !(element instanceof IJavaModel) && !(element instanceof IJavaProject))
modification.setParent(element);
return convertToJavaElements(modification.getChildren());
}
}
return false;
}
/**
* Test if intercepting an add deep within the pythonpath structure will correctly return an object
* from the python model.
*/
public void testInterceptAdd() throws Exception {
PythonNature nature = createNature(TestDependent.TEST_PYSRC_NAVIGATOR_LOC + "projroot/source/python");
project = new ProjectStub(new File(TestDependent.TEST_PYSRC_NAVIGATOR_LOC + "projroot"), nature);
file = new FileStub(project, new File(TestDependent.TEST_PYSRC_NAVIGATOR_LOC
+ "projroot/source/python/pack1/pack2/mod2.py"));
provider = new PythonModelProvider();
HashSet<Object> files = new HashSet<Object>();
files.add(file);
files.add(null);
files.add("string");
provider.interceptAdd(new PipelinedShapeModification(file.getParent(), files));
assertEquals(2, files.size());
for (Object wrappedResource : files) {
assertTrue((wrappedResource instanceof IWrappedResource && ((IWrappedResource) wrappedResource)
.getActualObject() == file) || wrappedResource.equals("string"));
}
}
/**
* @param element the element that should be gotten as an element from the pydev model
* @return a pydev element or the same element passed as a parameter.
*/
private Object getPythonModelElement(Object element) {
if (element instanceof IWrappedResource) {
return element;
}
INavigatorPipelineService pipelineService = this.getNavigatorContentService().getPipelineService();
if (element instanceof IAdaptable) {
IAdaptable adaptable = (IAdaptable) element;
IFile file = adaptable.getAdapter(IFile.class);
if (file != null) {
HashSet<Object> files = new ContributorTrackingSet(
(NavigatorContentService) this.getNavigatorContentService());
files.add(file);
pipelineService.interceptAdd(new PipelinedShapeModification(file.getParent(), files));
if (files.size() > 0) {
element = files.iterator().next();
}
}
}
return element;
}
public PipelinedShapeModification interceptAdd(PipelinedShapeModification addModification) {
Object parent= addModification.getParent();
if (parent instanceof IJavaProject) {
addModification.setParent(((IJavaProject)parent).getProject());
}
if (parent instanceof IWorkspaceRoot) {
deconvertJavaProjects(addModification);
}
convertToJavaElements(addModification);
return addModification;
}
private void deconvertJavaProjects(PipelinedShapeModification modification) {
Set<IProject> convertedChildren = new LinkedHashSet<IProject>();
for (Iterator<IAdaptable> iterator = modification.getChildren().iterator(); iterator.hasNext();) {
Object added = iterator.next();
if(added instanceof IJavaProject) {
iterator.remove();
convertedChildren.add(((IJavaProject)added).getProject());
}
}
modification.getChildren().addAll(convertedChildren);
}
/**
* Test if setting the project root as a source folder will return an object from the python model.
*/
public void testProjectIsRoot2() throws Exception {
String pythonpathLoc = TestDependent.TEST_PYSRC_NAVIGATOR_LOC + "projroot";
final HashSet<String> pythonPathSet = new HashSet<String>();
pythonPathSet.add(pythonpathLoc);
PythonNature nature = createNature(pythonPathSet);
WorkspaceRootStub workspaceRootStub = new WorkspaceRootStub();
project = new ProjectStub(new File(pythonpathLoc), nature);
provider = new PythonModelProvider();
FolderStub folder = new FolderStub(project,
new File(TestDependent.TEST_PYSRC_NAVIGATOR_LOC + "projroot/source"));
workspaceRootStub.addChild(project);
project.setParent(workspaceRootStub);
HashSet<Object> folders = new HashSet<Object>();
folders.add(folder);
PipelinedShapeModification addModification = new PipelinedShapeModification(project, folders);
addModification.setParent(project);
provider.interceptAdd(addModification);
assertEquals(1, addModification.getChildren().size());
//it should've been wrapped
assertTrue(addModification.getChildren().iterator().next() instanceof IWrappedResource);
}
public void testAddSourceFolderToSourceFolder() throws Exception {
final HashSet<String> pythonPathSet = new HashSet<String>();
pythonPathSet.add(TestDependent.TEST_PYSRC_NAVIGATOR_LOC + "projroot/source");
String source2Folder = TestDependent.TEST_PYSRC_NAVIGATOR_LOC + "projroot/source2";
File f = new File(source2Folder);
if (f.exists()) {
f.delete();
}
pythonPathSet.add(source2Folder); //still not created!
PythonNature nature = createNature(pythonPathSet);
project = new ProjectStub(new File(TestDependent.TEST_PYSRC_NAVIGATOR_LOC + "projroot"), nature, false);
provider = new PythonModelProvider();
Object[] children1 = provider.getChildren(project);
assertEquals(1, children1.length);
assertTrue(children1[0] instanceof PythonSourceFolder);
Set set = new HashSet();
f.mkdir();
try {
FolderStub source2FolderFile = new FolderStub(project, f);
set.add(source2FolderFile);
provider.interceptAdd(new PipelinedShapeModification(project, set));
assertEquals(1, set.size());
assertTrue(set.iterator().next() instanceof PythonSourceFolder);
} finally {
f.delete();
}
}
/**
* This method intercepts some addition to the tree and converts its elements to python
* elements.
*
* @see org.eclipse.ui.navigator.IPipelinedTreeContentProvider#interceptAdd(org.eclipse.ui.navigator.PipelinedShapeModification)
*/
@Override
public PipelinedShapeModification interceptAdd(PipelinedShapeModification addModification) {
if (DEBUG) {
System.out.println("interceptAdd");
}
final Map<PythonNature, Set<String>> natureToSourcePathSet = new HashMap<>();
convertToPythonElementsAddOrRemove(addModification, true, natureToSourcePathSet);
return addModification;
}
@Override
public PipelinedShapeModification interceptRemove(PipelinedShapeModification removeModification) {
if (DEBUG) {
System.out.println("interceptRemove");
}
final Map<PythonNature, Set<String>> natureToSourcePathSet = new HashMap<>();
convertToPythonElementsAddOrRemove(removeModification, false, natureToSourcePathSet);
return removeModification;
}
/**
* Helper for debugging the things we have in a modification
*/
private void debug(String desc, PipelinedShapeModification modification) {
System.out.println("\nDesc:" + desc);
Object parent = modification.getParent();
System.out.println("Parent:" + parent);
System.out.println("Children:");
for (Object o : modification.getChildren()) {
System.out.println(o);
}
}
@Override
public PipelinedShapeModification interceptAdd(final PipelinedShapeModification anAddModification) {
return null;
}
@Override
public PipelinedShapeModification interceptRemove(final PipelinedShapeModification aRemoveModification) {
return null;
}
@Override
public PipelinedShapeModification interceptAdd(
PipelinedShapeModification anAddModification) {
anAddModification.getChildren().clear();
return anAddModification;
}
@Override
public PipelinedShapeModification interceptRemove(
PipelinedShapeModification aRemoveModification) {
return aRemoveModification;
}
@Override
public PipelinedShapeModification interceptAdd(PipelinedShapeModification anAddModification) {
return anAddModification;
}
@Override
public PipelinedShapeModification interceptRemove(PipelinedShapeModification aRemoveModification) {
return null;
}
/**
* {@inheritDoc}
*/
public PipelinedShapeModification interceptAdd(final PipelinedShapeModification modification) {
convertToJavaElements(modification);
return modification;
}
/**
* {@inheritDoc}
*/
public PipelinedShapeModification interceptRemove(final PipelinedShapeModification modification) {
convertToJavaElements(modification);
return modification;
}
public PipelinedShapeModification interceptRemove(
PipelinedShapeModification removeModification) {
deconvertJavaProjects(removeModification);
convertToJavaElements(removeModification.getChildren());
return removeModification;
}
public void testFolderToSourceFolder() throws Exception {
final HashSet<String> pythonPathSet = new HashSet<String>();
pythonPathSet.add(TestDependent.TEST_PYSRC_NAVIGATOR_LOC + "projroot/source");
String source2Folder = TestDependent.TEST_PYSRC_NAVIGATOR_LOC + "projroot/source2";
File f = new File(source2Folder);
File f1 = new File(f, "childFolder");
if (f1.exists()) {
f1.delete();
}
if (f.exists()) {
f.delete();
}
pythonPathSet.add(source2Folder); //still not created!
PythonNature nature = createNature(pythonPathSet);
project = new ProjectStub(new File(TestDependent.TEST_PYSRC_NAVIGATOR_LOC + "projroot"), nature, false);
provider = new PythonModelProvider();
Object[] children1 = provider.getChildren(project);
assertEquals(1, children1.length);
assertTrue("Found: " + children1[0], children1[0] instanceof PythonSourceFolder);
f.mkdir();
f1.mkdir();
try {
FolderStub source2FolderFile = new FolderStub(project, f);
FolderStub source2FolderChild = new FolderStub(project, source2FolderFile, f1);
Set set = new HashSet();
set.add(source2FolderChild);
provider.interceptAdd(new PipelinedShapeModification(source2FolderFile, set));
assertEquals(1, set.size());
PythonFolder c = (PythonFolder) set.iterator().next();
PythonSourceFolder sourceFolder = c.getSourceFolder();
assertTrue(sourceFolder instanceof PythonSourceFolder);
set.clear();
set.add(source2FolderChild);
provider.interceptAdd(new PipelinedShapeModification(source2FolderFile, set));
} finally {
f1.delete();
f.delete();
}
}
public void testFolderToSourceFolder2() throws Exception {
final HashSet<String> pythonPathSet = new HashSet<String>();
pythonPathSet.add(TestDependent.TEST_PYSRC_NAVIGATOR_LOC + "projroot/source");
String source2Folder = TestDependent.TEST_PYSRC_NAVIGATOR_LOC + "projroot/source2";
File f = new File(source2Folder);
File f1 = new File(f, "childFolder");
File f2 = new File(f1, "rechildFolder");
if (f2.exists()) {
f2.delete();
}
if (f1.exists()) {
f1.delete();
}
if (f.exists()) {
f.delete();
}
pythonPathSet.add(source2Folder); //still not created!
PythonNature nature = createNature(pythonPathSet);
project = new ProjectStub(new File(TestDependent.TEST_PYSRC_NAVIGATOR_LOC + "projroot"), nature, false);
provider = new PythonModelProvider();
Object[] children1 = provider.getChildren(project);
assertEquals(1, children1.length);
assertTrue("Expected source folder. Received: " + children1[0], children1[0] instanceof PythonSourceFolder);
f.mkdir();
f1.mkdir();
f2.mkdir();
try {
FolderStub source2FolderFile = new FolderStub(project, f);
FolderStub source2FolderChild = new FolderStub(project, source2FolderFile, f1);
FolderStub source2FolderReChild = new FolderStub(project, source2FolderChild, f2);
Set set = new HashSet();
set.add(source2FolderReChild);
provider.interceptAdd(new PipelinedShapeModification(source2FolderChild, set));
assertEquals(1, set.size());
PythonFolder c = (PythonFolder) set.iterator().next();
PythonSourceFolder sourceFolder = c.getSourceFolder();
assertTrue(sourceFolder instanceof PythonSourceFolder);
set.clear();
set.add(source2FolderChild);
provider.interceptRemove(new PipelinedShapeModification(source2FolderFile, set));
assertTrue(set.iterator().next() instanceof PythonFolder);
// System.out.println(set);
set.clear();
set.add(source2FolderReChild);
provider.interceptAdd(new PipelinedShapeModification(source2FolderChild, set));
assertTrue(set.iterator().next() instanceof PythonFolder);
// System.out.println(set);
set.clear();
set.add(source2FolderChild);
provider.interceptRemove(new PipelinedShapeModification(source2FolderFile, set));
assertTrue(set.iterator().next() instanceof PythonFolder);
// System.out.println(set);
set.clear();
set.add(source2FolderReChild);
provider.interceptAdd(new PipelinedShapeModification(source2FolderChild, set));
assertTrue(set.iterator().next() instanceof PythonFolder);
// System.out.println(set);
} finally {
f2.delete();
f1.delete();
f.delete();
}
}
/**
* Converts the shape modification to use Python elements.
* @param natureToSourcePathSet
*
* @param modification: the shape modification to convert
* @param isAdd: boolean indicating whether this convertion is happening in an add operation
*/
@SuppressWarnings("unchecked")
private void convertToPythonElementsAddOrRemove(PipelinedShapeModification modification, boolean isAdd,
Map<PythonNature, Set<String>> natureToSourcePathSet) {
if (DEBUG) {
debug("Before", modification);
}
Object parent = modification.getParent();
if (parent instanceof IContainer) {
IContainer parentContainer = (IContainer) parent;
Object pythonParent = getResourceInPythonModel(parentContainer, true);
if (pythonParent instanceof IWrappedResource) {
IWrappedResource parentResource = (IWrappedResource) pythonParent;
modification.setParent(parentResource);
wrapChildren(parentResource, parentResource.getSourceFolder(), modification.getChildren(), isAdd,
natureToSourcePathSet);
} else if (pythonParent == null) {
Object parentInWrap = parentContainer;
PythonSourceFolder sourceFolderInWrap = null;
//this may happen when a source folder is added or some element that still doesn't have it's parent in the model...
//so, we have to get the parent's parent until we actually 'know' that it is not in the model (or until we run
//out of parents to try)
//the case in which we reproduce this is Test 1 (described in the class)
FastStack<Object> found = new FastStack<Object>(20);
while (true) {
//add the current to the found
if (parentContainer == null) {
break;
}
found.push(parentContainer);
if (parentContainer instanceof IProject) {
//we got to the project without finding any part of a python model already there, so, let's see
//if any of the parts was actually a source folder (that was still not added)
tryCreateModelFromProject((IProject) parentContainer, found, natureToSourcePathSet);
//and now, if it was created, try to convert it to the python model (without any further add)
convertToPythonElementsUpdateOrRefresh(modification.getChildren());
return;
}
Object p = getResourceInPythonModel(parentContainer, true);
if (p instanceof IWrappedResource) {
IWrappedResource wrappedResource = (IWrappedResource) p;
sourceFolderInWrap = wrappedResource.getSourceFolder();
while (found.size() > 0) {
Object f = found.pop();
if (f instanceof IResource) {
//no need to create it if it's already in the model!
Object child = sourceFolderInWrap.getChild((IResource) f);
if (child != null && child instanceof IWrappedResource) {
wrappedResource = (IWrappedResource) child;
continue;
}
}
//creating is enough to add it to the model
if (f instanceof IFile) {
wrappedResource = new PythonFile(wrappedResource, (IFile) f, sourceFolderInWrap);
} else if (f instanceof IFolder) {
wrappedResource = new PythonFolder(wrappedResource, (IFolder) f, sourceFolderInWrap);
}
}
parentInWrap = wrappedResource;
break;
}
parentContainer = parentContainer.getParent();
}
wrapChildren(parentInWrap, sourceFolderInWrap, modification.getChildren(), isAdd,
natureToSourcePathSet);
}
} else if (parent == null) {
wrapChildren(null, null, modification.getChildren(), isAdd, natureToSourcePathSet);
}
if (DEBUG) {
debug("After", modification);
}
}