Gwt-custom-widget
提供:Dev Guides
GWT-カスタムウィジェット
GWTは、カスタムユーザーインターフェイス要素を作成する3つの方法を提供します。 従うべき3つの一般的な戦略があります-
- 複合クラスを拡張してウィジェットを作成-これは、カスタムウィジェットを作成する最も一般的で簡単な方法です。 ここでは、既存のウィジェットを使用して、カスタムプロパティを持つ複合ビューを作成できます。
- * JAVAでGWT DOM APIを使用してウィジェットを作成します*-GWT基本ウィジェットはこの方法で作成されます。 それでも、カスタムウィジェットを作成する非常に複雑な方法であり、慎重に使用する必要があります。
- * JavaScriptを使用し、JSNIを使用してウィジェットにラップします*-これは通常、最後の手段としてのみ行う必要があります。 ネイティブメソッドのクロスブラウザーの影響を考慮すると、非常に複雑になり、デバッグも難しくなります。
複合クラスでカスタムウィジェットを作成する
この例では、GWTでのカスタムウィジェットの作成を示す簡単な手順を紹介します。 次の手順に従って、_GWT-基本ウィジェット_の章で作成したGWTアプリケーションを更新します-
ここでは、Compositeクラスを拡張してカスタムウィジェットを作成します。これは、カスタムウィジェットを構築する最も簡単な方法です。
Step | Description |
---|---|
1 | Create a project with a name HelloWorld under a package com.finddevguides as explained in the GWT - Create Application chapter. |
2 | Modify HelloWorld.gwt.xml, HelloWorld.css, HelloWorldl and HelloWorld.java as explained below. Keep rest of the files unchanged. |
3 | Compile and run the application to verify the result of the implemented logic. |
以下は、変更されたモジュール記述子 src/com.finddevguides/HelloWorld.gwt.xml の内容です。
<?xml version = "1.0" encoding = "UTF-8"?>
<module rename-to = 'helloworld'>
<!-- Inherit the core Web Toolkit stuff. -->
<inherits name = 'com.google.gwt.user.User'/>
<!-- Inherit the default GWT style sheet. -->
<inherits name = 'com.google.gwt.user.theme.clean.Clean'/>
<!-- Specify the app entry point class. -->
<entry-point class = 'com.finddevguides.client.HelloWorld'/>
<!-- Specify the paths for translatable code -->
<source path = 'client'/>
<source path = 'shared'/>
</module>
以下は、変更されたスタイルシートファイル war/HelloWorld.css の内容です。
body {
text-align: center;
font-family: verdana, sans-serif;
}
h1 {
font-size: 2em;
font-weight: bold;
color: #777777;
margin: 40px 0px 70px;
text-align: center;
}
以下は、変更されたHTMLホストファイル war/HelloWorldl の内容です。
<html>
<head>
<title>Hello World</title>
<link rel = "stylesheet" href = "HelloWorld.css"/>
<script language = "javascript" src = "helloworld/helloworld.nocache.js">
</script>
</head>
<body>
<h1>Custom Widget Demonstration</h1>
<div id = "gwtContainer"></div>
</body>
</html>
カスタムウィジェットの作成を示すJavaファイル src/com.finddevguides/HelloWorld.java の内容を以下に示します。
package com.finddevguides.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
public class HelloWorld implements EntryPoint {
/**
*A composite of a TextBox and a CheckBox that optionally enables it.
*/
private static class OptionalTextBox extends Composite implements
ClickHandler {
private TextBox textBox = new TextBox();
private CheckBox checkBox = new CheckBox();
private boolean enabled = true;
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
/**
*Style this widget using .optionalTextWidget CSS class.<br/>
* Style textbox using .optionalTextBox CSS class.<br/>
*Style checkbox using .optionalCheckBox CSS class.<br/>
* Constructs an OptionalTextBox with the given caption
*on the check.
* @param caption the caption to be displayed with the check box
*/
public OptionalTextBox(String caption) {
//place the check above the text box using a vertical panel.
HorizontalPanel panel = new HorizontalPanel();
//panel.setBorderWidth(1);
panel.setSpacing(10);
panel.add(checkBox);
panel.add(textBox);
//all composites must call initWidget() in their constructors.
initWidget(panel);
//set style name for entire widget
setStyleName("optionalTextWidget");
//set style name for text box
textBox.setStyleName("optionalTextBox");
//set style name for check box
checkBox.setStyleName("optionalCheckBox");
textBox.setWidth("200");
//Set the check box's caption, and check it by default.
checkBox.setText(caption);
checkBox.setValue(enabled);
checkBox.addClickHandler(this);
enableTextBox(enabled,checkBox.getValue());
}
public void onClick(ClickEvent event) {
if (event.getSource() == checkBox) {
//When the check box is clicked,
//update the text box's enabled state.
enableTextBox(enabled,checkBox.getValue());
}
}
private void enableTextBox(boolean enable,boolean isChecked){
enable = (enable && isChecked) || (!enable && !isChecked);
textBox.setStyleDependentName("disabled", !enable);
textBox.setEnabled(enable);
}
}
public void onModuleLoad() {
//Create an optional text box and add it to the root panel.
OptionalTextBox otb = new OptionalTextBox(
"Want to explain the solution?");
otb.setEnabled(true);
RootPanel.get().add(otb);
}
}
すべての変更が完了したら、link:/gwt/gwt_create_application [GWT-アプリケーションの作成]の章で行ったように、アプリケーションをコンパイルして開発モードで実行します。 すべてがあなたのアプリケーションでうまくいけば、これは次の結果を生成します-
次の点に注意してください
- Compositeウィジェットを拡張してカスタムウィジェットを作成するのは非常に簡単です。
- GWT組み込みウィジェット、TextBox、およびCheckBoxを使用してウィジェットを作成し、再利用性の概念を使用しました。
- TextBoxは、チェックボックスの状態に応じて無効/有効になります。 コントロールを有効/無効にするAPIを提供しました。
- 文書化されたCSSスタイルを介して内部ウィジェットスタイルを公開しました。