- 追加された行はこの色です。
- 削除された行はこの色です。
#author("2019-09-02T11:11:42+00:00","","")
#topicpath
----
#contents
**設定ページ [#q24fd585]
org.eclipse.ui.IWorkbenchPreferencePage はウィンドウ >> 設定 にある設定ページを表すインターフェースです。
#ref(preference.png)
実際はorg.eclipse.jface.preference.FieldEditorPreferencePage をextendsして使うのが多いようです。
***設定ページの拡張ポイント [#p618a200]
org.eclipse.ui.preferencePages
***plugin.xmlのサンプル [#n4e5a3ab]
<extension point="org.eclipse.ui.preferencePages">
<page
class="nu.mine.kino.plugin.hsqldb.ui.HsqldbPreferencePage"
id="nu.mine.kino.plugin.hsqldb.ui.HsqldbPreferencePage"
name="Hsqldb設定"/>
</extension>
***ソース [#i47c9138]
キー値の定数クラス
public interface IConstants {
public static final String USER_ID = "user_id";
public static final String PASSWORD = "password";
public static final String PORT = "port";
}
画面クラス
package nu.mine.kino.plugin.hsqldb.ui;
import org.apache.log4j.Logger;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.StringFieldEditor;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
/**
* @author Masatomi KINO
* @version $Revision$
*/
public class HsqldbPreferencePage extends FieldEditorPreferencePage implements
IWorkbenchPreferencePage {
/**
* Logger for this class
*/
private static final Logger logger = Logger
.getLogger(HsqldbPreferencePage.class);
public HsqldbPreferencePage() {
super(FieldEditorPreferencePage.GRID);
setDescription("Hsqldbに接続するための設定を行います。設定を変更した場合、
起動しているサーバは停止されます。");
setPreferenceStore(HsqldbUIPlugin.getDefault().getPreferenceStore());
↑ プラグインの IPreferenceStore フィールドと関連づけ
}
protected void createFieldEditors() {
{
addField(new StringFieldEditor(IConstants.USER_ID, "&UserID",
getFieldEditorParent()));
↑キー値と画面のフィールドを関連づけ
}
{
addField(new StringFieldEditor(IConstants.PASSWORD, "&Password",
getFieldEditorParent()));
↑キー値と画面のフィールドを関連づけ
}
{
addField(new StringFieldEditor(IConstants.PORT, "P&ort番号",
getFieldEditorParent()));
↑キー値と画面のフィールドを関連づけ
}
}
public void init(IWorkbench workbench) {
}
}
***設定値にアクセスする [#kb155f3c]
上のようにクラスを定義しておくと、実際に設定ページで設定した値には以下のようにアクセスすることができます。
String port = HsqldbUIPlugin.getDefault().getPreferenceStore()
.getString(IConstants.PORT);
***初期値を設定する [#f70f3dea]
さて、初期値(画面のsaとか9001とか)の設定は、別の拡張ポイント
org.eclipse.core.runtime.preferences
を使用します。この拡張ポイントに登録したクラスはプラグインの活性化時にロードされるので、その中で初期値をセットすればよいわけです。
拡張ポイントは以下のように設定しました。
<extension point="org.eclipse.core.runtime.preferences">
<initializer class="nu.mine.kino.plugin.hsqldb.ui.
HsqldbPreferenceInitializer"/>
</extension>
この拡張ポイントのJavaのクラスは
org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer;
です。このクラスを拡張する、以下のようなクラスを作りました。
package nu.mine.kino.plugin.hsqldb.ui;
import org.apache.log4j.Logger;
import org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer;
import org.eclipse.jface.preference.IPreferenceStore;
public class HsqldbPreferenceInitializer extends AbstractPreferenceInitializer {
public void initializeDefaultPreferences() {
IPreferenceStore store = HsqldbUIPlugin.getDefault()
.getPreferenceStore();
store.setDefault(IConstants.USER_ID, "sa");
store.setDefault(IConstants.PASSWORD, "");
store.setDefault(IConstants.PORT, "9001");
}
}
これで、プラグインが活性化されるときに上のメソッドが実行され、フィールドに初期値が設定されます。この初期値ははじめて設定ページを開いたときに表示されるのはもちろん「デフォルトの復元」などをクリックしたときもこの初期値に戻ります。
**TIPS [#l7191b19]
***OKボタンを押したときに、なんかやる [#xb3a575c]
IPreferencePage#performOk()をOverrideします。
public boolean performOk() {
String port = HsqldbUIPlugin.getDefault().getPreferenceStore()
.getString(IConstants.PORT);
String userid = HsqldbUIPlugin.getDefault().getPreferenceStore()
.getString(IConstants.USER_ID);
String password = HsqldbUIPlugin.getDefault().getPreferenceStore()
.getString(IConstants.PASSWORD);
HsqldbUIPlugin.getDefault().stopHsqldb();
return super.performOk();
}
***StringFieldEditor の他のエディタ [#d5eaed71]
addField に渡す引数 FieldEditorのサブクラスには StringFieldEditorの他にも以下のようなモノがあります。
String[][] labelAndValue = new String[][] {
new String[] { "Select 1", "1" },
new String[] { "Select 2", "2" },
new String[] { "Select 3", "3" }, };
addField(new RadioGroupFieldEditor("radio1", "Radio Group",
labelAndValue.length, labelAndValue, getFieldEditorParent()));
addField(new PathEditor("HOGE", "Path", "Chooser",
getFieldEditorParent()));
addField(new FileFieldEditor("file1", "File", true,
getFieldEditorParent()));
addField(new DirectoryFieldEditor("directory1", "Directory",
getFieldEditorParent()));
addField(new FontFieldEditor("font1", "Font", "Preview",
getFieldEditorParent()));
addField(new ColorFieldEditor("color1", "Color", getFieldEditorParent()));
#ref(01.png)
***StringFieldEditorなどエディタクラスを自作する [#x8107e97]
例1
package nu.mine.kino.plugin.google.ui.preferences;
import nu.mine.kino.plugin.google.ui.Utility;
import org.eclipse.jface.preference.StringButtonFieldEditor;
import org.eclipse.swt.widgets.Composite;
public class GoogleKeyFieldEditor extends StringButtonFieldEditor {
public GoogleKeyFieldEditor(String name, String labelText, Composite parent) {
super(name, labelText, parent);
setChangeButtonText("Key 取得");
}
/*
* (非 Javadoc)
*
* @see org.eclipse.jface.preference.StringButtonFieldEditor#changePressed()
*/
protected String changePressed() {
Utility.executeBrowser("http://www.google.com/apis/");
// このリターンする値に、テキストボックスを変更する。
// ただし、nullを返せば、何もしないらしい。
return null;
}
}
#ref(googleeditor.png)
例2~
たとえばURLの一覧を入力するフィールドエディタを作ってみました。既存のListEditorをextendsして作ります。
package nu.mine.kino.plugin.javadocsearch.pages;
import java.util.ArrayList;
import java.util.StringTokenizer;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.preference.ListEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class URLEditor extends ListEditor {
/**
* @param name
* 設定を取得するためのキー
* @param labelText
* このフィールドのラベル
* @param parent
* 親のComposite
*/
public URLEditor(String name, String labelText, Composite parent) {
super(name, labelText, parent);
}
/**
* ユーザ入力値を受け取るメソッドです。 ダイアログをユーザ入力を受け付け、
* 入力された文字列を返します。 キャンセルしたときなどはnullが返ります。
*
* @return 入力された文字列
* @see org.eclipse.jface.preference.ListEditor#getNewInputObject()
*/
protected String getNewInputObject() {
URLDialog dialog = new URLDialog(getShell());
int ret = dialog.open();
if (ret == IDialogConstants.OK_ID) {
String text = dialog.getText();
return text;
} else if (ret == IDialogConstants.CANCEL_ID) {
// [Cancel]ボタン押下
return null;
}
return null;
}
/**
* ページ内でリストを表示するときにStringからString[]に変換して表示文字列を取得するメソッド。
*
* @param stringList
* Eclipseに保持されている文字列
* @see org.eclipse.jface.preference.ListEditor#parseString(java.lang.String)
*/
protected String[] parseString(String stringList) {
StringTokenizer st = new StringTokenizer(stringList,
"\t" + "\n\r");
ArrayList v = new ArrayList();
while (st.hasMoreElements()) {
v.add(st.nextElement());
}
return (String[]) v.toArray(new String[v.size()]);
}
/**
* プリファレンスページで保存処理が行われたときに呼ばれるメソッドです。
*
* @param items
* 表示されているリスト。
* @see org.eclipse.jface.preference.ListEditor#createList(java.lang.String[])
*/
protected String createList(String[] items) {
StringBuffer path = new StringBuffer("");
for (int i = 0; i < items.length; i++) {
path.append(items[i]);
// path.append(File.pathSeparator);
path.append("\t");
}
return path.toString();
}
private class URLDialog extends Dialog {
/**
* ダイアログでユーザが入力している文字列(URL) <code>text</code> のコメント
*/
private String text;
public URLDialog(Shell parent) {
super(parent);
}
protected Point getInitialSize() {
return new Point(400, 150);
}
protected void configureShell(Shell newShell) {
super.configureShell(newShell);
newShell.setText("URLダイアログ");
}
/**
* URLを入力するダイアログを描画します。
*
* @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
*/
protected Control createDialogArea(Composite parent) {
Composite composite = (Composite) super.createDialogArea(parent);
// Text textBox = new Text(composite, SWT.MULTI | SWT.V_SCROLL
// | SWT.BORDER | SWT.WRAP);
Label label = new Label(composite, SWT.NONE);
label.setText("URLを入力してください。");
Text textBox = new Text(composite, SWT.SINGLE | SWT.BORDER);
textBox.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
textBox.setText("");
textBox.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
Text source = (Text) e.getSource();
text = source.getText();
}
});
return composite;
}
public String getText() {
return text;
}
}
}
#ref(urleditor.png)
***設定画面の変更をリスナに通知したい。 [#t03ebbaf]
設定画面で値を変更すると、イベントが発生します。そのイベントをListenするリスナーは
org.eclipse.jface.util.IPropertyChangeListener
です。イベントを発生させるクラス((つまり上のリスナをaddしたい先))は
org.eclipse.jface.preference.IPreferenceStore
です。従って、以下のようなコーディングで設定画面をListenすることができます。
HogePlugin.getDefault().getPreferenceStore().addPropertyChangeListener(
new IPropertyChangeListener() {
public void propertyChange(PropertyChangeEvent event) {
System.out.println(event.getSource());
System.out.println(event.getNewValue());
System.out.println(event.getOldValue());
}
});
HogePluginは設定画面を作成したときに、
setPreferenceStore(HogePlugin.getDefault().getPreferenceStore());
というように、その設定画面と関連づけられたプラグインクラスです。
発生したイベントのクラスは
org.eclipse.jface.util.PropertyChangeEvent
というモノで、このイベントクラスからは
-event.getSource():イベントを発生させたモノ
-event.getNewValue():変更後の値
-event.getOldValue():変更前の値
-event.getProperty():変更したフィールドのキー値
などのようにデータを取り出すことができます。
ちなみにこのイベントは、フィールドごとに発生します。つまり、複数のフィールドの値を変更して保存した場合は、そのフィールド数分イベントが発生します。
***設定画面左上のフィルタを使う [#n57165af]
設定画面の左上にテキストでフィルタする機能は、org.eclipse.ui.keywords 拡張ポイントを使用します。
<extension point="org.eclipse.ui.keywords">
<keyword id="nu.mine.kino.plugin.google.ui.keywords.sitesearch" label="ほげ hoge fuga" />
</extension>
というようにフィルタ文字列を羅列した拡張ポイントを定義して、
<extension point="org.eclipse.ui.preferencePages">
<page class="nu.mine.kino.plugin.google.ui.preferences.MainPreferencePage"
id="nu.mine.kino.plugin.google.ui.preferences.MainPreferencePage" name="設定画面。">
<keywordReference id="nu.mine.kino.plugin.google.ui.keywords.sitesearch" />
</page> ↑keyword拡張ポイントのid
</extension>
として設定画面に関連づけします。この場合はフィルタ入力欄に ほげ とか hoge とか fuga とか入れてフィルタすることができます。
-[[Danny's Blog Attempt: Adding keywords to eclipse rcp preferences lookup (search text field)>http://danielbaggio.blogspot.com/2008/07/adding-keywords-to-eclipse-rcp.html]]
***関連リンク [#k5a0e849]
-[[Preference Dialogのサンプル:http://jssst03.c.u-tokyo.ac.jp/~ichiyama/cgi-bin/hiki/hiki.cgi?PreferenceDialog]]
-[[プラグインの設定を保存するには:http://www.atmarkit.co.jp/fjava/rensai3/eclipsepde04/eclipsepde04_1.html]]
-[[PreferenceDialogを使ってツリー型の設定ダイアログを作る>http://cjasmin.fc2web.com/samples/preference_dialog.html]] Eclipseを使う場合は関係ないけど、このダイアログ自体を自分で作成することもできます。ワークベンチがまだ起動していないけど、設定画面を出したいとか、そんなときにも使えそうですね。
----
この記事は
#vote(おもしろかった,そうでもない)
#vote(おもしろかった[16],そうでもない[0])
-%%このままだと一度設定を開かないとデフォルト値が反映されないなぁ -- [[きの]] &new{2004-12-20 (月) 11:43:06};%%
-%%initializeDefaultPreferencesを呼ぶんですね -- [[きの]] &new{2004-12-27 (月) 00:14:49};%%
-Eclipse2.1のときのはなしです。これ↑ -- [[きの]] &new{2006-03-11 15:48:11 (土)};
#comment
#topicpath
SIZE(10){現在のアクセス:&counter;}