001/*
002 * Copyright (c) 1998, 2022, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.  Oracle designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Oracle in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
022 * or visit www.oracle.com if you need additional information or have any
023 * questions.
024 */
025
026package org.jdrupes.mdoclet.internal.doclets.formats.html;
027
028import javax.lang.model.element.Element;
029import javax.lang.model.element.PackageElement;
030
031import org.jdrupes.mdoclet.internal.doclets.formats.html.Navigation.PageMode;
032import org.jdrupes.mdoclet.internal.doclets.formats.html.markup.BodyContents;
033import org.jdrupes.mdoclet.internal.doclets.formats.html.markup.ContentBuilder;
034import org.jdrupes.mdoclet.internal.doclets.formats.html.markup.HtmlStyle;
035import org.jdrupes.mdoclet.internal.doclets.formats.html.markup.HtmlTree;
036import org.jdrupes.mdoclet.internal.doclets.toolkit.Content;
037import org.jdrupes.mdoclet.internal.doclets.toolkit.util.ClassTree;
038import org.jdrupes.mdoclet.internal.doclets.toolkit.util.DocFileIOException;
039import org.jdrupes.mdoclet.internal.doclets.toolkit.util.DocPath;
040import org.jdrupes.mdoclet.internal.doclets.toolkit.util.DocPaths;
041
042/**
043 * Class to generate Tree page for a package. The name of the file generated is
044 * "package-tree.html" and it is generated in the respective package directory.
045 */
046public class PackageTreeWriter extends AbstractTreeWriter {
047
048    /**
049     * Package for which tree is to be generated.
050     */
051    protected PackageElement packageElement;
052
053    private final BodyContents bodyContents = new BodyContents();
054
055    /**
056     * Constructor.
057     * @param configuration the configuration
058     * @param path the docpath to generate files into
059     * @param packageElement the current package
060     */
061    public PackageTreeWriter(HtmlConfiguration configuration, DocPath path,
062            PackageElement packageElement) {
063        super(configuration, path,
064            new ClassTree(
065                configuration.typeElementCatalog.allClasses(packageElement),
066                configuration));
067        this.packageElement = packageElement;
068    }
069
070    /**
071     * Construct a PackageTreeWriter object and then use it to generate the
072     * package tree page.
073     *
074     * @param configuration the configuration for this run.
075     * @param pkg      Package for which tree file is to be generated.
076     * @param noDeprecated  If true, do not generate any information for
077     * deprecated classes or interfaces.
078     * @throws DocFileIOException if there is a problem generating the package tree page
079     */
080    public static void generate(HtmlConfiguration configuration,
081            PackageElement pkg, boolean noDeprecated)
082            throws DocFileIOException {
083        DocPath path = configuration.docPaths.forPackage(pkg)
084            .resolve(DocPaths.PACKAGE_TREE);
085        PackageTreeWriter packgen
086            = new PackageTreeWriter(configuration, path, pkg);
087        packgen.generatePackageTreeFile();
088    }
089
090    /**
091     * Generate a separate tree file.
092     *
093     * @throws DocFileIOException if there is a problem generating the package tree file
094     */
095    protected void generatePackageTreeFile() throws DocFileIOException {
096        HtmlTree body = getPackageTreeHeader();
097        Content mainContent = new ContentBuilder();
098        Content headContent = packageElement.isUnnamed()
099            ? contents.getContent("doclet.Hierarchy_For_Unnamed_Package")
100            : contents.getContent("doclet.Hierarchy_For_Package",
101                getLocalizedPackageName(packageElement));
102        var heading = HtmlTree.HEADING(Headings.PAGE_TITLE_HEADING,
103            HtmlStyle.title, headContent);
104        var div = HtmlTree.DIV(HtmlStyle.header, heading);
105        mainContent.add(div);
106        if (configuration.packages.size() > 1) {
107            addLinkToAllPackages(mainContent);
108        }
109        addTree(classTree.classes(), "doclet.Class_Hierarchy", mainContent);
110        addTree(classTree.interfaces(), "doclet.Interface_Hierarchy",
111            mainContent);
112        addTree(classTree.annotationInterfaces(),
113            "doclet.Annotation_Type_Hierarchy", mainContent);
114        addTree(classTree.enumClasses(), "doclet.Enum_Hierarchy", mainContent);
115        addTree(classTree.recordClasses(), "doclet.Record_Class_Hierarchy",
116            mainContent);
117        bodyContents.addMainContent(mainContent);
118        bodyContents.setFooter(getFooter());
119        body.add(bodyContents);
120        printHtmlDocument(null, getDescription("tree", packageElement), body);
121    }
122
123    /**
124     * Get the package tree header.
125     *
126     * @return the package tree header
127     */
128    protected HtmlTree getPackageTreeHeader() {
129        String packageName = packageElement.isUnnamed() ? ""
130            : utils.getPackageName(packageElement);
131        String title = packageName + " "
132            + resources.getText("doclet.Window_Class_Hierarchy");
133        HtmlTree body = getBody(getWindowTitle(title));
134        bodyContents.setHeader(getHeader(PageMode.TREE, packageElement));
135        return body;
136    }
137
138    @Override
139    protected Navigation getNavBar(PageMode pageMode, Element element) {
140        Content linkContent
141            = getModuleLink(utils.elementUtils.getModuleOf(packageElement),
142                contents.moduleLabel);
143        return super.getNavBar(pageMode, element)
144            .setNavLinkModule(linkContent);
145    }
146
147    /**
148     * Add a link to the tree for all the packages.
149     *
150     * @param target the content to which the link will be added
151     */
152    protected void addLinkToAllPackages(Content target) {
153        var span = HtmlTree.SPAN(HtmlStyle.packageHierarchyLabel,
154            contents.packageHierarchies);
155        target.add(span);
156        var ul = HtmlTree.UL(HtmlStyle.horizontal)
157            .addStyle(HtmlStyle.contentsList);
158        // TODO the link should be more specific:
159        // it should point to the "all packages" section of the overview tree
160        ul.add(
161            getNavLinkToOverviewTree(resources.getText("doclet.All_Packages")));
162        target.add(ul);
163    }
164}