Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move up null check in GEF viewer when the selected editpart is updated. #442

Merged
merged 2 commits into from
May 21, 2024

Conversation

ptziegler
Copy link
Contributor

@ptziegler ptziegler commented May 13, 2024

The selection inside a GEF viewer is assumed to always be non-null. But
because the (implicit) null check is done after the editpart is added to
the internal list, it leaves the viewer in an inconsistent state.

To avoid this, the null check is done earlier, so that the exception is
thrown before the internal model is modified.

Removes the usage of raw types as much as possible and simplifies the
selection algorithm.
@ptziegler ptziegler marked this pull request as draft May 13, 2024 18:44
@ptziegler ptziegler changed the title Log an error whenever the selection is changed to a null editpart. Exclude null values when the selected editpart is updated. May 13, 2024
@ptziegler ptziegler linked an issue May 13, 2024 that may be closed by this pull request
@ptziegler ptziegler marked this pull request as ready for review May 13, 2024 19:35
@ptziegler
Copy link
Contributor Author

The problem can be observed in e.g. the following lines of SelectionManager.appendSelection():

selection.remove(editpart);
selection.add(editpart);
editpart.setSelected(EditPart.SELECTED_PRIMARY);

If the editpart is null, it is added to the internal list before an exception is thrown. When the selection is changed, an exception is thrown again, when the null-selection is removed again. I don't see how this can be the desired behavior and I don't believe this is something clients should rely on.

EditPart part = itr.next();
if (part == null) {
LOGGER.error(GEFMessages.SelectionManager_Selection_Is_Null);
continue;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really happy about this one, as it might cause issue when the last element is null and therefore SELECTED_PRIMARY is never set.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about adding after the loop an

if(!selection.isEmpty()){
   selection.getLast().setSelected(EditPart.SELECTED_PRIMARY);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After sleeping about it, I'm not quite sure if it's really a good idea to just not throw the exception anymore. I think recent commits already showed that seemingly safe changes can break someones editor.

Instead, it makes more sense to throw the exception before the internal selection is updated. Just so that everything remains consistent.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the new tests illustrate the desired behavior: The list of selected edit parts should never contain null.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I had last week a bug in 4diac IDE where I got an NPE at that point I agree with you. The NPE showed me an important bug in our code which otherwise would have gone unnoticed and with no user feedback. Would have been hard to notice and debug. Therefore I think your proposed improvement goes in the right direction.

@ptziegler ptziegler changed the title Exclude null values when the selected editpart is updated. Move up null check in GEF viewer when the selected editpart is updated. May 14, 2024
The selection inside a GEF viewer is assumed to always be non-null. But
because the (implicit) null check is done after the editpart is added to
the internal list, it leaves the viewer in an inconsistent state.

To avoid this, the null check is done earlier, so that the exception is
thrown before the internal model is modified.

Resolves eclipse#433
@azoitl azoitl merged commit 99048f6 into eclipse:master May 21, 2024
9 checks passed
@ptziegler ptziegler added this to the 3.20.0 milestone May 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improved handling of null values in AbstractEditPartViewer
2 participants