Data Model Class - Datamodel
ArrayList = dataArray (stores data for tableviewer) clearArray() - Method that clears the array
Table Viewer Class - DataTableViewer
The last column of the table displays a button
Dialog Class - DataDialog
Create Table Viewer:
viewer.setInput(DataModel.getInstance().getArrayData());
Dialog Button = Clear Results:
DataModel.getInstance().clearArray();
viewer.refresh();
Issue
When the user clicks the Clear Results button. It removes the displayed data from the table. But the buttons are still displaying in the table.
I know the problem has to be because the clear button is clearing the dataArray in the DataModel. The buttons are not getting notified that the data is now gone.
How can I refresh the buttons, so when the user clears the table, the buttons clear as well?
* EDIT **
I did not include all code, but I have tried to show the basics for my issue. Class where I am building a arraylist of data to show in the table.
public class DataModel {
static ArrayList data = new ArrayList();
private DataModel() {
}
public void buildArray(String id, String name) {
data.add(id, name)
}
public void clearArray() {
data.clear();
}
public ArrayList getDataArray() {
return data;
}
}
This is the table viewer class
public class DataTableViewer extends TableViewer {
public DataTableViewer() {
Table table = getTable();
createColumns();
table.setHeaderVisible(true);
table.setLinesVisible(true);
setContentProvider(new ArrayContentProvider());
}
private void createColumns() {
TableViewerColumn col = new TableViewerColumn(this , SWT.NONE);
col.getColumn().setWidth(150);
col.getColumn().setText("ID");
col.setLabelProvider(new ColorColumnLabelProvider());
col = new TableViewerColumn(this , SWT.NONE);
col.getColumn().setWidth(150);
col.getColumn().setText("Name");
col.setLabelProvider(new ColorColumnLabelProvider());
col = new TableViewerColumn(this , SWT.NONE);
col.getColumn().setWidth(50);
col.getColumn().setText("");
col.setLabelProvider(new ColorColumnLabelProvider());
}
public class ColorColumnLabelProvider extends ColumnLabelProvider {
@Override
public void update(final ViewerCell cell) {
Object element = cell.getElement();
if(element instanceof DataModel.SaveData) {
DataModel.SaveData p = (DataModel.SaveData) element;
switch(cell.getColumnIndex()) {
case 0: {
cell.setText(p.getID());
break;
}
case 1: {
cell.setText(p.getName());
break;
}
case 2: {
Map<Object, Button> buttons = new HashMap<Object, Button>();
TableItem item = (TableItem) cell.getItem();
Button button;
String filename = (((DataModel.SaveData) element).getID() + "/" + ((DataModel.SaveData) element).getName());
if(buttons.containsKey(cell.getElement())) {
button = buttons.get(cell.getElement());
}
else
{
button = new Button((Composite) cell.getViewerRow().getControl(),SWT.PUSH);
button.setImage(appReg.getImage("ICON"));
button.setData("file.id", filename);
buttons.put(cell.getElement(), button);
}
TableEditor editor = new TableEditor(item.getParent());
editor.grabHorizontal = true;
editor.grabVertical = true;
editor.setEditor(button , item, cell.getColumnIndex());
button.addListener(SWT.Selection, new SelectionListener());
editor.layout();
break;
}
class SelectionListener implements Listener {
public SelectionListener() {
}
@Override
public void handleEvent(Event event) {
if (event.widget instanceof Button) {
String fileId = (String) event.widget.getData("file.id");
final File viewerFile = new File(fileId);
try {
Desktop.getDesktop().open(viewerFile);
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}// End SelectionListener Class
}
This is the dialog class that displays the tableviewer built from the arraylist from the DataModel class and using the table from DataTableViewer class.
public class DataDialog extends TitleAreaDialog {
Button clearButton;
DataTableViewer viewer;
public DataDialog() {
}
protected Control createDialogArea() {
createClearResultsButton();
createTableViewer();
}
private void createTableViewer() {
viewer = new DataTableViewer(parent, SWT.BORDER|SWT.V_SCROLL|SWT.FULL_SELECTION | SWT.MULTI);
viewer.setInput(DataModel.getInstance().getDataArray());
}
protected void createClearResultsButton() {
clearButton = new Button(composite, SWT.PUSH);
clearButton.setText("Clear Results");
clearButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
boolean more = MessageDialog.openConfirm(null, "Confirmation Message", "Are you sure you want to remove all results from the table?");
if(more == true) {
clearTableRows();
}
}
});
}
public void clearTableRows() {
DataModel.getInstance().clearDataArray();
viewer.refresh();
}
}
The Basic Issue :
1. DialogData class - opens
User sees 2 rows in the table with a button for each row in the 3rd column.
Each button corresponds with specific data in their row (maybe filename)
The user is finished viewing the data in the table.
The user clicks on the clear button to remove the data
Clicked method clears the ArrayList data in DataModel class. The code then refreshes the viewer.
The user now sees a blank table, but the buttons are still visible.
I am trying to figure out how to dispose of the buttons or how to clear the Button map in the DataTableViewer Class, when the rest of the data is cleared.
I think when the data is clear, that does not reset the buttons. So the buttons that has already been create is still valid and stored in the Map.
I hope this is more understanding.