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 AbstractOverviewIndexWriter {
  56 
  57     /**
  58      * A Set of Packages to be documented.
  59      */
  60     protected SortedSet<PackageElement> packages;
  61 
  62     /**
  63      * Construct the PackageIndexWriter. Also constructs the grouping
  64      * information as provided on the command line by "-group" option. Stores
  65      * the order of groups specified by the user.
  66      *
  67      * @param configuration the configuration for this doclet
  68      * @param filename the path of the page to be generated
  69      * @see Group
  70      */
  71     public PackageIndexWriter(HtmlConfiguration configuration, DocPath filename) {
  72         super(configuration, filename);
  73         packages = configuration.packages;
  74     }
  75 
  76     /**
  77      * Generate the package index page.
  78      *
  79      * @param configuration the current configuration of the doclet.
  80      * @throws DocFileIOException if there is a problem generating the package index page
  81      */
  82     public static void generate(HtmlConfiguration configuration) throws DocFileIOException {
  83         DocPath filename = DocPaths.INDEX;
  84         PackageIndexWriter packgen = new PackageIndexWriter(configuration, filename);
  85         packgen.buildOverviewIndexFile("doclet.Window_Overview_Summary", "package index");
  86     }
  87 
  88     /**
  89      * Adds the packages list to the documentation tree.
  90      *
  91      * @param main the documentation tree to which the packages list will be added
  92      */
  93     @Override
  94     protected void addIndex(Content main) {
  95         Map<String, SortedSet<PackageElement>> groupPackageMap
  96                 = configuration.group.groupPackages(packages);
  97 
  98         if (!groupPackageMap.keySet().isEmpty()) {
  99             Table table =  new Table(HtmlStyle.overviewSummary)
 100                     .setHeader(getPackageTableHeader())
 101                     .setColumnStyles(HtmlStyle.colFirst, HtmlStyle.colLast)
 102                     .setDefaultTab(resources.getText("doclet.All_Packages"))
 103                     .setTabScript(i -> "show(" + i + ");")
 104                     .setTabId(i -> (i == 0) ? "t0" : ("t" + (1 << (i - 1))));
 105 
 106             // add the tabs in command-line order
 107             for (String groupName : configuration.group.getGroupList()) {
 108                 Set<PackageElement> groupPackages = groupPackageMap.get(groupName);
 109                 if (groupPackages != null) {
 110                     table.addTab(groupName, groupPackages::contains);
 111                 }
 112             }
 113 
 114             for (PackageElement pkg : configuration.packages) {
 115                 if (!pkg.isUnnamed()) {
 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             }
 124 
 125             Content div = HtmlTree.DIV(HtmlStyle.contentContainer, table.toContent());
 126             main.add(div);
 127 
 128             if (table.needsScript()) {
 129                 getMainBodyScript().append(table.getScript());
 130             }
 131         }
 132     }
 133 }