Initial EGit contribution to eclipse.org
[egit.git] / org.eclipse.egit.core / src / org / eclipse / egit / core / AdaptableFileTreeIterator.java
blob3fbab868a85434b00704e45889ae9807b22e2347
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;
23 /**
24 * Java IO file tree iterator that can adapt to a {@link ContainerTreeIterator}
25 * <p>
26 * The iterator automatically adapts to a {@link ContainerTreeIterator} when
27 * recursing into directories that are accessible from the given workspace root.
29 * @see org.eclipse.jgit.treewalk.FileTreeIterator
30 * @see org.eclipse.egit.core.ContainerTreeIterator
32 public class AdaptableFileTreeIterator extends FileTreeIterator {
34 IWorkspaceRoot root;
36 /**
37 * Create a new iterator to traverse the given directory and its children
38 * <p>
39 * The iterator will automatically adapt to a {@link ContainerTreeIterator}
40 * when encountering directories what can be mapped into the given workspace
41 * root.
43 * @param path
44 * the starting directory. This directory should correspond to
45 * the repository root.
46 * @param workspaceRoot
47 * the workspace root to check resource mapping against.
50 public AdaptableFileTreeIterator(final File path,
51 final IWorkspaceRoot workspaceRoot) {
52 super(path);
53 root = workspaceRoot;
56 /**
57 * Create a new iterator to traverse a subdirectory.
58 * <p>
59 * The iterator will automatically adapt to a {@link ContainerTreeIterator}
60 * when encountering directories what can be mapped into the given workspace
61 * root.
63 * @param path
64 * the subdirectory. This should be a directory contained within
65 * the parent directory.
66 * @param parent
67 * the parent iterator we were created from.
68 * @param workspaceRoot
69 * the workspace root to check resource mapping against.
71 protected AdaptableFileTreeIterator(final AdaptableFileTreeIterator parent,
72 File path, final IWorkspaceRoot workspaceRoot) {
73 super(parent, path);
74 root = workspaceRoot;
77 @Override
78 public AbstractTreeIterator createSubtreeIterator(Repository repo)
79 throws IncorrectObjectTypeException, IOException {
80 final File currentFile = ((FileEntry) current()).getFile();
81 final IContainer[] containers = root.findContainersForLocation(new Path(
82 currentFile.getAbsolutePath()));
83 if (containers.length > 0)
84 return new ContainerTreeIterator(this, containers[0]);
85 else
86 return new AdaptableFileTreeIterator(this, currentFile, root);