KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > project > ProjectExplorerLoader


1 package org.antlr.works.project;
2
3 import org.antlr.xjlib.foundation.XJUtils;
4
5 import java.io.File JavaDoc;
6 import java.util.*;
7 /*
8
9 [The "BSD licence"]
10 Copyright (c) 2005-2006 Jean Bovet
11 All rights reserved.
12
13 Redistribution and use in source and binary forms, with or without
14 modification, are permitted provided that the following conditions
15 are met:
16
17 1. Redistributions of source code must retain the above copyright
18 notice, this list of conditions and the following disclaimer.
19 2. Redistributions in binary form must reproduce the above copyright
20 notice, this list of conditions and the following disclaimer in the
21 documentation and/or other materials provided with the distribution.
22 3. The name of the author may not be used to endorse or promote products
23 derived from this software without specific prior written permission.
24
25 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
36 */

37
38 public class ProjectExplorerLoader {
39
40     protected Map groups = new HashMap();
41     protected String JavaDoc sourcePath;
42     protected ProjectExplorer explorer;
43
44     public ProjectExplorerLoader(ProjectExplorer projectExplorer) {
45         this.explorer = projectExplorer;
46     }
47
48     public boolean reload() {
49         boolean reset = false;
50
51         if(sourcePath == null || !sourcePath.equals(explorer.project.getSourcePath())) {
52             groups.clear();
53             sourcePath = explorer.project.getSourcePath();
54             reset = true;
55         }
56
57         if(sourcePath == null)
58             return reset;
59
60         List filesToRemove = getAllFiles();
61
62         for (Iterator iterator = XJUtils.sortedFilesInPath(sourcePath).iterator(); iterator.hasNext();) {
63             File JavaDoc f = (File JavaDoc) iterator.next();
64             String JavaDoc name = f.getName();
65
66             String JavaDoc type = ProjectFileItem.getFileType(name);
67             if(type.equals(ProjectFileItem.FILE_TYPE_UNKNOWN))
68                 continue;
69
70             ProjectFileItem item = addFileOfType(name, type);
71
72             // Remove this file from the filesToRemove list because it exists
73
filesToRemove.remove(item);
74         }
75
76         // Remove all files that where not existing on disk
77
for (Iterator iterator = filesToRemove.iterator(); iterator.hasNext();) {
78             ProjectFileItem fileItem = (ProjectFileItem) iterator.next();
79             explorer.project.closeFileItem(fileItem);
80             removeFileOfType(fileItem.getFileName(), fileItem.getFileType());
81         }
82
83         sortFilesInGroups();
84
85         return reset;
86     }
87
88     public void sortFilesInGroups() {
89         for (Iterator iterator = groups.keySet().iterator(); iterator.hasNext();) {
90             String JavaDoc name = (String JavaDoc) iterator.next();
91             List files = (List)groups.get(name);
92             if(files != null && !files.isEmpty())
93                 Collections.sort(files);
94         }
95     }
96
97     public ProjectFileItem addFileOfType(String JavaDoc name, String JavaDoc type) {
98         List files = getFiles(type);
99         for (Iterator iterator = files.iterator(); iterator.hasNext();) {
100             ProjectFileItem fileItem = (ProjectFileItem) iterator.next();
101             if(fileItem.getFileName().equals(name))
102                 return fileItem;
103         }
104         ProjectFileItem fileItem = new ProjectFileItem(explorer.project, name);
105         files.add(fileItem);
106         return fileItem;
107     }
108
109     public void removeFileOfType(String JavaDoc name, String JavaDoc type) {
110         List files = getFiles(type);
111         for (Iterator iterator = files.iterator(); iterator.hasNext();) {
112             ProjectFileItem fileItem = (ProjectFileItem) iterator.next();
113             if(fileItem.getFileName().equals(name)) {
114                 files.remove(fileItem);
115                 break;
116             }
117         }
118     }
119
120     public List getGroups() {
121         List sortedGroups = new ArrayList(groups.keySet());
122         Collections.sort(sortedGroups);
123         return sortedGroups;
124     }
125
126     public List getAllFiles() {
127         List allFiles = new ArrayList();
128         for (Iterator iterator = groups.values().iterator(); iterator.hasNext();) {
129             List files = (List) iterator.next();
130             allFiles.addAll(files);
131         }
132         return allFiles;
133     }
134
135     public ProjectFileItem getFileItemForFileName(String JavaDoc filename) {
136         for (Iterator groupIterator = groups.values().iterator(); groupIterator.hasNext();) {
137             List files = (List) groupIterator.next();
138             for (Iterator fileIterator = files.iterator(); fileIterator.hasNext();) {
139                 ProjectFileItem item = (ProjectFileItem) fileIterator.next();
140                 if(item.getFileName().equals(filename))
141                     return item;
142             }
143         }
144         return null;
145     }
146
147     public List getFiles(String JavaDoc name) {
148         List files = (List) groups.get(name);
149         if(files == null) {
150             files = new ArrayList();
151             groups.put(name, files);
152         }
153         return files;
154     }
155
156 }
157
Popular Tags