2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.modules.java.project;
44
import java.awt.Image;
45
import org.netbeans.api.java.queries.AccessibilityQuery;
46
import org.netbeans.api.queries.VisibilityQuery;
47
import org.openide.filesystems.FileObject;
48
import org.openide.util.NbBundle;
49
import org.openide.util.Utilities;
51
// XXX needs unit test
54
* Provides display name and icon utilities for
55
* {@link PackageViewChildren.PackageNode} and {@link PackageListView.PackageItem}.
58
public final class PackageDisplayUtils {
60
private PackageDisplayUtils() {}
62
/** whether to turn on #42589 */
63
private static final boolean TRUNCATE_PACKAGE_NAMES =
64
Boolean.getBoolean("org.netbeans.spi.java.project.support.ui.packageView.TRUNCATE_PACKAGE_NAMES"); // NOI18N
66
private static final Image PACKAGE = Utilities.loadImage("org/netbeans/spi/java/project/support/ui/package.gif"); // NOI18N
67
private static final Image PACKAGE_EMPTY = Utilities.loadImage("org/netbeans/spi/java/project/support/ui/packageEmpty.gif"); // NOI18N
68
private static final Image PACKAGE_PRIVATE = Utilities.loadImage("org/netbeans/spi/java/project/support/ui/packagePrivate.gif"); // NOI18N
69
private static final Image PACKAGE_PUBLIC = Utilities.loadImage("org/netbeans/spi/java/project/support/ui/packagePublic.gif"); // NOI18N
72
* Find the proper display label for a package.
73
* @param pkg the actual folder
74
* @param pkgname the dot-separated package name (<code>""</code> for default package)
75
* @return an appropriate display label for it
77
public static String getDisplayLabel(String pkgname) {
78
return computePackageName(pkgname, TRUNCATE_PACKAGE_NAMES);
82
* Find the proper tool tip for a package.
83
* May have more info than the display label.
84
* @param pkg the actual folder
85
* @param pkgname the dot-separated package name (<code>""</code> for default package)
86
* @return an appropriate display label for it
88
public static String getToolTip(FileObject pkg, String pkgname) {
89
String pkglabel = computePackageName(pkgname, false);
90
Boolean b = AccessibilityQuery.isPubliclyAccessible(pkg);
92
if (b.booleanValue()) {
93
return NbBundle.getMessage(PackageDisplayUtils.class, "LBL_public_package", pkglabel);
95
return NbBundle.getMessage(PackageDisplayUtils.class, "LBL_private_package", pkglabel);
98
return NbBundle.getMessage(PackageDisplayUtils.class, "LBL_package", pkglabel);
104
* Handles default package specially.
105
* @param truncate if true, show a truncated version to save display space
107
private static String computePackageName(String pkgname, boolean truncate) {
108
if (pkgname.length() == 0) {
109
return NbBundle.getMessage(PackageDisplayUtils.class, "LBL_DefaultPackage"); // NOI18N
112
// #42589: keep only first letter of first package component, up to three of others
113
return pkgname.replaceFirst("^([^.])[^.]+\\.", "$1.").replaceAll("([^.]{3})[^.]+\\.", "$1."); // NOI18N
123
* Find the proper display icon for a package.
124
* @param pkg the actual folder
125
* @param pkgname the dot-separated package name (<code>""</code> for default package)
126
* @return an appropriate display icon for it
128
public static Image getIcon(FileObject pkg, String pkgname) {
129
return getIcon( pkg, pkgname, isEmpty(pkg) );
132
/** Performance optiomization if the the isEmpty status is alredy known.
135
public static Image getIcon(FileObject pkg, String pkgname, boolean empty ) {
137
return PACKAGE_EMPTY;
139
Boolean b = pkg.isValid() ? AccessibilityQuery.isPubliclyAccessible(pkg) : null;
141
if (b.booleanValue()) {
142
return PACKAGE_PUBLIC;
144
return PACKAGE_PRIVATE;
154
* Check whether a package is empty (devoid of files except for subpackages).
156
public static boolean isEmpty( FileObject fo ) {
157
return isEmpty (fo, true );
161
* Check whether a package is empty (devoid of files except for subpackages).
162
* @param recurse specifies whether to check if subpackages are empty too.
164
public static boolean isEmpty( FileObject fo, boolean recurse ) {
165
FileObject[] kids = fo.getChildren();
166
for( int i = 0; i < kids.length; i++ ) {
167
// XXX consider using group.contains() here
168
if ( !kids[i].isFolder() && VisibilityQuery.getDefault().isVisible( kids[i] ) ) {
171
else if (recurse && VisibilityQuery.getDefault().isVisible( kids[i] ) && !isEmpty(kids[i])) {
179
* Check whether a package should be displayed.
180
* It should be displayed if {@link VisibilityQuery} says it should be,
181
* and it is either completely empty, or contains files (as opposed to
182
* containing some subpackages but no files).
184
public static boolean isSignificant(FileObject pkg) throws IllegalArgumentException {
185
if (!pkg.isFolder()) {
186
throw new IllegalArgumentException("Not a folder"); // NOI18N
188
// XXX consider using group.contains() here
189
if (!VisibilityQuery.getDefault().isVisible(pkg)) {
192
FileObject[] kids = pkg.getChildren();
193
boolean subpackages = false;
194
for (int i = 0; i < kids.length; i++) {
195
if (!VisibilityQuery.getDefault().isVisible(kids[i])) {
198
if (kids[i].isData()) {