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.HtmlTree;
  37 import jdk.javadoc.internal.doclets.toolkit.Content;
  38 import jdk.javadoc.internal.doclets.toolkit.util.DocFileIOException;
  39 import jdk.javadoc.internal.doclets.toolkit.util.DocPath;
  40 import jdk.javadoc.internal.doclets.toolkit.util.DocPaths;
  41 import jdk.javadoc.internal.doclets.toolkit.util.Group;
  42 
  43 /**
  44  * Generate the package index page "index.html".
  45  *
  46  *  <p><b>This is NOT part of any supported API.
  47  *  If you write code that depends on this, you do so at your own risk.
  48  *  This code and its internal interfaces are subject to change or
  49  *  deletion without notice.</b>
  50  */
  51 public class PackageIndexWriter extends AbstractOverviewIndexWriter {
  52 
  53     /**
  54      * A Set of Packages to be documented.
  55      */
  56     protected SortedSet<PackageElement> packages;
  57 
  58     /**
  59      * Construct the PackageIndexWriter. Also constructs the grouping
  60      * information as provided on the command line by "-group" option. Stores
  61      * the order of groups specified by the user.
  62      *
  63      * @param configuration the configuration for this doclet
  64      * @param filename the path of the page to be generated
  65      * @see Group
  66      */
  67     public PackageIndexWriter(HtmlConfiguration configuration, DocPath filename) {
  68         super(configuration, filename);
  69         packages = configuration.packages;
  70     }
  71 
  72     /**
  73      * Generate the package index page.
  74      *
  75      * @param configuration the current configuration of the doclet.
  76      * @throws DocFileIOException if there is a problem generating the package index page
  77      */
  78     public static void generate(HtmlConfiguration configuration) throws DocFileIOException {
  79         DocPath filename = DocPaths.INDEX;
  80         PackageIndexWriter packgen = new PackageIndexWriter(configuration, filename);
  81         packgen.buildOverviewIndexFile("doclet.Window_Overview_Summary", "package index");
  82     }
  83 
  84     /**
  85      * Adds the packages list to the documentation tree.
  86      *
  87      * @param main the documentation tree to which the packages list will be added
  88      */
  89     @Override
  90     protected void addIndex(Content main) {
  91         Map<String, SortedSet<PackageElement>> groupPackageMap
  92                 = configuration.group.groupPackages(packages);
  93 
  94         if (!groupPackageMap.keySet().isEmpty()) {
  95             Table table =  new Table(HtmlStyle.overviewSummary)
  96                     .setHeader(getPackageTableHeader())
  97                     .setColumnStyles(HtmlStyle.colFirst, HtmlStyle.colLast)
  98                     .setDefaultTab(resources.getText("doclet.All_Packages"))
  99                     .setTabScript(i -> "show(" + i + ");")
 100                     .setTabId(i -> (i == 0) ? "t0" : ("t" + (1 << (i - 1))));
 101 
 102             // add the tabs in command-line order
 103             for (String groupName : configuration.group.getGroupList()) {
 104                 Set<PackageElement> groupPackages = groupPackageMap.get(groupName);
 105                 if (groupPackages != null) {
 106                     table.addTab(groupName, groupPackages::contains);
 107                 }
 108             }
 109 
 110             for (PackageElement pkg : configuration.packages) {
 111                 if (!pkg.isUnnamed()) {
 112                     if (!(options.noDeprecated && utils.isDeprecated(pkg))) {
 113                         Content packageLinkContent = getPackageLink(pkg, getPackageName(pkg));
 114                         Content summaryContent = new ContentBuilder();
 115                         addSummaryComment(pkg, summaryContent);
 116                         table.addRow(pkg, packageLinkContent, summaryContent);
 117                     }
 118                 }
 119             }
 120 
 121             Content div = HtmlTree.DIV(HtmlStyle.contentContainer, table.toContent());
 122             main.add(div);
 123 
 124             if (table.needsScript()) {
 125                 getMainBodyScript().append(table.getScript());
 126             }
 127         }
 128     }
 129 }