/platform/lang-impl/src/com/intellij/openapi/roots/impl/libraries/RenameLibraryHandler.java

https://bitbucket.org/nbargnesi/idea · Java · 129 lines · 98 code · 13 blank · 18 comment · 12 complexity · 43067ec27ce2e1f7a45f352cbaaf7bf8 MD5 · raw file

  1. /*
  2. * Copyright 2000-2012 JetBrains s.r.o.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.intellij.openapi.roots.impl.libraries;
  17. import com.intellij.ide.IdeBundle;
  18. import com.intellij.ide.TitledHandler;
  19. import com.intellij.openapi.actionSystem.DataContext;
  20. import com.intellij.openapi.actionSystem.LangDataKeys;
  21. import com.intellij.openapi.application.ApplicationManager;
  22. import com.intellij.openapi.command.CommandProcessor;
  23. import com.intellij.openapi.command.undo.BasicUndoableAction;
  24. import com.intellij.openapi.command.undo.UndoManager;
  25. import com.intellij.openapi.command.undo.UndoableAction;
  26. import com.intellij.openapi.command.undo.UnexpectedUndoException;
  27. import com.intellij.openapi.diagnostic.Logger;
  28. import com.intellij.openapi.editor.Editor;
  29. import com.intellij.openapi.project.Project;
  30. import com.intellij.openapi.roots.libraries.Library;
  31. import com.intellij.openapi.ui.InputValidator;
  32. import com.intellij.openapi.ui.Messages;
  33. import com.intellij.openapi.util.Ref;
  34. import com.intellij.psi.PsiElement;
  35. import com.intellij.psi.PsiFile;
  36. import com.intellij.refactoring.rename.RenameHandler;
  37. import org.jetbrains.annotations.NotNull;
  38. import org.jetbrains.annotations.Nullable;
  39. /**
  40. * @author Konstantin Bulenkov
  41. */
  42. public class RenameLibraryHandler implements RenameHandler, TitledHandler {
  43. private static final Logger LOG = Logger.getInstance("#com.intellij.ide.projectView.actions.RenameModuleHandler");
  44. public boolean isAvailableOnDataContext(DataContext dataContext) {
  45. Library library = LangDataKeys.LIBRARY.getData(dataContext);
  46. return library != null;
  47. }
  48. public boolean isRenaming(DataContext dataContext) {
  49. return isAvailableOnDataContext(dataContext);
  50. }
  51. public void invoke(@NotNull Project project, Editor editor, PsiFile file, DataContext dataContext) {
  52. LOG.assertTrue(false);
  53. }
  54. public void invoke(@NotNull final Project project, @NotNull PsiElement[] elements, @NotNull DataContext dataContext) {
  55. final Library library = LangDataKeys.LIBRARY.getData(dataContext);
  56. LOG.assertTrue(library != null);
  57. Messages.showInputDialog(project,
  58. IdeBundle.message("prompt.enter.new.library.name"),
  59. IdeBundle.message("title.rename.library"),
  60. Messages.getQuestionIcon(),
  61. library.getName(),
  62. new MyInputValidator(project, library));
  63. }
  64. public String getActionTitle() {
  65. return IdeBundle.message("title.rename.library");
  66. }
  67. private static class MyInputValidator implements InputValidator {
  68. private final Project myProject;
  69. private final Library myLibrary;
  70. public MyInputValidator(Project project, Library library) {
  71. myProject = project;
  72. myLibrary = library;
  73. }
  74. public boolean checkInput(String inputString) {
  75. return inputString != null && inputString.length() > 0 && myLibrary.getTable().getLibraryByName(inputString) == null;
  76. }
  77. public boolean canClose(final String inputString) {
  78. final String oldName = myLibrary.getName();
  79. final Library.ModifiableModel modifiableModel = renameLibrary(inputString);
  80. if (modifiableModel == null) return false;
  81. final Ref<Boolean> success = Ref.create(Boolean.TRUE);
  82. CommandProcessor.getInstance().executeCommand(myProject, new Runnable() {
  83. public void run() {
  84. UndoableAction action = new BasicUndoableAction() {
  85. public void undo() throws UnexpectedUndoException {
  86. final Library.ModifiableModel modifiableModel = renameLibrary(oldName);
  87. if (modifiableModel != null) {
  88. modifiableModel.commit();
  89. }
  90. }
  91. @Override
  92. public void redo() throws UnexpectedUndoException {
  93. final Library.ModifiableModel modifiableModel = renameLibrary(inputString);
  94. if (modifiableModel != null) {
  95. modifiableModel.commit();
  96. }
  97. }
  98. };
  99. UndoManager.getInstance(myProject).undoableActionPerformed(action);
  100. ApplicationManager.getApplication().runWriteAction(new Runnable() {
  101. public void run() {
  102. modifiableModel.commit();
  103. }
  104. });
  105. }
  106. }, IdeBundle.message("command.renaming.module", oldName), null);
  107. return success.get().booleanValue();
  108. }
  109. @Nullable
  110. private Library.ModifiableModel renameLibrary(String inputString) {
  111. final Library.ModifiableModel modifiableModel = myLibrary.getModifiableModel();
  112. modifiableModel.setName(inputString);
  113. return modifiableModel;
  114. }
  115. }
  116. }