org.key_project.slicing.ui.HtmlFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of keyext.slicing Show documentation
Show all versions of keyext.slicing Show documentation
Computiation of the proof core (the essential rule applications to close a proof)
The newest version!
/* This file is part of KeY - https://key-project.org
* KeY is licensed under the GNU General Public License Version 2
* SPDX-License-Identifier: GPL-2.0-only */
package org.key_project.slicing.ui;
import java.util.Collection;
import java.util.Optional;
import javax.swing.*;
/**
* Utility class to generate HTML tables for UI purposes.
*
* @author Arne Keller
*/
public final class HtmlFactory {
private HtmlFactory() {
}
/**
* Generate an HTML table using the given column labels and row data.
*
* If clickable[i] is true, the cells in the i-th column will be links pointing to
* '#i_idx', where i is the column index and idx is generated by a call to the provided
* IndexFactory.
*
*
* If the alignment array is present, it must contain an entry for each column (null, "right"
* are useful values). The cells in that column will be aligned accordingly (default/null:
* left alignment).
*
*
* @param columnNames column labels
* @param clickable whether the cells in a column should be clickable links
* @param alignment text alignment of each column
* @param rows row data to display in the table
* @param indexFactory index factory
* @return HTML string
*/
public static String generateTable(
Collection columnNames,
boolean[] clickable,
Optional alignment,
Collection> rows,
IndexFactory indexFactory) {
if (columnNames.size() != clickable.length) {
throw new IllegalArgumentException();
}
StringBuilder stats = new StringBuilder("");
for (String a : columnNames) {
stats.append("").append(a).append(" ");
}
stats.append("");
for (Collection row : rows) {
stats.append("");
int i = 0;
for (String cell : row) {
stats.append("");
if (clickable[i]) {
stats.append("");
}
if (cell.startsWith("<")) {
// assume cell content is HTML
stats.append(cell);
} else {
stats.append(escapeText(cell));
}
if (clickable[i]) {
stats.append("");
}
stats.append(" ");
i++;
}
stats.append(" ");
}
stats.append("
");
return stats.toString();
}
/**
* Create a Swing component (JEditorPane) showing the given HTML document.
*
* @param html HTML document
* @return Swing component showing that HTML
*/
public static JComponent createComponent(String html) {
JEditorPane htmlContent = new JEditorPane("text/html", html);
htmlContent.setEditable(false);
htmlContent.setBorder(BorderFactory.createEmptyBorder());
htmlContent.setCaretPosition(0);
return htmlContent;
}
private static String escapeText(String text) {
return text
.replace("&", "&")
.replace("<", "<")
.replace(">", ">");
}
}