package jdk.javadoc.internal.doclets.formats.html;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.TreeSet;
import javax.lang.model.element.Element;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement;
import jdk.javadoc.internal.doclets.formats.html.markup.ContentBuilder;
import jdk.javadoc.internal.doclets.formats.html.markup.Entity;
import jdk.javadoc.internal.doclets.formats.html.markup.HtmlStyle;
import jdk.javadoc.internal.doclets.formats.html.markup.TagName;
import jdk.javadoc.internal.doclets.formats.html.markup.HtmlTree;
import jdk.javadoc.internal.doclets.formats.html.Navigation.PageMode;
import jdk.javadoc.internal.doclets.formats.html.markup.Text;
import jdk.javadoc.internal.doclets.toolkit.Content;
import jdk.javadoc.internal.doclets.toolkit.util.ClassUseMapper;
import jdk.javadoc.internal.doclets.toolkit.util.DocFileIOException;
import jdk.javadoc.internal.doclets.toolkit.util.DocPath;
import jdk.javadoc.internal.doclets.toolkit.util.DocPaths;
public class PackageUseWriter extends SubWriterHolderWriter {
final PackageElement packageElement;
final SortedMap<String, Set<TypeElement>> usingPackageToUsedClasses = new TreeMap<>();
public PackageUseWriter(HtmlConfiguration configuration,
ClassUseMapper mapper, DocPath filename,
PackageElement pkgElement) {
super(configuration, configuration.docPaths.forPackage(pkgElement).resolve(filename));
this.packageElement = pkgElement;
for (TypeElement usedClass : utils.getEnclosedTypeElements(pkgElement)) {
Set<TypeElement> usingClasses = mapper.classToClass.get(usedClass);
if (usingClasses != null) {
for (TypeElement usingClass : usingClasses) {
PackageElement usingPackage = utils.containingPackage(usingClass);
Set<TypeElement> usedClasses = usingPackageToUsedClasses
.get(utils.getPackageName(usingPackage));
if (usedClasses == null) {
usedClasses = new TreeSet<>(comparators.makeGeneralPurposeComparator());
usingPackageToUsedClasses.put(utils.getPackageName(usingPackage),
usedClasses);
}
usedClasses.add(usedClass);
}
}
}
}
public static void generate(HtmlConfiguration configuration,
ClassUseMapper mapper, PackageElement pkgElement)
throws DocFileIOException {
DocPath filename = DocPaths.PACKAGE_USE;
PackageUseWriter pkgusegen = new PackageUseWriter(configuration, mapper, filename, pkgElement);
pkgusegen.generatePackageUseFile();
}
protected void generatePackageUseFile() throws DocFileIOException {
HtmlTree body = getPackageUseHeader();
Content mainContent = new ContentBuilder();
if (usingPackageToUsedClasses.isEmpty()) {
mainContent.add(contents.getContent("doclet.ClassUse_No.usage.of.0", getLocalizedPackageName(packageElement)));
} else {
addPackageUse(mainContent);
}
bodyContents.addMainContent(mainContent);
bodyContents.setFooter(getFooter());
body.add(bodyContents);
printHtmlDocument(null,
getDescription("use", packageElement),
body);
}
protected void addPackageUse(Content contentTree) {
Content content = new ContentBuilder();
if (configuration.packages.size() > 1) {
addPackageList(content);
}
addClassList(content);
contentTree.add(content);
}
protected void addPackageList(Content contentTree) {
Content caption = contents.getContent(
"doclet.ClassUse_Packages.that.use.0",
getPackageLink(packageElement, getLocalizedPackageName(packageElement)));
Table table = new Table(HtmlStyle.summaryTable)
.setCaption(caption)
.setHeader(getPackageTableHeader())
.setColumnStyles(HtmlStyle.colFirst, HtmlStyle.colLast);
for (String pkgname: usingPackageToUsedClasses.keySet()) {
PackageElement pkg = utils.elementUtils.getPackageElement(pkgname);
Content packageLink = links.createLink(htmlIds.forPackage(pkg),
getLocalizedPackageName(pkg));
Content summary = new ContentBuilder();
if (pkg != null && !pkg.isUnnamed()) {
addSummaryComment(pkg, summary);
} else {
summary.add(Entity.NO_BREAK_SPACE);
}
table.addRow(packageLink, summary);
}
contentTree.add(table);
}
protected void addClassList(Content contentTree) {
TableHeader classTableHeader = new TableHeader(
contents.classLabel, contents.descriptionLabel);
HtmlTree ul = new HtmlTree(TagName.UL);
ul.setStyle(HtmlStyle.blockList);
for (String packageName : usingPackageToUsedClasses.keySet()) {
PackageElement usingPackage = utils.elementUtils.getPackageElement(packageName);
HtmlTree section = HtmlTree.SECTION(HtmlStyle.detail)
.setId(htmlIds.forPackage(usingPackage));
Content caption = contents.getContent(
"doclet.ClassUse_Classes.in.0.used.by.1",
getPackageLink(packageElement, getLocalizedPackageName(packageElement)),
getPackageLink(usingPackage, getLocalizedPackageName(usingPackage)));
Table table = new Table(HtmlStyle.summaryTable)
.setCaption(caption)
.setHeader(classTableHeader)
.setColumnStyles(HtmlStyle.colFirst, HtmlStyle.colLast);
for (TypeElement te : usingPackageToUsedClasses.get(packageName)) {
DocPath dp = pathString(te,
DocPaths.CLASS_USE.resolve(docPaths.forName(te)));
Content stringContent = Text.of(utils.getSimpleName(te));
Content typeContent = links.createLink(dp.fragment(htmlIds.forPackage(usingPackage).name()),
stringContent);
Content summary = new ContentBuilder();
addIndexComment(te, summary);
table.addRow(typeContent, summary);
}
section.add(table);
ul.add(HtmlTree.LI(section));
}
Content li = HtmlTree.SECTION(HtmlStyle.packageUses, ul);
contentTree.add(li);
}
private HtmlTree () {
String packageText = resources.getText("doclet.Package");
String name = packageElement.isUnnamed() ? "" : utils.getPackageName(packageElement);
String title = resources.getText("doclet.Window_ClassUse_Header", packageText, name);
HtmlTree bodyTree = getBody(getWindowTitle(title));
ContentBuilder headingContent = new ContentBuilder();
headingContent.add(contents.getContent("doclet.ClassUse_Title", packageText));
headingContent.add(new HtmlTree(TagName.BR));
headingContent.add(name);
Content heading = HtmlTree.HEADING_TITLE(Headings.PAGE_TITLE_HEADING,
HtmlStyle.title, headingContent);
Content div = HtmlTree.DIV(HtmlStyle.header, heading);
bodyContents.setHeader(getHeader(PageMode.USE, packageElement))
.addMainContent(div);
return bodyTree;
}
@Override
protected Navigation getNavBar(PageMode pageMode, Element element) {
Content linkContent = getModuleLink(utils.elementUtils.getModuleOf(element),
contents.moduleLabel);
return super.getNavBar(pageMode, element)
.setNavLinkModule(linkContent);
}
}