Java Code Examples for com.intellij.util.containers.ContainerUtilRt#newHashSet()

The following examples show how to use com.intellij.util.containers.ContainerUtilRt#newHashSet() . 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: ExternalSystemFacadeManager.java    From consulo with Apache License 2.0 6 votes vote down vote up
private static <V> void onProjectRename(@Nonnull Map<IntegrationKey, V> data,
                                        @Nonnull String oldName,
                                        @Nonnull String newName)
{
  Set<IntegrationKey> keys = ContainerUtilRt.newHashSet(data.keySet());
  for (IntegrationKey key : keys) {
    if (!key.getIdeProjectName().equals(oldName)) {
      continue;
    }
    IntegrationKey newKey = new IntegrationKey(newName,
                                               key.getIdeProjectLocationHash(),
                                               key.getExternalSystemId(),
                                               key.getExternalProjectConfigPath());
    V value = data.get(key);
    data.put(newKey, value);
    data.remove(key);
    if (value instanceof Consumer) {
      //noinspection unchecked
      ((Consumer)value).consume(newKey);
    }
  }
}
 
Example 2
Source File: ExternalSystemTasksTreeModel.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void ensureTasks(@Nonnull String externalProjectConfigPath, @Nonnull Collection<ExternalTaskPojo> tasks) {
    if (tasks.isEmpty()) {
      return;
    }
    ExternalSystemNode<ExternalProjectPojo> moduleNode = findProjectNode(externalProjectConfigPath);
    if (moduleNode == null) {
//      LOG.warn(String.format(
//        "Can't proceed tasks for module which external config path is '%s'. Reason: no such module node is found. Tasks: %s",
//        externalProjectConfigPath, tasks
//      ));
      return;
    }
    Set<ExternalTaskExecutionInfo> toAdd = ContainerUtilRt.newHashSet();
    for (ExternalTaskPojo task : tasks) {
      toAdd.add(buildTaskInfo(task));
    }
    for (int i = 0; i < moduleNode.getChildCount(); i++) {
      ExternalSystemNode<?> childNode = moduleNode.getChildAt(i);
      Object element = childNode.getDescriptor().getElement();
      if (element instanceof ExternalTaskExecutionInfo) {
        if (!toAdd.remove(element)) {
          removeNodeFromParent(childNode);
          //noinspection AssignmentToForLoopParameter
          i--;
        }
      }
    }

    if (!toAdd.isEmpty()) {
      for (ExternalTaskExecutionInfo taskInfo : toAdd) {
        insertNodeInto(new ExternalSystemNode<>(descriptor(taskInfo, taskInfo.getDescription(), myUiAware.getTaskIcon())), moduleNode);
      }
    }
  }
 
Example 3
Source File: ExternalSystemUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public MyMultiExternalProjectRefreshCallback(Project project, ProjectDataManager projectDataManager, int[] counter, ProjectSystemId externalSystemId) {
  myProject = project;
  myProjectDataManager = projectDataManager;
  myCounter = counter;
  myExternalSystemId = externalSystemId;
  myExternalModulePaths = ContainerUtilRt.newHashSet();
}
 
Example 4
Source File: ArrangementUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static <T> Set<T> flatten(@Nonnull Iterable<? extends Iterable<T>> data) {
  Set<T> result = ContainerUtilRt.newHashSet();
  for (Iterable<T> i : data) {
    for (T t : i) {
      result.add(t);
    }
  }
  return result;
}
 
Example 5
Source File: ExternalSystemImportingTestCase.java    From intellij-quarkus with Eclipse Public License 2.0 4 votes vote down vote up
private void doImportProject() {
  AbstractExternalSystemSettings systemSettings = ExternalSystemApiUtil.getSettings(myProject, getExternalSystemId());
  final ExternalProjectSettings projectSettings = getCurrentExternalProjectSettings();
  projectSettings.setExternalProjectPath(getProjectPath());
  //noinspection unchecked
  Set<ExternalProjectSettings> projects = ContainerUtilRt.newHashSet(systemSettings.getLinkedProjectsSettings());
  projects.remove(projectSettings);
  projects.add(projectSettings);
  //noinspection unchecked
  systemSettings.setLinkedProjectsSettings(projects);

  final Ref<Couple<String>> error = Ref.create();
  ImportSpec importSpec = createImportSpec();
  if (importSpec.getCallback() == null) {
    importSpec = new ImportSpecBuilder(importSpec).callback(new ExternalProjectRefreshCallback() {
      @Override
      public void onSuccess(@Nullable final DataNode<ProjectData> externalProject) {
        if (externalProject == null) {
          System.err.println("Got null External project after import");
          return;
        }
        ServiceManager.getService(ProjectDataManager.class).importData(externalProject, myProject, true);
        System.out.println("External project was successfully imported");
      }

      @Override
      public void onFailure(@NotNull String errorMessage, @Nullable String errorDetails) {
        error.set(Couple.of(errorMessage, errorDetails));
      }
    }).build();
  }

  ExternalSystemProgressNotificationManager notificationManager =
    ServiceManager.getService(ExternalSystemProgressNotificationManager.class);
  ExternalSystemTaskNotificationListenerAdapter listener = new ExternalSystemTaskNotificationListenerAdapter() {
    @Override
    public void onTaskOutput(@NotNull ExternalSystemTaskId id, @NotNull String text, boolean stdOut) {
      if (StringUtil.isEmptyOrSpaces(text)) return;
      (stdOut ? System.out : System.err).print(text);
    }
  };
  notificationManager.addNotificationListener(listener);
  try {
    ExternalSystemUtil.refreshProjects(importSpec);
  }
  finally {
    notificationManager.removeNotificationListener(listener);
  }

  if (!error.isNull()) {
    String failureMsg = "Import failed: " + error.get().first;
    if (StringUtil.isNotEmpty(error.get().second)) {
      failureMsg += "\nError details: \n" + error.get().second;
    }
    fail(failureMsg);
  }
}
 
Example 6
Source File: PythonInfoModifier.java    From intellij-pants-plugin with Apache License 2.0 4 votes vote down vote up
/**
 * Unfortunately Python plugin doesn't support package prefixes for source root.
 * To workaround it at the moment we will create two targets: one for tests and one for production sources.
 *
 * todo: remove once https://youtrack.jetbrains.com/issue/PY-16830 is resolved
 */
@Override
public void modify(
  @NotNull ProjectInfo projectInfo,
  @NotNull PantsCompileOptionsExecutor executor,
  @NotNull Logger log
) {
  TargetInfo sources = new TargetInfo();
  TargetInfo tests = new TargetInfo();
  final Set<String> pythonTargetNames = ContainerUtilRt.newHashSet();
  for (Map.Entry<String, TargetInfo> entry : projectInfo.getTargets().entrySet()) {
    final String targetName = entry.getKey();
    final TargetInfo targetInfo = entry.getValue();
    if (!targetInfo.isPythonTarget()) {
      continue;
    }
    pythonTargetNames.add(targetName);
    if (targetInfo.isTest()) {
      tests = tests.union(targetInfo);
    } else {
      sources = sources.union(targetInfo);
    }
  }
  if (sources.isEmpty()) {
    return;
  }
  projectInfo.removeTargets(pythonTargetNames);
  if (!pythonTargetNames.isEmpty() && log.isDebugEnabled()) {
    log.debug(String.format("Combining %d python targets", pythonTargetNames.size()));
  }

  sources.getTargets().removeAll(pythonTargetNames);
  projectInfo.addTarget("python:src", sources);
  if (!tests.isEmpty()) {
    // make sure src and test don't have common roots
    sources.getRoots().removeAll(tests.getRoots());
    tests.getTargets().removeAll(pythonTargetNames);
    tests.getTargets().add("python:src");
    projectInfo.addTarget("python:tests", tests);
  }
}
 
Example 7
Source File: ExternalSystemUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
/**
 * Asks to refresh all external projects of the target external system linked to the given ide project based on provided spec
 *
 * @param specBuilder import specification builder
 */
public static void refreshProjects(@Nonnull final ImportSpecBuilder specBuilder) {
  ImportSpec spec = specBuilder.build();

  ExternalSystemManager<?, ?, ?, ?, ?> manager = ExternalSystemApiUtil.getManager(spec.getExternalSystemId());
  if (manager == null) {
    return;
  }
  AbstractExternalSystemSettings<?, ?, ?> settings = manager.getSettingsProvider().fun(spec.getProject());
  final Collection<? extends ExternalProjectSettings> projectsSettings = settings.getLinkedProjectsSettings();
  if (projectsSettings.isEmpty()) {
    return;
  }

  final ProjectDataManager projectDataManager = ServiceManager.getService(ProjectDataManager.class);
  final int[] counter = new int[1];

  ExternalProjectRefreshCallback callback =
          new MyMultiExternalProjectRefreshCallback(spec.getProject(), projectDataManager, counter, spec.getExternalSystemId());

  Map<String, Long> modificationStamps = manager.getLocalSettingsProvider().fun(spec.getProject()).getExternalConfigModificationStamps();
  Set<String> toRefresh = ContainerUtilRt.newHashSet();
  for (ExternalProjectSettings setting : projectsSettings) {

    // don't refresh project when auto-import is disabled if such behavior needed (e.g. on project opening when auto-import is disabled)
    if (!setting.isUseAutoImport() && spec.isWhenAutoImportEnabled()) continue;

    if (spec.isForceWhenUptodate()) {
      toRefresh.add(setting.getExternalProjectPath());
    }
    else {
      Long oldModificationStamp = modificationStamps.get(setting.getExternalProjectPath());
      long currentModificationStamp = getTimeStamp(setting, spec.getExternalSystemId());
      if (oldModificationStamp == null || oldModificationStamp < currentModificationStamp) {
        toRefresh.add(setting.getExternalProjectPath());
      }
    }
  }

  if (!toRefresh.isEmpty()) {
    ExternalSystemNotificationManager.getInstance(spec.getProject()).clearNotifications(null, NotificationSource.PROJECT_SYNC, spec.getExternalSystemId());

    counter[0] = toRefresh.size();
    for (String path : toRefresh) {
      refreshProject(spec.getProject(), spec.getExternalSystemId(), path, callback, false, spec.getProgressExecutionMode());
    }
  }
}
 
Example 8
Source File: ExternalSystemUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
/**
 * Allows to answer if given ide project has 1-1 mapping with the given external project, i.e. the ide project has been
 * imported from external system and no other external projects have been added.
 * <p>
 * This might be necessary in a situation when project-level setting is changed (e.g. project name). We don't want to rename
 * ide project if it doesn't completely corresponds to the given ide project then.
 *
 * @param ideProject      target ide project
 * @param externalProject target external project
 * @return <code>true</code> if given ide project has 1-1 mapping to the given external project;
 * <code>false</code> otherwise
 */
public static boolean isOneToOneMapping(@Nonnull Project ideProject, @Nonnull DataNode<ProjectData> externalProject) {
  String linkedExternalProjectPath = null;
  for (ExternalSystemManager<?, ?, ?, ?, ?> manager : ExternalSystemApiUtil.getAllManagers()) {
    ProjectSystemId externalSystemId = manager.getSystemId();
    AbstractExternalSystemSettings systemSettings = ExternalSystemApiUtil.getSettings(ideProject, externalSystemId);
    Collection projectsSettings = systemSettings.getLinkedProjectsSettings();
    int linkedProjectsNumber = projectsSettings.size();
    if (linkedProjectsNumber > 1) {
      // More than one external project of the same external system type is linked to the given ide project.
      return false;
    }
    else if (linkedProjectsNumber == 1) {
      if (linkedExternalProjectPath == null) {
        // More than one external project of different external system types is linked to the current ide project.
        linkedExternalProjectPath = ((ExternalProjectSettings)projectsSettings.iterator().next()).getExternalProjectPath();
      }
      else {
        return false;
      }
    }
  }

  ProjectData projectData = externalProject.getData();
  if (linkedExternalProjectPath != null && !linkedExternalProjectPath.equals(projectData.getLinkedExternalProjectPath())) {
    // New external project is being linked.
    return false;
  }

  Set<String> externalModulePaths = ContainerUtilRt.newHashSet();
  for (DataNode<ModuleData> moduleNode : ExternalSystemApiUtil.findAll(externalProject, ProjectKeys.MODULE)) {
    externalModulePaths.add(moduleNode.getData().getLinkedExternalProjectPath());
  }
  externalModulePaths.remove(linkedExternalProjectPath);

  for (Module module : ModuleManager.getInstance(ideProject).getModules()) {
    String path = ExternalSystemApiUtil.getExtensionSystemOption(module, ExternalSystemConstants.LINKED_PROJECT_PATH_KEY);
    if (!StringUtil.isEmpty(path) && !externalModulePaths.remove(path)) {
      return false;
    }
  }
  return externalModulePaths.isEmpty();
}
 
Example 9
Source File: ExternalSystemUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
/**
 * Tries to obtain external project info implied by the given settings and link that external project to the given ide project.
 *
 * @param externalSystemId        target external system
 * @param projectSettings         settings of the external project to link
 * @param project                 target ide project to link external project to
 * @param executionResultCallback it might take a while to resolve external project info, that's why it's possible to provide
 *                                a callback to be notified on processing result. It receives <code>true</code> if an external
 *                                project has been successfully linked to the given ide project;
 *                                <code>false</code> otherwise (note that corresponding notification with error details is expected
 *                                to be shown to the end-user then)
 * @param isPreviewMode           flag which identifies if missing external project binaries should be downloaded
 * @param progressExecutionMode   identifies how progress bar will be represented for the current processing
 */
@SuppressWarnings("UnusedDeclaration")
public static void linkExternalProject(@Nonnull final ProjectSystemId externalSystemId,
                                       @Nonnull final ExternalProjectSettings projectSettings,
                                       @Nonnull final Project project,
                                       @Nullable final Consumer<Boolean> executionResultCallback,
                                       boolean isPreviewMode,
                                       @Nonnull final ProgressExecutionMode progressExecutionMode) {
  ExternalProjectRefreshCallback callback = new ExternalProjectRefreshCallback() {
    @SuppressWarnings("unchecked")
    @Override
    public void onSuccess(@Nullable final DataNode<ProjectData> externalProject) {
      if (externalProject == null) {
        if (executionResultCallback != null) {
          executionResultCallback.consume(false);
        }
        return;
      }
      AbstractExternalSystemSettings systemSettings = ExternalSystemApiUtil.getSettings(project, externalSystemId);
      Set<ExternalProjectSettings> projects = ContainerUtilRt.<ExternalProjectSettings>newHashSet(systemSettings.getLinkedProjectsSettings());
      projects.add(projectSettings);
      systemSettings.setLinkedProjectsSettings(projects);
      ensureToolWindowInitialized(project, externalSystemId);
      ExternalSystemApiUtil.executeProjectChangeAction(new DisposeAwareProjectChange(project) {
        @RequiredUIAccess
        @Override
        public void execute() {
          ProjectRootManagerEx.getInstanceEx(project).mergeRootsChangesDuring(new Runnable() {
            @Override
            public void run() {
              ProjectDataManager dataManager = ServiceManager.getService(ProjectDataManager.class);
              dataManager.importData(externalProject.getKey(), Collections.singleton(externalProject), project, true);
            }
          });
        }
      });
      if (executionResultCallback != null) {
        executionResultCallback.consume(true);
      }
    }

    @Override
    public void onFailure(@Nonnull String errorMessage, @Nullable String errorDetails) {
      if (executionResultCallback != null) {
        executionResultCallback.consume(false);
      }
    }
  };
  refreshProject(project, externalSystemId, projectSettings.getExternalProjectPath(), callback, isPreviewMode, progressExecutionMode);
}