+ public void setNode(Node node) {
+ Node oldNode = this.node;
+ this.node = node;
+ fireIfPropertyChanged(PROPERTY_NODE, oldNode, node);
+ }
+
+ //
+ // PRIVATE METHODS
+ //
+
+ /**
+ * Scans the given directory and recreates the file and directory structure
+ * in the given project file.
+ *
+ * @param directory
+ * The directory to scan
+ * @param projectFile
+ * The project file in which to recreate the directory and file
+ * structure
+ */
+ private void scanDirectory(File directory, ProjectFileImpl projectFile) {
+ if (!directory.isDirectory()) {
+ return;
+ }
+ for (File file : directory.listFiles()) {
+ ProjectFileImpl projectFileChild = projectFile.addFile(file.getName(), file.length(), file.isDirectory(), file.isHidden());
+ if (file.isDirectory()) {
+ scanDirectory(file, projectFileChild);
+ }
+ }
+ projectFile.sort();
+ }
+
+ /**
+ * Implementation of a {@link ProjectFile}.
+ *
+ * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
+ */
+ private static class ProjectFileImpl implements ProjectFile, Comparable<ProjectFileImpl> {
+
+ /** The parent of this project file. */
+ private final ProjectFileImpl parentProjectFile;
+
+ /** The name of this project file. */
+ private final String name;
+
+ /** The size of the file. */
+ private final long size;
+
+ /** Whether this project file is a directory. */
+ private final boolean directory;
+
+ /** Whether this file is hidden. */
+ private final boolean hidden;
+
+ /** This project file’s children. */
+ private List<ProjectFileImpl> childProjectFiles = new ArrayList<ProjectFileImpl>();
+
+ /**
+ * Creates a new project fie.
+ *
+ * @param parentProjectFile
+ * The parent of the project file, or <code>null</code> if
+ * the new project file does not have a parent
+ * @param name
+ * The name of the project file
+ * @param size
+ * The size of the file
+ * @param isDirectory
+ * <code>true</code> if this project file is a directory,
+ * <code>false</code> otherwise
+ * @param isHidden
+ * <code>true</code> if this project file is hidden,
+ * <code>false</code> otherwise
+ */
+ ProjectFileImpl(ProjectFileImpl parentProjectFile, String name, long size, boolean isDirectory, boolean isHidden) {
+ this.parentProjectFile = parentProjectFile;
+ this.name = name;
+ this.size = size;
+ this.directory = isDirectory;
+ this.hidden = isHidden;
+ }
+
+ //
+ // INTERFACE ProjectFile
+ //
+
+ /**
+ * @see net.pterodactylus.jsite.core.ProjectFile#getName()
+ */
+ public String getName() {
+ return name;
+ }
+
+ /**
+ * @see net.pterodactylus.jsite.core.ProjectFile#getParent()
+ */
+ public ProjectFile getParent() {
+ return parentProjectFile;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public long getSize() {
+ return size;
+ }
+
+ /**
+ * @see net.pterodactylus.jsite.core.ProjectFile#getParents()
+ */
+ public List<ProjectFile> getParents() {
+ List<ProjectFile> parentProjectFiles = new ArrayList<ProjectFile>();
+ ProjectFileImpl currentProjectFile = this;
+ do {
+ parentProjectFiles.add(0, currentProjectFile);
+ } while ((currentProjectFile = currentProjectFile.parentProjectFile) != null);
+ return parentProjectFiles;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ /* TODO - caching? */
+ public String getCompletePath() {
+ StringBuilder completePath = new StringBuilder();
+ ProjectFileImpl currentProjectFile = this;
+ while ((currentProjectFile != null) && (currentProjectFile.parentProjectFile != null)) {
+ completePath.insert(0, currentProjectFile.getName()).insert(0, File.separatorChar);
+ currentProjectFile = currentProjectFile.parentProjectFile;
+ }
+ return (completePath.length() > 0) ? completePath.substring(1) : "";
+ }
+
+ /**
+ * @see net.pterodactylus.jsite.core.ProjectFile#isFile()
+ */
+ public boolean isFile() {
+ return !directory;
+ }
+
+ /**
+ * @see net.pterodactylus.jsite.core.ProjectFile#isDirectory()
+ */
+ public boolean isDirectory() {
+ return directory;
+ }
+
+ /**
+ * @see net.pterodactylus.jsite.core.ProjectFile#isHidden()
+ */
+ public boolean isHidden() {
+ return hidden;
+ }
+
+ /**
+ * Returns the project file with the given name. The project file has to
+ * be a direct child of this project file.
+ *
+ * @param name
+ * The name of the file to get
+ * @return The project file, or <code>null</code> if there is no
+ * project file by that name
+ */
+ public ProjectFileImpl getFile(String name) {
+ if (!isDirectory()) {
+ return null;
+ }
+ for (ProjectFileImpl projectFile : childProjectFiles) {
+ if (projectFile.getName().equals(name)) {
+ return projectFile;
+ }
+ }
+ return null;
+ }
+
+ /**
+ * @see net.pterodactylus.jsite.core.ProjectFile#getFiles()
+ */
+ public List<ProjectFile> getFiles() {
+ List<ProjectFile> projectFiles = new ArrayList<ProjectFile>(childProjectFiles);
+ return projectFiles;
+ }
+
+ //
+ // ACTIONS
+ //
+
+ /**
+ * Adds a new project file as child to this project file.
+ *
+ * @param name
+ * The name of the file
+ * @param size
+ * The size of the file
+ * @param isDirectory
+ * <code>true</code> if the new file is a directory,
+ * <code>false</code> otherwise
+ * @param isHidden
+ * <code>true</code> if the new file is hidden,
+ * <code>false</code> otherwise
+ * @return The created project file
+ */
+ public ProjectFileImpl addFile(String name, long size, boolean isDirectory, boolean isHidden) {
+ ProjectFileImpl newProjectFile = new ProjectFileImpl(this, name, size, isDirectory, isHidden);
+ childProjectFiles.add(newProjectFile);
+ return newProjectFile;
+ }
+
+ /**
+ * Sorts the children of this file.
+ */
+ public void sort() {
+ Collections.sort(childProjectFiles);
+ }
+
+ //
+ // INTERFACE Comparable
+ //
+
+ /**
+ * {@inheritDoc}
+ */
+ public int compareTo(ProjectFileImpl otherProjectFileImpl) {
+ return name.compareTo(otherProjectFileImpl.name);
+ }
+