Integrating YouTube into IntelliJ (Part 1)
Join the DZone community and get the full member experience.
Join For FreeWhen you install it in IntelliJ, you will be able to specify a search string in the Settings dialog:
Once you've set the search string, you'll be able to search YouTube, with the results appearing in a simple message box:
Whenever you click OK, you will have a new search result. It would be better if the results appeared in a list in a toolbar, but I don't know how to create a new toolbar and am hoping someone will help me. Partly this might result in a semi-useful plugin for IntelliJ and partly it provides a nice introduction to how plugins are created.
- Create a new plugin module project.
- Read Interact with YouTube from Java. That's the approach we'll use in this plugin. Download the latest version of gdata.java. You'll also need mail.jar from JavaMail and activation.jar, which is part of the JDK or at least part of GlassFish. Put those two JARs into a folder, together with the 4 YouTube JARs listed in the article. Attach that folder to your plugin module project.
- Read Information for Plugin Developers, in particular, read The Basics of Plugin Development for IntelliJ IDEA. The instructions that follow are a variation on the "Hello World" example in the latter document.
- Firstly, follow the "Hello World" tutorial referred to in the previous step. I found most of it was helpful and self explanatory, except the "Adding Configuration" section, where the description of the creation of the GUI is out of date. But, somehow I figured it out, more or less. I also found that some of the classes and methods used in the tutorial are now deprecated, which indicates that there must be new versions of those classes and methods, which need to be incorporated into the "Hello World" tutorial.
- Here's the definition of my action, called "YouTubeAction":
package com.intellij.tutorial.youtube;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.application.Application;
import com.intellij.openapi.application.ApplicationManager;
public class YouTubeAction extends AnAction {
public void actionPerformed(AnActionEvent e) {
Application application = ApplicationManager.getApplication();
YouTubeApplicationComponent youTubeComponent = application.getComponent(YouTubeApplicationComponent.class);
youTubeComponent.searchYouTube();
}
public boolean displayTextInToolbar() {
return true;
}
} - Replace the "sayHello" method, that the "Hello World" tutorial tells you to create in the "YouTubeApplicationComponent" class, with the following two methods:
public void searchYouTube() {
try {
YouTubeService myService = new YouTubeService("mycompany-myapp-1");
String myFeed = "http://gdata.youtube.com/feeds/videos?start-index=1&max-results=25&vq=" + phrase + "&oi=spell";
printVideoFeed(myService, myFeed);
} catch (IOException ex) {
Logger.getLogger(YouTubeApplicationComponent.class.getName()).log(Level.SEVERE, null, ex);
} catch (ServiceException ex) {
Logger.getLogger(YouTubeApplicationComponent.class.getName()).log(Level.SEVERE, null, ex);
}
}
private static void printVideoFeed(YouTubeService service, String feedUrl) throws IOException, ServiceException {
VideoFeed videoFeed = service.getFeed(new URL(feedUrl), VideoFeed.class);
List<VideoEntry> allVideos = videoFeed.getEntries();
int i = 0;
while (i < allVideos.size()) {
VideoEntry allVideo = allVideos.get(i);
VideoEntry oneVideo = allVideo;
TextConstruct oneVideoTitle = oneVideo.getTitle();
String oneVideoTitleText = oneVideoTitle.getPlainText();
Messages.showMessageDialog(
oneVideoTitleText,
"Sample",
Messages.getInformationIcon());
i++;
}
} - At the same time, your plugin.xml file looks as follows, notice that I have two "add-to-group" elements, one that lets you invoke the action from the Window menu and the other from the main toolbar, to the left of the Help toolbar button:
<!DOCTYPE idea-plugin PUBLIC "Plugin/DTD" "http://plugins.intellij.net/plugin.dtd">
Here's a small tip: if you look in the IDEA/lib/resources.jar, you'll find a file called "ActionManager.xml", which defines all the group-ids, so you should look there if you want to find out where you want to let the user invoke the action from.
<idea-plugin>
<name>Search YouTube Plugin</name>
<description>My first IntelliJ Plugin</description>
<version>1.0</version>
<vendor>YourCompany</vendor>
<idea-version since-build="3000"/>
<application-components>
<component>
<implementation-class>com.intellij.tutorial.youtube.YouTubeApplicationComponent</implementation-class>
</component>
</application-components>
<actions>
<action id="SearchYouTube" class="com.intellij.tutorial.youtube.YouTubeAction" text="Search YouTube">
<add-to-group group-id="WindowMenu" anchor="first"/>
<add-to-group anchor="before" group-id="MainToolBar" relative-to-action="HelpTopics"/>
</action>
</actions>
</idea-plugin>
Opinions expressed by DZone contributors are their own.
Trending
-
Revolutionizing System Testing With AI and ML
-
Software Development: Best Practices and Methods
-
Getting Started With Istio in AWS EKS for Multicluster Setup
-
Introduction to Domain-Driven Design
Comments