KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > persistence > wizard > fromdb > DBSchemaFileList


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.persistence.wizard.fromdb;
21
22 import java.io.File JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.Comparator JavaDoc;
26 import java.util.Enumeration JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30 import org.netbeans.api.project.Project;
31 import org.netbeans.api.project.SourceGroup;
32 import org.openide.filesystems.FileObject;
33 import org.openide.filesystems.FileUtil;
34 import org.openide.util.NbBundle;
35
36 /**
37  *
38  * @author Andrei Badea
39  */

40 public class DBSchemaFileList {
41
42     private final Map JavaDoc<FileObject,String JavaDoc> dbschema2DisplayName = new HashMap JavaDoc<FileObject,String JavaDoc>();
43     private final List JavaDoc dbschemaList;
44
45     public DBSchemaFileList(Project project, FileObject configFilesFolder) {
46         SourceGroup[] sourceGroups = SourceGroupSupport.getJavaSourceGroups(project);
47
48         // XXX this recursive search is a potential performance problem
49
for (int i = 0; i < sourceGroups.length; i++) {
50             searchRoot(sourceGroups[i].getRootFolder(), sourceGroups[i].getDisplayName());
51         }
52
53         if (configFilesFolder != null) {
54             String JavaDoc configFilesDisplayName = NbBundle.getMessage(DBSchemaFileList.class, "LBL_Node_DocBase");
55             searchRoot(configFilesFolder, configFilesDisplayName);
56         }
57
58         List JavaDoc tempDBSchemaList = new ArrayList JavaDoc(dbschema2DisplayName.keySet());
59         Collections.sort(tempDBSchemaList, new DBSchemaComparator());
60
61         dbschemaList = Collections.unmodifiableList(tempDBSchemaList);
62     }
63
64     private void searchRoot(FileObject root, String JavaDoc rootDisplayName) {
65         Enumeration JavaDoc ch = root.getChildren(true);
66         while (ch.hasMoreElements()) {
67             FileObject f = (FileObject) ch.nextElement();
68             if (f.getExt().equals(DBSchemaManager.DBSCHEMA_EXT) && !f.isFolder()) {
69                 if (!dbschema2DisplayName.containsKey(f)) {
70                     String JavaDoc relativeParent = FileUtil.getRelativePath(root, f.getParent()) + File.separator;
71                     if (relativeParent.startsWith("/")) { // NOI18N
72
relativeParent = relativeParent.substring(1);
73                     }
74                     String JavaDoc relative = relativeParent + f.getName();
75                     String JavaDoc displayName = NbBundle.getMessage(DBSchemaFileList.class,
76                             "LBL_SchemaLocation", rootDisplayName, relative);
77                     dbschema2DisplayName.put(f, displayName);
78                 }
79             }
80         }
81     }
82
83     public List JavaDoc<FileObject> getFileList() {
84         return dbschemaList;
85     }
86
87     public String JavaDoc getDisplayName(FileObject dbschemaFile) {
88         return dbschema2DisplayName.get(dbschemaFile);
89     }
90
91     private final class DBSchemaComparator implements Comparator JavaDoc {
92
93         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
94             FileObject f1 = (FileObject)o1;
95             FileObject f2 = (FileObject)o2;
96
97             String JavaDoc displayName1 = dbschema2DisplayName.get(f1);
98             String JavaDoc displayName2 = dbschema2DisplayName.get(f2);
99
100             return displayName1.compareTo(displayName2);
101         }
102     }
103 }
104
Popular Tags