-
Notifications
You must be signed in to change notification settings - Fork 5.2k
/
TableHeader.java
176 lines (160 loc) · 6.27 KB
/
TableHeader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
* Copyright (c) 2017, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.javadoc.internal.doclets.formats.html;
import java.io.IOException;
import java.io.Writer;
import java.util.Arrays;
import java.util.List;
import jdk.javadoc.internal.doclets.formats.html.markup.ContentBuilder;
import jdk.javadoc.internal.doclets.formats.html.markup.HtmlAttr;
import jdk.javadoc.internal.doclets.formats.html.markup.HtmlStyle;
import jdk.javadoc.internal.doclets.formats.html.markup.HtmlTree;
import jdk.javadoc.internal.doclets.toolkit.Content;
/**
* A row of header cells for an HTML table.
*
* The header contains a list of {@code <th>} cells, providing the column headers.
* The attribute {@code scope="col"} is automatically added to each header cell.
* In addition, a series of style class names can be specified, to be applied one per cell.
*/
public class TableHeader extends Content {
/**
* The content to be put in each of the {@code <th>} cells in the header row.
*/
private final List<Content> cellContents;
/**
* The style class names for each of the {@code <th>} cells in the header row.
* If not set, default style names will be used.
*/
private List<HtmlStyle> styles;
private boolean[] sortable;
/**
* Creates a header row, with localized content for each cell.
* Resources keys will be converted to content using {@link Contents#getContent(String)}.
* @param contents a factory to get the content for each header cell.
* @param colHeaderKeys the resource keys for the content in each cell.
*/
public TableHeader(Contents contents, String... colHeaderKeys) {
this.cellContents = Arrays.stream(colHeaderKeys)
.map(contents::getContent)
.toList();
}
/**
* Creates a header row, with specified content for each cell.
* @param headerCellContents a content object for each header cell
*/
public TableHeader(Content... headerCellContents) {
this.cellContents = Arrays.asList(headerCellContents);
}
/**
* Creates a header row, with specified content for each cell.
* @param headerCellContents a content object for each header cell
*/
public TableHeader(List<Content> headerCellContents) {
this.cellContents = headerCellContents;
}
/**
* Set the style class names for each header cell.
* The number of names must match the number of cells given to the constructor.
* @param styles the style class names
* @return this object
*/
public TableHeader styles(HtmlStyle... styles) {
if (styles.length != cellContents.size()) {
throw new IllegalStateException();
}
this.styles = Arrays.asList(styles);
return this;
}
/**
* Makes the table sortable by the content of columns for which the
* argument boolean array contains {@code true}.
* @param sortable boolean array specifying sortable columns
* @return this object
*/
public TableHeader sortable(boolean... sortable) {
if (sortable.length != cellContents.size()) {
throw new IllegalStateException();
}
this.sortable = sortable;
return this;
}
/**
* Set the style class names for each header cell.
* The number of names must match the number of cells given to the constructor.
* @param styles the style class names
* @return this object
*/
public TableHeader styles(List<HtmlStyle> styles) {
if (styles.size() != cellContents.size()) {
throw new IllegalStateException();
}
this.styles = styles;
return this;
}
/**
* {@inheritDoc}
*
* @implSpec This implementation always returns {@code false}.
*
* @return {@code false}
*/
@Override
public boolean isEmpty() {
return false;
}
@Override
public boolean write(Writer out, String newline, boolean atNewline) throws IOException {
return toContent().write(out, newline, atNewline);
}
/**
* Converts this header to a {@link Content} object, for use in an {@link HtmlTree}.
* @return a Content object
*/
private Content toContent() {
Content header = new ContentBuilder();
int i = 0;
for (Content cellContent : cellContents) {
HtmlStyle style = (styles != null) ? styles.get(i)
: (i == 0) ? HtmlStyle.colFirst
: (i == (cellContents.size() - 1)) ? HtmlStyle.colLast
: (i == 1) ? HtmlStyle.colSecond : null;
var cell = HtmlTree.DIV(HtmlStyle.tableHeader, cellContent);
if (style != null) {
cell.addStyle(style);
}
if (sortable != null && sortable[i]) {
cell.put(HtmlAttr.ONCLICK, "sortTable(this, " + i + ", " + sortable.length +")");
// Current tables happen to be sorted by first column by default, this may not hold true for future uses.
if (i == 0) {
cell.addStyle("sort-asc");
}
}
header.add(cell);
i++;
}
return header;
}
}