Persistent Data

永続性のあるデータ

This section describes how to save the state of a XUL window.

このセクションでは、XUL ウィンドウの状態を保存する方法を説明します。

Remembering State

状態を記憶する

When building a large application, you will typically want to be able to save some of the state of a window across sessions. For example, the window should remember which toolbars are collapsed even after the user exits.

大きなアプリケーションを作る場合、しばしば、セッション間でウィンドウの状態が 保存できたらよいと思うでしょう。例えば、ウィンドウは、ユーザーがアプリケーションを 終了した後でも、どのツールバーが折り畳まれているのか記憶しておくべきです。

One possibility would be to write a script to collect information about what you would like to save and then save it to a file. However, that would be a pain to do for every application. Conveniently, XUL provides such a mechanism to save the state of a window.

一つの可能性は、何を保存したいのかに関する情報を集め、次いで、それをファイルに 保存するスクリプトを書くことでしょう。しかし、アプリケーションすべてでこれを 行なうのは苦痛です。便利なことに、XUL には、ウィンドウの状態を保存するメカニズムが あります。

The information is collected and stored in a RDF file (localstore.rdf) in the same directory as other user preferences. It holds state information about each window. This method has the advantage that it works with Mozilla user profiles, so that each user can have different settings.

そうした情報は、収集され、ユーザの他の設定情報と同じディレクトリの RDF ファイル (localstore.rdf) に保存されます。そこには、個々のウィンドウに関する 状態情報が保持されます。この方法には、Mozilla のユーザプロファイルと協働できる 利点があります。その結果、個々のユーザごとに異なる設定が可能となります。

XUL allows you to save the state of any element. You will typically want to save toolbar states, window positions and whether certain panels are displayed or not, but you can save almost anything.

XUL では、どの要素の状態も保存できます。多くの場合では、ツールバーの状態や ウィンドウの位置、あるパネルが表示されているかどうかを保存したいでしょうし、 実際ほとんど何でも保存することができます。

To allow the saving of state, you simply add a persist attribute to the element which holds a value you want to save. The persist attribute should be set to a space-separated list of attributes of the element that you want to save. The element must also have an id attribute in order to identify it.

状態を保存できるようにするには、保存したい値をもつ要素に persist 属性を追加するだけです。 persist 属性は、保存したい要素の属性を、空白で区切った リストで指定します。この要素は、識別用に id 属性も 持たなければなりません。

For example, to save the position of a window, you would do the following:

例えば、ウィンドウの大きさを保存するには、次のようにすればよいでしょう。

<window
  id="someWindow"
  width="200"
  height="300"
  persist="width height"
  .
  .
  .

The two attributes of the window element, the width and the height will be saved. You could add additional attributes by adding a space and another attribute name to the persist attribute. You can add the persist attribute to any element and store any attribute. You might use unusual values if you adjust attributes using a script.

window 要素の 2 つの属性である widthheight が保存されます。空白で区切って属性名を persist 属性に加えることによって、別の属性も保存することができます。 persist 属性はどの要素にも加えることができ、 どの属性も保存できます。スクリプトを使って属性を操作している場合、通常あり得ない 値を使う場合があるかもしれません。

Let's add the persist attribute to some of the elements in the find files dialog. To save the position of the window. To do this, we need to modify the window.

persist 属性を「ファイル検索」ダイアログの幾つかの 要素に追加しましょう。ウィンドウの位置を保存したいとします。このためには、 window を変更する必要があります。

<window
  id="findfile-window"
  title="Find Files"
  persist="screenX screenY width height"
  orient="horizontal"
  xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">

This will cause the x and y position of the window and the width and height of the window to be saved. We could extend it further to save the collapsed state of the splitter. It doesn't really make sense to save the current tab state.

これによって、ウィンドウの x 座標と y 座標、ウィンドウの幅と高さが保存されます。 これは、スプリッターの折り畳まれた状態を保存するよう拡張できます。実際には、 現在のタブの状態を保存しても意味はないでしょう。


(Next) Next, we'll look at using style sheets with XUL files.

(進む) 次は、XUL ファイルでスタイルシートを使うことについて見ることにしましょう。

Find files example so far: Source View

今のところの「ファイル検索」の例: Source View


Copyright (C) 1999 - 2004 XulPlanet.com
訳者: kmine
このドキュメントのオリジナルは XULPlanet において英語で公布されています。
またドキュメントの管理の言語は現在も英語です。この日本語訳は、
利用者の利便のために Mozilla Japan 翻訳部門によって提供されたものです。
フィードバックは英語で、元の著者に送って下さい。
翻訳された文書の一覧は、現在以下のURLで見ることが出来ます。
http://www.mozilla.gr.jp/jt/xul/progress.html