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