Geertjan is a DZone Zone Leader and has posted 467 posts at DZone. You can read more from them at their website. View Full User Profile

Integrating YouTube into IntelliJ (Part 1)

02.24.2008
| 6749 views |
  • submit to reddit
Let's create a plugin for IntelliJ that integrates a YouTube search, via the YouTube APIs. At the end of this article, you will have a plugin module that will let you do a simple search of YouTube movies. The plugin module source structure will look like this:

When 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.

  1. Create a new plugin module project.
  2. 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.
  3. 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.
  4. 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.
  5. 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;
    }

    }
  6. 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++;
    }
    }
  7. 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">
    <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>
    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.
And that's it! Now run the plugin module in debug mode. A new instance of IntelliJ will start up and you will have the functionality shown at the start of this article!

 

Published at DZone with permission of its author, Geertjan Wielenga.