設定ページ †org.eclipse.ui.IWorkbenchPreferencePage? はウィンドウ >> 設定 にある設定ページを表すインターフェースです。 実際はorg.eclipse.jface.preference.FieldEditorPreferencePage? をextendsして使うのが多いようです。 設定ページの拡張ポイント †org.eclipse.ui.preferencePages plugin.xmlのサンプル †<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> ソース †キー値の定数クラス 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) {
}
}
設定値にアクセスする †上のようにクラスを定義しておくと、実際に設定ページで設定した値には以下のようにアクセスすることができます。 String port = HsqldbUIPlugin.getDefault().getPreferenceStore()
.getString(IConstants.PORT);
初期値を設定する †さて、初期値(画面の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 †OKボタンを押したときに、なんかやる †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? の他のエディタ †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()));
StringFieldEditor?などエディタクラスを自作する †例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;
}
}
例2 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;
}
}
}
設定画面の変更をリスナに通知したい。 †設定画面で値を変更すると、イベントが発生します。そのイベントをListenするリスナーは org.eclipse.jface.util.IPropertyChangeListener です。イベントを発生させるクラス*1は 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 というモノで、このイベントクラスからは
などのようにデータを取り出すことができます。 ちなみにこのイベントは、フィールドごとに発生します。つまり、複数のフィールドの値を変更して保存した場合は、そのフィールド数分イベントが発生します。 設定画面左上のフィルタを使う †設定画面の左上にテキストでフィルタする機能は、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 とか入れてフィルタすることができます。 関連リンク †
この記事は
現在のアクセス:24648 |