From 8118afff2eb637b26871bf38093feaaf1f7ee8c5 Mon Sep 17 00:00:00 2001 From: Alexey Date: Thu, 2 Sep 2010 21:13:09 +0400 Subject: [PATCH] Updated version of the VFS sample plugin. --- samples/vfs/build/build.xml | 27 ----- samples/vfs/src/META-INF/plugin.xml | 9 +- .../openapi/samples/VfsSamplePlugin.java | 106 ----------------- samples/vfs/src/myVFS/VfsSamplePlugin.java | 111 ++++++++++++++++++ samples/vfs/vfs.iml | 7 +- samples/vfs/vfs.ipr | 85 +++++++++++--- 6 files changed, 185 insertions(+), 160 deletions(-) delete mode 100644 samples/vfs/build/build.xml delete mode 100644 samples/vfs/src/com/intellij/openapi/samples/VfsSamplePlugin.java create mode 100644 samples/vfs/src/myVFS/VfsSamplePlugin.java diff --git a/samples/vfs/build/build.xml b/samples/vfs/build/build.xml deleted file mode 100644 index fe6278abe8f7..000000000000 --- a/samples/vfs/build/build.xml +++ /dev/null @@ -1,27 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/samples/vfs/src/META-INF/plugin.xml b/samples/vfs/src/META-INF/plugin.xml index eb43ce4e4b4a..ff45e7859720 100644 --- a/samples/vfs/src/META-INF/plugin.xml +++ b/samples/vfs/src/META-INF/plugin.xml @@ -4,7 +4,7 @@ VfsSample - An example of using IDEA's virtual file system, dynamically keeps the count of Java files in sourcepaths + This plugin monitors the number of Java files in sourcepaths. 1.0 @@ -13,15 +13,16 @@ IntelliJ + - com.intellij.openapi.samples.VfsSamplePlugin + myVFS.VfsSamplePlugin - com.intellij.openapi.samples.VfsSamplePlugin + myVFS.VfsSamplePlugin diff --git a/samples/vfs/src/com/intellij/openapi/samples/VfsSamplePlugin.java b/samples/vfs/src/com/intellij/openapi/samples/VfsSamplePlugin.java deleted file mode 100644 index e83923176e1c..000000000000 --- a/samples/vfs/src/com/intellij/openapi/samples/VfsSamplePlugin.java +++ /dev/null @@ -1,106 +0,0 @@ -/* - * Copyright 2000-2005 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.intellij.openapi.samples; - -import com.intellij.openapi.components.ProjectComponent; -import com.intellij.openapi.fileTypes.FileTypeManager; -import com.intellij.openapi.fileTypes.StdFileTypes; -import com.intellij.openapi.project.Project; -import com.intellij.openapi.roots.ProjectRootManager; -import com.intellij.openapi.ui.Messages; -import com.intellij.openapi.vfs.VirtualFile; -import com.intellij.openapi.vfs.VirtualFileAdapter; -import com.intellij.openapi.vfs.VirtualFileEvent; -import com.intellij.openapi.vfs.VirtualFileManager; -import org.jetbrains.annotations.NotNull; - -public class VfsSamplePlugin implements ProjectComponent { - private final Project myProject; - - private static int ourJavaFilesCount; - - public VfsSamplePlugin(Project project) { - myProject = project; - } - - public void projectOpened() { - ProjectRootManager projectRootManager = ProjectRootManager.getInstance(myProject); - VirtualFile[] sourceRoots = projectRootManager.getContentSourceRoots(); - - ourJavaFilesCount = 0; - - for (VirtualFile sourceRoot : sourceRoots) { - countJavaFiles(sourceRoot); - } - - MyVfsListener vfsListener = new MyVfsListener(); - VirtualFileManager.getInstance().addVirtualFileListener(vfsListener,myProject); - } - - public void projectClosed() { - } - - public void initComponent() { - // empty - } - - public void disposeComponent() { - // empty - } - - @NotNull - public String getComponentName() { - return "VfsSample.VfsSamplePlugin"; - } - - private static void updateCount(VirtualFile file, int increase) { - FileTypeManager fileTypeManager = FileTypeManager.getInstance(); - if (!fileTypeManager.isFileIgnored(file.getName()) - && fileTypeManager.getFileTypeByFile(file) == StdFileTypes.JAVA) { - ourJavaFilesCount += increase; - //System.out.println("ourJavaFilesCount = " + ourJavaFilesCount); - } - } - - private static void countJavaFiles(VirtualFile virtualFile) { - VirtualFile[] children = virtualFile.getChildren(); - if (children == null) return; - - for (VirtualFile child : children) { - updateCount(child, +1); - countJavaFiles(child); - } - - } - - // ------------------------------------------------------------------------- - // MyVfsListener - // ------------------------------------------------------------------------- - - private static class MyVfsListener extends VirtualFileAdapter { - public void fileCreated(VirtualFileEvent event) { - updateCount(event.getFile(), +1); - Messages.showMessageDialog("A new Java file added. Now " + String.valueOf(ourJavaFilesCount) + - " Java files in this project.","Notification", Messages.getInformationIcon() ); - } - - public void fileDeleted(VirtualFileEvent event) { - updateCount(event.getFile(), -1); - Messages.showMessageDialog("A Java file deleted. Now " + String.valueOf(ourJavaFilesCount) + - " Java files in this project.","Notification", Messages.getInformationIcon() ); - } - } -} diff --git a/samples/vfs/src/myVFS/VfsSamplePlugin.java b/samples/vfs/src/myVFS/VfsSamplePlugin.java new file mode 100644 index 000000000000..6e3b8fa550fd --- /dev/null +++ b/samples/vfs/src/myVFS/VfsSamplePlugin.java @@ -0,0 +1,111 @@ +/* + * Copyright 2000-2005 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package myVFS; + +import com.intellij.openapi.components.ProjectComponent; +import com.intellij.openapi.fileTypes.FileTypeManager; +import com.intellij.openapi.fileTypes.StdFileTypes; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.roots.ProjectRootManager; +import com.intellij.openapi.ui.Messages; +import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.openapi.vfs.VirtualFileAdapter; +import com.intellij.openapi.vfs.VirtualFileEvent; +import com.intellij.openapi.vfs.VirtualFileManager; +import org.jetbrains.annotations.NotNull; + +public class VfsSamplePlugin implements ProjectComponent { + private final Project myProject; + + private static int ourJavaFilesCount; + + public VfsSamplePlugin(Project project) { + myProject = project; + } + + public void projectOpened() { + ProjectRootManager projectRootManager = ProjectRootManager.getInstance(myProject); + VirtualFile[] sourceRoots = projectRootManager.getContentSourceRoots(); + + ourJavaFilesCount = 0; + // Count the current number of Java files in the opened project. + for (VirtualFile sourceRoot : sourceRoots) { + countJavaFiles(sourceRoot); + } + // Add the Virtual File listener + MyVfsListener vfsListener = new MyVfsListener(); + VirtualFileManager.getInstance().addVirtualFileListener(vfsListener, myProject); + } + + public void projectClosed() { + } + + public void initComponent() { + // empty + } + + public void disposeComponent() { + // empty + } + + @NotNull + public String getComponentName() { + return "VfsSample.VfsSamplePlugin"; + } + // Increase or decrease the current number of Java files. Return true when a Java file has been added or removed from the opened project. + private static boolean updateCount(VirtualFile file, int increase) { + + FileTypeManager fileTypeManager = FileTypeManager.getInstance(); + if (!fileTypeManager.isFileIgnored(file.getName()) + && fileTypeManager.getFileTypeByFile(file) == StdFileTypes.JAVA) { + ourJavaFilesCount += increase; + return true; + //System.out.println("ourJavaFilesCount = " + ourJavaFilesCount); + } + return false; + } + // Count current number of Java file in the project. + private static void countJavaFiles(VirtualFile virtualFile) { + VirtualFile[] children = virtualFile.getChildren(); + if (children == null) return; + + for (VirtualFile child : children) { + updateCount(child, +1); + countJavaFiles(child); + } + + } + + // ------------------------------------------------------------------------- + // MyVfsListener + // ------------------------------------------------------------------------- + + private static class MyVfsListener extends VirtualFileAdapter { + public void fileCreated(VirtualFileEvent event) { + if (updateCount(event.getFile(), +1)) { + Messages.showMessageDialog("A new Java file added. Now " + String.valueOf(ourJavaFilesCount) + + " Java files in this project.", "Notification", Messages.getInformationIcon()); + } + } + + public void fileDeleted(VirtualFileEvent event) { + if (updateCount(event.getFile(), -1)) { + Messages.showMessageDialog("A Java file deleted. Now " + String.valueOf(ourJavaFilesCount) + + " Java files in this project.", "Notification", Messages.getInformationIcon()); + } + } + } +} diff --git a/samples/vfs/vfs.iml b/samples/vfs/vfs.iml index 74945e395e44..09647c68eae8 100644 --- a/samples/vfs/vfs.iml +++ b/samples/vfs/vfs.iml @@ -1,13 +1,12 @@ - + - - + - + diff --git a/samples/vfs/vfs.ipr b/samples/vfs/vfs.ipr index 94cc51f0f8b5..5b460280c13b 100644 --- a/samples/vfs/vfs.ipr +++ b/samples/vfs/vfs.ipr @@ -12,33 +12,28 @@ - - - + + + + - - - - - - -