gwt-facebook


Library for writing facebook applications using GWT

Please look at the new Graph API from facebook first

This library is built on the old JS API from facebook. The new OGraph library is better organized and simpler to use so you might want to look at that first.

You can see a preview of the new progject here http://code.google.com/p/gwtfb/

Wave

Public wave to discuss rewrite to the new Graph Api https://wave.google.com/wave/waveref/googlewave.com/w+GgCzSgiWB

Develop facebook apps with GWT

Latest Version

To get the latest version, checkout the source code.

About

gwt-facebook is a wrapper for facebook javascript API. With gwt-facebook you can work with pure java objects instead of dealing with native javascript and json strings.

If you find this project useful, please post a message to the group http://groups.google.com/group/gwt-facebook

Check out the live demo http://gwittit.appspot.com (250+ people has added the showcase app so far, over 200 downloads : - )

Install

  • Check out the sourcecode from trunk, copy the file build.properties.example to build.properties and set variable gwtinstall to point to your local gwt installation, type ant. Copy the fresh file gwt-facebook-VERSION.jar to YOURAPP/war/WEB-INF/lib

  • Insert javascript in your main html file, below the body tag ```

```

  • Create a file called xd_receiver.htm in your war catalog with the following content ```

```

A good way to start is to take a look at some sample code http://code.google.com/p/gwt-facebook/source/browse/trunk/samples/SampleApp/src/com/gwittit/sample/client/SampleApp.java

  • Or cutnpaste the following sample code to your main java file: ```

package com.gwittit.sample.client;

import java.util.List;

import com.google.gwt.core.client.EntryPoint; import com.google.gwt.core.client.GWT; import com.google.gwt.event.dom.client.ClickEvent; import com.google.gwt.event.dom.client.ClickHandler; import com.google.gwt.user.client.Window; import com.google.gwt.user.client.rpc.AsyncCallback; import com.google.gwt.user.client.ui.Anchor; import com.google.gwt.user.client.ui.HTML; import com.google.gwt.user.client.ui.RootPanel; import com.google.gwt.user.client.ui.VerticalPanel; import com.gwittit.client.facebook.ConnectState; import com.gwittit.client.facebook.FacebookApi; import com.gwittit.client.facebook.FacebookConnect; import com.gwittit.client.facebook.ui.ProfilePicsPanel; import com.gwittit.client.facebook.xfbml.Xfbml;

/** * Entry point classes define onModuleLoad(). * Demonstrates use of gwt-facebook. */ public class SampleApp implements EntryPoint {

/*
 * Api key defined in facebook.
 */
public static String API_KEY = "707cee0b003b01d52b2b6a707fa1202b";

/*
 * Get the api client
 */
private final FacebookApi apiClient = GWT.create ( FacebookApi.class );

/*
 * Display login link
 */
private final Anchor loginLink = new Anchor ( "Login with facebook connect");

/*
 * Display logout link
 */
private final Anchor logoutLink = new Anchor ( "Logout") ;

/*
 * Outer
 */
private final VerticalPanel outer = new VerticalPanel ();
/*
 * Add gui 
 */
private final VerticalPanel mainPanel = new VerticalPanel ();

private final HTML header = new HTML ( "<h1>gwt-facebook sample app</h1>" );
private final HTML waitingText = new HTML ( "Waiting for facebook connect state..." );
/**
 *  Executed login when user clicks loginlink
 */
private class LoginClickHandler implements ClickHandler {
    public void onClick(ClickEvent event) {
        FacebookConnect.requireSession ( new AsyncCallback<Boolean> () {
            public void onFailure(Throwable caught) {
                Window.alert ( "Failed" );
            }
            public void onSuccess(Boolean result) {
                renderWhenConnected ();
            }
        });
    }
}

/**
 * Fired when connect state is ready
 */
private class ThenRenderRestOfApp implements AsyncCallback<ConnectState> {
    public void onFailure(Throwable caught) {
        Window.alert ( "Failed to get facebook connect status, hit reload" );
    }
    public void onSuccess(ConnectState result) {
        mainPanel.remove ( waitingText );
        if ( result == ConnectState.connected ) {
            renderWhenConnected ();
        } else {
            renderWhenNotConnected ();
        }
    }
};

/**
 * Executed when user logs out
 */
private class LogoutClickHandler implements ClickHandler {
    public void onClick(ClickEvent event) {
        FacebookConnect.logoutAndRedirect ( "/" );
    }
};

/**
 * Show friends callback
 */
private class FriendsGetCallback implements AsyncCallback<List<Long>> {
    public void onFailure(Throwable caught) {
        Window.alert ( "Failed to load friends: " + caught );
    }
    public void onSuccess( List<Long> uids ) {
        renderFriends ( uids );
    }
}

/** 
 * Load app.
 */
public void onModuleLoad() {
    loginLink.addClickHandler ( new LoginClickHandler ()  );
    logoutLink.addClickHandler ( new LogoutClickHandler()  );

    mainPanel.add ( waitingText );

    // Initialize Facebook Connect
    FacebookConnect.init ( API_KEY );
    // Wait until we can determine status, then render rest of the app.
    FacebookConnect.waitUntilStatusReady ( new ThenRenderRestOfApp() );

    outer.add ( header );
    outer.add ( mainPanel );
    RootPanel.get ().add ( outer );
}

/**
 * UI rendered when connected
 */
public void renderWhenConnected () {
    mainPanel.clear ();
    mainPanel.add ( logoutLink );
    mainPanel.add ( new HTML ( " Your api key is " + apiClient.getApiKey ()  ) );
    getFriends();
}

/**
 * UI rendered when not connected
 */
public void renderWhenNotConnected () {
    mainPanel.clear ();
    mainPanel.add ( loginLink );
}

/**
 * Get list of friends
 */
public void getFriends() {
    apiClient.friendsGet ( new FriendsGetCallback() );
}

/**
 * Render list of friends
 */
public void renderFriends( List<Long> uids ) {
    ProfilePicsPanel ppp = new ProfilePicsPanel ( uids );
    ppp.getElement ().setId ( "FriendsGet" );
    mainPanel.add ( new HTML ( "Your Friends: ") ) ;
    mainPanel.add ( ppp );

    // *** This is cruicial ***
    // *** Tell facebook to render element, looking for id "FriendsGet" ***
    Xfbml.parse ( ppp );
}

}

```

Wiki

See wiki for implemented methods http://code.google.com/p/gwt-facebook/wiki/Facebook_API_Methods

Package Structure

com.gwittit.client.facebook com.gwittit.client.facebook.ui com.gwittit.client.facebook.xfbml

Need Widgets

Donate a widget to the project : )

Hints:
Friend selectBox: http://raibledesigns.com/rd/entry/creating_a_facebook_style_autocomplete

Project Information

Labels:
facebook gwt