KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > project > PackageDisplayUtils


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.java.project;
21
22 import java.awt.Image JavaDoc;
23 import org.netbeans.api.java.queries.AccessibilityQuery;
24 import org.netbeans.api.queries.VisibilityQuery;
25 import org.openide.filesystems.FileObject;
26 import org.openide.util.NbBundle;
27 import org.openide.util.Utilities;
28
29 // XXX needs unit test
30

31 /**
32  * Provides display name and icon utilities for
33  * {@link PackageViewChildren.PackageNode} and {@link PackageListView.PackageItem}.
34  * @author Jesse Glick
35  */

36 public final class PackageDisplayUtils {
37
38     private PackageDisplayUtils() {}
39     
40     /** whether to turn on #42589 */
41     private static final boolean TRUNCATE_PACKAGE_NAMES =
42         Boolean.getBoolean("org.netbeans.spi.java.project.support.ui.packageView.TRUNCATE_PACKAGE_NAMES"); // NOI18N
43

44     private static final Image JavaDoc PACKAGE = Utilities.loadImage("org/netbeans/spi/java/project/support/ui/package.gif"); // NOI18N
45
private static final Image JavaDoc PACKAGE_EMPTY = Utilities.loadImage("org/netbeans/spi/java/project/support/ui/packageEmpty.gif"); // NOI18N
46
private static final Image JavaDoc PACKAGE_PRIVATE = Utilities.loadImage("org/netbeans/spi/java/project/support/ui/packagePrivate.gif"); // NOI18N
47
private static final Image JavaDoc PACKAGE_PUBLIC = Utilities.loadImage("org/netbeans/spi/java/project/support/ui/packagePublic.gif"); // NOI18N
48

49     /**
50      * Find the proper display label for a package.
51      * @param pkg the actual folder
52      * @param pkgname the dot-separated package name (<code>""</code> for default package)
53      * @return an appropriate display label for it
54      */

55     public static String JavaDoc getDisplayLabel(String JavaDoc pkgname) {
56         return computePackageName(pkgname, TRUNCATE_PACKAGE_NAMES);
57     }
58     
59     /**
60      * Find the proper tool tip for a package.
61      * May have more info than the display label.
62      * @param pkg the actual folder
63      * @param pkgname the dot-separated package name (<code>""</code> for default package)
64      * @return an appropriate display label for it
65      */

66     public static String JavaDoc getToolTip(FileObject pkg, String JavaDoc pkgname) {
67         String JavaDoc pkglabel = computePackageName(pkgname, false);
68         Boolean JavaDoc b = AccessibilityQuery.isPubliclyAccessible(pkg);
69         if (b != null) {
70             if (b.booleanValue()) {
71                 return NbBundle.getMessage(PackageDisplayUtils.class, "LBL_public_package", pkglabel);
72             } else {
73                 return NbBundle.getMessage(PackageDisplayUtils.class, "LBL_private_package", pkglabel);
74             }
75         } else {
76             return NbBundle.getMessage(PackageDisplayUtils.class, "LBL_package", pkglabel);
77         }
78     }
79     
80     /**
81      * Get package name.
82      * Handles default package specially.
83      * @param truncate if true, show a truncated version to save display space
84      */

85     private static String JavaDoc computePackageName(String JavaDoc pkgname, boolean truncate) {
86         if (pkgname.length() == 0) {
87             return NbBundle.getMessage(PackageDisplayUtils.class, "LBL_DefaultPackage"); // NOI18N
88
} else {
89             if (truncate) {
90                 // #42589: keep only first letter of first package component, up to three of others
91
return pkgname.replaceFirst("^([^.])[^.]+\\.", "$1.").replaceAll("([^.]{3})[^.]+\\.", "$1."); // NOI18N
92
} else {
93                 return pkgname;
94             }
95         }
96     }
97
98      
99     
100     /**
101      * Find the proper display icon for a package.
102      * @param pkg the actual folder
103      * @param pkgname the dot-separated package name (<code>""</code> for default package)
104      * @return an appropriate display icon for it
105      */

106     public static Image JavaDoc getIcon(FileObject pkg, String JavaDoc pkgname) {
107         return getIcon( pkg, pkgname, isEmpty(pkg) );
108     }
109     
110     /** Performance optiomization if the the isEmpty status is alredy known.
111      *
112      */

113     public static Image JavaDoc getIcon(FileObject pkg, String JavaDoc pkgname, boolean empty ) {
114         if ( empty ) {
115             return PACKAGE_EMPTY;
116         } else {
117             Boolean JavaDoc b = pkg.isValid() ? AccessibilityQuery.isPubliclyAccessible(pkg) : null;
118             if (b != null) {
119                 if (b.booleanValue()) {
120                     return PACKAGE_PUBLIC;
121                 } else {
122                     return PACKAGE_PRIVATE;
123                 }
124             } else {
125                 return PACKAGE;
126             }
127         }
128     }
129     
130     
131     /**
132      * Check whether a package is empty (devoid of files except for subpackages).
133      */

134     public static boolean isEmpty( FileObject fo ) {
135         return isEmpty (fo, true );
136     }
137
138     /**
139      * Check whether a package is empty (devoid of files except for subpackages).
140      * @param recurse specifies whether to check if subpackages are empty too.
141      */

142     public static boolean isEmpty( FileObject fo, boolean recurse ) {
143         FileObject[] kids = fo.getChildren();
144         for( int i = 0; i < kids.length; i++ ) {
145             // XXX consider using group.contains() here
146
if ( !kids[i].isFolder() && VisibilityQuery.getDefault().isVisible( kids[i] ) ) {
147                 return false;
148             }
149             else if (recurse && VisibilityQuery.getDefault().isVisible( kids[i] ) && !isEmpty(kids[i])) {
150                     return false;
151             }
152         }
153         return true;
154     }
155     
156     /**
157      * Check whether a package should be displayed.
158      * It should be displayed if {@link VisibilityQuery} says it should be,
159      * and it is either completely empty, or contains files (as opposed to
160      * containing some subpackages but no files).
161      */

162     public static boolean isSignificant(FileObject pkg) throws IllegalArgumentException JavaDoc {
163         if (!pkg.isFolder()) {
164             throw new IllegalArgumentException JavaDoc("Not a folder"); // NOI18N
165
}
166         // XXX consider using group.contains() here
167
if (!VisibilityQuery.getDefault().isVisible(pkg)) {
168             return false;
169         }
170         FileObject[] kids = pkg.getChildren();
171         boolean subpackages = false;
172         for (int i = 0; i < kids.length; i++) {
173             if (!VisibilityQuery.getDefault().isVisible(kids[i])) {
174                 continue;
175             }
176             if (kids[i].isData()) {
177                 return true;
178             } else {
179                 subpackages = true;
180             }
181         }
182         return !subpackages;
183     }
184     
185 }
186
Popular Tags