Last active
August 29, 2015 14:08
-
-
Save jabaraster/7dc213175a6299d7ffc3 to your computer and use it in GitHub Desktop.
ModalWindow内のページとデータをやり取りするときの落とし穴 ref: http://qiita.com/jabaraster/items/2878dde75f5ce4d9d678
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ChildPage extends WebPage { | |
public ChildPage(final Callback pCallback) { | |
AjaxLink<?> link = new AjaxLink<Object>("link") { | |
@Override | |
public void onClick(AjaxRequestTarget pTarget) { | |
// リンクが押されたことを現在時刻と共に呼び出し元に通知 | |
pCallback.callback(pTarget, Calendar.getInstance().getTime()); | |
} | |
}; | |
this.add(link); | |
} | |
/** | |
* リンクが押されたことを通知するコールバック. | |
*/ | |
public interface Callback extends Serializable { | |
void callback(AjaxRequestTarget pTarget, Date pNow); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void callback(AjaxRequestTarget pTarget, Date pNow) { | |
// ModalWindowでリンクが押された時刻を記録してダイアログを閉じる | |
ParentPage.this.now = pNow; | |
modalWindow.close(pTarget); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void callback(AjaxRequestTarget pTarget, Date pNow) { | |
// ModalWindowでリンクが押された時刻を記録してダイアログを閉じる | |
ParentPage p = (ParentPage) ParentPage.this.getPageReference().getPage(); | |
p.now = pNow; | |
modalWindow.close(pTarget); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ParentPage extends WebPage { | |
private Date now = Calendar.getInstance().getTime(); | |
private Label nowLabel; | |
public ParentPage() { | |
// 時刻を表示するラベル | |
this.nowLabel = new Label("now", new AbstractReadOnlyModel<String>() { | |
@Override | |
public String getObject() { | |
return DateFormat.getDateTimeInstance().format(now); | |
} | |
}); | |
this.nowLabel.setOutputMarkupId(true); | |
this.add(this.nowLabel); | |
// ModalWindow | |
final ModalWindow modalWindow = new ModalWindow("modalWindow"); | |
modalWindow.setPageCreator(new PageCreator() { | |
@Override | |
public Page createPage() { | |
final ChildPage.Callback callback = new ChildPage.Callback() { | |
@Override | |
public void callback(AjaxRequestTarget pTarget, Date pNow) { | |
// ModalWindowでリンクが押された時刻を記録してダイアログを閉じる | |
ParentPage.this.now = pNow; | |
modalWindow.close(pTarget); | |
} | |
}; | |
return new ChildPage(callback); | |
} | |
}); | |
modalWindow.setWindowClosedCallback(new WindowClosedCallback() { | |
@Override | |
public void onClose(AjaxRequestTarget pTarget) { | |
// ModalWindowが閉じられたタイミングで時刻ラベルを再描画 | |
// (ブラウザ上の表記が更新されることを期待) | |
pTarget.add(nowLabel); | |
} | |
}); | |
this.add(modalWindow); | |
// ModalWindowを表示するためのリンク | |
AjaxLink<?> shower = new AjaxLink<Object>("shower") { | |
@Override | |
public void onClick(AjaxRequestTarget pTarget) { | |
modalWindow.show(pTarget); | |
} | |
}; | |
this.add(shower); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment