Geertjan's Blog

  • February 3, 2009

Twitter Search API Meets NetBeans Platform

Geertjan Wielenga
Product Manager
Twitter Search API doesn't have a Java interface (yet) unfortunately, it's simply a web based query:


Above is what you'd type in the browser to get a query result, again in the browser.

Here's what the result will look like in NetBeans IDE, once I finish up integrating it into the Twitter Integration that I blogged about yesterday. Right now, I have all the pieces that I need, which is great. So here's the result (in this case, I hardcoded the URL and the search string, but in the final version the user [that is, you] will be able to type it in yourself).

To create the above, I started by creating a "TwitterItem" POJO:

public class TwitterItem {
String name = "";
String text = "";
String image = "";
public String getImage() {
return image;
public void setImage(String image) {
this.image = image;
public String getText() {
return text;
public void setText(String text) {
this.text = text;
public String getName() {
return name;
public void setName(String name) {
this.name = name;

Then I had the standard combination of Explorer View + Explorer Manager + Node. Only the node is interesting. Here it is in full, I think it might be illustrative of something.

public class TwitterSearchChildFactory extends ChildFactory<TwitterItem> {
protected boolean createKeys(List keys) {
String imageContent = null;
URL twitterSearch = TwitterSearchSettings.getTwitterSearch();
try {
InputStream twitterStream = twitterSearch.openStream();//XMLUtil is the NetBeans Platform parser. See the related Javadoc for details.
Document doc = XMLUtil.parse(new InputSource(twitterStream), true, true, null, null);
NodeList nodeLst = doc.getElementsByTagName("entry");
for (int i = 0; i < nodeLst.getLength(); i++) {
org.w3c.dom.Node fstNode = nodeLst.item(i);
Element fstElmnt = (Element) fstNode;
NodeList titleList = fstElmnt.getElementsByTagName("title");
Element titleElement = (Element) titleList.item(0);
NodeList titleChildList = titleElement.getChildNodes();
NodeList nameList = fstElmnt.getElementsByTagName("name");
Element nameElement = (Element) nameList.item(0);
NodeList nameChildList = nameElement.getChildNodes();
NodeList linkList = fstElmnt.getElementsByTagName("link");
for (int k = 0; k < linkList.getLength(); k++) {
org.w3c.dom.Node mainNode = linkList.item(k);
NamedNodeMap map = mainNode.getAttributes();
for (int j = 0; j < map.getLength(); j++) {
org.w3c.dom.Node attrNode = map.item(j);
String attrName = attrNode.getNodeName();
if (attrName.equals("href")) {
imageContent = attrNode.getNodeValue();
String titleContent = ((org.w3c.dom.Node) titleChildList.item(0)).getNodeValue();
String nameContent = ((org.w3c.dom.Node) nameChildList.item(0)).getNodeValue();//Create the TwitterItem POJO
//and create a new node for each instance:

if (!titleContent.equals("")) {
TwitterItem twitterItem = new TwitterItem();
} catch (SAXException ex) {
} catch (IOException ex) {
return true;
protected Node createNodeForKey(TwitterItem item) {//For each POJO, thanks to "keys.add" above, we create a node:
return new TwitterSearchNode(item);
private class TwitterSearchNode extends AbstractNode {
private TwitterItem item;
private URL url;
public TwitterSearchNode(TwitterItem item) {
this.item = item;//Get the name and text from the POJO:
setDisplayName(item.getName() + ": " + item.getText());
}//Create thumb nail from the image. Why does it have to be so hard?
public Image getIcon(int arg0) {
try {
url = new URL(item.getImage());
} catch (MalformedURLException ex) {
Image image = Toolkit.getDefaultToolkit().getImage(url);
BufferedImage thumbImage = null;
try {
MediaTracker mediaTracker = new MediaTracker(new Container());
mediaTracker.addImage(image, 0);
int thumbWidth = 16;
int thumbHeight = 16;
thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = thumbImage.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);
} catch (InterruptedException ex) {
return thumbImage;

Finally, here's the "TwitterSearchSettings", which simply provides the hardcoded query. That's what the user will be able to influence, of course, in the final version. I have it in a separate file so that it'll be easy to find when I get to integrating the rest of it into the plugin.

public class TwitterSearchSettings {
static URL twitterSearch;
public static URL getTwitterSearch() {
try {
twitterSearch = new URL("http://search.twitter.com/search.atom?q=netbeans");
} catch (MalformedURLException ex) {
return twitterSearch;

Watch this space for an announcement of the next version of the Twitter Integration, which will include the above Twitter Search support. Obviously, once we have the above feature, we'll be able to search for 'NetBeans' and then easily access and answer users' questions in Twitter. How cool would that be.

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.