001/*
002 * Copyright (c) 1997, 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 java.util.*;
029
030import javax.lang.model.element.PackageElement;
031
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.Text;
035import org.jdrupes.mdoclet.internal.doclets.toolkit.Content;
036import org.jdrupes.mdoclet.internal.doclets.toolkit.util.DocFileIOException;
037import org.jdrupes.mdoclet.internal.doclets.toolkit.util.DocPath;
038import org.jdrupes.mdoclet.internal.doclets.toolkit.util.DocPaths;
039import org.jdrupes.mdoclet.internal.doclets.toolkit.util.Group;
040
041/**
042 * Generate the package index page "index.html".
043 */
044public class PackageIndexWriter extends AbstractOverviewIndexWriter {
045
046    /**
047     * A Set of Packages to be documented.
048     */
049    protected SortedSet<PackageElement> packages;
050
051    /**
052     * Construct the PackageIndexWriter. Also constructs the grouping
053     * information as provided on the command line by "-group" option. Stores
054     * the order of groups specified by the user.
055     *
056     * @param configuration the configuration for this doclet
057     * @param filename the path of the page to be generated
058     * @see Group
059     */
060    public PackageIndexWriter(HtmlConfiguration configuration,
061            DocPath filename) {
062        super(configuration, filename);
063        packages = configuration.packages;
064    }
065
066    /**
067     * Generate the package index page.
068     *
069     * @param configuration the current configuration of the doclet.
070     * @throws DocFileIOException if there is a problem generating the package index page
071     */
072    public static void generate(HtmlConfiguration configuration)
073            throws DocFileIOException {
074        DocPath filename = DocPaths.INDEX;
075        PackageIndexWriter packgen
076            = new PackageIndexWriter(configuration, filename);
077        packgen.buildOverviewIndexFile("doclet.Window_Overview_Summary",
078            "package index");
079    }
080
081    /**
082     * Adds the packages list to the documentation tree.
083     *
084     * @param target the content to which the packages list will be added
085     */
086    @Override
087    protected void addIndex(Content target) {
088        Map<String, SortedSet<PackageElement>> groupPackageMap
089            = configuration.group.groupPackages(packages);
090
091        if (!groupPackageMap.keySet().isEmpty()) {
092            var table = new Table<PackageElement>(HtmlStyle.summaryTable)
093                .setHeader(getPackageTableHeader())
094                .setColumnStyles(HtmlStyle.colFirst, HtmlStyle.colLast)
095                .setId(HtmlIds.ALL_PACKAGES_TABLE)
096                .setDefaultTab(contents.getContent("doclet.All_Packages"));
097
098            // add the tabs in command-line order
099            for (String groupName : configuration.group.getGroupList()) {
100                Set<PackageElement> groupPackages
101                    = groupPackageMap.get(groupName);
102                if (groupPackages != null) {
103                    table.addTab(Text.of(groupName), groupPackages::contains);
104                }
105            }
106
107            for (PackageElement pkg : configuration.packages) {
108                if (!(options.noDeprecated() && utils.isDeprecated(pkg))) {
109                    Content packageLinkContent
110                        = getPackageLink(pkg, getLocalizedPackageName(pkg));
111                    Content summaryContent = new ContentBuilder();
112                    addSummaryComment(pkg, summaryContent);
113                    table.addRow(pkg, packageLinkContent, summaryContent);
114                }
115            }
116
117            target.add(table);
118        }
119    }
120}