2009-04-27

JavaFX Script Learning: Data Binding and Triggers

One of the most useful feature in JavaFX is its Data Binding and Triggers functions. According to the official tutorial, Data binding is the ability to create an immediate and direct relationship between tow variables, This allow two or more variables can automatically change when the other or others change. At the same time, A Replace Trigger is a block of code that is attached to a variable, which when the variable changes, the code is automatically executed. So let's begin here.
Here is an example :


var x = 0;
def y = bind x;
x=1;
println(y); //y now equals 1
x = 333;
println(y); // y now is 333;

That is a extreme sample example to show the use of 'bind' keyword, You can know in that example, y is binded to x, and its value is changed with x, that's the basic use of bind. Note that y is declared as a def because we don't want any thing change y by assigning a value to it.
An other example:

var myStreet= " No.2 Phoenix Garden"
var myCity = "Xiamen"

def address = bind Address {
street : myStreet;
city : myCity;
};

println({address.street});
myStreet = "No.33 NoWhere"
println({address.street});


In this example the result of execute is :
No.2 Phoenix Garden
No.33 NoWhere

So, the conclusion is we can bind a variable with the value of a bound expression, which can be a basic type, an object, the outcome of a function, or the outcome of an expression. But in this example we must note that the changing of myStreet actually causes a new Address object to be created and then re-assigned to the address variable. If you don't want this happen, you can change the definition like this:

def address = bind Address{
street: bind myStreet;
city : myCity;
}

By this, you can change street without create a new object, and the bind in def line can be omited.

In next step, let us look at Binding and Functions.

Here is an example :

var scale = 1.0;

bound function makePoint(xPos : Number, yPos : Nubmer) : Point {
Point {
x: xPos * scale;
y: yPos * scale;
}
}

class Point {
var x : Number;
var y : Number;
}

var myX = 5.0;
var myY = 9.0;
def pt = bind makePoint(myX, myY);
println(pt.x);

myX *=2;
println(pt.x);

scale = 2.0;
println(pt.x);

At the head of this code, we announce a bound function by 'bound' keyword, and then we use our own myX and myY number to create a Point by that bound function, note we use bind keyword before the invocation of makePoint function, so pt binded to the outcome of makePoint function.
The result of executing is :
 
5.0
10.0
20.0

But if we remove the bound keyword from makePoint function(it became a non-bound function), the result would be:

5.0
10.0
10.0

The different between these two situation is a bound function will get re-invoked when every variable related to the function changed while the non-bound function only do that when one of their arguments change. And we MUST NOTE that bound function and bind keyword is work together.


In next step, let us look at Binding with Sequences:

Here is an example:

var q1 = [1..10];
def q2 = bind for (item in q1) item *8;
printSeqs();
insert 11 into q1;

function printSeqs() {
println("First Sequence:");
for( i in q1) { println(i) ; }
println("Second Sequence:");
for( i in q2) { println(i) ; }
}

You can guessed the result is first sequence is 1 to 10 with step of 1 and the second sequence is 8 to 80 with step of 8.
In this example, we bind two sequence by placing the bind before the for. For more understanding, we can try to change one or some items of q1, you can find the corresponding items q2 will automatically changed and others will not be affect, like we do in the example we insert a item 11 into the end of q1 and q2 automatically generated a item 88 at the end of itself.

Finally, let us look at the Replace Triggers.
It is arbitrary blocks of code that attach to variables and execute whenever the variable's value changes. Here is an example:

var test = "test" on replace oldValue {
println(" value has changed, old value is { oldValue}");
}

test = "no";

The trigger will fires two times, first is the initialized to "test" and the next is fires When the code "test= "no"; " executed, the program will report value has changed. NOTE that the oldValue store the value of test before the trigger was invoked.

2009-04-25

Ubuntu 快速配置

  新装了9.04,很多东西需要重新配置,未免麻烦,就备份一下这个快速配置的过程了:
已经安装完系统了,配好了网络.
首先是添加firefox的一些插件好下东西.
sudo apt-get install compizconfig-settings-manager
安装桌面配置的程序.我喜欢配置各个桌面的快捷键。

在窗口管理器添加右键在当前路径打开终端。
sudo apt-get install nautilus-open-terminal

在窗口管理器以root权限打开当前文件夹:
sudo apt-get install nautilus-gksu

安装基本编译环境:
sudo apt-get install build-essential

接下来安装ATI驱动:
不知道怎么搞的,新的催化剂不能直接gui安装,在我这里得用
sudo sh ati-driver-installer-9-3-x86.x86_64.run --buildpkg Ubuntu/9.04
生成一大堆包以后再安装。
安装需要的c++库和dkms
sudo apt-get install libstdc++5
之后安装所有生成的包:
sudo dpkg -i *.deb
重启到recovery模式,root login后
aticonfig --initial
----------------------------------
OK~~原来还以为可以,结果浪费了我半天时间,事实证明这个在我的X1250下不能用,官方驱动要9.4以后才支持这个版本的xorg,而正好ati在9.4以后不支持HD之外的显卡了,真漂亮。。。9.3装好的成果就是,libglx.so和fglrx_drv.so一个函数名有字符错误,另外一个就是和xorg client的版本不匹配..
而那个受限驱动,装好以后画屏。。
没办法,只能忍受该死的不断黑屏把.以后一定要买N卡!!!!!!!!!!!鄙视一下ATI


接下来编译安装多媒体支持:
参考以前些的两篇帖子:
http://rhingheart.blogspot.com/2009/03/mencoderffmpeg.html
http://rhingheart.blogspot.com/2009/03/flvh264.html

一个ogg的下载地址这里:
http://www.xiph.org/downloads/

另外,还要smplayer可以从这里入手:
http://smplayer.sourceforge.net/downloads.php?tr_lang=en

之后jdk mldonkey等用apt解决吧。
java中文变方块参考这个解决:
cd /usr/lib/jvm/java-1.5.0-sun-1.5.0.15/jre/lib/fonts
sudo mkdir fallback
cd fallback
sudo cp /usr/share/fonts/truetype/arphic/uming.ttc /usr/lib/jvm/java-1.5.0-sun-1.5.0.15/jre/lib/fonts/fallback

Ubuntu 9.04 国内教育网的源

从我这里看,还是中科大的源品质最好,没有出现404,交大有几个页面是404,所以就选择中科大了:

科大:
----------------------------------------
deb http://debian.ustc.edu.cn/ubuntu/ jaunty main restricted universe multiverse
deb http://debian.ustc.edu.cn/ubuntu/ jaunty-backports restricted universe multiverse
deb http://debian.ustc.edu.cn/ubuntu/ jaunty-proposed main restricted universe multiverse
deb http://debian.ustc.edu.cn/ubuntu/ jaunty-security main restricted universe multiverse
deb http://debian.ustc.edu.cn/ubuntu/ jaunty-updates main restricted universe multiverse
deb-src http://debian.ustc.edu.cn/ubuntu/ jaunty main restricted universe multiverse
deb-src http://debian.ustc.edu.cn/ubuntu/ jaunty-backports main restricted universe multiverse
deb-src http://debian.ustc.edu.cn/ubuntu/ jaunty-proposed main restricted universe multiverse
deb-src http://debian.ustc.edu.cn/ubuntu/ jaunty-security main restricted universe multiverse
deb-src http://debian.ustc.edu.cn/ubuntu/ jaunty-updates main restricted universe multiverse
-----------------------------------------

上交大:
-----------------------------------------
deb http://ftp.sjtu.edu.cn/ubuntu/ jaunty main multiverse restricted universe
deb http://ftp.sjtu.edu.cn/ubuntu/ jaunty-backports main multiverse restricted universe
deb http://ftp.sjtu.edu.cn/ubuntu/ jaunty-proposed main multiverse restricted universe
deb http://ftp.sjtu.edu.cn/ubuntu/ jaunty-security main multiverse restricted universe
deb http://ftp.sjtu.edu.cn/ubuntu/ jaunty-updates main multiverse restricted universe
deb http://ftp.sjtu.edu.cn/ubuntu-cn/ jaunty main multiverse restricted universe
deb-src http://ftp.sjtu.edu.cn/ubuntu/ jaunty main multiverse restricted universe
deb-src http://ftp.sjtu.edu.cn/ubuntu/ jaunty-backports main multiverse restricted universe
deb-src http://ftp.sjtu.edu.cn/ubuntu/ jaunty-proposed main multiverse restricted universe
deb-src http://ftp.sjtu.edu.cn/ubuntu/ jaunty-security main multiverse restricted universe
deb-src http://ftp.sjtu.edu.cn/ubuntu/ jaunty-updates main multiverse restricted universe
--------------------------------------

ps.ubuntu 9.04的新主题相当不错,启动速度也相当牛B,在我机器上只需要19秒左右就进系统了。。。