public code v1
This commit is contained in:
+310
@@ -0,0 +1,310 @@
|
||||
package flintstones.helper.html.table;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.eclipse.swt.widgets.Composite;
|
||||
import org.eclipse.swt.widgets.Control;
|
||||
|
||||
import flintstones.helper.html.HtmlWidget;
|
||||
import flintstones.helper.html.tags.B;
|
||||
import flintstones.helper.html.tags.HtmlTag;
|
||||
import flintstones.helper.html.tags.Table;
|
||||
import flintstones.helper.html.tags.Td;
|
||||
import flintstones.helper.html.tags.Text;
|
||||
import flintstones.helper.html.tags.Tr;
|
||||
|
||||
/**
|
||||
* The Class HtmlTable.
|
||||
*/
|
||||
class GenericHtmlTable extends HtmlWidget {
|
||||
|
||||
private int row = 0;
|
||||
private int col = 0;
|
||||
|
||||
private int numRows = 0;
|
||||
private int numCols = 0;
|
||||
|
||||
private Composite base;
|
||||
private Map<HtmlTag, String[]> content;
|
||||
|
||||
/**
|
||||
* Instantiates a new html table.
|
||||
*
|
||||
* @param base the base
|
||||
* @param values the values
|
||||
* @param vertical the vertical
|
||||
*/
|
||||
public GenericHtmlTable(Composite base, HtmlTag[] values, boolean vertical) {
|
||||
super(base);
|
||||
|
||||
this.base = base;
|
||||
|
||||
content = new LinkedHashMap<>();
|
||||
|
||||
HtmlTag table = body.add(new Table());
|
||||
if (vertical) {
|
||||
numRows = values.length;
|
||||
numCols = 1;
|
||||
for(int i = 0; i < values.length; i++) {
|
||||
HtmlTag[] arr = new HtmlTag[1];
|
||||
arr[0] = values[i];
|
||||
addRow(table, arr);
|
||||
}
|
||||
} else {
|
||||
numRows = 1;
|
||||
numCols = values.length;
|
||||
addRow(table, values);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiates a new html table.
|
||||
*
|
||||
* @param base the base
|
||||
* @param values the values
|
||||
* @param headers the headers
|
||||
* @param vertical the vertical
|
||||
*/
|
||||
public GenericHtmlTable(Composite base, HtmlTag[] values, String[] headers, boolean vertical) {
|
||||
super(base);
|
||||
|
||||
this.base = base;
|
||||
|
||||
content = new LinkedHashMap<>();
|
||||
|
||||
HtmlTag table = body.add(new Table());
|
||||
if (vertical) {
|
||||
numRows = values.length;
|
||||
numCols = 1;
|
||||
for(int i = 0; i < values.length; i++) {
|
||||
HtmlTag[] arr = new HtmlTag[1];
|
||||
arr[0] = values[i];
|
||||
addRow(table, arr, headers[i]);
|
||||
}
|
||||
} else {
|
||||
numRows = 1;
|
||||
numCols = values.length;
|
||||
addHeaderRow(table, headers, false);
|
||||
addRow(table, values);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiates a new html table.
|
||||
*
|
||||
* @param base the base
|
||||
* @param values the values
|
||||
* @param colHeaders the col headers
|
||||
* @param rowHeaders the row headers
|
||||
*/
|
||||
public GenericHtmlTable(Composite base, HtmlTag[][] values, String[] colHeaders, String[] rowHeaders) {
|
||||
super(base);
|
||||
|
||||
this.base = base;
|
||||
|
||||
content = new LinkedHashMap<>();
|
||||
|
||||
numRows = values.length;
|
||||
numCols = values[0].length;
|
||||
|
||||
HtmlTag table = body.add(new Table());
|
||||
addHeaderRow(table, colHeaders, true);
|
||||
|
||||
for(int i = 0; i < values.length; i++)
|
||||
addRow(table, values[i], rowHeaders[i]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiates a new html table.
|
||||
*
|
||||
* @param base the base
|
||||
* @param values the values
|
||||
* @param colHeaders the col headers
|
||||
*/
|
||||
public GenericHtmlTable(Composite base, HtmlTag[][] values, String[] colHeaders) {
|
||||
super(base);
|
||||
|
||||
this.base = base;
|
||||
|
||||
content = new LinkedHashMap<>();
|
||||
|
||||
numRows = values.length;
|
||||
numCols = values[0].length;
|
||||
|
||||
HtmlTag table = body.add(new Table());
|
||||
|
||||
addHeaderRow(table, colHeaders, false);
|
||||
|
||||
for(int i = 0; i < values.length; i++)
|
||||
addRow(table, values[i]);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the cell.
|
||||
*
|
||||
* @param content the content
|
||||
*/
|
||||
private void addCell(HtmlTag tr, HtmlTag content) {
|
||||
String[] classes = new String[] {
|
||||
"row_" + row,
|
||||
"col_" + col,
|
||||
"value_" + content
|
||||
};
|
||||
|
||||
HtmlTag td = tr.add(new Td(classes));
|
||||
td.add(content);
|
||||
|
||||
this.content.put(td, classes);
|
||||
|
||||
col++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the header cell.
|
||||
*
|
||||
* @param content the content
|
||||
* @param isHorizontal the is horizontal
|
||||
*/
|
||||
private void addHeaderCell(HtmlTag tr, String content, boolean isHorizontal) {
|
||||
|
||||
String cssClass = isHorizontal ? "table__header-horizontal" : "table__header-vertical";
|
||||
HtmlTag td = tr.add(new Td(new String[] {"table__header", cssClass}));
|
||||
|
||||
HtmlTag b = td.add(new B());
|
||||
b.add(new Text(content));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the row.
|
||||
*
|
||||
* @param values the values
|
||||
*/
|
||||
private void addRow(HtmlTag table, HtmlTag[] values) {
|
||||
this.addRow(table, values, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the row.
|
||||
*
|
||||
* @param values the values
|
||||
* @param rowHeader the row header
|
||||
*/
|
||||
private void addRow(HtmlTag table, HtmlTag[] values, String rowHeader) {
|
||||
|
||||
HtmlTag tr = table.add(new Tr());
|
||||
|
||||
if (rowHeader != null)
|
||||
addHeaderCell(tr, rowHeader, false);
|
||||
|
||||
for (HtmlTag value : values)
|
||||
addCell(tr, value);
|
||||
|
||||
col = 0;
|
||||
row++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the header row.
|
||||
*
|
||||
* @param values the values
|
||||
* @param emptyCell the empty cell
|
||||
*/
|
||||
private void addHeaderRow(HtmlTag table, String[] values, boolean emptyCell) {
|
||||
|
||||
HtmlTag tr = table.add(new Tr());
|
||||
|
||||
if(emptyCell)
|
||||
addHeaderCell(tr, "", true);
|
||||
|
||||
for (String value : values)
|
||||
addHeaderCell(tr, value, true);
|
||||
}
|
||||
|
||||
public int getNumberOfRows() {
|
||||
return numRows;
|
||||
}
|
||||
|
||||
public int getNumberOfColumns() {
|
||||
return numCols;
|
||||
}
|
||||
|
||||
public void refresh(String[][] values) {
|
||||
HtmlTag[][] valuesTag = Text.asText(values);
|
||||
|
||||
int row, col;
|
||||
String classes[];
|
||||
for(int i = 0; i < valuesTag.length; ++i) {
|
||||
for(int j = 0; j < valuesTag[i].length; ++j) {
|
||||
for(HtmlTag tag: content.keySet()) {
|
||||
classes = content.get(tag);
|
||||
row = Integer.parseInt(classes[0].replace("row_", ""));
|
||||
col = Integer.parseInt(classes[1].replace("col_", ""));
|
||||
if(row == i && col == j) {
|
||||
tag.clear();
|
||||
tag.deleteClass("value_");
|
||||
tag.add(valuesTag[row][col]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void refresh(String[] values) {
|
||||
HtmlTag[] valuesTag = Text.asText(values);
|
||||
|
||||
int col;
|
||||
String classes[];
|
||||
for(int i = 0; i < valuesTag.length; ++i) {
|
||||
for(HtmlTag tag: content.keySet()) {
|
||||
classes = content.get(tag);
|
||||
col = Integer.parseInt(classes[1].replace("col_", ""));
|
||||
if(col == i) {
|
||||
tag.clear();
|
||||
tag.deleteClass("value_");
|
||||
tag.add(valuesTag[col]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void dispose() {
|
||||
clearContent();
|
||||
for(Control child: base.getChildren())
|
||||
child.dispose();
|
||||
numCols = 0;
|
||||
numRows = 0;
|
||||
row = 0;
|
||||
col = 0;
|
||||
}
|
||||
|
||||
public void clearContent() {
|
||||
for(HtmlTag tag: content.keySet())
|
||||
tag.remove();
|
||||
content.clear();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see flintstones.helper.html.table.HtmlWidget#getResult()
|
||||
*/
|
||||
@Override
|
||||
public String getResult() {
|
||||
return body.toString();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see flintstones.helper.html.table.HtmlWidget#getStyle()
|
||||
*/
|
||||
@Override
|
||||
protected String getStyle() {
|
||||
return "table, th, td { border: 1px solid black; }"
|
||||
+ ".table__header{ background-color: aliceblue; width: 10%; white-space: nowrap; }"
|
||||
+ "table { border-collapse: collapse; }"
|
||||
+ "td{ padding: 5px; background-color: white; }"
|
||||
+ "table{ width: 100%; font-size: 13px; }";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package flintstones.helper.html.table;
|
||||
|
||||
import org.eclipse.swt.widgets.Composite;
|
||||
|
||||
import flintstones.helper.html.tags.HtmlTag;
|
||||
|
||||
public class HtmlComboTable extends GenericHtmlTable {
|
||||
|
||||
public HtmlComboTable(Composite base, HtmlTag[][] values, String[] colHeaders, String[] rowHeaders) {
|
||||
super(base, values, colHeaders, rowHeaders);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package flintstones.helper.html.table;
|
||||
|
||||
import org.eclipse.swt.widgets.Composite;
|
||||
|
||||
import flintstones.helper.html.tags.Text;
|
||||
|
||||
public class HtmlTextTable extends GenericHtmlTable {
|
||||
|
||||
public HtmlTextTable(Composite base, String[] values, boolean vertical) {
|
||||
super(base, Text.asText(values), vertical);
|
||||
}
|
||||
|
||||
public HtmlTextTable(Composite base, String[] values, String[] headers, boolean vertical) {
|
||||
super(base, Text.asText(values), headers, vertical);
|
||||
}
|
||||
|
||||
public HtmlTextTable(Composite base, String[][] values, String[] colHeaders, String[] rowHeaders) {
|
||||
super(base, Text.asText(values), colHeaders, rowHeaders);
|
||||
}
|
||||
|
||||
public HtmlTextTable(Composite base, String[][] values, String[] colHeaders) {
|
||||
super(base, Text.asText(values), colHeaders);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user