Gwt-composite-widget

提供:Dev Guides
移動先:案内検索

GWT-複合ウィジェット

前書き

*Composite* ウィジェットは、別のウィジェットをラップして、ラップされたウィジェットのメソッドを隠すことができるウィジェットの一種です。 パネルに追加すると、コンポジットは、ラップするウィジェットが追加されたかのように動作します。 複合は、単一のパネルに含まれる複数の他のウィジェットの集合から単一のウィジェットを作成するのに役立ちます。

クラス宣言

以下は com.google.gwt.user.client.ui.Composite クラスの宣言です-

public abstract class Composite
   extends Widget

クラスコンストラクター

Sr.No. Constructor & Description
1 Composite()

クラスメソッド

Sr.No. Function name & Description
1

protected Widget getWidget()

このコンポジットを定義する最上位のウィジェットへのサブクラスアクセスを提供します。

2

protected void initWidget(Widget widget)

ウィジェットがコンポジットによってラップされるように設定します。

3

boolean isAttached()

このウィジェットが現在ブラウザのドキュメントにアタッチされているかどうかを判断します(つまり、このウィジェットと基礎となるブラウザドキュメントの間にウィジェットの切れ目のないチェーンがあります)。

4

protected void onAttach()

このメソッドは、ウィジェットがブラウザのドキュメントに添付されたときに呼び出されます。

5

void onBrowserEvent(Event event)

ブラウザイベントが受信されるたびに発生します。

6

protected void onDetach()

このメソッドは、ウィジェットがブラウザのドキュメントから切り離されるときに呼び出されます。

7

protected void setWidget(Widget widget)

廃止予定です。 代わりにinitWidget(Widget)を使用してください

継承されるメソッド

このクラスは、次のクラスからメソッドを継承します-

  • com.google.gwt.user.client.ui.UIObject
  • com.google.gwt.user.client.ui.Widget
  • java.lang.Object

複合ウィジェットの例

この例では、GWTでの複合ウィジェットの使用法を示す簡単な手順を紹介します。 次の手順に従って、_GWTで作成したGWTアプリケーションを更新します-アプリケーションの作成_の章-

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>Composite 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.DecoratorPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

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();

     /**
 *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.
         VerticalPanel panel = new VerticalPanel();
        //panel.setBorderWidth(1);
         panel.setSpacing(10);
         panel.add(checkBox);
         panel.add(textBox);

         textBox.setWidth("200");
        //Set the check box's caption, and check it by default.
         checkBox.setText(caption);
         checkBox.setValue(true);
         checkBox.addClickHandler(this);

         DecoratorPanel decoratorPanel = new DecoratorPanel();

         decoratorPanel.add(panel);
        //All composites must call initWidget() in their constructors.
         initWidget(decoratorPanel);
      }

      public void onClick(ClickEvent event) {
         if (event.getSource() == checkBox) {
           //When the check box is clicked,
           //update the text box's enabled state.
            textBox.setEnabled(checkBox.getValue());
         }
      }
   }

   public void onModuleLoad() {
     //Create an optional text box and add it to the root panel.
      OptionalTextBox otb = new OptionalTextBox("Check this to enable me");
      RootPanel.get().add(otb);
   }
}

すべての変更が完了したら、link:/gwt/gwt_create_application [GWT-アプリケーションの作成]の章で行ったように、アプリケーションをコンパイルして開発モードで実行します。 すべてがあなたのアプリケーションでうまくいけば、これは次の結果を生成します-

GWT Composite Widget