1 /*
   2  * Copyright (c) 1998, 2018, 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 
  26 package jdk.javadoc.internal.doclets.formats.html;
  27 
  28 import javax.lang.model.element.PackageElement;
  29 
  30 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlConstants;
  31 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlStyle;
  32 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlTag;
  33 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlTree;
  34 import jdk.javadoc.internal.doclets.formats.html.markup.Links;
  35 import jdk.javadoc.internal.doclets.toolkit.Content;
  36 import jdk.javadoc.internal.doclets.toolkit.util.ClassTree;
  37 import jdk.javadoc.internal.doclets.toolkit.util.DocFileIOException;
  38 import jdk.javadoc.internal.doclets.toolkit.util.DocPath;
  39 import jdk.javadoc.internal.doclets.toolkit.util.DocPaths;
  40 
  41 
  42 /**
  43  * Class to generate Tree page for a package. The name of the file generated is
  44  * "package-tree.html" and it is generated in the respective package directory.
  45  *
  46  *  <p><b>This is NOT part of any supported API.
  47  *  If you write code that depends on this, you do so at your own risk.
  48  *  This code and its internal interfaces are subject to change or
  49  *  deletion without notice.</b>
  50  *
  51  * @author Atul M Dambalkar
  52  * @author Bhavesh Patel (Modified)
  53  */
  54 public class PackageTreeWriter extends AbstractTreeWriter {
  55 
  56     /**
  57      * Package for which tree is to be generated.
  58      */
  59     protected PackageElement packageElement;
  60 
  61     /**
  62      * The previous package name in the alpha-order list.
  63      */
  64     protected PackageElement prev;
  65 
  66     /**
  67      * The next package name in the alpha-order list.
  68      */
  69     protected PackageElement next;
  70 
  71     /**
  72      * Constructor.
  73      * @param configuration the configuration
  74      * @param path the docpath to generate files into
  75      * @param packageElement the current package
  76      * @param prev the previous package
  77      * @param next the next package
  78      */
  79     public PackageTreeWriter(HtmlConfiguration configuration,
  80                              DocPath path,
  81                              PackageElement packageElement,
  82                              PackageElement prev, PackageElement next) {
  83         super(configuration, path,
  84               new ClassTree(configuration.typeElementCatalog.allClasses(packageElement), configuration));
  85         this.packageElement = packageElement;
  86         this.prev = prev;
  87         this.next = next;
  88     }
  89 
  90     /**
  91      * Construct a PackageTreeWriter object and then use it to generate the
  92      * package tree page.
  93      *
  94      * @param configuration the configuration for this run.
  95      * @param pkg      Package for which tree file is to be generated.
  96      * @param prev     Previous package in the alpha-ordered list.
  97      * @param next     Next package in the alpha-ordered list.
  98      * @param noDeprecated  If true, do not generate any information for
  99      * deprecated classe or interfaces.
 100      * @throws DocFileIOException if there is a problem generating the package tree page
 101      */
 102     public static void generate(HtmlConfiguration configuration,
 103                                 PackageElement pkg, PackageElement prev,
 104                                 PackageElement next, boolean noDeprecated)
 105             throws DocFileIOException {
 106         DocPath path = DocPath.forPackage(pkg).resolve(DocPaths.PACKAGE_TREE);
 107         PackageTreeWriter packgen = new PackageTreeWriter(configuration, path, pkg, prev, next);
 108         packgen.generatePackageTreeFile();
 109     }
 110 
 111     /**
 112      * Generate a separate tree file for each package.
 113      * @throws DocFileIOException if there is a problem generating the package tree file
 114      */
 115     protected void generatePackageTreeFile() throws DocFileIOException {
 116         HtmlTree body = getPackageTreeHeader();
 117         HtmlTree htmlTree = (configuration.allowTag(HtmlTag.MAIN))
 118                 ? HtmlTree.MAIN()
 119                 : body;
 120         Content headContent = contents.getContent("doclet.Hierarchy_For_Package",
 121                 utils.getPackageName(packageElement));
 122         Content heading = HtmlTree.HEADING(HtmlConstants.TITLE_HEADING, false,
 123                 HtmlStyle.title, headContent);
 124         Content div = HtmlTree.DIV(HtmlStyle.header, heading);
 125         if (configuration.packages.size() > 1) {
 126             addLinkToMainTree(div);
 127         }
 128         htmlTree.addContent(div);
 129         HtmlTree divTree = new HtmlTree(HtmlTag.DIV);
 130         divTree.setStyle(HtmlStyle.contentContainer);
 131         addTree(classtree.baseClasses(), "doclet.Class_Hierarchy", divTree);
 132         addTree(classtree.baseInterfaces(), "doclet.Interface_Hierarchy", divTree);
 133         addTree(classtree.baseAnnotationTypes(), "doclet.Annotation_Type_Hierarchy", divTree);
 134         addTree(classtree.baseEnums(), "doclet.Enum_Hierarchy", divTree, true);
 135         htmlTree.addContent(divTree);
 136         if (configuration.allowTag(HtmlTag.MAIN)) {
 137             body.addContent(htmlTree);
 138         }
 139         HtmlTree tree = (configuration.allowTag(HtmlTag.FOOTER))
 140                 ? HtmlTree.FOOTER()
 141                 : body;
 142         addNavLinks(false, tree);
 143         addBottom(tree);
 144         if (configuration.allowTag(HtmlTag.FOOTER)) {
 145             body.addContent(tree);
 146         }
 147         printHtmlDocument(null, true, body);
 148     }
 149 
 150     /**
 151      * Get the package tree header.
 152      *
 153      * @return a content tree for the header
 154      */
 155     protected HtmlTree getPackageTreeHeader() {
 156         String packageName = packageElement.isUnnamed() ? "" : utils.getPackageName(packageElement);
 157         String title = packageName + " " + configuration.getText("doclet.Window_Class_Hierarchy");
 158         HtmlTree bodyTree = getBody(true, getWindowTitle(title));
 159         HtmlTree htmlTree = (configuration.allowTag(HtmlTag.HEADER))
 160                 ? HtmlTree.HEADER()
 161                 : bodyTree;
 162         addTop(htmlTree);
 163         addNavLinks(true, htmlTree);
 164         if (configuration.allowTag(HtmlTag.HEADER)) {
 165             bodyTree.addContent(htmlTree);
 166         }
 167         return bodyTree;
 168     }
 169 
 170     /**
 171      * Add a link to the tree for all the packages.
 172      *
 173      * @param div the content tree to which the link will be added
 174      */
 175     protected void addLinkToMainTree(Content div) {
 176         Content span = HtmlTree.SPAN(HtmlStyle.packageHierarchyLabel,
 177                 contents.packageHierarchies);
 178         div.addContent(span);
 179         HtmlTree ul = new HtmlTree (HtmlTag.UL);
 180         ul.setStyle(HtmlStyle.horizontal);
 181         ul.addContent(getNavLinkMainTree(configuration.getText("doclet.All_Packages")));
 182         div.addContent(ul);
 183     }
 184 
 185     /**
 186      * Get link for the previous package tree file.
 187      *
 188      * @return a content tree for the link
 189      */
 190     @Override
 191     protected Content getNavLinkPrevious() {
 192         if (prev == null) {
 193             return getNavLinkPrevious(null);
 194         } else {
 195             DocPath path = DocPath.relativePath(packageElement, prev);
 196             return getNavLinkPrevious(path.resolve(DocPaths.PACKAGE_TREE));
 197         }
 198     }
 199 
 200     /**
 201      * Get link for the next package tree file.
 202      *
 203      * @return a content tree for the link
 204      */
 205     @Override
 206     protected Content getNavLinkNext() {
 207         if (next == null) {
 208             return getNavLinkNext(null);
 209         } else {
 210             DocPath path = DocPath.relativePath(packageElement, next);
 211             return getNavLinkNext(path.resolve(DocPaths.PACKAGE_TREE));
 212         }
 213     }
 214 
 215     /**
 216      * Get the module link.
 217      *
 218      * @return a content tree for the module link
 219      */
 220     @Override
 221     protected Content getNavLinkModule() {
 222         Content linkContent = getModuleLink(utils.elementUtils.getModuleOf(packageElement),
 223                 contents.moduleLabel);
 224         Content li = HtmlTree.LI(linkContent);
 225         return li;
 226     }
 227 
 228     /**
 229      * Get link to the package summary page for the package of this tree.
 230      *
 231      * @return a content tree for the package link
 232      */
 233     @Override
 234     protected Content getNavLinkPackage() {
 235         Content linkContent = links.createLink(DocPaths.PACKAGE_SUMMARY,
 236                 contents.packageLabel);
 237         Content li = HtmlTree.LI(linkContent);
 238         return li;
 239     }
 240 }