2 * Copyright 2000-2009 JetBrains s.r.o.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.jetbrains
.idea
.svn
.actions
;
18 import com
.intellij
.openapi
.actionSystem
.*;
19 import com
.intellij
.openapi
.project
.Project
;
20 import com
.intellij
.openapi
.vcs
.AbstractVcsHelper
;
21 import com
.intellij
.openapi
.vcs
.ProjectLevelVcsManager
;
22 import com
.intellij
.openapi
.vcs
.VcsDirectoryMapping
;
23 import com
.intellij
.openapi
.vcs
.VcsException
;
24 import com
.intellij
.openapi
.vcs
.changes
.VcsDirtyScopeManager
;
25 import com
.intellij
.openapi
.vfs
.VirtualFile
;
26 import org
.jetbrains
.idea
.svn
.SvnBundle
;
27 import org
.jetbrains
.idea
.svn
.SvnStatusUtil
;
28 import org
.jetbrains
.idea
.svn
.SvnUtil
;
29 import org
.jetbrains
.idea
.svn
.SvnVcs
;
31 import java
.util
.Arrays
;
32 import java
.util
.List
;
34 public class ShareWholeProject
extends AnAction
{
36 public void update(final AnActionEvent e
) {
37 final MyChecker checker
= new MyChecker();
40 final Presentation presentation
= e
.getPresentation();
41 presentation
.setEnabled(checker
.isEnabled());
43 presentation
.setVisible(checker
.isVisible());
44 if (checker
.isEnabled()) {
45 presentation
.setText(SvnBundle
.message("action.share.whole.project.text"));
49 private static class MyChecker
{
50 private boolean myEnabled
;
51 private boolean myVisible
;
52 private Project myProject
;
53 private boolean myHadNoMappings
;
55 public void execute(final AnActionEvent e
) {
56 final DataContext dataContext
= e
.getDataContext();
57 myProject
= PlatformDataKeys
.PROJECT
.getData(dataContext
);
58 if (myProject
== null || myProject
.isDefault()) {
62 final VirtualFile baseDir
= myProject
.getBaseDir();
63 if (baseDir
== null) return;
65 final ProjectLevelVcsManager vcsManager
= ProjectLevelVcsManager
.getInstance(myProject
);
66 final MyCheckResult result
= checkMappings(baseDir
, vcsManager
);
68 if (MyCheckResult
.disable
.equals(result
)) return;
70 myHadNoMappings
= MyCheckResult
.notMapped
.equals(result
);
71 if (MyCheckResult
.notMapped
.equals(result
)) {
72 // no change list manager working
73 if(SvnUtil
.seemsLikeVersionedDir(baseDir
)) return;
74 } else if (SvnStatusUtil
.isUnderControl(myProject
, baseDir
)) {
78 if ((! myHadNoMappings
) && (! SvnVcs
.getInstance(myProject
).getSvnFileUrlMapping().isEmpty())) {
79 // there are some versioned dirs under project dir
83 // visible: already checked above
85 myEnabled
= (! vcsManager
.isBackgroundVcsOperationRunning());
88 private static enum MyCheckResult
{
94 private MyCheckResult
checkMappings(final VirtualFile baseDir
, final ProjectLevelVcsManager vcsManager
) {
95 final List
<VcsDirectoryMapping
> mappings
= vcsManager
.getDirectoryMappings();
97 boolean notMapped
= true;
98 boolean svnMappedToBase
= false;
99 for (VcsDirectoryMapping mapping
: mappings
) {
100 final String vcs
= mapping
.getVcs();
101 if (vcs
!= null && vcs
.length() > 0) {
103 if (SvnVcs
.VCS_NAME
.equals(vcs
)) {
104 if (mapping
.isDefaultMapping() || baseDir
.equals(mapping
.getDirectory())) {
105 svnMappedToBase
= true;
112 return svnMappedToBase ? MyCheckResult
.rootToSvn
:
113 (notMapped ? MyCheckResult
.notMapped
: MyCheckResult
.disable
);
116 public boolean isEnabled() {
120 public boolean isVisible() {
124 public Project
getProject() {
128 public boolean isHadNoMappings() {
129 return myHadNoMappings
;
134 public void actionPerformed(AnActionEvent e
) {
135 final MyChecker checker
= new MyChecker();
137 if (! checker
.isEnabled()) return;
139 final Project project
= checker
.getProject();
140 final VirtualFile baseDir
= project
.getBaseDir();
141 if (baseDir
== null) return;
142 boolean success
= false;
143 boolean excThrown
= false;
145 success
= ShareProjectAction
.share(project
, baseDir
);
146 } catch (VcsException exc
) {
147 AbstractVcsHelper
.getInstance(project
).showError(exc
, "Failed to Share Project");
150 // if success = false -> either action was cancelled or exception was thrown, so also check for exception
151 if (success
|| excThrown
) {
152 baseDir
.refresh(true, true, new Runnable() {
154 VcsDirtyScopeManager
.getInstance(project
).dirDirtyRecursively(project
.getBaseDir());
155 if (checker
.isHadNoMappings() && SvnUtil
.seemsLikeVersionedDir(baseDir
)) {
156 final ProjectLevelVcsManager vcsManager
= ProjectLevelVcsManager
.getInstance(project
);
157 vcsManager
.setDirectoryMappings(Arrays
.asList(new VcsDirectoryMapping("", SvnVcs
.VCS_NAME
)));