Java Code Examples for com.intellij.openapi.externalSystem.util.ExternalSystemApiUtil#executeProjectChangeAction()

The following examples show how to use com.intellij.openapi.externalSystem.util.ExternalSystemApiUtil#executeProjectChangeAction() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ExternalSystemAutoImporter.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public void onSuccess(@javax.annotation.Nullable final DataNode<ProjectData> externalProject) {
  if (externalProject != null) {
    ExternalSystemApiUtil.executeProjectChangeAction(new DisposeAwareProjectChange(myProject) {
      @RequiredUIAccess
      @Override
      public void execute() {
        ProjectRootManagerEx.getInstanceEx(myProject).mergeRootsChangesDuring(new Runnable() {
          @Override
          public void run() {
            myProjectDataManager.importData(externalProject.getKey(), Collections.singleton(externalProject), myProject, true);
          }
        });
      }
    });
  }
}
 
Example 2
Source File: ModuleDataService.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public void removeData(@Nonnull final Collection<? extends Module> modules, @Nonnull final Project project, boolean synchronous) {
  if (modules.isEmpty()) {
    return;
  }
  ExternalSystemApiUtil.executeProjectChangeAction(synchronous, new DisposeAwareProjectChange(project) {
    @RequiredUIAccess
    @Override
    @RequiredWriteAction
    public void execute() {
      ModuleManager moduleManager = ModuleManager.getInstance(project);

      for (Module module : modules) {
        if (module.isDisposed()) continue;
        moduleManager.disposeModule(module);
      }
    }
  });
}
 
Example 3
Source File: LibraryDataService.java    From consulo with Apache License 2.0 6 votes vote down vote up
public void importLibrary(@Nonnull final String libraryName, @Nonnull final Map<OrderRootType, Collection<File>> libraryFiles, @Nonnull final Project project, boolean synchronous) {
  ExternalSystemApiUtil.executeProjectChangeAction(synchronous, new DisposeAwareProjectChange(project) {
    @RequiredUIAccess
    @Override
    public void execute() {
      // Is assumed to be called from the EDT.
      final LibraryTable libraryTable = ProjectLibraryTable.getInstance(project);
      final LibraryTable.ModifiableModel projectLibraryModel = libraryTable.getModifiableModel();
      final Library intellijLibrary;
      try {
        intellijLibrary = projectLibraryModel.createLibrary(libraryName);
      }
      finally {
        projectLibraryModel.commit();
      }
      final Library.ModifiableModel libraryModel = intellijLibrary.getModifiableModel();
      try {
        registerPaths(libraryFiles, libraryModel, libraryName);
      }
      finally {
        libraryModel.commit();
      }
    }
  });
}
 
Example 4
Source File: AbstractExternalModuleImportProvider.java    From consulo with Apache License 2.0 5 votes vote down vote up
/**
 * The whole import sequence looks like below:
 * <p>
 * <pre>
 * <ol>
 *   <li>Get project view from the gradle tooling api without resolving dependencies (downloading libraries);</li>
 *   <li>Allow to adjust project settings before importing;</li>
 *   <li>Create IJ project and modules;</li>
 *   <li>Ask gradle tooling api to resolve library dependencies (download the if necessary);</li>
 *   <li>Configure libraries used by the gradle project at intellij;</li>
 *   <li>Configure library dependencies;</li>
 * </ol>
 * </pre>
 * <p>
 *
 * @param projectWithResolvedLibraries gradle project with resolved libraries (libraries have already been downloaded and
 *                                     are available at file system under gradle service directory)
 * @param project                      current intellij project which should be configured by libraries and module library
 *                                     dependencies information available at the given gradle project
 */
private void setupLibraries(@Nonnull final DataNode<ProjectData> projectWithResolvedLibraries, final Project project) {
  ExternalSystemApiUtil.executeProjectChangeAction(new DisposeAwareProjectChange(project) {
    @RequiredUIAccess
    @Override
    public void execute() {
      ProjectRootManagerEx.getInstanceEx(project).mergeRootsChangesDuring(new Runnable() {
        @Override
        public void run() {
          if (ExternalSystemApiUtil.isNewProjectConstruction()) {
            // Clean existing libraries (if any).
            LibraryTable projectLibraryTable = ProjectLibraryTable.getInstance(project);
            if (projectLibraryTable == null) {
              LOG.warn("Can't resolve external dependencies of the target gradle project (" + project + "). Reason: project " + "library table is undefined");
              return;
            }
            LibraryTable.ModifiableModel model = projectLibraryTable.getModifiableModel();
            try {
              for (Library library : model.getLibraries()) {
                model.removeLibrary(library);
              }
            }
            finally {
              model.commit();
            }
          }

          // Register libraries.
          myProjectDataManager.importData(Collections.<DataNode<?>>singletonList(projectWithResolvedLibraries), project, false);
        }
      });
    }
  });
}
 
Example 5
Source File: ModuleDataService.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void importData(@Nonnull final Collection<DataNode<ModuleData>> toImport, @Nonnull final Project project, final boolean synchronous) {
  if (toImport.isEmpty()) {
    return;
  }
  if (!project.isInitialized()) {
    myFuture = AppExecutorUtil.getAppScheduledExecutorService()
            .schedule(new ImportModulesTask(project, toImport, synchronous), PROJECT_INITIALISATION_DELAY_MS, TimeUnit.MILLISECONDS);
    return;
  }
  ExternalSystemApiUtil.executeProjectChangeAction(synchronous, new DisposeAwareProjectChange(project) {
    @RequiredUIAccess
    @Override
    public void execute() {
      final Collection<DataNode<ModuleData>> toCreate = filterExistingModules(toImport, project);
      if (!toCreate.isEmpty()) {
        createModules(toCreate, project);
      }
      for (DataNode<ModuleData> node : toImport) {
        Module module = ProjectStructureHelper.findIdeModule(node.getData(), project);
        if (module != null) {
          syncPaths(module, node.getData());
        }
      }
    }
  });
}
 
Example 6
Source File: AbstractDependencyDataService.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void setScope(@Nonnull final DependencyScope scope, @Nonnull final ExportableOrderEntry dependency, boolean synchronous) {
  ExternalSystemApiUtil.executeProjectChangeAction(synchronous, new DisposeAwareProjectChange(dependency.getOwnerModule()) {
    @RequiredUIAccess
    @Override
    public void execute() {
      doForDependency(dependency, new Consumer<ExportableOrderEntry>() {
        @Override
        public void consume(ExportableOrderEntry entry) {
          entry.setScope(scope);
        }
      });
    }
  });
}
 
Example 7
Source File: AbstractDependencyDataService.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void setExported(final boolean exported, @Nonnull final ExportableOrderEntry dependency, boolean synchronous) {
  ExternalSystemApiUtil.executeProjectChangeAction(synchronous, new DisposeAwareProjectChange(dependency.getOwnerModule()) {
    @RequiredUIAccess
    @Override
    public void execute() {
      doForDependency(dependency, new Consumer<ExportableOrderEntry>() {
        @Override
        public void consume(ExportableOrderEntry entry) {
          entry.setExported(exported);
        }
      });
    }
  });
}
 
Example 8
Source File: AbstractDependencyDataService.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void removeData(@Nonnull Collection<? extends ExportableOrderEntry> toRemove, @Nonnull final Module module, boolean synchronous) {
  if (toRemove.isEmpty()) {
    return;
  }
  for (final ExportableOrderEntry dependency : toRemove) {
    ExternalSystemApiUtil.executeProjectChangeAction(synchronous, new DisposeAwareProjectChange(dependency.getOwnerModule()) {
      @RequiredUIAccess
      @Override
      public void execute() {
        ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(module);
        final ModifiableRootModel moduleRootModel = moduleRootManager.getModifiableModel();
        try {
          // The thing is that intellij created order entry objects every time new modifiable model is created,
          // that's why we can't use target dependency object as is but need to get a reference to the current
          // entry object from the model instead.
          for (OrderEntry entry : moduleRootModel.getOrderEntries()) {
            if (entry instanceof ExportableOrderEntry) {
              ExportableOrderEntry orderEntry = (ExportableOrderEntry)entry;
              if (orderEntry.getPresentableName().equals(dependency.getPresentableName()) &&
                  orderEntry.getScope().equals(dependency.getScope())) {
                moduleRootModel.removeOrderEntry(entry);
                break;
              }
            }
            else if (entry.getPresentableName().equals(dependency.getPresentableName())) {
              moduleRootModel.removeOrderEntry(entry);
              break;
            }
          }
        }
        finally {
          moduleRootModel.commit();
        }
      }
    });
  }
}
 
Example 9
Source File: LibraryDataService.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void removeData(@Nonnull final Collection<? extends Library> libraries, @Nonnull final Project project, boolean synchronous) {
  if (libraries.isEmpty()) {
    return;
  }
  ExternalSystemApiUtil.executeProjectChangeAction(synchronous, new DisposeAwareProjectChange(project) {
    @RequiredUIAccess
    @Override
    public void execute() {
      final LibraryTable libraryTable = ProjectLibraryTable.getInstance(project);
      final LibraryTable.ModifiableModel model = libraryTable.getModifiableModel();
      try {
        for (Library library : libraries) {
          String libraryName = library.getName();
          if (libraryName != null) {
            Library libraryToRemove = model.getLibraryByName(libraryName);
            if (libraryToRemove != null) {
              model.removeLibrary(libraryToRemove);
            }
          }
        }
      }
      finally {
        model.commit();
      }
    }
  });
}
 
Example 10
Source File: LibraryDependencyDataService.java    From consulo with Apache License 2.0 4 votes vote down vote up
public void importData(@Nonnull final Collection<DataNode<LibraryDependencyData>> nodesToImport, @Nonnull final Module module, final boolean synchronous) {
  ExternalSystemApiUtil.executeProjectChangeAction(synchronous, new DisposeAwareProjectChange(module) {
    @RequiredUIAccess
    @Override
    public void execute() {
      importMissingProjectLibraries(module, nodesToImport, synchronous);

      // The general idea is to import all external project library dependencies and module libraries which don't present at the
      // ide side yet and remove all project library dependencies and module libraries which present at the ide but not at
      // the given collection.
      // The trick is that we should perform module settings modification inside try/finally block against target root model.
      // That means that we need to prepare all necessary data, obtain a model and modify it as necessary.
      Map<Set<String>/* library paths */, LibraryDependencyData> moduleLibrariesToImport = ContainerUtilRt.newHashMap();
      Map<String/* library name + scope */, LibraryDependencyData> projectLibrariesToImport = ContainerUtilRt.newHashMap();
      Set<LibraryDependencyData> toImport = ContainerUtilRt.newLinkedHashSet();

      boolean hasUnresolved = false;
      for (DataNode<LibraryDependencyData> dependencyNode : nodesToImport) {
        LibraryDependencyData dependencyData = dependencyNode.getData();
        LibraryData libraryData = dependencyData.getTarget();
        hasUnresolved |= libraryData.isUnresolved();
        switch (dependencyData.getLevel()) {
          case MODULE:
            if (!libraryData.isUnresolved()) {
              Set<String> paths = ContainerUtilRt.newHashSet();
              for (String path : libraryData.getPaths(LibraryPathType.BINARY)) {
                paths.add(ExternalSystemApiUtil.toCanonicalPath(path) + dependencyData.getScope().name());
              }
              moduleLibrariesToImport.put(paths, dependencyData);
              toImport.add(dependencyData);
            }
            break;
          case PROJECT:
            projectLibrariesToImport.put(libraryData.getInternalName() + dependencyData.getScope().name(), dependencyData);
            toImport.add(dependencyData);
        }
      }

      ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(module);
      final ModifiableRootModel moduleRootModel = moduleRootManager.getModifiableModel();
      LibraryTable moduleLibraryTable = moduleRootModel.getModuleLibraryTable();
      LibraryTable libraryTable = ProjectLibraryTable.getInstance(module.getProject());
      try {
        filterUpToDateAndRemoveObsolete(moduleLibrariesToImport, projectLibrariesToImport, toImport, moduleRootModel, hasUnresolved);

        // Import missing library dependencies.
        if (!toImport.isEmpty()) {
          importMissing(toImport, moduleRootModel, moduleLibraryTable, libraryTable, module);
        }
      }
      finally {
        moduleRootModel.commit();
      }
    }
  });
}