~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to java/project/src/org/netbeans/modules/java/project/PackageDisplayUtils.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 */
 
41
 
 
42
package org.netbeans.modules.java.project;
 
43
 
 
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;
 
50
 
 
51
// XXX needs unit test
 
52
 
 
53
/**
 
54
 * Provides display name and icon utilities for
 
55
 * {@link PackageViewChildren.PackageNode} and {@link PackageListView.PackageItem}.
 
56
 * @author Jesse Glick
 
57
 */
 
58
public final class PackageDisplayUtils {
 
59
 
 
60
    private PackageDisplayUtils() {}
 
61
    
 
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
 
65
 
 
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
 
70
 
 
71
    /**
 
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
 
76
     */
 
77
    public static String getDisplayLabel(String pkgname) {
 
78
        return computePackageName(pkgname, TRUNCATE_PACKAGE_NAMES);
 
79
    }
 
80
    
 
81
    /**
 
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
 
87
     */
 
88
    public static String getToolTip(FileObject pkg, String pkgname) {
 
89
        String pkglabel = computePackageName(pkgname, false);
 
90
        Boolean b = AccessibilityQuery.isPubliclyAccessible(pkg);
 
91
        if (b != null) {
 
92
            if (b.booleanValue()) {
 
93
                return NbBundle.getMessage(PackageDisplayUtils.class, "LBL_public_package", pkglabel);
 
94
            } else {
 
95
                return NbBundle.getMessage(PackageDisplayUtils.class, "LBL_private_package", pkglabel);
 
96
            }
 
97
        } else {
 
98
            return NbBundle.getMessage(PackageDisplayUtils.class, "LBL_package", pkglabel);
 
99
        }
 
100
    }
 
101
    
 
102
    /**
 
103
     * Get package name.
 
104
     * Handles default package specially.
 
105
     * @param truncate if true, show a truncated version to save display space
 
106
     */
 
107
    private static String computePackageName(String pkgname, boolean truncate) {
 
108
        if (pkgname.length() == 0) {
 
109
            return NbBundle.getMessage(PackageDisplayUtils.class, "LBL_DefaultPackage"); // NOI18N
 
110
        } else {
 
111
            if (truncate) {
 
112
                // #42589: keep only first letter of first package component, up to three of others
 
113
                return pkgname.replaceFirst("^([^.])[^.]+\\.", "$1.").replaceAll("([^.]{3})[^.]+\\.", "$1."); // NOI18N
 
114
            } else {
 
115
                return pkgname;
 
116
            }
 
117
        }
 
118
    }
 
119
 
 
120
     
 
121
    
 
122
    /**
 
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
 
127
     */
 
128
    public static Image getIcon(FileObject pkg, String pkgname) {
 
129
        return getIcon( pkg, pkgname, isEmpty(pkg) );
 
130
    }
 
131
    
 
132
    /** Performance optiomization if the the isEmpty status is alredy known.
 
133
     * 
 
134
     */
 
135
    public static Image getIcon(FileObject pkg, String pkgname, boolean empty ) {
 
136
        if ( empty ) {
 
137
            return PACKAGE_EMPTY;
 
138
        } else {
 
139
            Boolean b = pkg.isValid() ? AccessibilityQuery.isPubliclyAccessible(pkg) : null;
 
140
            if (b != null) {
 
141
                if (b.booleanValue()) {
 
142
                    return PACKAGE_PUBLIC;
 
143
                } else {
 
144
                    return PACKAGE_PRIVATE;
 
145
                }
 
146
            } else {
 
147
                return PACKAGE;
 
148
            } 
 
149
        }
 
150
    }
 
151
    
 
152
    
 
153
    /**
 
154
     * Check whether a package is empty (devoid of files except for subpackages).
 
155
     */
 
156
    public static boolean isEmpty( FileObject fo ) {    
 
157
        return isEmpty (fo, true );
 
158
    }
 
159
 
 
160
    /**
 
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.
 
163
     */
 
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] ) ) {
 
169
                return false;
 
170
            }  
 
171
            else if (recurse && VisibilityQuery.getDefault().isVisible( kids[i] ) && !isEmpty(kids[i])) {
 
172
                    return false;
 
173
            }
 
174
        }
 
175
        return true;
 
176
    }
 
177
    
 
178
    /**
 
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).
 
183
     */
 
184
    public static boolean isSignificant(FileObject pkg) throws IllegalArgumentException {
 
185
        if (!pkg.isFolder()) {
 
186
            throw new IllegalArgumentException("Not a folder"); // NOI18N
 
187
        }
 
188
        // XXX consider using group.contains() here
 
189
        if (!VisibilityQuery.getDefault().isVisible(pkg)) {
 
190
            return false;
 
191
        }
 
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])) {
 
196
                continue;
 
197
            }
 
198
            if (kids[i].isData()) {
 
199
                return true;
 
200
            } else {
 
201
                subpackages = true;
 
202
            }
 
203
        }
 
204
        return !subpackages;
 
205
    }
 
206
    
 
207
}