Search |
||
Weather on SunTechDaysPosted by malenkov on April 6, 2009 at 3:00 AM PDT
The SunTechDays conference will take place in Saint-Petersburg this week. I'm going to participate in it and hold the "Developing Innovative Multimedia JavaFX Applications" seminar together with my colleague. I'll be talking about accessing the web services and deploying applications. My presentation will be based on the WeatherWidget sample that was slightly refactored. So, what kind of weather to expect on the conference? Module 6: Accessing Web ServicesConsider a task of accessing web services by using the Weather application that displays weather forecasts retrieved from Yahoo! Weather RSS feed. The RSS details are available here. The 6.1. HTTP NetworkingThe This class supports the following methods of the HTTP request: http://weather.yahooapis.com/forecastrss?d=5&p=ENXX0004&u=c When using the Once the Use the HttpRequest {
location: url
...
onInput: function(input) {
try {
// read data from input stream
...
} finally {
input.close()
}
}
}.enqueue()
A request's HTTP operation can be canceled by calling the 6.2. Data ProcessingThe The parser can be used in combination with the def parser = PullParser {
...
onEvent: function(event) {
if (event.type == PullParser.START_ELEMENT) {
if (event.qname.prefix == "yweather") {
if (event.qname.name == "location") {
location = event.getAttributeValue("city");
...
}
...
}
}
}
}
Also the parser.seek(QName{prefix:"yweather" name:"location"});
location = parser.event.getAttributeValue("city");
Advantages of the
6.3. XML SupportingThere are two classes to work with XML: Module 7: Deploying a JavaFX ApplicationThere are three basic models of deploying JavaFX application: var applet = __PROFILE__ == "browser";
// = FX.getProperty("javafx.runtime.isApplet") != null
var mobile = __PROFILE__ == "mobile";
// = FX.getProperty("javafx.me.profiles") != null
var normal = __PROFILE__ == "desktop";
// = not applet and not mobile
The current implementation uses the properties given in the commented lines. 7.1. Desktop: Standalone ApplicationThis is the easiest model of deployment. All you need is to have a javafx -jar application.jar [arguments] javafx -cp application.jar package.mainclass [arguments] A disadvantage of this model is that you have to ensure required JavaSE and JavaFX versions are installed on your computer.
7.2. Desktop: Java WebStart by using JNLPJava WebStart is a tool for deploying standalone applications on desktop by using the Java Network Launching Protocol. To deploy your application by using Java WebStart, create a <?xml version="1.0" encoding="UTF-8"?>
<jnlp spec="1.0+"
codebase="http://my.server.com/path/to/app/"
href="Weather.jnlp">
<information>
<title>Weather Widget</title>
<vendor>Sergey Malenkov</vendor>
<homepage href="http://weblogs.java.net/blog/malenkov/"/>
<description>Yahoo! Weather Forecast</description>
<offline-allowed/>
<shortcut>
<desktop/>
</shortcut>
</information>
<resources>
<j2se version="1.5+"/>
<extension name="JavaFX Runtime"
href="http://dl.javafx.com/1.1/javafx-rt.jnlp"/>
<jar href="Weather.jar" main="true"/>
</resources>
<application-desc main-class="Weather">
<argument>code=RSXX0091&u=c</argument>
</application-desc>
</jnlp>
The 7.3. Browser: Java Plug-in with AppletJava Plug-in is a tool for deploying Java applets that run inside a web browser. Java 6 update 10 has a new Java Plug-in architecture that unifies deployment between Java WebStart and the Java Plug-in. JavaFX provides a wrapper class that enables executing JavaFX applications as applets. To deploy an application as an applet, create a <?xml version="1.0" encoding="UTF-8"?>
<jnlp spec="1.0+"
codebase="http://my.server.com/path/to/app/"
href="Weather_browser.jnlp">
<information>
<title>Weather Widget</title>
<vendor>Sergey Malenkov</vendor>
<homepage href="http://weblogs.java.net/blog/malenkov/"/>
<description>Yahoo! Weather Forecast</description>
<offline-allowed/>
<shortcut>
<desktop/>
</shortcut>
</information>
<resources>
<j2se version="1.5+"/>
<extension name="JavaFX Runtime"
href="http://dl.javafx.com/1.1/javafx-rt.jnlp"/>
<jar href="Weather.jar" main="true"/>
</resources>
<applet-desc name="Weather"
main-class="com.sun.javafx.runtime.adapter.Applet"
width="200"
height="200">
<param name="MainJavaFXScript" value="Weather"/>
</applet-desc>
</jnlp>
Whenever your JavaFX application is launched by using the Java WebStart technology, the JNLP client checks for updates. You can improve the startup time of your JavaFX application if you enable update checks to be performed in the background. Add the following line to the <update check="background"/> Use JavaScript to call the <script src="http://dl.javafx.com/1.1/dtfx.js"></script>
<script>
javafx(
{
draggable: true,
archive: "Weather.jar",
width: 200,
height: 200,
code: "Weather",
name: "Weather"
},
{
code: "RSXX0091&u=c"
}
);
</script>
Note, the second parameter is used to pass arguments to the application. You may omit it, if not needed. Set the
Once the application is launched, press the ALT key and drag the application from the browser. This is the default behavior that can be changed by using the Stage {
...
extensions: AppletStageExtension {
useDefaultClose: false
shouldDragStart: function(event) {
return applet and event.primaryButtonDown
}
onDragStarted: function() {
applet = false
}
onAppletRestored: function() {
applet = true
}
}
}
In the preceding code the applet is draggable only when it is located in the browser window. It is implemented by using the local variable named 7.4. Mobile: JavaFX Mobile EmulatorJavaFX Mobile Emulator is provided with the JavaFX 1.1 SDK. It displays the application as it would look on a typical mobile device. JavaFX 1.1 Mobile Emulator supports the common API only. Also it limits the size of a MIDlet-Name: Weather MIDlet-Version: 1.0 MIDlet-Vendor: Sergey Malenkov MicroEdition-Profile: JAVAFX-1.1 MicroEdition-Configuration: CLDC-1.1 MIDlet-1: Weather,,Weather_MIDlet MIDlet-Jar-URL: Weather.jar MIDlet-Jar-Size: 79095 id: RSXX0091&u=c Note, the same descriptor is used to pass arguments to the application. For example, the last line specifies the string value for the emulator -Xdescriptor:Weather.jad
7.5. Arguments ParsingFrom the previous sections you've learned how to pass arguments to your application. Now consider parsing arguments in the application. It worth mentioning that the browser and mobile deploying models pass arguments as key-values. Use the following function to retrieve the value by using the key. This function returns var code = FX.getArgument("code");
You can use two methods to retrieve arguments when using the function run(args: String[]) {
var code: Object;
for (arg in args) {
code = if (indexof arg > 0)
then "{code} {arg}"
else arg
}
println('code: {code}');
}
Second, you can use the following utility function: def args = FX.getArguments(); There is no big difference between two those approaches. When using the first one, all variables defined within the 7.6. JavaFX PackagerJavaFX Packager utility creates an application in a format that is specific to a target profile, either desktop or mobile. Its documentation is included in the JavaFX 1.1 SDK, however, is not available on web. The NetBeans IDE incorporates this utility and makes it available to users when they choose an execution model. Use the following command to run the JavaFX Packager utility: javafxpackager -src D:\projects\Weather\sources -appClass Weather Consider the basic options you can use to run this application:
Running the exampleStart desktop applications to track the weather forecasts in Tallinn, Saint-Petersburg, Prague, Menlo Park and Santa Clara. The source file of the example is available. »
Related Topics >>
Programming Comments
Comments are listed in date ascending order (oldest first)
|
||
|
|