Gwt-bookmark-support

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

GWT-ブックマークのサポート

GWTは、_GWT-History Class_章を参照できるHistoryクラスを使用したブラウザー履歴管理をサポートしています。

GWTは、*トークン*という用語を使用します。これは、アプリケーションが特定の状態に戻るために解析できる単なる文字列です。 アプリケーションはこのトークンをブラウザーの履歴にURLフラグメントとして保存します。

_GWT-履歴クラス_の章では、コードを記述することにより、トークンの作成と履歴での設定を処理します。

この記事では、トークンの作成と履歴管理を自動的に行い、ブックマークのアプリケーション機能を提供する特別なウィジェットHyperlinkについて説明します。

ブックマークの例

この例では、簡単な手順でGWTアプリケーションのブックマークを示します。

_GWT-Create Application_の章で作成した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>
      <iframe src = "javascript:''"id = "__gwt_historyFrame"
         style = "width:0;height:0;border:0"></iframe>
      <h1> Bookmarking Demonstration</h1>
      <div id = "gwtContainer"></div>
   </body>
</html>

Javaファイル src/com.finddevguides/HelloWorld.java の以下のコンテンツを使用して、GWTコードでのブックマークのデモを行います。

package com.finddevguides.client;

import com.google.gwt.core.client.EntryPoint;

import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;

import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class HelloWorld implements EntryPoint {

   private TabPanel tabPanel;

   private void selectTab(String historyToken){
     /*parse the history token*/
      try {
         if (historyToken.substring(0, 9).equals("pageIndex")) {
            String tabIndexToken = historyToken.substring(9, 10);
            int tabIndex = Integer.parseInt(tabIndexToken);
           /*Select the specified tab panel*/
            tabPanel.selectTab(tabIndex);
         } else {
            tabPanel.selectTab(0);
         }
      } catch (IndexOutOfBoundsException e) {
         tabPanel.selectTab(0);
      }
   }

  /**
 *This is the entry point method.
   */
   public void onModuleLoad() {
     /*create a tab panel to carry multiple pages*/
      tabPanel = new TabPanel();

     /*create pages*/
      HTML firstPage = new HTML("<h1>We are on first Page.</h1>");
      HTML secondPage = new HTML("<h1>We are on second Page.</h1>");
      HTML thirdPage = new HTML("<h1>We are on third Page.</h1>");

      String firstPageTitle = "First Page";
      String secondPageTitle = "Second Page";
      String thirdPageTitle = "Third Page";

      Hyperlink firstPageLink = new Hyperlink("1", "pageIndex0");
      Hyperlink secondPageLink = new Hyperlink("2", "pageIndex1");
      Hyperlink thirdPageLink = new Hyperlink("3", "pageIndex2");

      HorizontalPanel linksHPanel = new HorizontalPanel();
      linksHPanel.setSpacing(10);
      linksHPanel.add(firstPageLink);
      linksHPanel.add(secondPageLink);
      linksHPanel.add(thirdPageLink);

     /*If the application starts with no history token,
         redirect to a pageIndex0*/
      String initToken = History.getToken();

      if (initToken.length() == 0) {
         History.newItem("pageIndex0");
         initToken = "pageIndex0";
      }

      tabPanel.setWidth("400");
     /* add pages to tabPanel*/
      tabPanel.add(firstPage, firstPageTitle);
      tabPanel.add(secondPage,secondPageTitle);
      tabPanel.add(thirdPage, thirdPageTitle);

     /*add value change handler to History
      * this method will be called, when browser's Back button
 *or Forward button are clicked.
      * and URL of application changes.
       * */
      History.addValueChangeHandler(new ValueChangeHandler<String>() {
         @Override
         public void onValueChange(ValueChangeEvent<String> event) {
            selectTab(event.getValue());
         }
      });

      selectTab(initToken);

      VerticalPanel vPanel = new VerticalPanel();

      vPanel.setSpacing(10);
      vPanel.add(tabPanel);
      vPanel.add(linksHPanel);

     /*add controls to RootPanel*/
      RootPanel.get().add(vPanel);
   }
}

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

GWTブックマークデモ

  • 1、2、または3をクリックします。 インデックスによってタブが変わることがわかります。
  • 1、2、または3をクリックすると、アプリケーションのURLが変更され、#pageIndexがURLに追加されます。
  • ブラウザの戻るボタンと進むボタンが有効になっていることも確認できます。
  • ブラウザの「戻る」ボタンと「進む」ボタンを使用すると、それに応じて異なるタブが選択されます。
  • 1、2、または3を右クリックします。 開く、新しいウィンドウで開く、新しいタブで開く、お気に入りに追加などのオプションを表示できます。
  • 3を右クリックします。 お気に入りに追加を選択します。 ブックマークをページ3として保存します。
  • お気に入りを開き、ページ3を選択します。 3番目のタブが選択されています。