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