Handle correctly 'Confirm open new project' option when project attachment is possibl...
authorDmitry Trofimov <dmitry.trofimov@jetbrains.com>
Mon, 19 Dec 2016 23:30:53 +0000 (00:30 +0100)
committerDmitry Trofimov <dmitry.trofimov@jetbrains.com>
Mon, 19 Dec 2016 23:57:49 +0000 (00:57 +0100)
When the option selector says that there should be no confirmation dialog, there is no way to attach a project even if is generally allowed. So to fix that we add an action 'Attach project' which is enabled when there is no confirmation dialog on project open.

platform/platform-impl/src/com/intellij/platform/AttachProjectAction.kt [new file with mode: 0644]
platform/platform-impl/src/com/intellij/platform/PlatformProjectOpenProcessor.java
platform/platform-resources/src/idea/PlatformActions.xml

diff --git a/platform/platform-impl/src/com/intellij/platform/AttachProjectAction.kt b/platform/platform-impl/src/com/intellij/platform/AttachProjectAction.kt
new file mode 100644 (file)
index 0000000..6babe84
--- /dev/null
@@ -0,0 +1,76 @@
+/*
+ * Copyright 2000-2016 JetBrains s.r.o.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.intellij.platform
+
+import com.intellij.ide.GeneralSettings
+import com.intellij.ide.actions.OpenProjectFileChooserDescriptor
+import com.intellij.openapi.actionSystem.AnAction
+import com.intellij.openapi.actionSystem.AnActionEvent
+import com.intellij.openapi.actionSystem.CommonDataKeys
+import com.intellij.openapi.fileChooser.FileChooser
+import com.intellij.openapi.project.DumbAware
+import com.intellij.openapi.project.Project
+import com.intellij.openapi.ui.Messages
+import com.intellij.openapi.util.io.FileUtil
+import com.intellij.openapi.vfs.VirtualFile
+import com.intellij.project.isProjectDirectoryExistsUsingIo
+import com.intellij.projectImport.ProjectAttachProcessor
+import java.nio.file.Paths
+
+/**
+ * This action is enabled when confirmOpenNewProject option is set in settings to either OPEN_PROJECT_NEW_WINDOW or
+ * OPEN_PROJECT_SAME_WINDOW, so there is no dialog shown on open directory action, which makes attaching a new project impossible.
+ * This action provides a way to do that in this case.
+ *
+ * @author traff
+ */
+class AttachProjectAction : AnAction("Attach project"), DumbAware {
+  override fun update(e: AnActionEvent?) {
+    e?.presentation?.isEnabledAndVisible = ProjectAttachProcessor.canAttachToProject() &&
+                                           GeneralSettings.getInstance().confirmOpenNewProject != GeneralSettings.OPEN_PROJECT_ASK
+  }
+
+  override fun actionPerformed(e: AnActionEvent?) {
+    val descriptor = OpenProjectFileChooserDescriptor(false)
+    val project = e?.getData(CommonDataKeys.PROJECT)
+
+
+    FileChooser.chooseFiles(descriptor, project, null) { files ->
+      attachProject(files[0], project)
+    }
+  }
+}
+
+
+fun attachProject(virtualFile: VirtualFile, project: Project?) {
+  var baseDir: VirtualFile? = virtualFile
+
+  if (!baseDir!!.isDirectory) {
+    baseDir = virtualFile.parent
+    while (baseDir != null) {
+      if (isProjectDirectoryExistsUsingIo(baseDir)) {
+        break
+      }
+      baseDir = baseDir.parent
+    }
+  }
+
+  if (baseDir != null) {
+    PlatformProjectOpenProcessor.attachToProject(project, Paths.get(FileUtil.toSystemDependentName(baseDir.getPath())), null)
+  } else {
+    Messages.showErrorDialog("Project not found in ${virtualFile.path}", "Can't Attach Project")
+  }
+}
\ No newline at end of file
index b953296f808e0a52928cedfe9bb0e85e2549f71b..11b1ce8bbde7d857fc5aba42480ec303c035b3a8 100644 (file)
@@ -140,7 +140,7 @@ public class PlatformProjectOpenProcessor extends ProjectOpenProcessor {
         projectToClose = openProjects[openProjects.length - 1];
       }
 
-      if (ProjectAttachProcessor.canAttachToProject()) {
+      if (ProjectAttachProcessor.canAttachToProject() && GeneralSettings.getInstance().getConfirmOpenNewProject() == GeneralSettings.OPEN_PROJECT_ASK) {
         final OpenOrAttachDialog dialog = new OpenOrAttachDialog(projectToClose, isReopen, isReopen ? "Reopen Project" : "Open Project");
         if (!dialog.showAndGet()) {
           return null;
@@ -250,7 +250,7 @@ public class PlatformProjectOpenProcessor extends ProjectOpenProcessor {
     return moduleRef.get();
   }
 
-  private static boolean attachToProject(Project project, @NotNull Path projectDir, ProjectOpenedCallback callback) {
+  public static boolean attachToProject(Project project, @NotNull Path projectDir, ProjectOpenedCallback callback) {
     for (ProjectAttachProcessor processor : Extensions.getExtensions(ProjectAttachProcessor.EP_NAME)) {
       if (processor.attachToProject(project, projectDir, callback)) {
         return true;
index 3c4da9089d51cdd08b8293f2b736b1c9c574c5fd..21a417db6cc52cb5a73930b4bcfc2f91e61e5fef 100644 (file)
         <group id="FileOpenGroup">
           <action id="NewDummyProject" class="com.intellij.ide.actions.NewDummyProjectAction" text="New Dummy Project" internal="true"/>
           <action id="OpenFile" class="com.intellij.ide.actions.OpenFileAction" icon="AllIcons.Actions.Menu_open"/>
+          <action id="AttachProject" class="com.intellij.platform.AttachProjectAction"/>
           <group id="$LRU" popup="true">
             <group id="RecentProjectListGroup" class="com.intellij.ide.actions.RecentProjectsGroup" popup="false"/>
             <separator/>