SetCommand doesn't work with Boolean [message #1283215] |
Thu, 03 April 2014 06:58 |
Emre T Messages: 119 Registered: April 2013 |
Senior Member |
|
|
Hello,
i have a custom class extending the EditingSupport for a JFace TableViewer and its columns/cells. The underlying models being shown in the UI are EMF model instances with different attributes varying from simple Strings to Booleans etc. In this editing support class I differentiate between different data types residing in the cells of the table viewer and implement accordingly different cell editors. Simple TextCellEditor for String or int and ComboBoxViewerCellEditor for Enums or Boolean.
The method getCellEditor(Object element) where I call the corresponding cell editors seems to be working correctly. But the setValue(Object element, Object value), in which I use the EMF Edit commands to make use of the command stack via EditingDomain is not functioning correctly for the Boolean values. The method looks like the following:
protected CellEditor getCellEditor(Object element) {
EAttribute attr = (EAttribute) ((IEMFEditObservable) element).getStructuralFeature();
Object obj = ((EObject) ((IEMFEditObservable) element).getObserved()).eGet(attr);
Object dataType = (Object) attr.getEType();
if (dataType instanceof EEnum) {
logger.info(((Object) attr.getEType()));
EEnum eEnum = (EEnum) ((EAttribute) attr).getEType();
literals = eEnum.getELiterals();
String[] elements = new String[literals.size()];
int i = 0;
for (EEnumLiteral literal : literals) {
elements[i++] = literal.getName();
}
comboBoxEditor = new ComboBoxViewerCellEditor(viewer.getTable(), SWT.DROP_DOWN | SWT.READ_ONLY);
comboBoxEditor.setLabelProvider(new LabelProvider());
comboBoxEditor.setContenProvider(new ArrayContentProvider());
int j = 0;
do {
comboBoxEditor.setInput(elements);
j++;
} while (j <= 0);
return comboBoxEditor;
} else if (dataType instanceof EDataType) {
EDataType eDataType = (EDataType) dataType;
if (eDataType.getInstanceClass() == String.class || eDataType.getInstanceClass() == int.class) {
return textEditor;
} else if (eDataType.getInstanceClass() == Boolean.class || eDataType.getInstanceClass() == Boolean.TYPE) {
comboBoxEditor = new ComboBoxViewerCellEditor(viewer.getTable(), SWT.DROP_DOWN | SWT.READ_ONLY);
comboBoxEditor.setLabelProvider(new LabelProvider());
comboBoxEditor.setContenProvider(new ArrayContentProvider());
comboBoxEditor.setInput(Arrays.asList(new Boolean[] {(Boolean) obj, !(Boolean) obj}));
}
return comboBoxEditor;
} else {
return new TextCellEditor(viewer.getTable());
}
}
@Override
protected void setValue(Object element, Object value) {
EAttribute attr = (EAttribute) ((IEMFEditObservable) element).getStructuralFeature();
Object obj = ((EObject) ((IEMFEditObservable) element).getObserved()).eGet(attr);
editingDomain = AdapterFactoryEditingDomain.getEditingDomainFor(attr.eContainer());
Command cmd = null;
if (obj instanceof String) {
......
} else if (obj instanceof Integer) {
......
} else if (obj instanceof EEnumLiteral) {
EEnumLiteral newEnum = ((EEnumLiteral) obj).getEEnum().getEEnumLiteral((String) value);
if (obj instanceof Enumerator) {
Enumerator oldEnum = (Enumerator) obj;
if (oldEnum != null) {
cmd = SetCommand.create(editingDomain, ((IEMFEditObservable) element).getObserved(), attr,
newEnum);
editingDomain.getCommandStack().execute(cmd);
}
}
} else if (obj instanceof Boolean) {
Boolean oldBoolean = Boolean.valueOf((Boolean) obj);
Boolean newBoolean = Boolean.valueOf((Boolean) value);
if (newBoolean == null || (newBoolean.toString()).trim().length() == 0) {
cmd = SetCommand.create(editingDomain, ((IEMFEditObservable) element).getObserved(), attr, null);
} else if (oldBoolean != null && oldBoolean != newBoolean) {
cmd = SetCommand.create(editingDomain, ((IEMFEditObservable) element).getObserved(), attr, newBoolean);
}
} else {
// do nothing
}
viewer.update(element, null);
}
As you can see I am using the same logic in the setValue() for the Boolean attribute as for the Enums (which works fine), the only difference is that I need to access the Boolean from within the EDataType. The SetCommand in the end becomes executable. But the underlying model instance is somehow not changing. Further debugging brought me no further. Do you have any idea, where the problem might be?
Any help will be appreciated!
Thanks,
Emre
|
|
|
Re: SetCommand doesn't work with Boolean [message #1283235 is a reply to message #1283215] |
Thu, 03 April 2014 07:26 |
Felix Dorner Messages: 295 Registered: March 2012 |
Senior Member |
|
|
On 03/04/2014 08:58, Emre T wrote:
> Hello,
> i have a custom class extending the EditingSupport for a JFace
> TableViewer and its columns/cells. The underlying models being shown in
> the UI are EMF model instances with different attributes varying from
> simple Strings to Booleans etc. In this editing support class I
> differentiate between different data types residing in the cells of the
> table viewer and implement accordingly different cell editors. Simple
> TextCellEditor for String or int and ComboBoxViewerCellEditor for Enums
> or Boolean.
>
> The method getCellEditor(Object element) where I call the corresponding
> cell editors seems to be working correctly. But the setValue(Object
> element, Object value), in which I use the EMF Edit commands to make use
> of the command stack via EditingDomain is not functioning correctly for
> the Boolean values. The method looks like the following:
>
> protected CellEditor getCellEditor(Object element) {
> EAttribute attr = (EAttribute) ((IEMFEditObservable)
> element).getStructuralFeature();
> Object obj = ((EObject) ((IEMFEditObservable)
> element).getObserved()).eGet(attr);
> Object dataType = (Object) attr.getEType();
> if (dataType instanceof EEnum) {
> logger.info(((Object) attr.getEType()));
>
> EEnum eEnum = (EEnum) ((EAttribute) attr).getEType();
> literals = eEnum.getELiterals();
> String[] elements = new String[literals.size()];
> int i = 0;
> for (EEnumLiteral literal : literals) {
> elements[i++] = literal.getName();
> }
>
> comboBoxEditor = new
> ComboBoxViewerCellEditor(viewer.getTable(), SWT.DROP_DOWN | SWT.READ_ONLY);
> comboBoxEditor.setLabelProvider(new LabelProvider());
> comboBoxEditor.setContenProvider(new ArrayContentProvider());
> int j = 0;
> do {
> comboBoxEditor.setInput(elements);
> j++;
> } while (j <= 0);
> return comboBoxEditor;
>
> } else if (dataType instanceof EDataType) {
> EDataType eDataType = (EDataType) dataType;
>
> if (eDataType.getInstanceClass() == String.class ||
> eDataType.getInstanceClass() == int.class) {
> return textEditor;
>
> } else if (eDataType.getInstanceClass() == Boolean.class ||
> eDataType.getInstanceClass() == Boolean.TYPE) {
> comboBoxEditor = new
> ComboBoxViewerCellEditor(viewer.getTable(), SWT.DROP_DOWN | SWT.READ_ONLY);
> comboBoxEditor.setLabelProvider(new LabelProvider());
> comboBoxEditor.setContenProvider(new
> ArrayContentProvider());
> comboBoxEditor.setInput(Arrays.asList(new Boolean[] {(Boolean) obj,
> !(Boolean) obj}));
> }
>
> return comboBoxEditor;
> } else {
>
> return new TextCellEditor(viewer.getTable());
>
> }
> }
>
> @Override
> protected void setValue(Object element, Object value) {
>
> EAttribute attr = (EAttribute) ((IEMFEditObservable)
> element).getStructuralFeature();
> Object obj = ((EObject) ((IEMFEditObservable)
> element).getObserved()).eGet(attr);
> editingDomain =
> AdapterFactoryEditingDomain.getEditingDomainFor(attr.eContainer());
> Command cmd = null;
>
> if (obj instanceof String) {
> ......
> } else if (obj instanceof Integer) {
> ......
> } else if (obj instanceof EEnumLiteral) {
> EEnumLiteral newEnum = ((EEnumLiteral)
> obj).getEEnum().getEEnumLiteral((String) value);
> if (obj instanceof Enumerator) {
> Enumerator oldEnum = (Enumerator) obj;
> if (oldEnum != null) {
> cmd = SetCommand.create(editingDomain,
> ((IEMFEditObservable) element).getObserved(), attr,
> newEnum);
> editingDomain.getCommandStack().execute(cmd);
> }
> }
>
> } else if (obj instanceof Boolean) {
> Boolean oldBoolean = Boolean.valueOf((Boolean) obj);
> Boolean newBoolean = Boolean.valueOf((Boolean) value);
> if (newBoolean == null ||
> (newBoolean.toString()).trim().length() == 0) {
> cmd = SetCommand.create(editingDomain,
> ((IEMFEditObservable) element).getObserved(), attr, null);
> } else if (oldBoolean != null && oldBoolean !=
> newBoolean) {
> cmd = SetCommand.create(editingDomain,
> ((IEMFEditObservable) element).getObserved(), attr, newBoolean);
> }
> } else {
> // do nothing
> }
> viewer.update(element, null);
> }
>
> As you can see I am using the same logic in the setValue() for the
> Boolean attribute as for the Enums (which works fine), the only
> difference is that I need to access the Boolean from within the
> EDataType. The SetCommand in the end becomes executable. But the
> underlying model instance is somehow not changing. Further debugging
> brought me no further. Do you have any idea, where the problem might be?
Hmm, for EEnum you execute the command, in the body for the boolean you
don't. Maybe just a missing execute?
|
|
|
|
Powered by
FUDForum. Page generated in 0.02661 seconds