private String checkValidSourceFolder(String text) throws CoreException { validatedSourceFolder = null; if (text == null || text.trim().length() == 0) { return "The source folder must be filled."; } IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); IResource resource = root.findMember(new Path(text)); if (resource == null) { return "The source folder was not found in the workspace."; } if (!(resource instanceof IContainer)) { return "The source folder was found in the workspace but is not a container."; } IProject project = resource.getProject(); if (project == null) { return "Unable to find the project related to the source folder."; } IPythonPathNature nature = PythonNature.getPythonPathNature(project); if (nature == null) { return "The pydev nature is not configured on the project: " + project.getName(); } String full = resource.getFullPath().toString(); String[] srcPaths = PythonNature.getStrAsStrItems(nature.getProjectSourcePath(true)); for (String str : srcPaths) { if (str.equals(full)) { validatedSourceFolder = (IContainer) resource; return null; } } return "The selected source folder is not recognized as a valid source folder."; }
/** * @param f * @return * @throws CoreException */ public String getSrcFolderFromFolder(IFolder f) throws CoreException { IPythonPathNature nature = PythonNature.getPythonPathNature(f.getProject()); if (nature != null) { String[] srcPaths = PythonNature.getStrAsStrItems(nature.getProjectSourcePath(true)); String relFolder = f.getFullPath().toString() + "/"; for (String src : srcPaths) { if (relFolder.startsWith(src + "/")) { return src; } } } return null; }
/** * @param topLevel * @return */ private boolean createSourceFolderSelect(Composite topLevel) { Label label; label = new Label(topLevel, SWT.NONE); label.setText("Source Folder"); textSourceFolder = new Text(topLevel, SWT.BORDER); textSourceFolder.addKeyListener(this); btBrowseSourceFolder = new Button(topLevel, SWT.NONE); setLayout(label, textSourceFolder, btBrowseSourceFolder); btBrowseSourceFolder.addSelectionListener( new SelectionListener() { public void widgetSelected(SelectionEvent e) { try { PythonPackageSelectionDialog dialog = new PythonPackageSelectionDialog(getShell(), true); dialog.open(); Object firstResult = dialog.getFirstResult(); if (firstResult instanceof SourceFolder) { SourceFolder f = (SourceFolder) firstResult; textSourceFolder.setText(f.folder.getFullPath().toString()); } } catch (Exception e1) { Log.log(e1); } } public void widgetDefaultSelected(SelectionEvent e) {} }); Object element = selection.getFirstElement(); try { if (element instanceof IAdaptable) { IAdaptable adaptable = (IAdaptable) element; element = adaptable.getAdapter(IFile.class); if (element == null) { element = adaptable.getAdapter(IProject.class); } if (element == null) { element = adaptable.getAdapter(IFolder.class); } } if (element instanceof IFile) { IFile f = (IFile) element; element = f.getParent(); } if (element instanceof IProject) { IPythonPathNature nature = PythonNature.getPythonPathNature((IProject) element); if (nature != null) { String[] srcPaths = PythonNature.getStrAsStrItems(nature.getProjectSourcePath(true)); if (srcPaths.length > 0) { textSourceFolder.setText(srcPaths[0]); return true; } } } if (element instanceof IFolder) { IFolder f = (IFolder) element; String srcPath = getSrcFolderFromFolder(f); if (srcPath == null) { return true; } textSourceFolder.setText(srcPath); return true; } } catch (Exception e) { Log.log(e); } return false; }