/*
* Main.fx
*
* Created on 17 Jun, 2009, 9:08:36 AM
*/
package uploaddownload;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.text.Font;
import javafx.io.http.HttpRequest;
import javafx.scene.control.ProgressBar;
import javafx.scene.layout.VBox;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.LayoutInfo;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.geometry.VPos;
import 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: new 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: true
}
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 = new javax.swing.JFileChooser();
var outputFile = jFileChooser.showSaveDialog(null);
if(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: false
}
|