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