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 AbstractOverviewIndexWriter {
  57 
  58     /**
  59      * Modules to be documented.
  60      */
  61     protected SortedSet<ModuleElement> modules;
  62 
  63     /**
  64      * Construct the ModuleIndexWriter.
  65      *
  66      * @param configuration the configuration object
  67      * @param filename the name of the generated file
  68      */
  69     public ModuleIndexWriter(HtmlConfiguration configuration, DocPath filename) {
  70         super(configuration, filename);
  71         modules = configuration.modules;
  72     }
  73 
  74     /**
  75      * Generate the module index page.
  76      *
  77      * @param configuration the current configuration of the doclet.
  78      * @throws DocFileIOException if there is a problem generating the module index page
  79      */
  80     public static void generate(HtmlConfiguration configuration) throws DocFileIOException {
  81         DocPath filename = DocPaths.INDEX;
  82         ModuleIndexWriter mdlgen = new ModuleIndexWriter(configuration, filename);
  83         mdlgen.buildOverviewIndexFile("doclet.Window_Overview_Summary", "module index");
  84     }
  85 
  86     /**
  87      * Adds the list of modules.
  88      *
  89      * @param main the documentation tree to which the modules list will be added
  90      */
  91     @Override
  92     protected void addIndex(Content main) {
  93         Map<String, SortedSet<ModuleElement>> groupModuleMap
  94                 = configuration.group.groupModules(modules);
  95 
  96         if (!groupModuleMap.keySet().isEmpty()) {
  97             TableHeader tableHeader = new TableHeader(contents.moduleLabel, contents.descriptionLabel);
  98             Table table =  new Table(HtmlStyle.overviewSummary)
  99                     .setHeader(tableHeader)
 100                     .setColumnStyles(HtmlStyle.colFirst, HtmlStyle.colLast)
 101                     .setDefaultTab(resources.getText("doclet.All_Modules"))
 102                     .setTabScript(i -> "show(" + i + ");")
 103                     .setTabId(i -> (i == 0) ? "t0" : ("t" + (1 << (i - 1))));
 104 
 105             // add the tabs in command-line order
 106             for (String groupName : configuration.group.getGroupList()) {
 107                 Set<ModuleElement> groupModules = groupModuleMap.get(groupName);
 108                 if (groupModules != null) {
 109                     table.addTab(groupName, groupModules::contains);
 110                 }
 111             }
 112 
 113             for (ModuleElement mdle : modules) {
 114                 if (!mdle.isUnnamed()) {
 115                     if (!(configuration.nodeprecated && utils.isDeprecated(mdle))) {
 116                         Content moduleLinkContent = getModuleLink(mdle, new StringContent(mdle.getQualifiedName().toString()));
 117                         Content summaryContent = new ContentBuilder();
 118                         addSummaryComment(mdle, summaryContent);
 119                         table.addRow(mdle, moduleLinkContent, summaryContent);
 120                     }
 121                 }
 122             }
 123 
 124             Content div = HtmlTree.DIV(HtmlStyle.contentContainer, table.toContent());
 125             main.add(div);
 126 
 127             if (table.needsScript()) {
 128                 mainBodyScript.append(table.getScript());
 129             }
 130         }
 131     }
 132 }