Plugin and Play - Integrating YouTube with Eclipse
Interacting with YouTube
To make testing this easy, and to make the search code usable elsewhere, it makes sense to create a separate class to handle taking a search term and returning a list of results.
The code listing for this is as follows.
/**
* Return a list of video entries back to the calling method
*/
public VideoFeed getResults(String author, String title) throws IOException, ServiceException
{
YouTubeService myService = new YouTubeService("mycompany-myapp-1");
YouTubeQuery query = new YouTubeQuery(new URL(VIDEO_FEED));
//set the author
if( (author != null) && author.length() > 0)
{
query.setAuthor(author);
}
//set the actual query string
if((title != null) && title.length() > 0)
{
query.setVideoQuery(title);
}
//choose most viewed as the ordering
query.setOrderBy(YouTubeQuery.OrderBy.VIEW_COUNT);
//for the fun of it - include anything :-)
query.setIncludeRacy(true);
//get the video feed
VideoFeed feed = myService.query(query,VideoFeed.class);
return feed;
}
Create a View For Searching
Rather than launching a plain old pop up dialog, we’ll do something slightly more advanced, to fit in with the Eclipse look & feel. This will also show how to use further extension points.
Using the org.eclipse.ui.views extension point, this time we’ll use the example template to get the necessary code generated faster.

As you fill out the example, use the TableViewer – we will be changing how the view looks, but selecting this gives a good headstart, and provides all the method stubs we will need.
On completion of this dialog, you will notice a number of new entries in the plugin editor, including use of the org.eclipse.ui.views and org.eclipse.ui.perspectiveExtension extension points.
If you run the application now, and choose Show View/Other – you will see a YouTube entry in the list, where you can choose to Search YouTube. If you click on this it will display the Search window at the bottom section of the screen. A number of default entries are provided in the table component.

Also, we will update our SearchHandler code so that when the action is executed for Search that it will also open the YouTube view. In the execute method, add the following code – you can get the id of the view by clicking on it in the plugin editor and copying the value of the id field. It is good practise to add the ID as a constant for each view for easy access in your code.
try
{
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView("com.eclipsezone.youtube.views.YouTubeView");
}
catch (WorkbenchException e)
{
e.printStackTrace();
}
James is a DZone Zone Leader and has posted 231 posts at DZone. You can read more from them at their website.
- Login or register to post comments
- 9374 reads
- Printer-friendly version
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)









