app. This method works, but it’s highly
annoying and cumbersome. Often, the
user gets lost between all the flipping
screens and page redirections.
To solve this problem, we will use
the WebView. Rather than opening a
new Web browser, which might make
the user lose context, we can embed
the Twitter page directly in our app.
Once the user approves the app, we can
grab the pin code out of the document
without requiring the user to type it in
manually. We can do this by using the
WebView.document property. Here’s
how it works.
First, you need a Twitter library to do
the OAuth authentication. While it is possible to do everything by hand, you would
have to deal with encryption and hash
code generation, which is very annoying
and error-prone. I am using the open
source Twitter4J library, which takes care
of these details for you (see Listing 1).
First, we must initialize Twitter4J using
the consumer key and consumer secret
specific to your app. You can get these
values from your app configuration page
on dev.twitter.com. Then, request the
authorization URL. This is the URL that
we must show to users so they
can approve the app. Right now,
we are on the main thread, but
all JavaFX controls must be
accessed on the GUI thread, so
we call
Application.launch() to
open up a window on the right
thread, as shown in Listing 2.
//connect to twitter and get an authorization URL
twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(consumerKey,consumerSecret);
request Token = twitter.getOAuthRequest Token();
authURL = request Token.getAuthorizationURL();
...
JAVA IN ACTION
Download all listings in this issue as text
<div id="oauth_pin">
<code>12334928</code></div>
several times: first when the app starts,
then when the initial page is loaded, and
then when we get to the pin page. We
care only about the pin page, so the code
safely does nothing in the other cases.
of the page rather than merely read it?
The WebView lets you change the DOM
through the returned Document object,
or you can inject JavaScript into the page
to manipulate the DOM.
For the next example, I will load up
a Web page and then use the injection
method to change its styling on the fly
so the page will be more readable (see
Listing 6).
ABOUT US
The grabPin method is what will process the actual document. The document from the WebEngine is an actual
W3C Document Object Model (DOM)
document object, so we can use the
standard APIs such as getElementById()
to parse it. In this case, we want to look
for the oauth_pin element, which is a DIV
containing the pin that was given to the
user. We want the text contents of the
code element inside of it (see Listing 4).
Notice that I am returning early if the
document is null or if the OAuth pin is
missing. This is because the WebEngine
document property might change
blog
Changing Web Content
Now, what if we wanted to
actually change the contents
The JavaFX 2.0
new features and
graphical back end
let developers
create amazing
interfaces that
are both attractive
and responsive.
The steps are the same as
before: load up a page and
add a document listener. In
this case, we will load my
tech blog, joshondesign.com,
which has a nice design but
can be a bit fancy for reading
long amounts of text. It would
be nice to change the page to
a “reading” mode that uses
a plain font and black and
white. This would also be good
for printing. The modifyDoc