1 /*
   2  * Copyright (c) 1997, 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 
  26 package jdk.javadoc.internal.doclets.formats.html;
  27 
  28 import jdk.javadoc.internal.doclets.formats.html.markup.Table;
  29 
  30 import java.util.*;
  31 
  32 import javax.lang.model.element.PackageElement;
  33 
  34 import jdk.javadoc.internal.doclets.formats.html.markup.ContentBuilder;
  35 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlStyle;
  36 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlTag;
  37 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlTree;
  38 import jdk.javadoc.internal.doclets.toolkit.Content;
  39 import jdk.javadoc.internal.doclets.toolkit.util.DocFileIOException;
  40 import jdk.javadoc.internal.doclets.toolkit.util.DocPath;
  41 import jdk.javadoc.internal.doclets.toolkit.util.DocPaths;
  42 import jdk.javadoc.internal.doclets.toolkit.util.Group;
  43 
  44 /**
  45  * Generate the package index page "index.html".
  46  *
  47  *  <p><b>This is NOT part of any supported API.
  48  *  If you write code that depends on this, you do so at your own risk.
  49  *  This code and its internal interfaces are subject to change or
  50  *  deletion without notice.</b>
  51  *
  52  * @author Atul M Dambalkar
  53  * @author Bhavesh Patel (Modified)
  54  */
  55 public class PackageIndexWriter extends AbstractPackageIndexWriter {
  56 
  57     /**
  58      * Construct the PackageIndexWriter. Also constructs the grouping
  59      * information as provided on the command line by "-group" option. Stores
  60      * the order of groups specified by the user.
  61      *
  62      * @param configuration the configuration for this doclet
  63      * @param filename the path of the page to be generated
  64      * @see Group
  65      */
  66     public PackageIndexWriter(HtmlConfiguration configuration, DocPath filename) {
  67         super(configuration, filename);
  68     }
  69 
  70     /**
  71      * Generate the package index page.
  72      *
  73      * @param configuration the current configuration of the doclet.
  74      * @throws DocFileIOException if there is a problem generating the package index page
  75      */
  76     public static void generate(HtmlConfiguration configuration) throws DocFileIOException {
  77         DocPath filename = DocPaths.INDEX;
  78         PackageIndexWriter packgen = new PackageIndexWriter(configuration, filename);
  79         packgen.buildPackageIndexFile("doclet.Window_Overview_Summary", "package index");
  80     }
  81 
  82     /**
  83      * Depending upon the grouping information and their titles, add
  84      * separate table indices for each package group.
  85      *
  86      * @param header the documentation tree to which the navigational links will be added
  87      * @param main the documentation tree to which the packages list will be added
  88      */
  89     @Override
  90     protected void addIndex(Content header, Content main) {
  91         addIndexContents(header, main);
  92     }
  93 
  94     /**
  95      * {@inheritDoc}
  96      */
  97     @Override
  98     protected void addPackagesList(Content main) {
  99         Map<String, SortedSet<PackageElement>> groupPackageMap
 100                 = configuration.group.groupPackages(packages);
 101 
 102         if (!groupPackageMap.keySet().isEmpty()) {
 103             Table table =  new Table(HtmlStyle.overviewSummary)
 104                     .setHeader(getPackageTableHeader())
 105                     .setColumnStyles(HtmlStyle.colFirst, HtmlStyle.colLast)
 106                     .setDefaultTab(resources.getText("doclet.All_Packages"))
 107                     .setTabScript(i -> "show(" + i + ");")
 108                     .setTabId(i -> (i == 0) ? "t0" : ("t" + (1 << (i - 1))));
 109 
 110             // add the tabs in command-line order
 111             for (String groupName : configuration.group.getGroupList()) {
 112                 Set<PackageElement> groupPackages = groupPackageMap.get(groupName);
 113                 if (groupPackages != null) {
 114                     table.addTab(groupName, groupPackages::contains);
 115                 }
 116             }
 117 
 118             for (PackageElement pkg : configuration.packages) {
 119                 if (!pkg.isUnnamed()) {
 120                     if (!(configuration.nodeprecated && utils.isDeprecated(pkg))) {
 121                         Content packageLinkContent = getPackageLink(pkg, getPackageName(pkg));
 122                         Content summaryContent = new ContentBuilder();
 123                         addSummaryComment(pkg, summaryContent);
 124                         table.addRow(pkg, packageLinkContent, summaryContent);
 125                     }
 126                 }
 127             }
 128 
 129             Content div = HtmlTree.DIV(HtmlStyle.contentContainer, table.toContent());
 130             main.add(div);
 131 
 132             if (table.needsScript()) {
 133                 getMainBodyScript().append(table.getScript());
 134             }
 135         }
 136     }
 137 
 138     /**
 139      * Adds the overview summary comment for this documentation. Add one line
 140      * summary at the top of the page and generate a link to the description,
 141      * which is added at the end of this page.
 142      *
 143      * @param main the documentation tree to which the overview header will be added
 144      */
 145     @Override
 146     protected void addOverviewHeader(Content main) {
 147         addConfigurationTitle(main);
 148         if (!utils.getFullBody(configuration.overviewElement).isEmpty()) {
 149             HtmlTree div = new HtmlTree(HtmlTag.DIV);
 150             div.setStyle(HtmlStyle.contentContainer);
 151             addOverviewComment(div);
 152             main.add(div);
 153         }
 154     }
 155 
 156     /**
 157      * Adds the overview comment as provided in the file specified by the
 158      * "-overview" option on the command line.
 159      *
 160      * @param htmltree the documentation tree to which the overview comment will
 161      *                 be added
 162      */
 163     protected void addOverviewComment(Content htmltree) {
 164         if (!utils.getFullBody(configuration.overviewElement).isEmpty()) {
 165             addInlineComment(configuration.overviewElement, htmltree);
 166         }
 167     }
 168 
 169     /**
 170      * Adds the top text (from the -top option), the upper
 171      * navigation bar, and then the title (from the"-title"
 172      * option), at the top of page.
 173      *
 174      * @param header the documentation tree to which the navigation bar header will be added
 175      */
 176     @Override
 177     protected void addNavigationBarHeader(Content header) {
 178         addTop(header);
 179         navBar.setUserHeader(getUserHeaderFooter(true));
 180         header.add(navBar.getContent(true));
 181     }
 182 
 183     /**
 184      * Adds the lower navigation bar and the bottom text
 185      * (from the -bottom option) at the bottom of page.
 186      *
 187      * @param footer the documentation tree to which the navigation bar footer will be added
 188      */
 189     @Override
 190     protected void addNavigationBarFooter(Content footer) {
 191         navBar.setUserFooter(getUserHeaderFooter(false));
 192         footer.add(navBar.getContent(false));
 193         addBottom(footer);
 194     }
 195 }