1 /*
2  * Copyright (c) 2018, 2020, 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 java.util.ArrayList;
29 import java.util.List;
30 
31 import javax.lang.model.element.TypeElement;
32 
33 import com.sun.source.doctree.DocTree;
34 import jdk.javadoc.internal.doclets.formats.html.markup.BodyContents;
35 import jdk.javadoc.internal.doclets.formats.html.markup.ContentBuilder;
36 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlStyle;
37 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlTree;
38 import jdk.javadoc.internal.doclets.formats.html.Navigation.PageMode;
39 import jdk.javadoc.internal.doclets.formats.html.markup.Table;
40 import jdk.javadoc.internal.doclets.formats.html.markup.TableHeader;
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 import jdk.javadoc.internal.doclets.toolkit.util.IndexBuilder;
46 import jdk.javadoc.internal.doclets.toolkit.util.IndexItem;
47 
48 /**
49  * Generate the file with list of all the classes in this run.
50  */
51 public class AllClassesIndexWriter extends HtmlDocletWriter {
52 
53     /**
54      * Index of all the classes.
55      */
56     protected IndexBuilder indexBuilder;
57 
58     /**
59      * Construct AllClassesIndexWriter object. Also initializes the indexBuilder variable in this
60      * class.
61      *
62      * @param configuration The current configuration
63      * @param filename Path to the file which is getting generated.
64      * @param indexBuilder Unicode based Index from {@link IndexBuilder}
65      */
AllClassesIndexWriter(HtmlConfiguration configuration, DocPath filename, IndexBuilder indexBuilder)66     public AllClassesIndexWriter(HtmlConfiguration configuration,
67             DocPath filename, IndexBuilder indexBuilder) {
68         super(configuration, filename);
69         this.indexBuilder = indexBuilder;
70     }
71 
72     /**
73      * Create AllClassesIndexWriter object.
74      *
75      * @param configuration The current configuration
76      * @param indexBuilder IndexBuilder object for all classes index.
77      * @throws DocFileIOException
78      */
generate(HtmlConfiguration configuration, IndexBuilder indexBuilder)79     public static void generate(HtmlConfiguration configuration,
80             IndexBuilder indexBuilder) throws DocFileIOException {
81         generate(configuration, indexBuilder, DocPaths.ALLCLASSES_INDEX);
82     }
83 
generate(HtmlConfiguration configuration, IndexBuilder indexBuilder, DocPath fileName)84     private static void generate(HtmlConfiguration configuration, IndexBuilder indexBuilder,
85             DocPath fileName) throws DocFileIOException {
86         AllClassesIndexWriter allClassGen = new AllClassesIndexWriter(configuration,
87                 fileName, indexBuilder);
88         allClassGen.buildAllClassesFile();
89     }
90 
91     /**
92      * Print all the classes in the file.
93      */
buildAllClassesFile()94     protected void buildAllClassesFile() throws DocFileIOException {
95         String label = resources.getText("doclet.All_Classes");
96         Content header = new ContentBuilder();
97         addTop(header);
98         Navigation navBar = new Navigation(null, configuration, PageMode.ALL_CLASSES, path);
99         navBar.setUserHeader(getUserHeaderFooter(true));
100         header.add(navBar.getContent(Navigation.Position.TOP));
101         Content allClassesContent = new ContentBuilder();
102         addContents(allClassesContent);
103         Content mainContent = new ContentBuilder();
104         mainContent.add(allClassesContent);
105         Content footer = HtmlTree.FOOTER();
106         navBar.setUserFooter(getUserHeaderFooter(false));
107         footer.add(navBar.getContent(Navigation.Position.BOTTOM));
108         addBottom(footer);
109         HtmlTree bodyTree = getBody(getWindowTitle(label));
110         bodyTree.add(new BodyContents()
111                 .setHeader(header)
112                 .addMainContent(mainContent)
113                 .setFooter(footer));
114         printHtmlDocument(null, "class index", bodyTree);
115     }
116 
117     /**
118      * Add all types to the content tree.
119      *
120      * @param content HtmlTree content to which the links will be added
121      */
addContents(Content content)122     protected void addContents(Content content) {
123         Table table = new Table(HtmlStyle.typeSummary, HtmlStyle.summaryTable)
124                 .setHeader(new TableHeader(contents.classLabel, contents.descriptionLabel))
125                 .setRowScopeColumn(1)
126                 .setColumnStyles(HtmlStyle.colFirst, HtmlStyle.colLast)
127                 .setId("all-classes-table")
128                 .setDefaultTab(resources.getText("doclet.All_Classes"))
129                 .addTab(resources.interfaceSummary, utils::isInterface)
130                 .addTab(resources.classSummary, e -> utils.isOrdinaryClass((TypeElement)e))
131                 .addTab(resources.enumSummary, utils::isEnum)
132                 .addTab(resources.exceptionSummary, e -> utils.isException((TypeElement)e))
133                 .addTab(resources.errorSummary, e -> utils.isError((TypeElement)e))
134                 .addTab(resources.annotationTypeSummary, utils::isAnnotationType)
135                 .setTabScript(i -> "show(" + i + ");");
136         for (Character unicode : indexBuilder.keys()) {
137             for (IndexItem indexItem : indexBuilder.getMemberList(unicode)) {
138                 TypeElement typeElement = (TypeElement) indexItem.getElement();
139                 if (typeElement == null || !utils.isCoreClass(typeElement)) {
140                     continue;
141                 }
142                 addTableRow(table, typeElement);
143             }
144         }
145         Content titleContent = contents.allClassesLabel;
146         Content pHeading = HtmlTree.HEADING_TITLE(Headings.PAGE_TITLE_HEADING,
147                 HtmlStyle.title, titleContent);
148         Content headerDiv = HtmlTree.DIV(HtmlStyle.header, pHeading);
149         content.add(headerDiv);
150         if (!table.isEmpty()) {
151             content.add(table);
152             if (table.needsScript()) {
153                 getMainBodyScript().append(table.getScript());
154             }
155         }
156     }
157 
158     /**
159      * Add table row.
160      *
161      * @param table the table to which the row will be added
162      * @param klass the type to be added to the table
163      */
addTableRow(Table table, TypeElement klass)164     protected void addTableRow(Table table, TypeElement klass) {
165         List<Content> rowContents = new ArrayList<>();
166         Content classLink = getLink(new LinkInfoImpl(
167                 configuration, LinkInfoImpl.Kind.INDEX, klass));
168         ContentBuilder description = new ContentBuilder();
169         if (utils.isDeprecated(klass)) {
170             description.add(getDeprecatedPhrase(klass));
171             List<? extends DocTree> tags = utils.getDeprecatedTrees(klass);
172             if (!tags.isEmpty()) {
173                 addSummaryDeprecatedComment(klass, tags.get(0), description);
174             }
175         } else {
176             addSummaryComment(klass, description);
177         }
178         rowContents.add(classLink);
179         rowContents.add(description);
180         table.addRow(klass, rowContents);
181     }
182 }
183