Improve error reporting in the branch dialog
[egit/imyousuf.git] / org.spearce.egit.core / src / org / spearce / egit / core / AdaptableFileTreeIterator.java
blob61211f64060611deeac37815028c1f1078921074
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 * See LICENSE for the full license text, also available.
7 *******************************************************************************/
9 package org.spearce.egit.core;
11 import java.io.File;
12 import java.io.IOException;
14 import org.eclipse.core.resources.IContainer;
15 import org.eclipse.core.resources.IWorkspaceRoot;
16 import org.eclipse.core.runtime.Path;
17 import org.spearce.jgit.errors.IncorrectObjectTypeException;
18 import org.spearce.jgit.lib.Repository;
19 import org.spearce.jgit.treewalk.AbstractTreeIterator;
20 import org.spearce.jgit.treewalk.FileTreeIterator;
22 /**
23 * Java IO file tree iterator that can adapt to a {@link ContainerTreeIterator}
24 * <p>
25 * The iterator automatically adapts to a {@link ContainerTreeIterator} when
26 * recursing into directories that are accessible from the given workspace root.
28 * @see org.spearce.jgit.treewalk.FileTreeIterator
29 * @see org.spearce.egit.core.ContainerTreeIterator
31 public class AdaptableFileTreeIterator extends FileTreeIterator {
33 IWorkspaceRoot root;
35 /**
36 * Create a new iterator to traverse the given directory and its children
37 * <p>
38 * The iterator will automatically adapt to a {@link ContainerTreeIterator}
39 * when encountering directories what can be mapped into the given workspace
40 * root.
42 * @param path
43 * the starting directory. This directory should correspond to
44 * the repository root.
45 * @param workspaceRoot
46 * the workspace root to check resource mapping against.
49 public AdaptableFileTreeIterator(final File path,
50 final IWorkspaceRoot workspaceRoot) {
51 super(path);
52 root = workspaceRoot;
55 /**
56 * Create a new iterator to traverse a subdirectory.
57 * <p>
58 * The iterator will automatically adapt to a {@link ContainerTreeIterator}
59 * when encountering directories what can be mapped into the given workspace
60 * root.
62 * @param path
63 * the subdirectory. This should be a directory contained within
64 * the parent directory.
65 * @param parent
66 * the parent iterator we were created from.
67 * @param workspaceRoot
68 * the workspace root to check resource mapping against.
70 protected AdaptableFileTreeIterator(final AdaptableFileTreeIterator parent,
71 File path, final IWorkspaceRoot workspaceRoot) {
72 super(parent, path);
73 root = workspaceRoot;
76 @Override
77 public AbstractTreeIterator createSubtreeIterator(Repository repo)
78 throws IncorrectObjectTypeException, IOException {
79 final File currentFile = ((FileEntry) current()).getFile();
80 final IContainer[] containers = root.findContainersForLocation(new Path(
81 currentFile.getAbsolutePath()));
82 if (containers.length > 0)
83 return new ContainerTreeIterator(this, containers[0]);
84 else
85 return new AdaptableFileTreeIterator(this, currentFile, root);