Search in sources :

Example 1 with ModuleListener

use of com.intellij.openapi.project.ModuleListener in project intellij-community by JetBrains.

the class MavenProjectsManagerWatcher method start.

public synchronized void start() {
    final MessageBusConnection myBusConnection = myProject.getMessageBus().connect(myChangedDocumentsQueue);
    myBusConnection.subscribe(VirtualFileManager.VFS_CHANGES, new MyFileChangeListener());
    myBusConnection.subscribe(ProjectTopics.PROJECT_ROOTS, new MyRootChangesListener());
    myChangedDocumentsQueue.makeUserAware(myProject);
    myChangedDocumentsQueue.activate();
    myBusConnection.subscribe(ProjectTopics.MODULES, new ModuleListener() {

        @Override
        public void moduleRemoved(@NotNull Project project, @NotNull Module module) {
            MavenProject mavenProject = myManager.findProject(module);
            if (mavenProject != null && !myManager.isIgnored(mavenProject)) {
                VirtualFile file = mavenProject.getFile();
                if (myManager.isManagedFile(file) && myManager.getModules(mavenProject).isEmpty()) {
                    myManager.removeManagedFiles(Collections.singletonList(file));
                } else {
                    myManager.setIgnoredState(Collections.singletonList(mavenProject), true);
                }
            }
        }

        @Override
        public void moduleAdded(@NotNull final Project project, @NotNull final Module module) {
            // this method is needed to return non-ignored status for modules that were deleted (and thus ignored) and then created again with a different module type
            if (myManager.isMavenizedModule(module)) {
                MavenProject mavenProject = myManager.findProject(module);
                if (mavenProject != null)
                    myManager.setIgnoredState(Collections.singletonList(mavenProject), false);
            }
        }
    });
    DocumentAdapter myDocumentListener = new DocumentAdapter() {

        @Override
        public void documentChanged(DocumentEvent event) {
            Document doc = event.getDocument();
            VirtualFile file = FileDocumentManager.getInstance().getFile(doc);
            if (file == null)
                return;
            String fileName = file.getName();
            boolean isMavenFile = fileName.equals(MavenConstants.POM_XML) || fileName.equals(MavenConstants.PROFILES_XML) || isSettingsFile(file) || fileName.startsWith("pom.");
            if (!isMavenFile)
                return;
            synchronized (myChangedDocuments) {
                myChangedDocuments.add(doc);
            }
            myChangedDocumentsQueue.queue(new Update(MavenProjectsManagerWatcher.this) {

                @Override
                public void run() {
                    final Document[] copy;
                    synchronized (myChangedDocuments) {
                        copy = myChangedDocuments.toArray(new Document[myChangedDocuments.size()]);
                        myChangedDocuments.clear();
                    }
                    MavenUtil.invokeLater(myProject, () -> new WriteAction() {

                        @Override
                        protected void run(@NotNull Result result) throws Throwable {
                            for (Document each : copy) {
                                PsiDocumentManager.getInstance(myProject).commitDocument(each);
                                ((FileDocumentManagerImpl) FileDocumentManager.getInstance()).saveDocument(each, false);
                            }
                        }
                    }.execute());
                }
            });
        }
    };
    EditorFactory.getInstance().getEventMulticaster().addDocumentListener(myDocumentListener, myBusConnection);
    final MavenGeneralSettings.Listener mySettingsPathsChangesListener = new MavenGeneralSettings.Listener() {

        @Override
        public void changed() {
            updateSettingsFilePointers();
            onSettingsChange();
        }
    };
    myGeneralSettings.addListener(mySettingsPathsChangesListener);
    Disposer.register(myChangedDocumentsQueue, new Disposable() {

        @Override
        public void dispose() {
            myGeneralSettings.removeListener(mySettingsPathsChangesListener);
            mySettingsFilesPointers.clear();
        }
    });
    updateSettingsFilePointers();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Disposable(com.intellij.openapi.Disposable) MessageBusConnection(com.intellij.util.messages.MessageBusConnection) ModuleListener(com.intellij.openapi.project.ModuleListener) ModuleRootListener(com.intellij.openapi.roots.ModuleRootListener) VirtualFilePointerListener(com.intellij.openapi.vfs.pointers.VirtualFilePointerListener) ModuleListener(com.intellij.openapi.project.ModuleListener) WriteAction(com.intellij.openapi.application.WriteAction) DocumentAdapter(com.intellij.openapi.editor.event.DocumentAdapter) DocumentEvent(com.intellij.openapi.editor.event.DocumentEvent) Document(com.intellij.openapi.editor.Document) Update(com.intellij.util.ui.update.Update) Result(com.intellij.openapi.application.Result) Project(com.intellij.openapi.project.Project) Module(com.intellij.openapi.module.Module)

Example 2 with ModuleListener

use of com.intellij.openapi.project.ModuleListener in project intellij-community by JetBrains.

the class MvcModuleStructureSynchronizer method initComponent.

@Override
public void initComponent() {
    final MessageBusConnection connection = myProject.getMessageBus().connect();
    connection.subscribe(ProjectTopics.PROJECT_ROOTS, new ModuleRootListener() {

        @Override
        public void rootsChanged(ModuleRootEvent event) {
            myModificationTracker.incModificationCount();
            queue(SyncAction.SyncLibrariesInPluginsModule, myProject);
            queue(SyncAction.UpgradeFramework, myProject);
            queue(SyncAction.CreateAppStructureIfNeeded, myProject);
            queue(SyncAction.UpdateProjectStructure, myProject);
            queue(SyncAction.EnsureRunConfigurationExists, myProject);
            updateProjectViewVisibility();
        }
    });
    connection.subscribe(ProjectTopics.MODULES, new ModuleListener() {

        @Override
        public void moduleAdded(@NotNull Project project, @NotNull Module module) {
            queue(SyncAction.UpdateProjectStructure, module);
            queue(SyncAction.CreateAppStructureIfNeeded, module);
        }
    });
    connection.subscribe(VirtualFileManager.VFS_CHANGES, new BulkVirtualFileListenerAdapter(new VirtualFileAdapter() {

        @Override
        public void fileCreated(@NotNull final VirtualFileEvent event) {
            myModificationTracker.incModificationCount();
            final VirtualFile file = event.getFile();
            final String fileName = event.getFileName();
            if (MvcModuleStructureUtil.APPLICATION_PROPERTIES.equals(fileName) || isApplicationDirectoryName(fileName)) {
                queue(SyncAction.UpdateProjectStructure, file);
                queue(SyncAction.EnsureRunConfigurationExists, file);
            } else if (isLibDirectory(file) || isLibDirectory(event.getParent())) {
                queue(SyncAction.UpdateProjectStructure, file);
            } else {
                if (!myProject.isInitialized())
                    return;
                final Module module = ProjectRootManager.getInstance(myProject).getFileIndex().getModuleForFile(file);
                if (module == null) {
                    // Maybe it is creation of a plugin in plugin directory.
                    if (file.isDirectory()) {
                        if (myPluginRoots.contains(file.getParent())) {
                            queue(SyncAction.UpdateProjectStructure, myProject);
                            return;
                        }
                        if (!myOutOfModuleDirectoryCreatedActionAdded) {
                            queue(SyncAction.OutOfModuleDirectoryCreated, myProject);
                            myOutOfModuleDirectoryCreatedActionAdded = true;
                        }
                    }
                    return;
                }
                if (!MvcConsole.isUpdatingVfsByConsoleProcess(module))
                    return;
                final MvcFramework framework = MvcFramework.getInstance(module);
                if (framework == null)
                    return;
                if (framework.isToReformatOnCreation(file) || file.isDirectory()) {
                    ApplicationManager.getApplication().invokeLater(() -> {
                        if (!file.isValid())
                            return;
                        if (!framework.hasSupport(module))
                            return;
                        final List<VirtualFile> files = new ArrayList<>();
                        if (file.isDirectory()) {
                            ModuleRootManager.getInstance(module).getFileIndex().iterateContentUnderDirectory(file, new ContentIterator() {

                                @Override
                                public boolean processFile(VirtualFile fileOrDir) {
                                    if (!fileOrDir.isDirectory() && framework.isToReformatOnCreation(fileOrDir)) {
                                        files.add(file);
                                    }
                                    return true;
                                }
                            });
                        } else {
                            files.add(file);
                        }
                        PsiManager manager = PsiManager.getInstance(myProject);
                        for (VirtualFile virtualFile : files) {
                            PsiFile psiFile = manager.findFile(virtualFile);
                            if (psiFile != null) {
                                new ReformatCodeProcessor(myProject, psiFile, null, false).run();
                            }
                        }
                    }, module.getDisposed());
                }
            }
        }

        @Override
        public void fileDeleted(@NotNull VirtualFileEvent event) {
            myModificationTracker.incModificationCount();
            final VirtualFile file = event.getFile();
            if (isLibDirectory(file) || isLibDirectory(event.getParent())) {
                queue(SyncAction.UpdateProjectStructure, file);
            }
        }

        @Override
        public void contentsChanged(@NotNull VirtualFileEvent event) {
            final String fileName = event.getFileName();
            if (MvcModuleStructureUtil.APPLICATION_PROPERTIES.equals(fileName)) {
                queue(SyncAction.UpdateProjectStructure, event.getFile());
            }
        }

        @Override
        public void fileMoved(@NotNull VirtualFileMoveEvent event) {
            myModificationTracker.incModificationCount();
        }

        @Override
        public void propertyChanged(@NotNull VirtualFilePropertyEvent event) {
            if (VirtualFile.PROP_NAME.equals(event.getPropertyName())) {
                myModificationTracker.incModificationCount();
            }
        }
    }));
}
Also used : MessageBusConnection(com.intellij.util.messages.MessageBusConnection) ModuleListener(com.intellij.openapi.project.ModuleListener) PsiManager(com.intellij.psi.PsiManager) ReformatCodeProcessor(com.intellij.codeInsight.actions.ReformatCodeProcessor) NotNull(org.jetbrains.annotations.NotNull) Project(com.intellij.openapi.project.Project) BulkVirtualFileListenerAdapter(com.intellij.openapi.vfs.impl.BulkVirtualFileListenerAdapter) PsiFile(com.intellij.psi.PsiFile) Module(com.intellij.openapi.module.Module)

Example 3 with ModuleListener

use of com.intellij.openapi.project.ModuleListener in project intellij-community by JetBrains.

the class LightPlatformTestCase method doSetup.

public static void doSetup(@NotNull LightProjectDescriptor descriptor, @NotNull LocalInspectionTool[] localInspectionTools, @NotNull Disposable parentDisposable) throws Exception {
    assertNull("Previous test " + ourTestCase + " hasn't called tearDown(). Probably overridden without super call.", ourTestCase);
    IdeaLogger.ourErrorsOccurred = null;
    ApplicationManager.getApplication().assertIsDispatchThread();
    boolean reusedProject = true;
    if (ourProject == null || ourProjectDescriptor == null || !ourProjectDescriptor.equals(descriptor)) {
        initProject(descriptor);
        reusedProject = false;
    }
    ProjectManagerEx projectManagerEx = ProjectManagerEx.getInstanceEx();
    projectManagerEx.openTestProject(ourProject);
    if (reusedProject) {
        DumbService.getInstance(ourProject).queueTask(new UnindexedFilesUpdater(ourProject));
    }
    MessageBusConnection connection = ourProject.getMessageBus().connect(parentDisposable);
    connection.subscribe(ProjectTopics.MODULES, new ModuleListener() {

        @Override
        public void moduleAdded(@NotNull Project project, @NotNull Module module) {
            fail("Adding modules is not permitted in LightIdeaTestCase.");
        }
    });
    clearUncommittedDocuments(getProject());
    InspectionsKt.configureInspections(localInspectionTools, getProject(), parentDisposable);
    assertFalse(getPsiManager().isDisposed());
    Boolean passed = null;
    try {
        passed = StartupManagerEx.getInstanceEx(getProject()).startupActivityPassed();
    } catch (Exception ignored) {
    }
    assertTrue("open: " + getProject().isOpen() + "; disposed:" + getProject().isDisposed() + "; startup passed:" + passed + "; all open projects: " + Arrays.asList(ProjectManager.getInstance().getOpenProjects()), getProject().isInitialized());
    CodeStyleSettingsManager.getInstance(getProject()).setTemporarySettings(new CodeStyleSettings());
    final FileDocumentManager manager = FileDocumentManager.getInstance();
    if (manager instanceof FileDocumentManagerImpl) {
        Document[] unsavedDocuments = manager.getUnsavedDocuments();
        manager.saveAllDocuments();
        ApplicationManager.getApplication().runWriteAction(((FileDocumentManagerImpl) manager)::dropAllUnsavedDocuments);
        assertEmpty("There are unsaved documents", Arrays.asList(unsavedDocuments));
    }
    // startup activities
    UIUtil.dispatchAllInvocationEvents();
    ((FileTypeManagerImpl) FileTypeManager.getInstance()).drainReDetectQueue();
}
Also used : MessageBusConnection(com.intellij.util.messages.MessageBusConnection) ModuleListener(com.intellij.openapi.project.ModuleListener) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) FileDocumentManagerImpl(com.intellij.openapi.fileEditor.impl.FileDocumentManagerImpl) Document(com.intellij.openapi.editor.Document) FileTypeManagerImpl(com.intellij.openapi.fileTypes.impl.FileTypeManagerImpl) IncorrectOperationException(com.intellij.util.IncorrectOperationException) IOException(java.io.IOException) Project(com.intellij.openapi.project.Project) CodeStyleSettings(com.intellij.psi.codeStyle.CodeStyleSettings) Module(com.intellij.openapi.module.Module) ProjectManagerEx(com.intellij.openapi.project.ex.ProjectManagerEx) UnindexedFilesUpdater(com.intellij.util.indexing.UnindexedFilesUpdater)

Example 4 with ModuleListener

use of com.intellij.openapi.project.ModuleListener in project android by JetBrains.

the class DeveloperServices method initializeFor.

private static void initializeFor(@NotNull final Module module) {
    if (ourServices.containsKey(module)) {
        return;
    }
    for (DeveloperServiceCreators creators : DeveloperServiceCreators.EP_NAME.getExtensions()) {
        try {
            for (DeveloperServiceCreator creator : creators.getCreators()) {
                DeveloperService service = creator.createService(module);
                if (service != null) {
                    ourServices.put(module, service);
                }
            }
        } catch (Exception e) {
            LOG.warn("Caught exception while initializing services", e);
        }
    }
    getBuildSystemOperations(module.getProject()).initializeServices(module, new Runnable() {

        @Override
        public void run() {
            for (DeveloperService service : ourServices.get(module)) {
                service.updateInstalledState();
            }
        }
    });
    MessageBusConnection connection = module.getMessageBus().connect(module);
    connection.subscribe(ProjectTopics.MODULES, new ModuleListener() {

        @Override
        public void moduleRemoved(@NotNull Project project, @NotNull Module moduleRemoved) {
            if (module == moduleRemoved) {
                ourServices.removeAll(module);
            }
        }
    });
}
Also used : Project(com.intellij.openapi.project.Project) MessageBusConnection(com.intellij.util.messages.MessageBusConnection) ModuleListener(com.intellij.openapi.project.ModuleListener) Module(com.intellij.openapi.module.Module)

Example 5 with ModuleListener

use of com.intellij.openapi.project.ModuleListener in project intellij-community by JetBrains.

the class CloudGitChooseAccountStepImpl method updateDataModel.

@Override
public void updateDataModel() {
    super.updateDataModel();
    final MultiMap<CloudGitProjectRoot, DetectedSourceRoot> project2sourceRoots = new MultiMap<>();
    new RootIterator() {

        CloudGitProjectRoot lastProjectRoot = null;

        @Override
        protected void processProjectRoot(CloudGitProjectRoot root) {
            lastProjectRoot = root;
            project2sourceRoots.put(lastProjectRoot, new ArrayList<>());
        }

        @Override
        protected void processJavaSourceRoot(DetectedSourceRoot root) {
            project2sourceRoots.putValue(lastProjectRoot, root);
        }
    }.iterate();
    List<ModuleDescriptor> modules = new ArrayList<>(myProjectDescriptor.getModules());
    for (Map.Entry<CloudGitProjectRoot, Collection<DetectedSourceRoot>> project2sourceRootsEntry : project2sourceRoots.entrySet()) {
        final CloudGitProjectRoot projectRoot = project2sourceRootsEntry.getKey();
        final File directory = projectRoot.getDirectory();
        ModuleDescriptor moduleDescriptor = new ModuleDescriptor(directory, StdModuleTypes.JAVA, project2sourceRootsEntry.getValue());
        final String applicationName = projectRoot.getApplicationName();
        moduleDescriptor.addConfigurationUpdater(new ModuleBuilder.ModuleConfigurationUpdater() {

            @Override
            public void update(@NotNull final Module module, @NotNull ModifiableRootModel rootModel) {
                final MessageBusConnection connection = module.getProject().getMessageBus().connect();
                connection.subscribe(ProjectTopics.MODULES, new ModuleListener() {

                    @Override
                    public void moduleAdded(@NotNull Project project, @NotNull Module addedModule) {
                        if (addedModule == module) {
                            StartupManager.getInstance(project).runWhenProjectIsInitialized(() -> onModuleAdded(module));
                            connection.disconnect();
                        }
                    }
                });
            }

            private void onModuleAdded(Module module) {
                createRunConfiguration(module, applicationName);
                GitInit.refreshAndConfigureVcsMappings(module.getProject(), projectRoot.getRepositoryRoot(), directory.getAbsolutePath());
            }
        });
        modules.add(moduleDescriptor);
    }
    myProjectDescriptor.setModules(modules);
}
Also used : MessageBusConnection(com.intellij.util.messages.MessageBusConnection) ModuleListener(com.intellij.openapi.project.ModuleListener) ModuleBuilder(com.intellij.ide.util.projectWizard.ModuleBuilder) ArrayList(java.util.ArrayList) NotNull(org.jetbrains.annotations.NotNull) DetectedSourceRoot(com.intellij.ide.util.projectWizard.importSources.DetectedSourceRoot) ModifiableRootModel(com.intellij.openapi.roots.ModifiableRootModel) MultiMap(com.intellij.util.containers.MultiMap) ModuleDescriptor(com.intellij.ide.util.importProject.ModuleDescriptor) Project(com.intellij.openapi.project.Project) Collection(java.util.Collection) Module(com.intellij.openapi.module.Module) Map(java.util.Map) MultiMap(com.intellij.util.containers.MultiMap) File(java.io.File)

Aggregations

Module (com.intellij.openapi.module.Module)8 ModuleListener (com.intellij.openapi.project.ModuleListener)8 Project (com.intellij.openapi.project.Project)8 MessageBusConnection (com.intellij.util.messages.MessageBusConnection)8 NotNull (org.jetbrains.annotations.NotNull)3 Document (com.intellij.openapi.editor.Document)2 ReformatCodeProcessor (com.intellij.codeInsight.actions.ReformatCodeProcessor)1 Facet (com.intellij.facet.Facet)1 FacetManagerAdapter (com.intellij.facet.FacetManagerAdapter)1 ModuleDescriptor (com.intellij.ide.util.importProject.ModuleDescriptor)1 ModuleBuilder (com.intellij.ide.util.projectWizard.ModuleBuilder)1 DetectedSourceRoot (com.intellij.ide.util.projectWizard.importSources.DetectedSourceRoot)1 Disposable (com.intellij.openapi.Disposable)1 Result (com.intellij.openapi.application.Result)1 WriteAction (com.intellij.openapi.application.WriteAction)1 DocumentAdapter (com.intellij.openapi.editor.event.DocumentAdapter)1 DocumentEvent (com.intellij.openapi.editor.event.DocumentEvent)1 FileDocumentManager (com.intellij.openapi.fileEditor.FileDocumentManager)1 FileDocumentManagerImpl (com.intellij.openapi.fileEditor.impl.FileDocumentManagerImpl)1 FileTypeManagerImpl (com.intellij.openapi.fileTypes.impl.FileTypeManagerImpl)1