Yes I see now that it is returning null. I think there’s something wrong in the companion method. For some reason the table ID always return null no matter what ID I set it to.
At the top of the class, I have this:
TreeTable treeTable = null;
Here’s my method:
public String initBox(GroupBoxLayout groupBoxLayout, String delimiter, String htmlData, String ignoreType, Double ignoreValue,
boolean differenceOnly) {
log.info("in the companion method");
//log.debug(htmlData);
/*try {
File compareFile = new File("htmldata.xml");
FileUtils.writeStringToFile(compareFile,htmlData);
}
catch (IOException e)
{
log.error("could not write htmldata.xml file");
}*/
log.info("Done printing the data");
com.vaadin.ui.Panel panel = (com.vaadin.ui.Panel) WebComponentsHelper.unwrap(groupBoxLayout);
((com.vaadin.ui.Layout) panel.getContent()).removeAllComponents();
/*if(table != null)
{
((com.vaadin.ui.Layout) panel.getContent()).removeAllComponents();
}*/
//com.vaadin.ui.Grid grid = new com.vaadin.ui.Grid();
treeTable = new com.vaadin.ui.TreeTable();
treeTable.setId("ResultTable");
log.info("Created new table with ID " + treeTable.getId());
IndexedContainer ic = new IndexedContainer();
Document doc = Jsoup.parse(htmlData);
htmlData = null;
Elements tables = doc.getElementsByTag("table");
Element htmlTable = tables.get(1);
Elements rows = RemoveLinks(htmlTable);
if(rows.size() < 2)
{
//Create header table
Table headerTable = new Table();
headerTable.addContainerProperty("No records were returned",String.class,"");
//headerTable.setHeight("10%");
headerTable.setWidth("100%");
((com.vaadin.ui.Layout) panel.getContent()).addComponent(headerTable);
return headerTable.getId();
}
int headerRowNum = GetHeaderRowPos(rows, delimiter);
Element headerRow = rows.get(headerRowNum);
Elements tdHeaders = headerRow.getElementsByTag("td");
Element refHeaders = tdHeaders.get(1);
String headers = AppendDiffColumns(refHeaders.text(),delimiter);
String[] finalHeaders = headers.split(delimiter);
for(int index = 0; index < finalHeaders.length; index=index+2)
if(finalHeaders[index].equals("")) {
finalHeaders[index] = "Header" + index;
finalHeaders[index+1] = "Header" + index + finalHeaders[index+1];
}
treeTable.setColumnCollapsingAllowed(true);
//Add headers to container
Integer headerNum = 0;
for(String p : finalHeaders)
{
treeTable.addContainerProperty(p,String.class,"");
if(p.endsWith("Diff"))
treeTable.setColumnCollapsed(p,true);
}
//Add rowclass flag header
treeTable.addContainerProperty("class", String.class,"");
try {
for (int i = headerRowNum+1; i < rows.size(); i++) {
Elements cells = rows.get(i).getElementsByTag("td");
Element refCells = cells.get(1);
Element compCells = cells.get(3);
String refCellsDat = AppendDiffData(refCells.text(),delimiter);
String compCellsDat = AppendDiffData(compCells.text(),delimiter);
String[] refCellsData = refCellsDat.split(delimiter, -1);
String[] compCellsData = compCellsDat.split(delimiter,-1);
if (refCellsData.length < finalHeaders.length) {
//fill the ref data with blank values
refCellsData = new String[finalHeaders.length];
Arrays.fill(refCellsData, "");
}
if (compCellsData.length < finalHeaders.length) {
//fill the ref data with blank values
compCellsData = new String[finalHeaders.length];
Arrays.fill(compCellsData, "");
}
String[] refAttr = new String[]{refCells.attr("class")};
String[] compAttr = new String[]{compCells.attr("class")};
//Determine whether the rows are the same or if they are different
boolean isSame = isSameRows(refCells, compCells);
String[] arrayRef = new String[refCellsData.length + refAttr.length];
String[] arrayComp = new String[compCellsData.length + compAttr.length];
System.arraycopy(refCellsData, 0, arrayRef, 0, refCellsData.length);
System.arraycopy(refAttr, 0, arrayRef, refCellsData.length, refAttr.length);
System.arraycopy(compCellsData, 0, arrayComp, 0, compCellsData.length);
System.arraycopy(compAttr, 0, arrayComp, compCellsData.length, compAttr.length);
if (isSame) {
Object itemRefId = ic.addItem();
//Object itemCompId = ic.addItem();
if(differenceOnly == false)
treeTable.addItem(arrayRef, itemRefId);
//table.addItem(arrayComp, itemCompId);
//log.debug("Added:" + Arrays.deepToString(arrayRef));
//log.debug("Added:" + Arrays.deepToString(arrayComp));
} else {
//the rows are different so we need to determine which fields are different
Object itemRefId = ic.addItem();
Object itemCompId = ic.addItem();
for (int j = 0; j < arrayRef.length-1; j=j+2) {
if (!arrayRef[j].equals(arrayComp[j])) {
String difference = GetDifference(arrayRef[j],arrayComp[j],ignoreType,ignoreValue);
if(!difference.equals("")) {
arrayRef[j] = "[" + arrayRef[j] + "]";
arrayComp[j] = "[" + arrayComp[j] + "]";
arrayComp[j + 1] = "[" + difference + "]";
}
}
}
treeTable.addItem(arrayRef, itemRefId);
treeTable.addItem(arrayComp, itemCompId);
treeTable.setParent(itemCompId, itemRefId);
treeTable.setChildrenAllowed(itemCompId,false);
}
}
}
catch(Exception e)
{
log.error("Error adding rows: " + e.getMessage());
}
log.info("number of rows=" + rows.size());
//table.setContainerDataSource(ic);
//table.setWidth("100%");
treeTable.addStyleName("comparison");
// Set cell style generator
treeTable.setCellStyleGenerator(new Table.CellStyleGenerator() {
@Override
public String getStyle(Table source, Object itemId, Object propertyId) {
if(propertyId != null ) {
Item item = source.getItem(itemId);
if(item.getItemProperty(propertyId).getValue().getClass() == String.class) {
String rowValue = (String) item.getItemProperty("class").getValue();
String cellValue = (String)item.getItemProperty(propertyId).getValue();
if(rowValue.equals("ch1") || rowValue.equals("del"))
{
if(cellValue.startsWith("["))
return "yellow";
else
return "reference";
}
else if(rowValue.equals("ch2") || rowValue.equals("ins"))
{
if(cellValue.startsWith("["))
return "yellow";
else
return "compare";
}
else {
return null;
}
} else {
return null;
}
} else {
return null;
}
}
});
treeTable.setVisibleColumns(finalHeaders);
treeTable.setSizeFull();
//table.setSizeUndefined();
//table.setPageLength(table.size());
//( panel.getContent()).setSizeUndefined();
((com.vaadin.ui.Layout) panel.getContent()).addComponent(treeTable);
return treeTable.getId();
}