@Override
  public void actionPerformed(AnActionEvent e) {
    Project project = CommonDataKeys.PROJECT.getData(e.getDataContext());
    assert project != null;

    ProjectUtil.closeAndDispose(project);
    WelcomeFrame.showIfNoProjectOpened();
  }
  public void testPantsIdeaPluginGoal() throws Throwable {
    assertEmpty(ModuleManager.getInstance(myProject).getModules());

    /** Check whether Pants supports `idea-plugin` goal. */
    PantsUtil.findPantsExecutable(getProjectFolder().getPath());
    final GeneralCommandLine commandLinePantsGoals =
        PantsUtil.defaultCommandLine(getProjectFolder().getPath());
    commandLinePantsGoals.addParameter("goals");
    final ProcessOutput cmdOutputGoals =
        PantsUtil.getCmdOutput(commandLinePantsGoals.withWorkDirectory(getProjectFolder()), null);
    assertEquals(commandLinePantsGoals.toString() + " failed", 0, cmdOutputGoals.getExitCode());
    if (!cmdOutputGoals.getStdout().contains("idea-plugin")) {
      return;
    }

    /** Generate idea project via `idea-plugin` goal. */
    final GeneralCommandLine commandLine =
        PantsUtil.defaultCommandLine(getProjectFolder().getPath());
    final File outputFile = FileUtil.createTempFile("project_dir_location", ".out");
    commandLine.addParameters(
        "idea-plugin",
        "--no-open",
        "--output-file=" + outputFile.getPath(),
        "testprojects/tests/java/org/pantsbuild/testproject/::");
    final ProcessOutput cmdOutput =
        PantsUtil.getCmdOutput(commandLine.withWorkDirectory(getProjectFolder()), null);
    assertEquals(commandLine.toString() + " failed", 0, cmdOutput.getExitCode());
    String projectDir = FileUtil.loadFile(outputFile);

    myProject = ProjectUtil.openProject(projectDir + "/project.ipr", myProject, false);
    // Invoke post startup activities.
    UIUtil.dispatchAllInvocationEvents();
    /**
     * Under unit test mode, {@link com.intellij.ide.impl.ProjectUtil#openProject} will force open a
     * project in a new window, so Project SDK has to be reset. In practice, this is not needed.
     */
    ApplicationManager.getApplication()
        .runWriteAction(
            new Runnable() {
              @Override
              public void run() {
                final JavaSdk javaSdk = JavaSdk.getInstance();
                ProjectRootManager.getInstance(myProject)
                    .setProjectSdk(
                        ProjectJdkTable.getInstance().getSdksOfType(javaSdk).iterator().next());
              }
            });

    JUnitConfiguration runConfiguration =
        generateJUnitConfiguration(
            "testprojects_tests_java_org_pantsbuild_testproject_matcher_matcher",
            "org.pantsbuild.testproject.matcher.MatcherTest",
            null);

    assertAndRunPantsMake(runConfiguration);
    assertSuccessfulJUnitTest(runConfiguration);
  }
  /** Init components */
  private void initListeners() {
    FileChooserDescriptor fcd = FileChooserDescriptorFactory.createSingleFolderDescriptor();
    fcd.setShowFileSystemRoots(true);
    fcd.setTitle(GitBundle.getString("clone.destination.directory.title"));
    fcd.setDescription(GitBundle.getString("clone.destination.directory.description"));
    fcd.setHideIgnored(false);
    myParentDirectory.addActionListener(
        new ComponentWithBrowseButton.BrowseFolderActionListener<JTextField>(
            fcd.getTitle(),
            fcd.getDescription(),
            myParentDirectory,
            myProject,
            fcd,
            TextComponentAccessor.TEXT_FIELD_WHOLE_TEXT) {
          @Override
          protected VirtualFile getInitialFile() {
            // suggest project base directory only if nothing is typed in the component.
            String text = getComponentText();
            if (text.length() == 0) {
              VirtualFile file = myProject.getBaseDir();
              if (file != null) {
                return file;
              }
            }
            return super.getInitialFile();
          }
        });

    final DocumentListener updateOkButtonListener =
        new DocumentAdapter() {
          @Override
          protected void textChanged(DocumentEvent e) {
            updateButtons();
          }
        };
    myParentDirectory.getChildComponent().getDocument().addDocumentListener(updateOkButtonListener);
    String parentDir = GitRememberedInputs.getInstance().getCloneParentDir();
    if (StringUtil.isEmptyOrSpaces(parentDir)) {
      parentDir = ProjectUtil.getBaseDir();
    }
    myParentDirectory.setText(parentDir);

    myDirectoryName.getDocument().addDocumentListener(updateOkButtonListener);

    myTestButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(final ActionEvent e) {
            test();
          }
        });

    setOKActionEnabled(false);
    myTestButton.setEnabled(false);
  }
  public void testExternalFileModificationWhileProjectClosed() throws Exception {
    VirtualFile root = ProjectRootManager.getInstance(myProject).getContentRoots()[0];

    PsiClass objectClass =
        myJavaFacade.findClass(
            CommonClassNames.JAVA_LANG_OBJECT, GlobalSearchScope.allScope(getProject()));
    assertNotNull(objectClass);
    checkUsages(objectClass, new String[] {});
    FileBasedIndex.getInstance()
        .getContainingFiles(
            TodoIndex.NAME,
            new TodoIndexEntry("todo", true),
            GlobalSearchScope.allScope(getProject()));

    final String projectLocation = myProject.getPresentableUrl();
    assert projectLocation != null : myProject;
    PlatformTestUtil.saveProject(myProject);
    final VirtualFile content = ModuleRootManager.getInstance(getModule()).getContentRoots()[0];
    Project project = myProject;
    ProjectUtil.closeAndDispose(project);
    InjectedLanguageManagerImpl.checkInjectorsAreDisposed(project);

    assertTrue("Project was not disposed", myProject.isDisposed());
    myModule = null;

    final File file = new File(root.getPath(), "1.java");
    assertTrue(file.exists());

    FileUtil.writeToFile(file, "class A{ Object o;}".getBytes(CharsetToolkit.UTF8_CHARSET));
    root.refresh(false, true);

    LocalFileSystem.getInstance().refresh(false);

    myProject = ProjectManager.getInstance().loadAndOpenProject(projectLocation);
    InjectedLanguageManagerImpl.pushInjectors(getProject());

    setUpModule();
    setUpJdk();
    ProjectManagerEx.getInstanceEx().openTestProject(myProject);
    UIUtil.dispatchAllInvocationEvents(); // startup activities

    runStartupActivities();
    PsiTestUtil.addSourceContentToRoots(getModule(), content);

    assertNotNull(myProject);
    myPsiManager = (PsiManagerImpl) PsiManager.getInstance(myProject);
    myJavaFacade = JavaPsiFacadeEx.getInstanceEx(myProject);

    objectClass =
        myJavaFacade.findClass(
            CommonClassNames.JAVA_LANG_OBJECT, GlobalSearchScope.allScope(getProject()));
    assertNotNull(objectClass);
    checkUsages(objectClass, new String[] {"1.java"});
  }
  @Override
  protected void doOpenProject(
      @NotNull String projectPath, Project projectToClose, boolean forceOpenInNewFrame) {
    if (new File(projectPath).isDirectory()
        && !new File(projectPath, Project.DIRECTORY_STORE_FOLDER).exists()) {
      VirtualFile projectDir =
          LocalFileSystem.getInstance()
              .findFileByPath(FileUtil.toSystemIndependentName(projectPath));
      PlatformProjectOpenProcessor processor = PlatformProjectOpenProcessor.getInstanceIfItExists();
      if (projectDir != null && processor != null) {
        processor.doOpenProject(projectDir, projectToClose, forceOpenInNewFrame);
        return;
      }
    }

    ProjectUtil.openProject(projectPath, projectToClose, forceOpenInNewFrame);
  }
Ejemplo n.º 6
0
  @Override
  protected void doOKAction() {
    super.doOKAction();

    if (myCurrentProject != null) {
      int exitCode =
          Messages.showDialog(
              IdeBundle.message("prompt.open.project.in.new.frame"),
              IdeBundle.message("title.open.project"),
              new String[] {
                IdeBundle.message("button.newframe"), IdeBundle.message("button.existingframe")
              },
              1,
              Messages.getQuestionIcon());
      if (exitCode == 1) {
        ProjectUtil.closeAndDispose(myCurrentProject);
      }
    }

    final ProjectOptions myOptions = new ProjectOptions();
    myOptions.setProjectName(myProjectName.getText());
    myOptions.setProjectPath(myProjectPath.getPath());
    myOptions.setCreateNewLanguage(false);
    myOptions.setCreateNewSolution(false);
    myOptions.setStorageScheme(myProjectFormatPanel.isDefault());

    // invoke later is for plugins to be ready
    ApplicationManager.getApplication()
        .invokeLater(
            new Runnable() {
              @Override
              public void run() {
                try {
                  ProjectFactory factory = new ProjectFactory(myCurrentProject, myOptions);
                  Project project = factory.createProject();
                  myCurrentTemplateItem
                      .getTemplateFiller()
                      .fillProjectWithModules(project.getComponent(MPSProject.class));
                  factory.activate();
                } catch (ProjectNotCreatedException e) {
                  Messages.showErrorDialog(getContentPane(), e.getMessage());
                }
              }
            });
  }
 public boolean processCheckedOutDirectory(Project project, File directory) {
   File[] files =
       directory.listFiles(
           (FilenameFilter) new GlobFilenameFilter("*" + ProjectFileType.DOT_DEFAULT_EXTENSION));
   if (files != null && files.length > 0) {
     int rc =
         Messages.showYesNoDialog(
             project,
             VcsBundle.message("checkout.open.project.prompt", files[0].getPath()),
             VcsBundle.message("checkout.title"),
             Messages.getQuestionIcon());
     if (rc == 0) {
       ProjectUtil.openProject(files[0].getPath(), project, false);
     }
     return true;
   }
   return false;
 }
  public void testDocumentFromAlienProjectDoesNotEndsUpInMyUncommittedList() throws Exception {
    PsiFile file = getPsiManager().findFile(createFile());

    final Document document = getPsiDocumentManager().getDocument(file);

    File temp = createTempDirectory();
    final Project alienProject =
        createProject(new File(temp, "alien.ipr"), DebugUtil.currentStackTrace());
    boolean succ2 = ProjectManagerEx.getInstanceEx().openProject(alienProject);
    assertTrue(succ2);

    try {
      PsiManager alienManager = PsiManager.getInstance(alienProject);
      final String alienText = "alien";

      LightVirtualFile alienVirt = new LightVirtualFile("foo.java", alienText);
      final PsiFile alienFile = alienManager.findFile(alienVirt);
      final PsiDocumentManagerImpl alienDocManager =
          (PsiDocumentManagerImpl) PsiDocumentManager.getInstance(alienProject);
      final Document alienDocument = alienDocManager.getDocument(alienFile);
      // alienDocument.putUserData(CACHED_VIEW_PROVIDER, new MockFileViewProvider(alienFile));
      assertEquals(0, alienDocManager.getUncommittedDocuments().length);
      assertEquals(0, getPsiDocumentManager().getUncommittedDocuments().length);

      ApplicationManager.getApplication()
          .runWriteAction(
              new Runnable() {
                public void run() {
                  getPsiDocumentManager()
                      .documentChanged(
                          new DocumentEventImpl(
                              alienDocument,
                              0,
                              "",
                              "",
                              alienDocument.getModificationStamp(),
                              false));
                  assertEquals(0, getPsiDocumentManager().getUncommittedDocuments().length);
                  assertEquals(0, alienDocManager.getUncommittedDocuments().length);

                  alienDocManager.documentChanged(
                      new DocumentEventImpl(
                          alienDocument, 0, "", "", alienDocument.getModificationStamp(), false));
                  assertEquals(0, getPsiDocumentManager().getUncommittedDocuments().length);
                  assertEquals(1, alienDocManager.getUncommittedDocuments().length);

                  getPsiDocumentManager()
                      .documentChanged(
                          new DocumentEventImpl(
                              document, 0, "", "", document.getModificationStamp(), false));
                  assertEquals(1, getPsiDocumentManager().getUncommittedDocuments().length);
                  assertEquals(1, alienDocManager.getUncommittedDocuments().length);

                  alienDocManager.documentChanged(
                      new DocumentEventImpl(
                          document, 0, "", "", document.getModificationStamp(), false));
                  assertEquals(1, getPsiDocumentManager().getUncommittedDocuments().length);
                  assertEquals(1, alienDocManager.getUncommittedDocuments().length);
                }
              });
    } finally {
      ProjectUtil.closeAndDispose(alienProject);
    }
  }