1 /* 2 * Copyright (c) 2017, 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 java.util.Arrays; 29 import java.util.List; 30 import java.util.stream.Collectors; 31 32 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlStyle; 33 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlTag; 34 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlTree; 35 import jdk.javadoc.internal.doclets.toolkit.Content; 36 37 /** 38 * A row header for an HTML table. 39 * 40 * The header contains a list of {@code <th>} cells, providing the column headers. 41 * The attribute {@code scope="col"} is automatically added to each header cell. 42 * In addition, a series of style class names can be specified, to be applied one per cell. 43 * 44 */ 45 public class TableHeader { 46 47 /** 48 * The content to be put in each of the {@code <th>} cells in the header row. 49 */ 50 private final List<Content> cellContents; 51 /** 52 * The style class names for each of the {@code <th>} cells in the header row. 53 * If not set, default style names will be used. 54 */ 55 private List<HtmlStyle> styles; 56 57 /** 58 * Creates a header row, with localized content for each cell. 59 * Resources keys will be converted to content using {@link Contents#getContent(String)}. 60 * @param contents a factory to get the content for each header cell. 61 * @param colHeaderKeys the resource keys for the content in each cell. 62 */ 63 TableHeader(Contents contents, String... colHeaderKeys) { 64 this.cellContents = Arrays.stream(colHeaderKeys) 65 .map((key) -> contents.getContent(key)) 66 .collect(Collectors.toList()); 67 } 68 69 /** 70 * Creates a header row, with specified content for each cell. 71 * @param headerCellContents a content object for each header cell 72 */ 73 TableHeader(Content... headerCellContents) { 74 this.cellContents = Arrays.asList(headerCellContents); 75 } 76 77 /** 78 * Set the style class names for each header cell. 79 * The number of names must match the number of cells given to the constructor. 80 * @param styles the style class names 81 * @return this object 82 */ 83 TableHeader styles(HtmlStyle... styles) { 84 if (styles.length != cellContents.size()) { 85 throw new IllegalStateException(); 86 } 87 this.styles = Arrays.asList(styles); 88 return this; 89 } 90 91 /** 92 * Converts this header to a {@link Content} object, for use in an {@link HtmlTree}. 93 * @returns a Content object 94 */ 95 Content toContent() { 96 String scope = "col"; 97 Content tr = new HtmlTree(HtmlTag.TR); 98 int i = 0; 99 for (Content cellContent : cellContents) { 100 HtmlStyle style = (styles != null) ? styles.get(i) 101 : (i == 0) ? HtmlStyle.colFirst 102 : (i == (cellContents.size() - 1)) ? HtmlStyle.colLast 103 : (i == 1) ? HtmlStyle.colSecond : null; 104 Content cell = (style == null) ? HtmlTree.TH(scope, cellContent) 105 : HtmlTree.TH(style, scope, cellContent); 106 tr.addContent(cell); 107 i++; 108 } 109 return tr; 110 } 111 112 }