package pyUML.backend; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.Vector; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IFolder; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.Path; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.IEditorReference; import org.eclipse.ui.IWorkbenchPage; import org.eclipse.ui.PlatformUI; import org.python.pydev.plugin.nature.PythonNature; /** * Some Helper methods for Eclipse-specific tasks */ public class EclipseHelperMethods { /** * Closes an open Editor, if it is already opened * @param project the current project * @param editorName the Name of the editor to close (or a REGEX) * @return true, if an editor was closed, false, if nothing was done */ public static boolean closeEditorByName(IProject project, String editorName){ IEditorReference editor = firstOpenEditorByName(editorName); if (editor == null) return false; IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); return page.closeEditor(editor.getEditor(true), true); } /** * Looks for an open editor with a name that matches the given regex. * Returns a list of matching editors or an empty list, if no such editor is open. * @param editorNameRegex * @return */ public static List lookForOpenEditorByName(String editorNameRegex) { IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); List editorList = new Vector(); for (IEditorReference editor : page.getEditorReferences()) { if (editor.getTitle().matches(editorNameRegex)) { editorList.add(editor); } } return editorList; } /** * Looks for an open editor with a name that matches the given regex. * Returns the first matching editors or null, if no such editor is open. * @param editorNameRegex * @return */ public static IEditorReference firstOpenEditorByName(String editorNameRegex) { List editorList = lookForOpenEditorByName(editorNameRegex); if (editorList.size() == 0) return null; else return editorList.get(0); } /** * Reads the content of an IFile and returns teh String of the content * @param file the IFile to read * @return The file content String, null on error */ public static String iFileToString(IFile file){ String fileContents = ""; try{ BufferedReader reader = new BufferedReader(new InputStreamReader(file.getContents())); String line; while ((line = reader.readLine()) != null) { fileContents+= line+"\n"; } return fileContents; } catch (IOException e) { MessageDialog.openError(null, "Error Reading File", "Cannot Read file "+file+"\nReason:\n"+e.getMessage()); } catch (CoreException e) { MessageDialog.openError(null, "Error Reading File", "Cannot Read file "+file+"\nReason:\n"+e.getMessage()); } return null; } /** * Writes a string to an IFile. The old file content is replaced completely. * * @param file the IFile to update * @param s the string to write to the file * @return true on success, false otherwise */ public static boolean StringToIFile(IFile file, String s) { try{ file.setContents(JavaHelperMethods.stringToStream(s), IResource.FORCE, null); EclipseHelperMethods.updateFile(file); return true; } catch (CoreException err) { MessageDialog.openError(null, "Error writing file", err.getMessage()); } return false; } /** * marks the ressource as updated in eclipse * @param file the changed IFile */ public static void updateFile(IFile file) { try { file.refreshLocal(1, null); } catch (CoreException e) { System.out.println("Error updating ressource "+file.getName()); } } /** * marks the resource as updated in eclipse * @param path the full path of the changed file */ public static void updateFile(String path, IProject project) { try { IFile file = createFile(path, project); file.refreshLocal(1, null); } catch (CoreException e) { System.out.println("Error updating ressource "+path); e.printStackTrace(); } } /** * marks the folder as updated in eclipse * @param path the full path of the changed file */ public static void updateFolder(String path, IProject project) { try { IFolder file = createFolder(path, project); file.refreshLocal(IResource.DEPTH_INFINITE, null); } catch (CoreException e) { System.out.println("Error updating folder "+path); e.printStackTrace(); } } /** * creates an IFile resource out of an absolute Path and a project * (the file is not created on the file system, only an IFile Object * with appropriate path is created) */ public static IFile createFile(String path, IProject project) { // cut everything in front of project name String localPath = path.replace(project.getLocation().toOSString(), ""); IPath relativeFilePath = project.getFullPath().append(localPath); return project.getWorkspace().getRoot().getFile(relativeFilePath); } /** * creates an IFolder resource out of an absolute Path and a project * (the folder is not created on the file system, only an IFolder Object * with appropriate path is created) */ public static IFolder createFolder(String path, IProject project) { // cut everything in front of project name String localPath = path.replace(project.getLocation().toOSString(), ""); IPath relativeFilePath = project.getFullPath().append(localPath); return project.getWorkspace().getRoot().getFolder(relativeFilePath); } /** * Saves all Editors opened in the Workbench. */ public static void saveAllOpenEditors() { // save all open editors // (this may fail if the process is forked) // (-> catch NullpointerException and do not save, then) try { IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); page.saveAllEditors(false); } catch (NullPointerException e) {} } public static boolean isPythonProject(IProject project) { PythonNature store = new PythonNature(); store.setProject(project); try { store.getPythonPathNature().getProjectSourcePathSet(false); } catch (CoreException e) { return false; } catch (NullPointerException e) { return false; } catch (Exception e) { e.printStackTrace(); } return true; } /** * Finds the source folder(s) of this python project. * If an error occurs (e.g. the project is no python * project), the project location is returned. * Source folders contained in other source folders are * removed from list. * * @param project * @return */ public static Set getPythonSrcDirs(IProject project) { PythonNature store = new PythonNature(); store.setProject(project); Set srcPaths = new HashSet(); Set srcStrings = null; try { srcStrings = store.getPythonPathNature().getProjectSourcePathSet(true); } catch (CoreException e) { srcPaths.add(project.getLocation()); return srcPaths; } catch (Exception e) { e.printStackTrace(); } srcDirLoop: for (String dirString : srcStrings) { // continue, if this dir is already contained in any other dir for (String dirString2 : srcStrings) { if (dirString != dirString2 && dirString2.startsWith(dirString)) continue srcDirLoop; } IPath srcPath = new Path(dirString); IPath srcDir = srcPath.removeFirstSegments(1); //remove project dir IPath completeSrcPath = project.getLocation().append(srcDir); srcPaths.add(completeSrcPath); } return srcPaths; } /** * This method chooses from the list of available Projects * in the Workspace the project to work with, which is * -> a python project * -> if there are several, the user us asked which one to use * -> if there is no project selected, the user asked to select one * * @return the pyDev project that was selected * null, if no project could be chosen */ public static IProject selectProject() { IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects(); List pyDevProjects = new Vector(); // find all pyDev-Projects for (IProject project : projects) { if (project.isOpen() && isPythonProject(project)) pyDevProjects.add(project); } if (pyDevProjects.size() == 0) return null; if (pyDevProjects.size() == 1) return (pyDevProjects.get(0)); // let user choose the project to use return ChooseProjectDialog.runChooseProject((Shell) null, pyDevProjects); } }