Bug 420479 - [Commands] Add HandlerUtil.getCurrentStructuredSelection
Summary: [Commands] Add HandlerUtil.getCurrentStructuredSelection
Status: RESOLVED FIXED
Alias: None
Product: Platform
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 4.4   Edit
Hardware: PC Mac OS X
: P3 enhancement (vote)
Target Milestone: 4.6 M5   Edit
Assignee: Lars Vogel CLA
QA Contact:
URL:
Whiteboard:
Keywords: noteworthy
Depends on:
Blocks: 486267
  Show dependency tree
 
Reported: 2013-10-28 05:38 EDT by Alex Blewitt CLA
Modified: 2016-01-21 16:03 EST (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Alex Blewitt CLA 2013-10-28 05:38:08 EDT
Many operations in Eclipse use a StructuredSelection instead of the base selection, and there's a lot of if(selection instanceof IStructuredSelection) messages.

If a helper method in HandlerUtil could be provided to add this functionality then it would simplify adding handlers. The code could look like:

	/**
	 * Return the current structured selection.
	 * 
	 * @param event
	 *            The execution event that contains the application context
	 * @return the current structured selection, or <code>null</code>.
	 */
	public static ISStructuredelection getCurrentSelection(ExecutionEvent event) {
          ISelection selection = getCurrentSelection(event);
          if(selection instanceof IStructuredSelection) {
            return (IStructuredSelection)selection;
          } else {
            return null;
          }
        }

The same thing could be done for the checked equivalent:

	/**
	 * Return the current structured selection.
	 * 
	 * @param event
	 *            The execution event that contains the application context
	 * @return the current structured selection. Will not return <code>null</code>.
	 * @throws ExecutionException
	 *             If the current selection variable is not found.
	 */
	public static IStructuredelection getCurrentSelectionChecked(ExecutionEvent event)
			throws ExecutionException {
		Object o = getVariableChecked(event,
				ISources.ACTIVE_CURRENT_SELECTION_NAME);
		if (!(o instanceof IStructuredSelection)) {
			incorrectTypeFound(event, ISources.ACTIVE_CURRENT_SELECTION_NAME,
					ISelection.class, o.getClass());
		}
		return (IStructuredSelection) o;
	}
Comment 1 Lars Vogel CLA 2016-01-13 07:31:45 EST
I
Comment 2 Eclipse Genie CLA 2016-01-13 07:34:39 EST
New Gerrit change created: https://git.eclipse.org/r/64243
Comment 3 Lars Vogel CLA 2016-01-13 07:36:43 EST
The suggested patch adds a getCurrentStructuredSelection method which returns an empty collection if a structured selection is not available. 

This is similar to the implementation of StructuredViewer but different from getCurrentSelection in HandlerUtil which returns null. 

IMHO following the StructuredViewer pattern makes it simpler for caller,e.g, org.eclipse.ltk.internal.ui.refactoring.actions.DeleteResourcesHandler.  

But if someone disagrees we can change it to return null.
Comment 4 Lars Vogel CLA 2016-01-19 15:42:30 EST
If there are no objects again my approach, I commit the changes this week.
Comment 6 Thomas Schindl CLA 2016-01-21 09:20:37 EST
In Java8 returning an Optional<ISStructuredelection> would feel more natural but null most likely aligns better with the current API
Comment 7 Thomas Schindl CLA 2016-01-21 09:23:04 EST
ah i see you return empty - I'm not a fan of that!
Comment 8 Thomas Schindl CLA 2016-01-21 09:25:19 EST
Just to give you an impression how Optional would feel!

HandlerUtil.getStructuredSelection(...).ifPersent( System.err::println )
Comment 9 Lars Vogel CLA 2016-01-21 09:37:32 EST
(In reply to Thomas Schindl from comment #7)
> ah i see you return empty - I'm not a fan of that!

This is consistent to the Viewer API.
Comment 10 Thomas Schindl CLA 2016-01-21 10:07:36 EST
Yes but I think you should be consistent with the HandlerUtil-API!
Comment 11 Lars Vogel CLA 2016-01-21 10:15:34 EST
(In reply to Thomas Schindl from comment #10)
> Yes but I think you should be consistent with the HandlerUtil-API!

I had the same concerns issued in Comment 3. The problem is that this would make the calling code more complex. I contacted Dani about this last week to see if he has a strong opinion about this. 

I don't mind changing the new method to null, if more people think a (badly designed but) consistent API within a class is better than an easier to use API. Again, this new HandlerUtil API is similar to the Viewer API.
Comment 12 Dani Megert CLA 2016-01-21 10:38:58 EST
(In reply to Lars Vogel from comment #11)
> (In reply to Thomas Schindl from comment #10)
> > Yes but I think you should be consistent with the HandlerUtil-API!
> 
> I had the same concerns issued in Comment 3. The problem is that this would
> make the calling code more complex. I contacted Dani about this last week to
> see if he has a strong opinion about this. 
> 
> I don't mind changing the new method to null, if more people think a (badly
> designed but) consistent API within a class is better than an easier to use
> API. Again, this new HandlerUtil API is similar to the Viewer API.


In this case it is fine since we already have a method that allows you to get that information. Returning EMPTY prevents that every user of the new API has to do a 'null' check, which is usually not interesting for clients. However, it should be clearly mentioned in the first sentence of the Javadoc.

In addition, this method cannot be used since it is not declared 'static'.

I've fixed both issues with http://git.eclipse.org/c/platform/eclipse.platform.ui.git/commit/?id=d9ef2d749733bb76ab0a0fe6d534cae027c8b081
Comment 13 Lars Vogel CLA 2016-01-21 10:41:17 EST
Thanks Dani for the clarification and Tom for the input.
Comment 14 Thomas Schindl CLA 2016-01-21 13:29:40 EST
 
> In this case it is fine since we already have a method that allows you to
> get that information. Returning EMPTY prevents that every user of the new
> API has to do a 'null' check, which is usually not interesting for clients.

as is outlined in a java8 API you'd mark this with returning an Optional but I think this would make the API alien to the other code, the problem is that with returning EMPTY I don't know if there's really a selection provider who is pushing an empty selection or the current selection simply isn't a IStructuredSelection but I don't feel very strong on this.
Comment 15 Dani Megert CLA 2016-01-21 16:03:07 EST
(In reply to Thomas Schindl from comment #14)
>  
> > In this case it is fine since we already have a method that allows you to
> > get that information. Returning EMPTY prevents that every user of the new
> > API has to do a 'null' check, which is usually not interesting for clients.
> 
> as is outlined in a java8 API you'd mark this with returning an Optional but
> I think this would make the API alien to the other code, the problem is that
> with returning EMPTY I don't know if there's really a selection provider who
> is pushing an empty selection or the current selection simply isn't a
> IStructuredSelection but I don't feel very strong on this.

If you feel strong on this you call the old API ;-).