1 /**************************************************************************
2 Copyright 2005 Webstersmalley
3
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
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
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.
15 *************************************************************************/
16
17
18 package com.webstersmalley.picweb.utils;
19
20 import java.io.File;
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.Iterator;
24 import java.util.List;
25
26
27 /***
28 * @author Matthew Smalley
29 */
30 public class FolderContents
31 {
32 private static FolderFileFilter folderFileFilter = new FolderFileFilter();
33 private static PictureFilenameFilter pictureFilenameFilter = new PictureFilenameFilter();
34
35 private String rootPath;
36 private String relativePath;
37 private String path;
38
39 private List folders;
40 private List pictures;
41 private int pictureCount = 0;
42 private int folderDepth;
43 private String topName;
44
45 public FolderContents(String rootPath)
46 {
47 this.rootPath = rootPath;
48 this.relativePath = ".";
49 this.path = rootPath + File.separator + relativePath;
50 this.folderDepth = 0;
51 this.topName = "Pictures";
52 recurse();
53 }
54
55 private FolderContents(String rootPath, String relativePath, int folderDepth)
56 {
57 this.rootPath = rootPath;
58 this.relativePath = relativePath;
59 this.path = rootPath + File.separator + relativePath;
60 this.folderDepth = folderDepth;
61 recurse();
62 }
63
64 /***
65 *
66 */
67 private void recurse()
68 {
69 File root = new File(path);
70 if (!root.isDirectory())
71 {
72 throw new RuntimeException("Error - given root path (" + path + ") is not a folder or cannot be found.");
73 }
74 if (topName == null)
75 {
76 topName = root.getName();
77 }
78 topName = topName.replaceAll("'", "");
79 folders = new ArrayList();
80 pictures = new ArrayList();
81
82 File[] childFolders = root.listFiles(folderFileFilter);
83 for (int i = 0; i < childFolders.length; i++)
84 {
85 FolderContents child = new FolderContents(rootPath, relativePath + File.separator + childFolders[i].getName(), folderDepth + 1);
86 folders.add(child);
87 pictureCount += child.getPictureCount();
88 }
89
90 File[] pictureFiles = root.listFiles(pictureFilenameFilter);
91 for (int i = 0; i < pictureFiles.length; i++)
92 {
93 pictures.add(pictureFiles[i]);
94 }
95 pictureCount += pictures.size();
96
97 Collections.sort(folders, new FolderContentsComparator());
98 Collections.sort(pictures);
99 }
100
101 public int getPictureCount()
102 {
103 return pictureCount;
104 }
105
106 public String getRelativePath()
107 {
108 return relativePath;
109 }
110
111 public String toString()
112 {
113 StringBuffer sb = new StringBuffer();
114 Iterator it = folders.iterator();
115 sb.append(getRelativePath());
116 sb.append(" ");
117 sb.append(getPictureCount());
118 sb.append("\n");
119 while (it.hasNext())
120 {
121 FolderContents child = (FolderContents)it.next();
122 sb.append(child.toString());
123 }
124 it = pictures.iterator();
125 while (it.hasNext())
126 {
127 File picture = (File)it.next();
128 sb.append(relativePath);
129 sb.append(File.separator);
130 sb.append(picture.getName());
131 sb.append("\n");
132 }
133 return sb.toString();
134 }
135
136 public String getDisplayName()
137 {
138 return getTopName() + " " + getPictureCount();
139 }
140
141 public static void main(String[] args)
142 {
143 FolderContents contents = new FolderContents("c:/pics");
144 System.out.println(contents);
145 }
146
147 /***
148 * @return
149 */
150 public boolean hasChildren()
151 {
152 return (folders.size() > 0);
153 }
154
155 public List getChildren()
156 {
157 return folders;
158 }
159
160 public List getPictures()
161 {
162 return pictures;
163 }
164
165 public int getFolderDepth()
166 {
167 return folderDepth;
168 }
169
170 public String getTopName()
171 {
172 return topName;
173 }
174 }