/*
* Main.fx
*
* Created on 17 Jun, 2009, 9:08:36 AM
*/
[b]package [/b]uploaddownload;
[b]import [/b]javafx.stage.Stage;
[b]import [/b]javafx.scene.Scene;
[b]import [/b]javafx.scene.text.Font;
[b]import [/b]javafx.io.http.HttpRequest;
[b]import [/b]javafx.scene.control.ProgressBar;
[b]import [/b]javafx.scene.layout.VBox;
[b]import [/b]javafx.scene.image.Image;
[b]import [/b]javafx.scene.image.ImageView;
[b]import [/b]javafx.scene.layout.LayoutInfo;
[b]import [/b]javafx.scene.control.Button;
[b]import [/b]javafx.scene.layout.HBox;
[b]import [/b]javafx.geometry.VPos;
[b]import [/b]javafx.scene.control.Label;
/**
* @author Rakesh Menon
*/
var fileURL = "http://farm3.static.flickr.com/2322/2224416159_63abc10bbb_o_d.jpg";
var toRead : Long = 0;
var read : Long = 0;
function downloadFile(url : String, outputFile : java.io.File) {
def getRequest: HttpRequest = HttpRequest {
location: url
sink: [b]new [/b]java.io.FileOutputStream(outputFile)
onStarted: function() { println("onStarted"); }
onConnecting: function() { println("onConnecting") }
onDoneConnect: function() { println("onDoneConnect") }
onReadingHeaders: function() { println("onReadingHeaders") }
onResponseCode: function(code : Integer) { println("onResponseCode - {code}") }
onResponseMessage: function(msg : String) { println("onResponseMessage - {msg}") }
onReading: function() { println("onReading") }
onToRead: function(bytes: Long) {
toRead = bytes;
println("onToRead({bytes})");
}
onRead: function(bytes: Long) {
read = bytes;
println("onRead({bytes}) - {read * 100/toRead}%");
}
onException: function(ex: java.lang.Exception) {
println("onException - {ex}");
}
onDoneRead: function() { println("onDoneRead") }
onDone: function() { println("onDone") }
}
getRequest.start();
}
/**
* Client
*/
var imageView = ImageView {
image: Image {
url: "{__DIR__}images/thumbnail.jpg"
width: 240
height: 151
backgroundLoading: [b]true[/b]
}
layoutInfo: LayoutInfo { width: 240 height: 151 }
}
var label = Label {
font : Font { size : 12 }
text: bind "Downloaded - {read * 100/(toRead + 1)}%"
layoutInfo: LayoutInfo { vpos: VPos.CENTER maxWidth: 120 minWidth: 120 width: 120 height: 30 }
}
var button = Button {
text: "Save"
layoutInfo: LayoutInfo { width: 100 height: 30 }
action: function() {
var jFileChooser = [b]new [/b]javax.swing.JFileChooser();
var outputFile = jFileChooser.showSaveDialog([b]null[/b]);
[b]if[/b](outputFile == javax.swing.JFileChooser.APPROVE_OPTION) {
downloadFile(fileURL, jFileChooser.getSelectedFile());
toRead = 0;
read = 0;
}
}
}
var hBox = HBox {
spacing: 10
content: [ label, button ]
}
var progressBar = ProgressBar {
progress: bind (read/((toRead + 1) as Number))
layoutInfo: LayoutInfo { width: 240 height: 30 }
}
var vBox = VBox {
spacing: 10
content: [ imageView, hBox, progressBar ]
translateX: 10
translateY: 10
}
Stage {
title: "Download File"
width: 270
height: 280
scene: Scene {
content: [ vBox ]
}
resizable: [b]false[/b]
}