PageRenderTime 21ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/src/org/bitbucket/connectors/jetbrains/BitbucketShareAction.java

https://bitbucket.org/atlassian/jetbrains-bitbucket-connector/
Java | 133 lines | 111 code | 17 blank | 5 comment | 26 complexity | 8196d1937430a27bd57451bdf15f1e63 MD5 | raw file
  1. package org.bitbucket.connectors.jetbrains;
  2. import com.intellij.openapi.actionSystem.AnActionEvent;
  3. import com.intellij.openapi.actionSystem.PlatformDataKeys;
  4. import com.intellij.openapi.application.ApplicationManager;
  5. import com.intellij.openapi.fileEditor.FileDocumentManager;
  6. import com.intellij.openapi.project.DumbAwareAction;
  7. import com.intellij.openapi.project.Project;
  8. import com.intellij.openapi.ui.Messages;
  9. import com.intellij.openapi.vcs.AbstractVcs;
  10. import com.intellij.openapi.vcs.ProjectLevelVcsManager;
  11. import com.intellij.openapi.vcs.VcsDirectoryMapping;
  12. import com.intellij.openapi.vcs.changes.VcsDirtyScopeManager;
  13. import com.intellij.openapi.vfs.VirtualFile;
  14. import org.bitbucket.connectors.jetbrains.ui.BitbucketBundle;
  15. import org.bitbucket.connectors.jetbrains.ui.BitbucketShareDialog;
  16. import org.bitbucket.connectors.jetbrains.vcs.GitHandler;
  17. import org.bitbucket.connectors.jetbrains.vcs.HgHandler;
  18. import org.bitbucket.connectors.jetbrains.vcs.VcsHandler;
  19. import org.jetbrains.annotations.NotNull;
  20. import java.util.*;
  21. /**
  22. * User: leha2000
  23. * Date: Apr 15, 2011
  24. * Time: 12:16:00 PM
  25. */
  26. public class BitbucketShareAction extends DumbAwareAction {
  27. public BitbucketShareAction() {
  28. super(BitbucketBundle.message("share-on-bitbucket"), BitbucketBundle.message("share-on-bitbucket"), BitbucketUtil.ICON);
  29. }
  30. public void update(AnActionEvent e) {
  31. Project project = e.getData(PlatformDataKeys.PROJECT);
  32. boolean enabled = project != null && !project.isDefault();
  33. e.getPresentation().setVisible(enabled);
  34. e.getPresentation().setEnabled(enabled);
  35. }
  36. @Override
  37. public void actionPerformed(AnActionEvent e) {
  38. ApplicationManager.getApplication().runWriteAction(new Runnable() {
  39. public void run() {
  40. FileDocumentManager.getInstance().saveAllDocuments();
  41. }
  42. });
  43. Project project = e.getData(PlatformDataKeys.PROJECT);
  44. Set<String> names = new HashSet<String>();
  45. for (RepositoryInfo r : BitbucketUtil.getRepositories(project, true)) {
  46. names.add(r.getName());
  47. }
  48. BitbucketShareDialog dialog = new BitbucketShareDialog(project, names, true);
  49. final VirtualFile root = project.getBaseDir();
  50. boolean hg = new HgHandler().getRepositoryRoot(root) != null;
  51. boolean git = new GitHandler().getRepositoryRoot(root) != null;
  52. if (hg) {
  53. dialog.enableGit(false);
  54. } else if (git) {
  55. dialog.setGit(true);
  56. dialog.enableGit(false);
  57. }
  58. dialog.show();
  59. if (!dialog.isOK()) {
  60. return;
  61. }
  62. share(project, dialog.getRepositoryName(), dialog.getDescription(), dialog.isSshRepositoryAccess(), dialog.isGit());
  63. }
  64. private void share(final Project project, String name, String description, boolean ssh, boolean git) {
  65. final VirtualFile root = project.getBaseDir();
  66. final VcsHandler vcsHandler = git ? new GitHandler() : new HgHandler();
  67. VirtualFile vcsRoot = vcsHandler.getRepositoryRoot(root);
  68. if (vcsRoot == null) {
  69. vcsHandler.initRepository(project, root);
  70. } else if (vcsRoot != root) {
  71. Messages.showErrorDialog(project, BitbucketBundle.message("publish-part-err"), BitbucketBundle.message("share-project-on-bitbucket"));
  72. return;
  73. }
  74. refreshAndConfigureVcsMappings(project, root, "", vcsHandler.getVcs(project));
  75. BitbucketUtil.share(project, root, name, description, ssh, git, vcsHandler);
  76. }
  77. public static void refreshAndConfigureVcsMappings(final Project project, final VirtualFile root, final String path, final AbstractVcs vcs) {
  78. root.refresh(false, false);
  79. ProjectLevelVcsManager vcsManager = ProjectLevelVcsManager.getInstance(project);
  80. final List<VcsDirectoryMapping> vcsDirectoryMappings = new ArrayList<VcsDirectoryMapping>(vcsManager.getDirectoryMappings());
  81. VcsDirectoryMapping mapping = new VcsDirectoryMapping(path, vcs.getName());
  82. for (int i = 0; i < vcsDirectoryMappings.size(); i++) {
  83. VcsDirectoryMapping m = vcsDirectoryMappings.get(i);
  84. if (m.getDirectory().equals(path)) {
  85. if (m.getVcs().length() == 0) {
  86. vcsDirectoryMappings.set(i, mapping);
  87. mapping = null;
  88. break;
  89. } else if (m.getVcs().equals(mapping.getVcs())) {
  90. mapping = null;
  91. break;
  92. }
  93. }
  94. }
  95. if (mapping != null) {
  96. vcsDirectoryMappings.add(mapping);
  97. }
  98. vcsManager.setDirectoryMappings(vcsDirectoryMappings);
  99. vcsManager.updateActiveVcss();
  100. refreshFiles(project, Collections.singleton(root));
  101. }
  102. public static void refreshFiles(@NotNull final Project project, @NotNull final Collection<VirtualFile> affectedFiles) {
  103. final VcsDirtyScopeManager dirty = VcsDirtyScopeManager.getInstance(project);
  104. for (VirtualFile file : affectedFiles) {
  105. if (!file.isValid()) {
  106. continue;
  107. }
  108. file.refresh(false, true);
  109. if (file.isDirectory()) {
  110. dirty.dirDirtyRecursively(file);
  111. } else {
  112. dirty.fileDirty(file);
  113. }
  114. }
  115. }
  116. }