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