SWT - Using EditingSupport in my TableViewer
-
06-07-2021 - |
Question
In my main dailog, I have a JFace TableViewer. The last column of the table is a ComboBoxCellEditor. They have the option of No, Yes, Both. This all works as designed.
But here is my issue.
- If the user selects Both for the value.
- I need to run a method that will get current row data from the Array
- Change the value from both to No
- Make a copy of the data then change the value to Yes
- Add both back in to the Array
- Refresh the table
Table Example
From -
1002 | 001 | sss | part | both(user changed from default)
To -
1002 | 001 | sss | part | No
1002 | 001 | sss | part | Yes
I am trying to figure out after both is selected how to run the method to do the rest. I am assuming it has to be some sort of a listener. Please have a look at my EditingSupport code and show me where I would start my method to do the rest.
public class OptionEditingSupport extends EditingSupport
{
private ComboBoxCellEditor cellEditor;
public OptionEditingSupport(ColumnViewer viewer) {
super(viewer);
cellEditor = new ComboBoxCellEditor(((TableViewer)viewer).getTable(), new String[]{"No", "Yes", "Both"}, SWT.DROP_DOWN);
//cellEditor.setValue(0);
}
protected CellEditor getCellEditor(Object element) {
return cellEditor;
}
protected boolean canEdit(Object element) {
return true;
}
protected Object getValue(Object element) {
return 0;
}
protected void setValue(Object element, Object value)
{
if((element instanceof AplotDatasetData) && (value instanceof Integer)) {
Integer choice = (Integer)value;
//String option = (choice == 0? "Yes":"No":"Both");
String option = ((AplotDatasetData)element).getMarkupValue();;
if(choice == 0) {
option = "No";
}
else if(choice == 1) {
option = "Yes";
}
else {
option = "Both";
}
((AplotDatasetData)element).setMarkupValue(option);
getViewer().update(element, null);
}
}
}
La solution
As far as I understood your question, you want to duplicate one of your objects, add it to you model and refresh the viewer.
And all this should happen when the user selects "both"
in the combobox. You already know, when this happens. You will end up in the else
case of your setValue
method. Then you can do what you have to do there:
protected void setValue(Object element, Object value)
{
if((element instanceof AplotDatasetData) && (value instanceof Integer)) {
Integer choice = (Integer)value;
String option = ((AplotDatasetData)element).getMarkupValue();
if(choice == 0) {
option = "No";
}
else if(choice == 1) {
option = "Yes";
}
else {
option = "Both";
// create a copy of your element
// add it to your model
// update the viewer
}
getViewer().update(element, null);
}
}