rename file available for non-editor popups (IDEA-80320)
authorAnna Kozlova <anna.kozlova@jetbrains.com>
Fri, 27 Jan 2012 14:28:06 +0000 (18:28 +0400)
committerAnna Kozlova <anna.kozlova@jetbrains.com>
Fri, 27 Jan 2012 14:29:10 +0000 (18:29 +0400)
platform/lang-impl/src/com/intellij/refactoring/rename/RenameDialog.java
platform/lang-impl/src/com/intellij/refactoring/rename/RenameFileAction.java [new file with mode: 0644]
platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/EditorTabbedContainer.java
platform/platform-resources/src/idea/LangActions.xml
plugins/groovy/src/META-INF/plugin.xml
plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/javaToGrovyRename/RenameJavaFileToGroovyFileAction.java [deleted file]

index d4c8d662af43cdf3418def20d6676de0aa6d37dd..7ab5099e12a9e44348f5b529b003d0667727cdb5 100644 (file)
@@ -118,7 +118,7 @@ public class RenameDialog extends RefactoringDialog {
     myNameSuggestionsField = new NameSuggestionsField(suggestedNames, myProject, FileTypes.PLAIN_TEXT, myEditor) {
       @Override
       protected boolean shouldSelectAll() {
-        return myEditor.getSettings().isPreselectRename();
+        return myEditor == null || myEditor.getSettings().isPreselectRename();
       }
     };
     if (myPsiElement instanceof PsiFile && myEditor == null) {
diff --git a/platform/lang-impl/src/com/intellij/refactoring/rename/RenameFileAction.java b/platform/lang-impl/src/com/intellij/refactoring/rename/RenameFileAction.java
new file mode 100644 (file)
index 0000000..a0aa127
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2000-2009 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.refactoring.rename;
+
+import com.intellij.openapi.actionSystem.*;
+import com.intellij.openapi.project.DumbAware;
+import com.intellij.openapi.project.Project;
+import com.intellij.openapi.vfs.VirtualFile;
+import com.intellij.psi.PsiFile;
+
+/**
+ * @author ven
+ */
+public class RenameFileAction extends AnAction implements DumbAware {
+  public static final String RENAME_FILE = "Rename File";
+
+  public void actionPerformed(final AnActionEvent e) {
+    final PsiFile file = e.getData(LangDataKeys.PSI_FILE);
+    assert file != null;
+    final VirtualFile virtualFile = file.getVirtualFile();
+    assert virtualFile != null;
+    final Project project = e.getData(PlatformDataKeys.PROJECT);
+    assert project != null;
+    new RenameDialog(project, file, file, null).show();
+  }
+
+  public void update(AnActionEvent e) {
+    PsiFile file = e.getData(LangDataKeys.PSI_FILE);
+    Presentation presentation = e.getPresentation();
+    boolean enabled = file != null && e.getPlace() != ActionPlaces.EDITOR_POPUP && e.getData(PlatformDataKeys.PROJECT) != null;
+    presentation.setEnabled(enabled);
+    presentation.setVisible(enabled);
+    if (enabled) {
+      presentation.setText(RENAME_FILE);
+      presentation.setDescription(RENAME_FILE);
+    }
+  }
+}
index 244021290c1ca171ba605ba0827f501bc0e8feca..6dfcfde538f93bd3722edd7c073c344554a48425 100644 (file)
@@ -46,8 +46,8 @@ import com.intellij.ui.docking.DragSession;
 import com.intellij.ui.switcher.SwitchProvider;
 import com.intellij.ui.switcher.SwitchTarget;
 import com.intellij.ui.tabs.*;
-import com.intellij.ui.tabs.impl.JBTabsImpl;
 import com.intellij.ui.tabs.impl.JBEditorTabs;
+import com.intellij.ui.tabs.impl.JBTabsImpl;
 import com.intellij.util.ui.AwtVisitor;
 import com.intellij.util.ui.TimedDeadzone;
 import com.intellij.util.ui.UIUtil;
@@ -86,7 +86,7 @@ final class EditorTabbedContainer implements Disposable, CloseAction.CloseTarget
       public ActionGroup get() {
         return (ActionGroup)CustomActionsSchema.getInstance().getCorrectedAction(IdeActions.GROUP_EDITOR_TAB_POPUP);
       }
-    }, ActionPlaces.EDITOR_POPUP, false).setNavigationActionsEnabled(false).addTabMouseListener(new TabMouseListener()).getPresentation()
+    }, ActionPlaces.EDITOR_TAB_POPUP, false).setNavigationActionsEnabled(false).addTabMouseListener(new TabMouseListener()).getPresentation()
       .setTabDraggingEnabled(true).setUiDecorator(new UiDecorator() {
       @NotNull
       public UiDecoration getDecoration() {
index 12a87ccd6ed0052b6286bfca7cf7ca2e383e3459..7238c588fb279428eeb87d3b2a3f8642ab7c4b20 100644 (file)
               class = "com.intellij.refactoring.actions.RefactoringQuickListPopupAction"
               text = "Refactor This..." description="Context aware popup with list of refactoring actions"/>
       <action id="RenameElement" class="com.intellij.refactoring.actions.RenameElementAction"/>
+      <action id="RenameFile" class="com.intellij.refactoring.rename.RenameFileAction"/>
       <action id="ChangeSignature" class="com.intellij.refactoring.actions.ChangeSignatureAction"/>
       <separator/>
       <action id="Move" class="com.intellij.refactoring.actions.MoveAction"/>
       <reference ref="AddToFavorites"/>
       <reference ref="AddAllToFavorites"/>
       <separator/>
+      <reference ref="RenameFile"/>
+      <separator/>
       <reference ref="MoveToChangeList"/>
       <separator/>
       <reference ref="RunContextPopupGroup"/>
index ee955978e75ce1fd711da434519aa437100d6bac..9a1384fa312ad103ac6912e0938accf5266158fb 100644 (file)
       <add-to-group group-id="Internal"/>
     </group>
 
-    <action id="RenameJavaFileToGroovyFileAction"
-            class="org.jetbrains.plugins.groovy.refactoring.javaToGrovyRename.RenameJavaFileToGroovyFileAction"
-            text="Rename To Groovy" description="Rename Java File to Groovy">
-      <add-to-group group-id="EditorTabPopupMenu" anchor="last"/>
-    </action>
     <action id="ExcludeFromStubGeneration"
             class="org.jetbrains.plugins.groovy.compiler.ExcludeFromStubGenerationAction"
             text="Exclude from stub generation" description="Don't generate Java stubs for this Groovy file on compilation">
diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/javaToGrovyRename/RenameJavaFileToGroovyFileAction.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/javaToGrovyRename/RenameJavaFileToGroovyFileAction.java
deleted file mode 100644 (file)
index d94b4fd..0000000
+++ /dev/null
@@ -1,115 +0,0 @@
-/*
- * Copyright 2000-2009 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 org.jetbrains.plugins.groovy.refactoring.javaToGrovyRename;
-
-import com.intellij.lang.Language;
-import com.intellij.lang.StdLanguages;
-import com.intellij.openapi.actionSystem.AnAction;
-import com.intellij.openapi.actionSystem.AnActionEvent;
-import com.intellij.openapi.actionSystem.DataKeys;
-import com.intellij.openapi.actionSystem.Presentation;
-import com.intellij.openapi.application.AccessToken;
-import com.intellij.openapi.application.ApplicationManager;
-import com.intellij.openapi.application.WriteAction;
-import com.intellij.openapi.command.CommandProcessor;
-import com.intellij.openapi.fileTypes.StdFileTypes;
-import com.intellij.openapi.module.Module;
-import com.intellij.openapi.module.ModuleUtil;
-import com.intellij.openapi.project.DumbAware;
-import com.intellij.openapi.project.Project;
-import com.intellij.openapi.ui.Messages;
-import com.intellij.openapi.vfs.VirtualFile;
-import com.intellij.psi.PsiFile;
-import com.intellij.util.IncorrectOperationException;
-import org.jetbrains.plugins.groovy.GroovyFileType;
-import org.jetbrains.plugins.groovy.extensions.GroovyScriptType;
-import org.jetbrains.plugins.groovy.extensions.GroovyScriptTypeDetector;
-import org.jetbrains.plugins.groovy.lang.psi.GroovyFile;
-import org.jetbrains.plugins.groovy.util.LibrariesUtil;
-
-/**
- * @author ven
- */
-public class RenameJavaFileToGroovyFileAction extends AnAction implements DumbAware {
-  public void actionPerformed(final AnActionEvent e) {
-    final PsiFile file = e.getData(DataKeys.PSI_FILE);
-    assert isEnabled(file);
-    assert file != null;
-    VirtualFile virtualFile = file.getVirtualFile();
-    assert virtualFile != null;
-    String newExt = file.getLanguage() == StdLanguages.JAVA ? GroovyFileType.DEFAULT_EXTENSION : StdFileTypes.JAVA.getDefaultExtension();
-
-    final String newName = virtualFile.getNameWithoutExtension() + "." + newExt;
-    final Project project = e.getData(DataKeys.PROJECT);
-    CommandProcessor.getInstance().executeCommand(project, new Runnable() {
-      public void run() {
-        AccessToken accessToken = WriteAction.start();
-
-        try {
-          try {
-            file.setName(newName);
-          } catch (final IncorrectOperationException e1) {
-            ApplicationManager.getApplication().invokeLater(new Runnable() {
-              public void run() {
-                Messages.showErrorDialog(project, "Cannot rename java file" + e1.getMessage(), "Canot Rename");
-              }
-            });
-          }
-        }
-        finally {
-          accessToken.finish();
-        }
-      }
-    }, "Rename File", null);
-  }
-
-  public void update(AnActionEvent e) {
-    PsiFile file = e.getData(DataKeys.PSI_FILE);
-    Presentation presentation = e.getPresentation();
-    boolean enabled = isEnabled(file);
-    presentation.setEnabled(enabled);
-    presentation.setVisible(enabled);
-    if (enabled) {
-      assert file != null;
-      presentation.setText(file.getLanguage() == StdLanguages.JAVA ? "Rename To Groovy" : "Rename To Java");
-      presentation.setDescription(file.getLanguage() == StdLanguages.JAVA ? "Rename Java File to Groovy" : "Rename Groovy File to Java");
-    }
-  }
-
-  private static boolean isEnabled(PsiFile file) {
-    if (file == null) {
-      return false;
-    }
-
-    final Language language = file.getLanguage();
-    if (language == GroovyFileType.GROOVY_LANGUAGE) {
-      if (((GroovyFile)file).isScript()) {
-        final GroovyScriptType type = GroovyScriptTypeDetector.getScriptType((GroovyFile)file);
-        if (type != GroovyScriptTypeDetector.DEFAULT_TYPE) {
-          return false;
-        }
-      }
-      return true;
-    }
-
-    if (!(language == StdLanguages.JAVA)) {
-      return false;
-    }
-
-    final Module module = ModuleUtil.findModuleForPsiElement(file);
-    return module != null && LibrariesUtil.hasGroovySdk(module);
-  }
-}