⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pythonabstractpathpage.java

📁 Python Development Environment (Python IDE plugin for Eclipse). Features editor, code completion, re
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        if(shouldCreatePackageSelect()){
            return textPackage.getText().length() > 0;
        }else{
            return false;
        }
    }

    /**
     * @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) {
                    PydevPlugin.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());
                    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) {
            PydevPlugin.log(e);
        }
        return false;
    }

    /**
     * @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());
            String relFolder = f.getFullPath().toString()+"/";
            for (String src : srcPaths) {
                if(relFolder.startsWith(src+"/")){
                    return src;
                }
            }
        }
        return null;
    }

    private void setLayout(Label label, Text text, Button bt) {
        GridData data;
        
        data = new GridData();
        data.grabExcessHorizontalSpace = false;
        label.setLayoutData(data);
        
        data = new GridData(GridData.FILL_HORIZONTAL);
        data.grabExcessHorizontalSpace = true;
        text.setLayoutData(data);
        
        if(bt != null){
            data = new GridData();
            bt.setLayoutData(data);
            bt.setText("Browse...");
        }
    }

    
    
    
    
    //listener interface
    public void keyPressed(KeyEvent e) {
    }
    
    public void keyReleased(KeyEvent e) {
        validatePage();
    }

    private void validatePage() {
        try {
            if (textProject != null) {
                if(checkError(checkValidProject(textProject.getText()))){
                    return;
                }
            }
            if (textSourceFolder != null) {
                if(checkError(checkValidSourceFolder(textSourceFolder.getText()))){
                    return;
                }
            }
            if (textPackage != null) {
                if(checkError(checkValidPackage(textPackage.getText()))){
                    return;
                }

            }
            if (textName != null) {
                if(checkError(checkValidName(textName.getText()))){
                    return;
                }
            }
            setErrorMessage(null);
            setMessage(getDescription());
            setPageComplete(true);
        } catch (Exception e) {
            PydevPlugin.log(e);
            setErrorMessage("Error while validating page:"+e.getMessage());
            setPageComplete(false);
        }
    }
    
    private String checkValidProject(String text) {
        validatedProject = null;
        if(text == null || text.trim().length() == 0 ){
            return "The project name must be filled.";
        }
        IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
        IProject project = root.getProject(text);
        if(!project.exists()){
            return "The project selected does not exist in the workspace.";
        }
        validatedProject = project;
        return null;
    }
    
    private boolean checkError(String error) {
        if (error != null) {
            setErrorMessage(error);
            setPageComplete(false);
            return true;
        }
        return false;
    }


    private String checkValidName(String text) {
        validatedName = null;
        if(text == null || text.trim().length() == 0 ){
            return "The name must be filled.";
        }
        if(shouldCreateSourceFolderSelect()){
            if(validatedSourceFolder == null){
                return "The source folder was not correctly validated.";
            }
        }else if(validatedProject == null){
            return "The project was not correctly validated.";
        }
        if(shouldCreatePackageSelect()){
            if(validatedPackage == null){
                return "The package was not correctly validated.";
            }
        }
        if(text.indexOf(' ') != -1){
            return "The name may not contain spaces";
        }
        if(shouldCreatePackageSelect()){
            //it is a new file not a new package
            if(text.indexOf('.') != -1){
                return "The name may not contain dots";
            }
        }
        //there are certainly other invalid chars, but let's leave it like that...
        char[] invalid = new char[]{'/', '\\', ',', '*', '(', ')', '{', '}','[',']'
                };
        for (char c : invalid){
            if(text.indexOf(c) != -1){
                return "The name must not contain '"+c+"'.";
            }
        }

        if(text.endsWith(".")){
            return "The name may not end with a dot";
        }
        validatedName = text;
        return null;
    }

    private String checkValidPackage(String text) {
        validatedPackage = null;
        //there is a chance that the package is the default project, so, the validation below may not be valid.
        //if(text == null || text.trim().length() == 0 ){
        //}
        
        if(text.indexOf('/') != -1){
            return "The package name must not contain '/'.";
        }
        if(text.indexOf('\\') != -1){
            return "The package name must not contain '\\'.";
        }
        if(text.endsWith(".")){
            return "The package may not end with a dot";
        }
        text = text.replace('.', '/');
        if(validatedSourceFolder == null){
            return "The source folder was not correctly validated.";
        }
        IPath path = validatedSourceFolder.getFullPath().append(text);
        IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
        IResource resource = root.findMember(path);
        if(resource == null){
            return "The package was not found in the workspace.";
        }
        if(!(resource instanceof IContainer)){
            return "The resource found for the package is not a valid container.";
        }
        if(!resource.exists()){
            return "The package selected does not exist in the filesystem.";
        }
        validatedPackage = (IContainer) resource;
        return null;
    }

    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());
        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.";
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -