001/*
002 * Copyright (c) 2018, 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.PackageElement;
029
030import org.jdrupes.mdoclet.internal.doclets.formats.html.Navigation.PageMode;
031import org.jdrupes.mdoclet.internal.doclets.formats.html.markup.BodyContents;
032import org.jdrupes.mdoclet.internal.doclets.formats.html.markup.ContentBuilder;
033import org.jdrupes.mdoclet.internal.doclets.formats.html.markup.HtmlStyle;
034import org.jdrupes.mdoclet.internal.doclets.formats.html.markup.HtmlTree;
035import org.jdrupes.mdoclet.internal.doclets.formats.html.markup.Text;
036import org.jdrupes.mdoclet.internal.doclets.toolkit.Content;
037import org.jdrupes.mdoclet.internal.doclets.toolkit.util.DocFileIOException;
038import org.jdrupes.mdoclet.internal.doclets.toolkit.util.DocPath;
039import org.jdrupes.mdoclet.internal.doclets.toolkit.util.DocPaths;
040
041/**
042 * Generate the file with list of all the packages in this run.
043 */
044public class AllPackagesIndexWriter extends HtmlDocletWriter {
045
046    /**
047     * Construct AllPackagesIndexWriter object.
048     *
049     * @param configuration The current configuration
050     * @param filename Path to the file which is getting generated.
051     */
052    public AllPackagesIndexWriter(HtmlConfiguration configuration,
053            DocPath filename) {
054        super(configuration, filename);
055    }
056
057    /**
058     * Create AllPackagesIndexWriter object.
059     *
060     * @param configuration The current configuration
061     * @throws DocFileIOException
062     */
063    public static void generate(HtmlConfiguration configuration)
064            throws DocFileIOException {
065        generate(configuration, DocPaths.ALLPACKAGES_INDEX);
066    }
067
068    private static void generate(HtmlConfiguration configuration,
069            DocPath fileName) throws DocFileIOException {
070        AllPackagesIndexWriter allPkgGen
071            = new AllPackagesIndexWriter(configuration, fileName);
072        allPkgGen.buildAllPackagesFile();
073    }
074
075    /**
076     * Print all the packages in the file.
077     */
078    protected void buildAllPackagesFile() throws DocFileIOException {
079        String label = resources.getText("doclet.All_Packages");
080        Content mainContent = new ContentBuilder();
081        addPackages(mainContent);
082        Content titleContent = contents.allPackagesLabel;
083        var pHeading = HtmlTree.HEADING_TITLE(Headings.PAGE_TITLE_HEADING,
084            HtmlStyle.title, titleContent);
085        var headerDiv = HtmlTree.DIV(HtmlStyle.header, pHeading);
086        HtmlTree body = getBody(getWindowTitle(label));
087        body.add(new BodyContents()
088            .setHeader(getHeader(PageMode.ALL_PACKAGES))
089            .addMainContent(headerDiv)
090            .addMainContent(mainContent)
091            .setFooter(getFooter()));
092        printHtmlDocument(null, "package index", body);
093    }
094
095    /**
096     * Add all the packages to the content.
097     *
098     * @param target the content to which the links will be added
099     */
100    protected void addPackages(Content target) {
101        var table = new Table<PackageElement>(HtmlStyle.summaryTable)
102            .setCaption(Text.of(contents.packageSummaryLabel.toString()))
103            .setHeader(new TableHeader(contents.packageLabel,
104                contents.descriptionLabel))
105            .setColumnStyles(HtmlStyle.colFirst, HtmlStyle.colLast);
106        for (PackageElement pkg : configuration.packages) {
107            if (!(options.noDeprecated() && utils.isDeprecated(pkg))) {
108                Content packageLinkContent
109                    = getPackageLink(pkg, getLocalizedPackageName(pkg));
110                Content summaryContent = new ContentBuilder();
111                addSummaryComment(pkg, summaryContent);
112                table.addRow(pkg, packageLinkContent, summaryContent);
113            }
114        }
115        target.add(table);
116    }
117}