1/*
2 * Copyright (c) 1997, 2016, 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
26package jdk.javadoc.internal.doclets.formats.html;
27
28import java.util.SortedSet;
29
30import javax.lang.model.element.PackageElement;
31
32import jdk.javadoc.internal.doclets.formats.html.markup.HtmlConstants;
33import jdk.javadoc.internal.doclets.formats.html.markup.HtmlStyle;
34import jdk.javadoc.internal.doclets.formats.html.markup.HtmlTag;
35import jdk.javadoc.internal.doclets.formats.html.markup.HtmlTree;
36import jdk.javadoc.internal.doclets.formats.html.markup.StringContent;
37import jdk.javadoc.internal.doclets.toolkit.Content;
38import jdk.javadoc.internal.doclets.toolkit.util.ClassTree;
39import jdk.javadoc.internal.doclets.toolkit.util.DocFileIOException;
40import jdk.javadoc.internal.doclets.toolkit.util.DocPath;
41import jdk.javadoc.internal.doclets.toolkit.util.DocPaths;
42
43/**
44 * Generate Class Hierarchy page for all the Classes in this run.  Use
45 * ClassTree for building the Tree. The name of
46 * the generated file is "overview-tree.html" and it is generated in the
47 * current or the destination directory.
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 Atul M Dambalkar
55 * @author Bhavesh Patel (Modified)
56 */
57public class TreeWriter extends AbstractTreeWriter {
58
59    /**
60     * Packages in this run.
61     */
62    SortedSet<PackageElement> packages;
63
64    /**
65     * True if there are no packages specified on the command line,
66     * False otherwise.
67     */
68    private final boolean classesOnly;
69
70    /**
71     * Constructor to construct TreeWriter object.
72     *
73     * @param configuration the current configuration of the doclet.
74     * @param filename String filename
75     * @param classtree the tree being built.
76     */
77    public TreeWriter(ConfigurationImpl configuration, DocPath filename, ClassTree classtree) {
78        super(configuration, filename, classtree);
79        packages = configuration.packages;
80        classesOnly = packages.isEmpty();
81    }
82
83    /**
84     * Create a TreeWriter object and use it to generate the
85     * "overview-tree.html" file.
86     *
87     * @param configuration the configuration for this doclet
88     * @param classtree the class tree being documented.
89     * @throws  DocFileIOException if there is a problem generating the overview tree page
90     */
91    public static void generate(ConfigurationImpl configuration,
92                                ClassTree classtree) throws DocFileIOException {
93        DocPath filename = DocPaths.OVERVIEW_TREE;
94        TreeWriter treegen = new TreeWriter(configuration, filename, classtree);
95        treegen.generateTreeFile();
96    }
97
98    /**
99     * Generate the interface hierarchy and class hierarchy.
100     *
101     * @throws DocFileIOException if there is a problem generating the overview tree page
102     */
103    public void generateTreeFile() throws DocFileIOException {
104        HtmlTree body = getTreeHeader();
105        Content headContent = contents.hierarchyForAllPackages;
106        Content heading = HtmlTree.HEADING(HtmlConstants.TITLE_HEADING, false,
107                HtmlStyle.title, headContent);
108        Content div = HtmlTree.DIV(HtmlStyle.header, heading);
109        addPackageTreeLinks(div);
110        HtmlTree htmlTree = (configuration.allowTag(HtmlTag.MAIN))
111                ? HtmlTree.MAIN()
112                : body;
113        htmlTree.addContent(div);
114        HtmlTree divTree = new HtmlTree(HtmlTag.DIV);
115        divTree.addStyle(HtmlStyle.contentContainer);
116        addTree(classtree.baseClasses(), "doclet.Class_Hierarchy", divTree);
117        addTree(classtree.baseInterfaces(), "doclet.Interface_Hierarchy", divTree);
118        addTree(classtree.baseAnnotationTypes(), "doclet.Annotation_Type_Hierarchy", divTree);
119        addTree(classtree.baseEnums(), "doclet.Enum_Hierarchy", divTree, true);
120        htmlTree.addContent(divTree);
121        if (configuration.allowTag(HtmlTag.MAIN)) {
122            body.addContent(htmlTree);
123        }
124        if (configuration.allowTag(HtmlTag.FOOTER)) {
125            htmlTree = HtmlTree.FOOTER();
126        } else {
127            htmlTree = body;
128        }
129        addNavLinks(false, htmlTree);
130        addBottom(htmlTree);
131        if (configuration.allowTag(HtmlTag.FOOTER)) {
132            body.addContent(htmlTree);
133        }
134        printHtmlDocument(null, true, body);
135    }
136
137    /**
138     * Add the links to all the package tree files.
139     *
140     * @param contentTree the content tree to which the links will be added
141     */
142    protected void addPackageTreeLinks(Content contentTree) {
143        //Do nothing if only unnamed package is used
144        if (isUnnamedPackage()) {
145            return;
146        }
147        if (!classesOnly) {
148            Content span = HtmlTree.SPAN(HtmlStyle.packageHierarchyLabel,
149                    contents.packageHierarchies);
150            contentTree.addContent(span);
151            HtmlTree ul = new HtmlTree(HtmlTag.UL);
152            ul.addStyle(HtmlStyle.horizontal);
153            int i = 0;
154            for (PackageElement pkg : packages) {
155                // If the package name length is 0 or if -nodeprecated option
156                // is set and the package is marked as deprecated, do not include
157                // the page in the list of package hierarchies.
158                if (pkg.isUnnamed() ||
159                        (configuration.nodeprecated && utils.isDeprecated(pkg))) {
160                    i++;
161                    continue;
162                }
163                DocPath link = pathString(pkg, DocPaths.PACKAGE_TREE);
164                Content li = HtmlTree.LI(getHyperLink(link,
165                        new StringContent(utils.getPackageName(pkg))));
166                if (i < packages.size() - 1) {
167                    li.addContent(", ");
168                }
169                ul.addContent(li);
170                i++;
171            }
172            contentTree.addContent(ul);
173        }
174    }
175
176    /**
177     * Get the tree header.
178     *
179     * @return a content tree for the tree header
180     */
181    protected HtmlTree getTreeHeader() {
182        String title = configuration.getText("doclet.Window_Class_Hierarchy");
183        HtmlTree bodyTree = getBody(true, getWindowTitle(title));
184        HtmlTree htmlTree = (configuration.allowTag(HtmlTag.HEADER))
185                ? HtmlTree.HEADER()
186                : bodyTree;
187        addTop(htmlTree);
188        addNavLinks(true, htmlTree);
189        if (configuration.allowTag(HtmlTag.HEADER)) {
190            bodyTree.addContent(htmlTree);
191        }
192        return bodyTree;
193    }
194
195    private boolean isUnnamedPackage() {
196        return packages.size() == 1 && packages.first().isUnnamed();
197    }
198}
199