ラベル JavaFX の投稿を表示しています。 すべての投稿を表示
ラベル JavaFX の投稿を表示しています。 すべての投稿を表示

2012年12月21日金曜日

えっ!いろふさんならさっきテストコードで見かけたよ! #irof_history

これはいろふAdvent Calendar 2012の21日目の記事です。

昨日は@tan_go238さんの『irofコマンドをbrewでインストールしてみる 』でした。

明日は@zer0_uさんの『ぬくもりろふ #irof_history』になります。


いろふさんはいろんなところにいる所で有名ですが…


本人のブログを見ると、結構テストにいることが多いみたいです。

で、僕も先ほどテストで見たんですよ。





実装は単純です。


単に比べているものがequalsで等しいか、

nullでないかだけで結果を返しています。





TODO


このライブラリーしょぼいので以下の実装を予定しています。

  • いろふさんのツイートが二時間ない場合はfailになる
  • @Irofアノテーションを付与すると、すべてのクラスにirofメソッドが作成される(Groovyのみ)
  • Twitterと連携して落ちたテストについてツイートする

以下は開発中の画面です。

2012年12月16日日曜日

Trying Groovy's @Vetoable bindings in JavaFX Application #javafx-ja #JavaFX

Hello, these day's Advent Calendar rush is making me much confusion.

I'm mike_neck.

This post is written as 16th day's post in JavaFX Advent Calendar 2012.

This post is a translated version of my 13th day's post in JavaFX Advent Calendar 2012, titled
JavaFXでGroovyのVetoableが機能するか試してみた #javafx-ja
.

Yesterday we read @tarchan's great post titled
JavaFXで電子書籍リーダーを作ってみた
.



What's the Groovy Vetoable


Groovy's Vetoable is a one of AST transformations which is useful to GUI(thus means a Swing) developers.

When creating Swing UIs, you're often interested in monitoring the changes of value of certain UI elements. For this purpose, the usual approach is to use JavaBeans PropertyChangeListeners to be notified when the value of a class field changes. You then end up writing this very common boiler-plate code in your Java beans:





and then the result of the main method will be like as follows.





But with Groovy's @Vetoable annotation, we can add PropertyChangeListener much easily.

  • Add @Vetoable annotation to properties which requires being scanned its change.
  • Get instance.
  • Add PropertyChangeListener as Closure.





and then result of this script will be like as follows.





(For more detail, please refer Bindable and Vetoable transformation)


Applying @Vetoable to JavaFX


I want to apply this @Vetoable's interesting feature to JavaFX UI.

So we are going to make a simple application with a text field and labels whose text is affected by inputs of the text field.


Features

There are a text field and two labels.




If we input "mike" into the text field

  • the left label shows the contents of the text field (i.e. "mike").
  • the right label shows message from "mike" (here "Hello mike").




Designing View and Controller

With Scene Builder we are going to design the view and controller for the application.

1. Add Controller to AnchorPane.




2. Add an event method to a onKeyReleased event of the text field.




3. Add fx:ids to the text field and the labels.




4. Save it as fxml file.


Reading fxml file and @FXML annotation

JavaFX injects some objects and events into some fields and methods annotated with @FXML annotation in a Controller class, when reading fxml file created with Scene Builder.





And the model class is here.





About Controller class

JavaFX executes the method initialize after reading fxml file.

In this sample, initialize methods create an instance of the Person class with @Vetoable annotation, and then gave its vetoableChange an Closure which changes message field of the instance after the name fields is changed.

The method named keyReleased, which is called after user releases his key, changes the name field. And it changes the text of two labels from the Person instance.


Execute it!

Here we run this application.




Too simple view.

Let's input Japanese character "みけ".






Labels are changed successfully as the contents of text field are input.


What is an advantage to use Groovy @Vetoable annotation?


It is much testable with Groovy's @Vetoable annotation than without it.

Please look at the initialize method shown before.

This method provides ...

  • creates an instance of Person class
  • gave vetoableChange

But become more simple.

These code should be in Person class.





Then the controller class becomes more simple.





We can test its behavior without starting any JavaFX application.









We were bothered by the Thread of JavaFX, when we had thought of testing application.

But making JavaFX application with Groovy takes us more simplistic way to test JavaFX Application.


Conclusion


Let's use Groovy into JavaFX application!

Tomorrow we will read @rewtheblow's post.

2012年12月13日木曜日

JavaFXでGroovyのVetoableが機能するか試してみた #javafx-ja

Advent Calendarたくさん書きすぎて、

何のAdvent Calendarを書いているかわからなくなってきている

ミケです。

これは『JavaFX Advent Calendar』の第13日目のエントリーになります。

昨日はtaizさんの「JavaFX Media playerのちょっと面白い機能」でした。



GroovyのBindable and Vetoable transformation


JavaFXなのにGroovyのことを少し説明しておきます。

Bindable and Vetoable transformationというのは、

Groovy Beans(Java Beansとまあほぼ同等)に関する値変化を検知して、

何かしらの処理を挟み込むGroovyの機能です。


Vetoable


ちょっと言葉だけで説明したのでは

わかりづらいと思いますので、

実例を書きます。




クラス Personageというフィールドに@Vetoableアノテーションを付与しました。

これによって、値変化を検出することができるようになります。

その後の部分、person.vetoableChange = {//do something}の部分で処理を記述します。

ちょっと前に書いてある部分の

Person.metaClass.define {// define something}

誕生日メソッドを追加しているだけです。


で、実行結果が次のとおりです。



見事、ageの値変化によって、処理を実行させることができました。

なお、もっと興味のある方はこちらを御覧ください。


JavaFXでこれ使えないかな…


というわけで、値変化に応じて処理を実行させてUIの表示を変えられないか、

そんなことを考えてみました。


とりあえず、テキストフィールドに値を入力して、

ラベルが変更されるような簡単なアプリケーションを

作ってみたいと思います。


とりあえず、Scene Builderで、

テキストフィールドとラベルがあるような

画面を作ります。




で、作りたいのは、テキストフィールドに入力すると

左下のラベルには同じ文字列、

右下のラベルには挨拶文がでるようなアプリケーションです。




さて、AnchorPaneにコントロールを貼り付けたら、

AnchorPaneにコントローラーを割り当てます。




次に、テキストフィールドにイベントメソッドを割り当てます。




それから、使うテキストフィールドやらラベルやらにfx:idを割り当てておきます。



こうして、Scene Builderでfxmlを作成すると、

@FXMLアノテーションによって

コントローラークラスに自動的にインジェクションされます。





で、モデルになっているクラスはこちら。





コードの概要



コントローラークラスのメソッドinitializeメソッドは、

fxmlファイルを読み込んだ時点で実行されるメソッドです。

そのメソッドにおいて、

@Vetoableを割り当てたPersonクラスを

インスタンス化して、さらにvetoableChangeを割り当てます。


また、テキストフィールドでキーがリリースされた後に呼び出される

keyReleasedメソッドにおいてPersonインスタンスの

Vetoableなフィールドnameの値を変更します。

そして、その結果を左下のラベルに、

vetoableChangeで実行された結果を右下のラベルに

それぞれ反映します。


実行結果



では、こいつを実行してみましょう。




なんか、かわいげのない画面です。

テキストフィールドに文字を入力してみます。






いい感じにラベルに文字列が反映されていますね。


これ何がうれしいの?



先ほどのinitializeメソッドでは

  • Personをインスタンス化
  • personの値変化を付与

という処理をしていますが、

言ってしまえばこれらの処理は、全部Personにさせるべきなんです。





そうすると、コントローラーはより単純な記述になります。





そして、この振る舞いについて、テストを書くことができます。





テスト実行






テストしづらいと思っていたJavaFXのテストが少し容易になった気がします。


こんな感じでJavaFXにGroovyを利用することで、

テストしやすくなるかもしれません。


おしまい


そんなわけで、皆さんもGroovyでJavaFXをやってみてはいかがでしょうか。

明日は2巡目の@skrbさんです。よろしくおねがいします。

2012年6月27日水曜日

JavaFXのApplication Threadと戯れる-その2

ニャル子さんが終わったので、アイコンを元に戻しました。

みけです。

スレッド周り


Fx-Js-JUnitの話で少しだけ触れましたが、

JavaFXアプリケーションはスレッド周りが大変です。

単品のJavaFXアプリケーションを作る分には、

それほど問題はありませんが、

JUnitと合体させたものを作ろうとすると、

スレッドに関する知識がないと

マジで難しくなります。

死ねます。

死なないで下さい。


プログラムが処理されていく順番をしっかり覚える


というわけで、マルチスレッドなプログラムの処理順を

しっかり抑えておくことが大切です。

というわけで、アプリケーションの起動から終了に至るまでの

順番をログに出力するサンプルコードを書いてみました。



これは単純にアプリケーションを起動して、

終了するだけのコードです。


クイズ


さて、ここで問題です。

Application.launch(App)の後にある

アプリ起動したというログが出力されるのは何番目でしょうか?


宣伝


7月2日に@skrbさん主催の

『第 7 回 JavaFX 勉強会 ツール特集』にてLTやります。

ユーストもあります。

ぜひお楽しみに!


答え


実行した結果を以下に示します。



アプリ起動したは、アプリ終わっちゃうの?の後に着ていますね。

要するにApplication.launch(App)の後はスレッドは残ったまま、

アプリケーションの終了を待機してしまいます。

したがって、「アプリを起動して、それから何かの操作をアプリに対して実行して」

というシナリオでテストを書く場合には、

必ず別スレッドでアプリケーションを起動する

ようにしましょう。