Fix build
[egit.git] / org.eclipse.egit.core / src / org / eclipse / egit / core / AdaptableFileTreeIterator.java
blob466f7be084f0d9d6ccebdc17b6d2b06c97bc09a7
1 /*******************************************************************************
2 * Copyright (C) 2009, Tor Arne Vestbø <torarnv@gmail.com>
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *******************************************************************************/
10 package org.eclipse.egit.core;
12 import java.io.File;
13 import java.io.IOException;
15 import org.eclipse.core.resources.IContainer;
16 import org.eclipse.core.resources.IWorkspaceRoot;
17 import org.eclipse.core.runtime.Path;
18 import org.eclipse.jgit.errors.IncorrectObjectTypeException;
19 import org.eclipse.jgit.lib.Repository;
20 import org.eclipse.jgit.treewalk.AbstractTreeIterator;
21 import org.eclipse.jgit.treewalk.FileTreeIterator;
22 import org.eclipse.jgit.util.FS;
24 /**
25 * Java IO file tree iterator that can adapt to a {@link ContainerTreeIterator}
26 * <p>
27 * The iterator automatically adapts to a {@link ContainerTreeIterator} when
28 * recursing into directories that are accessible from the given workspace root.
30 * @see org.eclipse.jgit.treewalk.FileTreeIterator
31 * @see org.eclipse.egit.core.ContainerTreeIterator
33 public class AdaptableFileTreeIterator extends FileTreeIterator {
35 IWorkspaceRoot root;
37 /**
38 * Create a new iterator to traverse the given directory and its children
39 * <p>
40 * The iterator will automatically adapt to a {@link ContainerTreeIterator}
41 * when encountering directories what can be mapped into the given workspace
42 * root.
44 * @param path
45 * the starting directory. This directory should correspond to
46 * the repository root.
47 * @param workspaceRoot
48 * the workspace root to check resource mapping against.
51 public AdaptableFileTreeIterator(final File path,
52 final IWorkspaceRoot workspaceRoot) {
53 super(path, FS.DETECTED);
54 root = workspaceRoot;
57 /**
58 * Create a new iterator to traverse a subdirectory.
59 * <p>
60 * The iterator will automatically adapt to a {@link ContainerTreeIterator}
61 * when encountering directories what can be mapped into the given workspace
62 * root.
64 * @param path
65 * the subdirectory. This should be a directory contained within
66 * the parent directory.
67 * @param parent
68 * the parent iterator we were created from.
69 * @param workspaceRoot
70 * the workspace root to check resource mapping against.
72 protected AdaptableFileTreeIterator(final AdaptableFileTreeIterator parent,
73 File path, final IWorkspaceRoot workspaceRoot) {
74 super(parent, path, FS.DETECTED);
75 root = workspaceRoot;
78 @Override
79 public AbstractTreeIterator createSubtreeIterator(Repository repo)
80 throws IncorrectObjectTypeException, IOException {
81 final File currentFile = ((FileEntry) current()).getFile();
82 final IContainer[] containers = root.findContainersForLocation(new Path(
83 currentFile.getAbsolutePath()));
84 if (containers.length > 0)
85 return new ContainerTreeIterator(this, containers[0]);
86 else
87 return new AdaptableFileTreeIterator(this, currentFile, root);