SVN auth
[fedora-idea.git] / plugins / svn4idea / src / org / jetbrains / idea / svn / SvnFormatSelector.java
blob54f71b3817e3a39581489a2daf7fe54537c00726
1 /*
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;
18 import com.intellij.openapi.application.ApplicationManager;
19 import com.intellij.openapi.project.Project;
20 import com.intellij.openapi.util.Ref;
21 import com.intellij.ui.GuiUtils;
22 import org.jetbrains.annotations.NotNull;
23 import org.jetbrains.annotations.Nullable;
24 import org.jetbrains.idea.svn.dialogs.SvnMapDialog;
25 import org.jetbrains.idea.svn.dialogs.UpgradeFormatDialog;
26 import org.tmatesoft.svn.core.SVNErrorCode;
27 import org.tmatesoft.svn.core.SVNErrorMessage;
28 import org.tmatesoft.svn.core.SVNException;
29 import org.tmatesoft.svn.core.internal.wc.admin.ISVNAdminAreaFactorySelector;
30 import org.tmatesoft.svn.core.internal.wc.admin.SVNAdminAreaFactory;
32 import java.io.File;
33 import java.lang.reflect.InvocationTargetException;
34 import java.util.ArrayList;
35 import java.util.Collection;
36 import java.util.Collections;
37 import java.util.Iterator;
39 public class SvnFormatSelector implements ISVNAdminAreaFactorySelector {
40 public Collection getEnabledFactories(File path, Collection factories, boolean writeAccess) throws SVNException {
41 if (ApplicationManager.getApplication().isUnitTestMode()) {
42 return factories;
45 if (! writeAccess) {
46 return factories;
49 final WorkingCopyFormat format = getWorkingCopyFormat(path);
50 Collection result = format2Factories(format, factories);
52 if (result == null) {
53 final WorkingCopyFormat presetFormat = SvnWorkingCopyFormatHolder.getPresetFormat();
54 if (presetFormat != null) {
55 result = format2Factories(presetFormat, factories);
59 if (result == null) {
60 throw new SVNException(SVNErrorMessage.create(SVNErrorCode.WC_NOT_DIRECTORY));
62 return result;
65 @Nullable
66 static Collection format2Factories(final WorkingCopyFormat format, final Collection factories) {
67 if (WorkingCopyFormat.ONE_DOT_SIX.equals(format)) {
68 return factories;
69 } else if (WorkingCopyFormat.ONE_DOT_FIVE.equals(format)) {
70 return factoriesFor15(factories);
71 } else if (WorkingCopyFormat.ONE_DOT_FOUR.equals(format)) {
72 return factoriesFor14(factories);
73 } else if (WorkingCopyFormat.ONE_DOT_THREE.equals(format)) {
74 return factoriesFor13(factories);
76 return null;
79 private static Collection<SVNAdminAreaFactory> factoriesFor13(final Collection factories) {
80 for (Iterator iterator = factories.iterator(); iterator.hasNext();) {
81 final SVNAdminAreaFactory factory = (SVNAdminAreaFactory) iterator.next();
82 final int supportedVersion = factory.getSupportedVersion();
83 if (WorkingCopyFormat.ONE_DOT_THREE.getFormat() == supportedVersion) {
84 return Collections.singletonList(factory);
87 return Collections.emptyList();
90 private static Collection<SVNAdminAreaFactory> factoriesFor14(final Collection factories) {
91 final Collection<SVNAdminAreaFactory> result = new ArrayList<SVNAdminAreaFactory>(2);
92 for (Iterator iterator = factories.iterator(); iterator.hasNext();) {
93 final SVNAdminAreaFactory factory = (SVNAdminAreaFactory) iterator.next();
94 final int supportedVersion = factory.getSupportedVersion();
95 if ((WorkingCopyFormat.ONE_DOT_FOUR.getFormat() == supportedVersion) ||
96 (WorkingCopyFormat.ONE_DOT_THREE.getFormat() == supportedVersion)) {
97 result.add(factory);
100 return result;
103 private static Collection<SVNAdminAreaFactory> factoriesFor15(final Collection factories) {
104 final Collection<SVNAdminAreaFactory> result = new ArrayList<SVNAdminAreaFactory>(2);
105 for (Iterator iterator = factories.iterator(); iterator.hasNext();) {
106 final SVNAdminAreaFactory factory = (SVNAdminAreaFactory) iterator.next();
107 final int supportedVersion = factory.getSupportedVersion();
108 if ((WorkingCopyFormat.ONE_DOT_FOUR.getFormat() == supportedVersion) ||
109 (WorkingCopyFormat.ONE_DOT_THREE.getFormat() == supportedVersion) ||
110 (WorkingCopyFormat.ONE_DOT_FIVE.getFormat() == supportedVersion)) {
111 result.add(factory);
114 return result;
117 public static String showUpgradeDialog(final File path, final Project project, final boolean display13format, final String mode,
118 @NotNull final Ref<Boolean> wasOk) {
119 assert ! ApplicationManager.getApplication().isUnitTestMode();
120 final String[] newMode = new String[] {mode};
121 try {
122 GuiUtils.runOrInvokeAndWait(new Runnable() {
123 public void run() {
124 wasOk.set(displayUpgradeDialog(project, path, display13format, newMode));
128 catch (InterruptedException e) {
131 catch (InvocationTargetException e) {
134 ApplicationManager.getApplication().getMessageBus().syncPublisher(SvnMapDialog.WC_CONVERTED).run();
135 return newMode[0];
138 public static WorkingCopyFormat getWorkingCopyFormat(final File path) {
139 int format = 0;
140 // it is enough to check parent and this.
141 try {
142 format = SVNAdminAreaFactory.checkWC(path, false);
143 } catch (SVNException e) {
146 try {
147 if (format == 0 && path.getParentFile() != null) {
148 format = SVNAdminAreaFactory.checkWC(path.getParentFile(), false);
150 } catch (SVNException e) {
154 return WorkingCopyFormat.getInstance(format);
157 private static boolean displayUpgradeDialog(Project project, File path, final boolean dispay13format, String[] newMode) {
158 UpgradeFormatDialog dialog = new UpgradeFormatDialog(project, path, false);
159 dialog.setData(dispay13format, newMode[0]);
160 dialog.show();
161 if (dialog.isOK()) {
162 newMode[0] = dialog.getUpgradeMode();
164 return dialog.isOK();