update copyright
[fedora-idea.git] / plugins / svn4idea / src / org / jetbrains / idea / svn / dialogs / browserCache / SyntheticWorker.java
blob5ee07d8db8fe82375ce4df7bcf425b131aa1246a
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.dialogs.browserCache;
18 import com.intellij.util.NotNullFunction;
19 import org.jetbrains.annotations.NotNull;
20 import org.jetbrains.idea.svn.dialogs.RepositoryTreeNode;
21 import org.tmatesoft.svn.core.SVNDirEntry;
22 import org.tmatesoft.svn.core.SVNException;
23 import org.tmatesoft.svn.core.SVNNodeKind;
24 import org.tmatesoft.svn.core.SVNURL;
25 import org.tmatesoft.svn.core.wc.SVNRevision;
27 import java.util.*;
29 public class SyntheticWorker {
30 private final SvnRepositoryCache myCache;
31 private final SVNURL myUrl;
33 public SyntheticWorker(final SVNURL url) {
34 myCache = SvnRepositoryCache.getInstance();
35 myUrl = url;
38 public void removeSelf() {
39 final String parentUrl;
40 try {
41 parentUrl = myUrl.removePathTail().toString();
43 catch (SVNException e) {
44 return;
47 final List<SVNDirEntry> children = myCache.getChildren(parentUrl);
48 if (children == null) {
49 return;
51 for (Iterator<SVNDirEntry> iterator = children.iterator(); iterator.hasNext();) {
52 final SVNDirEntry entry = iterator.next();
53 if (myUrl.equals(entry.getURL())) {
54 iterator.remove();
57 myCache.put(parentUrl, children);
60 public void addSyntheticChildToSelf(final SVNURL newUrl, final SVNURL repositoryUrl, final String name, final boolean isDir) {
61 final String currentUrlAsString = myUrl.toString();
63 final List<SVNDirEntry> children = myCache.getChildren(currentUrlAsString);
64 if (children == null) {
65 return;
67 children.add(createSyntheticEntry(newUrl, repositoryUrl, name, isDir));
69 Collections.sort(children, new Comparator<SVNDirEntry>() {
70 public int compare(final SVNDirEntry o1, final SVNDirEntry o2) {
71 final boolean dirStatus = SVNNodeKind.DIR.equals(o1.getKind()) ^ SVNNodeKind.DIR.equals(o1.getKind());
72 if (dirStatus) {
73 return SVNNodeKind.DIR.equals(o1.getKind()) ? -1 : 1;
75 return o1.toString().compareTo(o2.toString());
77 });
78 myCache.put(currentUrlAsString, children);
81 public void copyTreeToSelf(final RepositoryTreeNode node) {
82 try {
83 node.doOnSubtree(new Adder(node.getURL().removePathTail().toString().length(), myUrl));
85 catch (SVNException e) {
90 public static void removeTreeOf(final RepositoryTreeNode node) {
91 node.doOnSubtree(new Remover());
94 public static SVNDirEntry createSyntheticEntry(final SVNURL newUrl, final SVNURL repositoryUrl, final String name, final boolean isDir) {
95 return new SVNDirEntry(newUrl, repositoryUrl, name, isDir ? SVNNodeKind.DIR : SVNNodeKind.FILE, 0, false, SVNRevision.UNDEFINED.getNumber(), null, null);
98 private static class Remover implements NotNullFunction<RepositoryTreeNode, Object> {
99 private final SvnRepositoryCache myCache = SvnRepositoryCache.getInstance();
101 @NotNull
102 public Object fun(final RepositoryTreeNode repositoryTreeNode) {
103 myCache.remove(repositoryTreeNode.getURL().toString());
104 return Boolean.FALSE;
108 private class Adder implements NotNullFunction<RepositoryTreeNode, Object> {
109 private final int myOldPrefixLen;
110 private final SVNURL myNewParentUrl;
112 private Adder(final int oldPrefixLen, final SVNURL newParentUrl) {
113 myOldPrefixLen = oldPrefixLen;
114 myNewParentUrl = newParentUrl;
117 @NotNull
118 public Object fun(final RepositoryTreeNode repositoryTreeNode) {
119 final List<SVNDirEntry> children = myCache.getChildren(repositoryTreeNode.getURL().toString());
120 if (children == null) {
121 return Boolean.FALSE;
123 final List<SVNDirEntry> newChildren = new ArrayList<SVNDirEntry>(children.size());
125 try {
126 for (SVNDirEntry child : children) {
127 newChildren.add(createSyntheticEntry(convertUrl(child.getURL()), child.getRepositoryRoot(), child.getName(), SVNNodeKind.DIR.equals(child.getKind())));
129 myCache.put(convertUrl(repositoryTreeNode.getURL()).toString(), newChildren);
131 catch (SVNException e) {
134 return Boolean.FALSE;
137 private SVNURL convertUrl(final SVNURL currentUrl) throws SVNException {
138 return myNewParentUrl.appendPath(currentUrl.toString().substring(myOldPrefixLen), true);