1 /*
   2  * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package jdk.javadoc.internal.doclets.formats.html;
  26 
  27 import javax.lang.model.element.PackageElement;
  28 
  29 import jdk.javadoc.internal.doclets.formats.html.markup.BodyContents;
  30 import jdk.javadoc.internal.doclets.formats.html.markup.ContentBuilder;
  31 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlStyle;
  32 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlTag;
  33 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlTree;
  34 import jdk.javadoc.internal.doclets.formats.html.markup.Navigation;
  35 import jdk.javadoc.internal.doclets.formats.html.markup.Navigation.PageMode;
  36 import jdk.javadoc.internal.doclets.formats.html.markup.StringContent;
  37 import jdk.javadoc.internal.doclets.formats.html.markup.Table;
  38 import jdk.javadoc.internal.doclets.formats.html.markup.TableHeader;
  39 import jdk.javadoc.internal.doclets.toolkit.Content;
  40 import jdk.javadoc.internal.doclets.toolkit.util.DocFileIOException;
  41 import jdk.javadoc.internal.doclets.toolkit.util.DocPath;
  42 import jdk.javadoc.internal.doclets.toolkit.util.DocPaths;
  43 
  44 /**
  45  * Generate the file with list of all the packages in this run.
  46  */
  47 public class AllPackagesIndexWriter extends HtmlDocletWriter {
  48 
  49     /**
  50      * Construct AllPackagesIndexWriter object.
  51      *
  52      * @param configuration The current configuration
  53      * @param filename Path to the file which is getting generated.
  54      */
  55     public AllPackagesIndexWriter(HtmlConfiguration configuration, DocPath filename) {
  56         super(configuration, filename);
  57     }
  58 
  59     /**
  60      * Create AllPackagesIndexWriter object.
  61      *
  62      * @param configuration The current configuration
  63      * @throws DocFileIOException
  64      */
  65     public static void generate(HtmlConfiguration configuration) throws DocFileIOException {
  66         generate(configuration, DocPaths.ALLPACKAGES_INDEX);
  67     }
  68 
  69     private static void generate(HtmlConfiguration configuration, DocPath fileName) throws DocFileIOException {
  70         AllPackagesIndexWriter allPkgGen = new AllPackagesIndexWriter(configuration, fileName);
  71         allPkgGen.buildAllPackagesFile();
  72     }
  73 
  74     /**
  75      * Print all the packages in the file.
  76      */
  77     protected void buildAllPackagesFile() throws DocFileIOException {
  78         String label = resources.getText("doclet.All_Packages");
  79         Content headerContent = new ContentBuilder();
  80         Navigation navBar = new Navigation(null, configuration, PageMode.ALLPACKAGES, path);
  81         addTop(headerContent);
  82         navBar.setUserHeader(getUserHeaderFooter(true));
  83         headerContent.add(navBar.getContent(true));
  84         HtmlTree div = new HtmlTree(HtmlTag.DIV);
  85         div.setStyle(HtmlStyle.allPackagesContainer);
  86         addPackages(div);
  87         Content titleContent = contents.allPackagesLabel;
  88         Content pHeading = HtmlTree.HEADING(Headings.PAGE_TITLE_HEADING, true,
  89                 HtmlStyle.title, titleContent);
  90         Content headerDiv = HtmlTree.DIV(HtmlStyle.header, pHeading);
  91         Content footer = HtmlTree.FOOTER();
  92         navBar.setUserFooter(getUserHeaderFooter(false));
  93         footer.add(navBar.getContent(false));
  94         addBottom(footer);
  95         HtmlTree bodyTree = getBody(getWindowTitle(label));
  96         bodyTree.add(new BodyContents()
  97                 .setHeader(headerContent)
  98                 .addMainContent(headerDiv)
  99                 .addMainContent(div)
 100                 .setFooter(footer)
 101                 .toContent());
 102         printHtmlDocument(null, "package index", bodyTree);
 103     }
 104 
 105     /**
 106      * Add all the packages to the content tree.
 107      *
 108      * @param content HtmlTree content to which the links will be added
 109      */
 110     protected void addPackages(Content content) {
 111         Table table = new Table(HtmlStyle.packagesSummary)
 112                 .setCaption(getTableCaption(new StringContent(resources.packageSummary)))
 113                 .setHeader(new TableHeader(contents.packageLabel, contents.descriptionLabel))
 114                 .setColumnStyles(HtmlStyle.colFirst, HtmlStyle.colLast);
 115         for (PackageElement pkg : configuration.packages) {
 116             if (!(configuration.nodeprecated && utils.isDeprecated(pkg))) {
 117                 Content packageLinkContent = getPackageLink(pkg, getPackageName(pkg));
 118                 Content summaryContent = new ContentBuilder();
 119                 addSummaryComment(pkg, summaryContent);
 120                 table.addRow(pkg, packageLinkContent, summaryContent);
 121             }
 122         }
 123         content.add(table.toContent());
 124     }
 125 }