Receiving Tweets in the NetBeans IDE Notifications Window

NetBeans IDE 7.4 provides a Notifications window as a centralized mechanism for displaying all application-wide messages.

http://wiki.netbeans.org/NewAndNoteworthyNB74#Notifications

These messages are not related to specific projects or files, that's what the Action Items window is for. For example, if the IDE is taking a long time to do something, you'll get a message in the Notifications window inviting you to file an issue.

But how about using it as a Twitter Viewer as well? I'm on Twitter a lot and all I really care about are Tweets that relate to NetBeans IDE. So, below I have integrated Twitter4J and now, after the IDE's main window is showing, a Twitter search is done in the background every 10 seconds for tweets containing the text "netbeans", with the results automatically displayed in the Notifications window, because all calls to org.openide.awt.NotificationDisplayer cause, aside from a balloon message, an entry to be created in the Notifications window.

Click to enlarge the image. Handily, new Tweets in the list are marked in bold, until I select them, at which point they're no longer bold because I have read them. I have no need to actually write my tweets in the IDE, but I do find it handy to know when there's a Tweet out there that I might like to respond to. Then I click the link in the panel to the right of the list and the browser opens at the Tweet that I'd like to do something with.

This really helps my productivity significantly since I no longer need to go to the browser to search for "netbeans" on Twitter. Instead, every 10 seconds while I'm programming in the IDE, which is most of the day, I get new Tweets delivered directly into my IDE. And then I can decide what to do with them. Since all of them relate directly to NetBeans, this really enhances my workflow quite a lot.

The code:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashSet;
import java.util.Set;
import org.openide.awt.HtmlBrowser.URLDisplayer;
import org.openide.awt.NotificationDisplayer;
import org.openide.awt.StatusDisplayer;
import org.openide.util.Exceptions;
import org.openide.util.ImageUtilities;
import org.openide.util.RequestProcessor;
import org.openide.windows.OnShowing;
import twitter4j.Query;
import twitter4j.QueryResult;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.conf.ConfigurationBuilder;

@OnShowing
public class OnShowingFindTweets implements Runnable {

    private static RequestProcessor RP;
    private final RequestProcessor.Task REFRESHER;
    private static int REFRESH_DELAY;
    private final Set<Status> statusHistory;

    public OnShowingFindTweets() {
        RP = new RequestProcessor(OnShowingFindTweets.class);
        REFRESHER = RP.create(this);
        REFRESH_DELAY = 10000;
        statusHistory = new HashSet<Status>();
    }
    
    @Override
    public void run() {
        StatusDisplayer.getDefault().setStatusText("Looking for more Tweets...");
        refreshTweets();
        REFRESHER.schedule(REFRESH_DELAY);
    }

    public void refreshTweets() {
        ConfigurationBuilder configBuilder = new ConfigurationBuilder();
        configBuilder.setOAuthConsumerKey("xxx");
        configBuilder.setOAuthConsumerSecret("xxx");
        configBuilder.setOAuthAccessToken("xxx");
        configBuilder.setOAuthAccessTokenSecret("xxx");
        TwitterFactory tf = new TwitterFactory(configBuilder.build());
        Twitter twitter = tf.getInstance();
        Query query = new Query("netbeans");
        QueryResult result;
        try {
            result = twitter.search(query);
            for (final Status status : result.getTweets()) {
                if (!statusHistory.contains(status)) {
                    final String url = "http://twitter.com/" + 
                            status.getUser().getScreenName() + "/status/" + status.getId();
                    NotificationDisplayer.getDefault().notify(
                            "@" + status.getUser().getScreenName() + ": " + status.getText(),
                            ImageUtilities.loadImageIcon("org/demo/icon.png", false),
                            //description:
                            url,
                            //action listener:
                            new ActionListener() {
                                @Override
                                public void actionPerformed(ActionEvent e) {
                                    try {
                                        URLDisplayer.getDefault().showURL(
                                        new URL(url));
                                    } catch (MalformedURLException eee) {
                                    }
                                }
                            });
                    statusHistory.add(status);
                }
            }
        } catch (TwitterException ex) {
            Exceptions.printStackTrace(ex);
        }
        
    }

}

On Twitter4J, the following proved very helpful:

http://xmeng.wordpress.com/2011/07/10/how-to-handle-sign-in-with-twitter-using-twitter4j/

Comments:

My only suggestion would be passing the query as a parameter to make it more generic, but is great!

Posted by Javier Ortiz on August 15, 2013 at 07:30 AM PDT #

Hi,

I am using Netbeans 7.3.1 and Twitter is not bydefault added to available REST WebServices. While adding new REST WebServices in Services Panel, It's asking Url(May be wsdl for twitter). What should be valule for this URL Field?

Do you have any idea about this?

Posted by Anuj on September 29, 2013 at 09:36 AM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today