001    /*
002     * Copyright 1998-2008 Sun Microsystems, Inc.  All Rights Reserved.
003     * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004     *
005     * This code is free software; you can redistribute it and/or modify it
006     * under the terms of the GNU General Public License version 2 only, as
007     * published by the Free Software Foundation.  Sun designates this
008     * particular file as subject to the "Classpath" exception as provided
009     * by Sun in the LICENSE file that accompanied this code.
010     *
011     * This code is distributed in the hope that it will be useful, but WITHOUT
012     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013     * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014     * version 2 for more details (a copy is included in the LICENSE file that
015     * accompanied this code).
016     *
017     * You should have received a copy of the GNU General Public License version
018     * 2 along with this work; if not, write to the Free Software Foundation,
019     * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020     *
021     * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022     * CA 95054 USA or visit www.sun.com if you need additional information or
023     * have any questions.
024     */
025    
026    package com.sun.tools.doclets.formats.html;
027    
028    import com.sun.tools.doclets.internal.toolkit.util.*;
029    import com.sun.tools.doclets.internal.toolkit.*;
030    
031    import com.sun.javadoc.*;
032    import java.io.*;
033    import java.util.*;
034    /**
035     * Class to generate file for each package contents in the left-hand bottom
036     * frame. This will list all the Class Kinds in the package. A click on any
037     * class-kind will update the right-hand frame with the clicked class-kind page.
038     *
039     * @author Atul M Dambalkar
040     */
041    public class PackageFrameWriter extends HtmlDocletWriter {
042    
043        /**
044         * The package being documented.
045         */
046        private PackageDoc packageDoc;
047    
048        /**
049         * The classes to be documented.  Use this to filter out classes
050         * that will not be documented.
051         */
052        private Set<ClassDoc> documentedClasses;
053    
054        /**
055         * The name of the output file.
056         */
057        public static final String OUTPUT_FILE_NAME = "package-frame.html";
058    
059        /**
060         * Constructor to construct PackageFrameWriter object and to generate
061         * "package-frame.html" file in the respective package directory.
062         * For example for package "java.lang" this will generate file
063         * "package-frame.html" file in the "java/lang" directory. It will also
064         * create "java/lang" directory in the current or the destination directory
065         * if it doesen't exist.
066         *
067         * @param configuration the configuration of the doclet.
068         * @param packageDoc PackageDoc under consideration.
069         */
070        public PackageFrameWriter(ConfigurationImpl configuration,
071                                  PackageDoc packageDoc)
072                                  throws IOException {
073            super(configuration, DirectoryManager.getDirectoryPath(packageDoc), OUTPUT_FILE_NAME, DirectoryManager.getRelativePath(packageDoc));
074            this.packageDoc = packageDoc;
075            if (configuration.root.specifiedPackages().length == 0) {
076                documentedClasses = new HashSet<ClassDoc>(Arrays.asList(configuration.root.classes()));
077            }
078        }
079    
080        /**
081         * Generate a package summary page for the left-hand bottom frame. Construct
082         * the PackageFrameWriter object and then uses it generate the file.
083         *
084         * @param configuration the current configuration of the doclet.
085         * @param packageDoc The package for which "pacakge-frame.html" is to be generated.
086         */
087        public static void generate(ConfigurationImpl configuration,
088                                    PackageDoc packageDoc) {
089            PackageFrameWriter packgen;
090            try {
091                packgen = new PackageFrameWriter(configuration, packageDoc);
092                String pkgName = Util.getPackageName(packageDoc);
093                packgen.printHtmlHeader(pkgName, configuration.metakeywords.getMetaKeywords(packageDoc), false);
094                packgen.printPackageHeader(pkgName);
095                packgen.generateClassListing();
096                packgen.printBodyHtmlEnd();
097                packgen.close();
098            } catch (IOException exc) {
099                configuration.standardmessage.error(
100                            "doclet.exception_encountered",
101                            exc.toString(), OUTPUT_FILE_NAME);
102                throw new DocletAbortException();
103            }
104        }
105    
106        /**
107         * Generate class listing for all the classes in this package. Divide class
108         * listing as per the class kind and generate separate listing for
109         * Classes, Interfaces, Exceptions and Errors.
110         */
111        protected void generateClassListing() {
112            Configuration config = configuration();
113            if (packageDoc.isIncluded()) {
114                generateClassKindListing(packageDoc.interfaces(),
115                    configuration.getText("doclet.Interfaces"));
116                generateClassKindListing(packageDoc.ordinaryClasses(),
117                    configuration.getText("doclet.Classes"));
118                generateClassKindListing(packageDoc.enums(),
119                    configuration.getText("doclet.Enums"));
120                generateClassKindListing(packageDoc.exceptions(),
121                    configuration.getText("doclet.Exceptions"));
122                generateClassKindListing(packageDoc.errors(),
123                    configuration.getText("doclet.Errors"));
124                generateClassKindListing(packageDoc.annotationTypes(),
125                    configuration.getText("doclet.AnnotationTypes"));
126            } else {
127                String name = Util.getPackageName(packageDoc);
128                generateClassKindListing(config.classDocCatalog.interfaces(name),
129                    configuration.getText("doclet.Interfaces"));
130                generateClassKindListing(config.classDocCatalog.ordinaryClasses(name),
131                    configuration.getText("doclet.Classes"));
132                generateClassKindListing(config.classDocCatalog.enums(name),
133                    configuration.getText("doclet.Enums"));
134                generateClassKindListing(config.classDocCatalog.exceptions(name),
135                    configuration.getText("doclet.Exceptions"));
136                generateClassKindListing(config.classDocCatalog.errors(name),
137                    configuration.getText("doclet.Errors"));
138                generateClassKindListing(config.classDocCatalog.annotationTypes(name),
139                    configuration.getText("doclet.AnnotationTypes"));
140            }
141        }
142    
143        /**
144         * Generate specific class kind listing. Also add label to the listing.
145         *
146         * @param arr Array of specific class kinds, namely Class or Interface or
147         * Exception or Error.
148         * @param label Label for the listing
149         */
150        protected void generateClassKindListing(ClassDoc[] arr, String label) {
151            if(arr.length > 0) {
152                Arrays.sort(arr);
153                printPackageTableHeader();
154                fontSizeStyle("+1", "FrameHeadingFont");
155                boolean printedHeader = false;
156                for (int i = 0; i < arr.length; i++) {
157                    if (documentedClasses != null &&
158                        !documentedClasses.contains(arr[i])) {
159                        continue;
160                    }
161                    if (!Util.isCoreClass(arr[i]) || !
162                        configuration.isGeneratedDoc(arr[i])) {
163                        continue;
164                    }
165                    if (!printedHeader) {
166                        print(label);
167                        fontEnd();
168                        println("&nbsp;");
169                        fontStyle("FrameItemFont");
170                        printedHeader = true;
171                    }
172                    br();
173                    printLink(new LinkInfoImpl(
174                        LinkInfoImpl.PACKAGE_FRAME,
175                        arr[i],
176                        (arr[i].isInterface() ?
177                            italicsText(arr[i].name()) :
178                            arr[i].name()),"classFrame")
179                    );
180                }
181                fontEnd();
182                printPackageTableFooter();
183                println();
184            }
185        }
186    
187        /**
188         * Print the package link at the top of the class kind listing. Clicking
189         * this link, package-summary page will appear in the right hand frame.
190         *
191         * @param heading Top Heading to be used for the class kind listing.
192         */
193        protected void printPackageHeader(String heading) {
194            fontSizeStyle("+1", "FrameTitleFont");
195            printTargetPackageLink(packageDoc, "classFrame", heading);
196            fontEnd();
197        }
198    
199        /**
200         * The table for the class kind listing.
201         */
202        protected void printPackageTableHeader() {
203            table();
204            tr();
205            tdNowrap();
206        }
207    
208        /**
209         * Closing Html tags for table of class kind listing.
210         */
211        protected void printPackageTableFooter() {
212            tdEnd();
213            trEnd();
214            tableEnd();
215        }
216    }