|
||||||||||
|
||||||||||
GroovyUsing Groovy
Language GuideGroovy FeaturesModulesExamplesUseful LinksIDE SupportSupportCommunityDevelopersTools we use
Feeds
|
IntroductionLet's share on this page the different IDEA OpenAPI tips and tricks we will encounter along the road.
How to retrieve the current project in AnAction.actionPerformed()
Project project = (Project) e.getDataContext().getData(DataConstants.PROJECT); How to write messages on the status bar
WindowManager.getInstance().getStatusBar(project).setInfo("Message") How to get the selected filesDepending on the presentation/view, we may get one or several selected files with:
VirtualFile[] selectedFiles = FileEditorManager.getInstance(project).getSelectedFiles(); How to log statements in IDEA's log fileWhen you want to add some logging statements for debugging purpose in your actions, you may leverage from the log4j API used by IDEA. So you simply need to create your logger object in your class, and then use it to log some information, as follows:
private Logger LOG = Logger.getInstance("GroovyJ.RunAction"); // … later in your class... LOG.info("Something happened that needs to be logged"); How to retrieve the classpath associated with a fileWhen you want to compile a file, you need to know the classpath of the module it belongs to. Say you already have the project and the VirtualFile (that you retrieved thanks to the above tips). Here is how to do so:
public static File[] getClasspathForVFile(final Project project, final VirtualFile vFile) { final ModuleManager mgr = ModuleManager.getInstance(project); final Module[] modules = mgr.getModules(); final Module module = modules[0]; final FileTypeManager ftmgr = FileTypeManager.getInstance(); final ModuleRootManager mrm = ModuleRootManager.getInstance(module); final VirtualFile[] files = mrm.getFiles(OrderRootType.CLASSES); final ArrayList paths = new ArrayList(files.length); for (int i = 0; i < files.length; i++) { final VirtualFile virtualFile = files[i]; final FileType fileType = ftmgr.getFileTypeByFile(virtualFile); if (!virtualFile.isValid()) continue; if (!virtualFile.isDirectory() && !fileType.equals(FileType.ARCHIVE)) continue; final File file; final VirtualFileSystem fileSystem = virtualFile.getFileSystem(); if (fileSystem instanceof LocalFileSystem) file = new File(virtualFile.getPath().replace('/', File.separatorChar)); else if (fileSystem instanceof JarFileSystem) { final JarFileSystem jarFileSystem = (JarFileSystem) fileSystem; try { final ZipFile jarFile = jarFileSystem.getJarFile(virtualFile); file = new File(jarFile.getName()); } catch (IOException e) { LOG.error("Error getting jar vFile: " + e); continue; } } else continue; paths.add(file); } return (File[]) paths.toArray(new File[paths.size()]); } After that, once you have the array of files, you may concatenate all the paths and create a classpath String like you would use on the command-line when specifying a path. |
|||||||||
|