// 下階層用テンプレート #topicpath ---- //ここにコンテンツを記述します。 #contents いよいよ検索結果一覧を表示するビューを作成します。とりあえず nu.mine.kino.plugin.google.ui.views.GoogleResultView としました。 ***plugin.xml [#mb316f89] plugin.xml(抜粋)は以下のようになりました。 <extension point="org.eclipse.ui.views"> <category name="Googleプラグイン" id="nu.mine.kino.plugin.google"/> <view class="nu.mine.kino.plugin.google.ui.views.GoogleResultView" icon="icons/icon.gif" category="nu.mine.kino.plugin.google" name="Google検索結果" id="nu.mine.kino.plugin.google.ui.views.GoogleResultView"/> </extension> ***ウィジェットを配置していく [#m237c183] どんどんウィジェットを配置していきます。とりあえず、こんな感じになりました。 #ref(prototype.png) ***ColumnLayoutDataでカラムの設定をする [#ad2187b5] SWTだけではなくて、JFaceも使うときはカラムの制御を[[ColumnLayoutData:http://www.eclipse.org/documentation/html/plugins/org.eclipse.platform.doc.isv/doc/reference/api/org/eclipse/jface/viewers/ColumnLayoutData.html]]でやるのがスマートみたいです((とりあえずEclipseのMarkerViewなどの実装はそうなってた))。実際それに習って変更します。[[ColumnLayoutData:http://www.eclipse.org/documentation/html/plugins/org.eclipse.platform.doc.isv/doc/reference/api/org/eclipse/jface/viewers/ColumnLayoutData.html]]はフィールドの幅などの情報を表現するクラスです。 ポイントは -フィールドにあらかじめ[[ColumnLayoutData:http://www.eclipse.org/documentation/html/plugins/org.eclipse.platform.doc.isv/doc/reference/api/org/eclipse/jface/viewers/ColumnLayoutData.html]]で「初期設定」をセットしておく -ヘッダ名やヘッダに表示するアイコンの情報、またビューワの各カラムにオブジェクトが渡されたときにオブジェクトを文字列に変換する変換処理、などを備えた自作のクラス[[IField:http://kino.mine.nu/cgi-bin/viewvc.cgi/nu.mine.kino.plugin.google.ui/source/nu/mine/kino/plugin/google/ui/views/fields/IField.java]]を作成し、それをフィールドに保持しておく -画面表示時に、mementoを参照し、mementoから表示幅を取得してみる、取得できなかったらフィールドの「初期設定」を使う、 というところですね。わけがわからないと思うので、もっと具体的に、コーディングはこんな感じになります。 -フィールド: // TableLayoutと一緒になって使う。各カラムの表示幅など情報を設定する。 private final ColumnLayoutData[] DEFAULT_COLUMN_LAYOUTS = { new ColumnWeightData(100), new ColumnWeightData(100), new ColumnWeightData(100) }; private final IField[] VISIBLE_FIELDS = { new TitleField(), new SummaryField(), new URLField() }; -テーブルの構築: TableLayout layout = new TableLayout(); table.setLayout(layout); table.setHeaderVisible(true); table.setLinesVisible(true); ColumnLayoutData[] columnLayouts = getColumnLayouts();<-後述 for (int i = 0; i < columnLayouts.length; i++) { layout.addColumnData(columnLayouts[i]); TableColumn tc = new TableColumn(table, SWT.NONE, i); tc.setText(VISIBLE_FIELDS[i].getColumnHeaderText()); <-後述2 tc.setImage(VISIBLE_FIELDS[i].getColumnHeaderImage()); <-後述2 // tc.addSelectionListener(headerListener); } -カラムの設定情報を、mementoかフィールドから取得するロジック(後述) private ColumnLayoutData[] getColumnLayouts() { if (memento == null) { return DEFAULT_COLUMN_LAYOUTS; } // ↓実際はmementoからColumnLayoutData[]を構築して返す return DEFAULT_COLUMN_LAYOUTS; } -自前のIFieldクラスからデータを生成する(後述2) package nu.mine.kino.plugin.google.ui.views.fields; import org.eclipse.swt.graphics.Image; /** * テーブル形式のビューワのカラム情報を扱うインターフェースです。 * * @author Masatomi KINO * @version $Revision$ */ public interface IField { /** * @return The text to be displayed in the column header for this field. */ String getColumnHeaderText(); /** * @return The image to be displayed in the column header for this field or * <code>null<code>. */ Image getColumnHeaderImage(); /** * @param obj * @return The String value of the object for this particular field to be * displayed to the user. */ String getValue(Object obj); /** * @param obj * @return The image value of the object for this particular field to be * displayed to the user or <code>null<code>. */ Image getImage(Object obj); } その実装クラス public class TitleField implements IField { public String getColumnHeaderText() { return "Title"; } public Image getColumnHeaderImage() { // TODO 自動生成されたメソッド・スタブ return null; } public String getValue(Object obj) { // TODO 自動生成されたメソッド・スタブ return obj.toString(); } public Image getImage(Object obj) { // TODO 自動生成されたメソッド・スタブ return null; } } となるような感じです。 こうすることで、 -mementoから前回の設定値などのリストアがやりやすくなる -後に作成する、LabelProviderの実装クラスにもIFieldを渡してやることで、いやなswitch分をポリモフィズムで削除できそう -ソートロジックなどもIFieldに実装すればポリモフィズム云々 などの効果が得られそうです。 ***ソースコード [#k1062fb8] 最後にソースはこんな感じです。 //$Id$ //作成日: 2005/05/21 package nu.mine.kino.plugin.google.ui.views; import nu.mine.kino.plugin.google.ui.views.fields.IField; import nu.mine.kino.plugin.google.ui.views.fields.SummaryField; import nu.mine.kino.plugin.google.ui.views.fields.TitleField; import nu.mine.kino.plugin.google.ui.views.fields.URLField; import org.eclipse.jface.action.Action; import org.eclipse.jface.action.IMenuManager; import org.eclipse.jface.action.IToolBarManager; import org.eclipse.jface.viewers.ArrayContentProvider; import org.eclipse.jface.viewers.ColumnLayoutData; import org.eclipse.jface.viewers.ColumnWeightData; import org.eclipse.jface.viewers.TableLayout; import org.eclipse.jface.viewers.TableViewer; import org.eclipse.swt.SWT; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Table; import org.eclipse.swt.widgets.TableColumn; import org.eclipse.ui.IMemento; import org.eclipse.ui.IViewSite; import org.eclipse.ui.PartInitException; import org.eclipse.ui.part.ViewPart; public class GoogleResultView extends ViewPart { // TableLayoutと一緒になって使う。各カラムの表示幅など情報を設定する。 private final ColumnLayoutData[] DEFAULT_COLUMN_LAYOUTS = { new ColumnWeightData(200), new ColumnWeightData(200), new ColumnWeightData(200) }; private final IField[] VISIBLE_FIELDS = { new TitleField(), new SummaryField(), new URLField() }; private Table table; private Action doubleClickAction; private Action openBrowserAction; private IMemento memento; public void init(IViewSite site, IMemento memento) throws PartInitException { super.init(site, memento); this.memento = memento; } public void createPartControl(Composite parent) { Composite container = new Composite(parent, SWT.NONE); container.setLayout(new FillLayout()); final TableViewer viewer = new TableViewer(container, SWT.FULL_SELECTION | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL); viewer.setContentProvider(new ArrayContentProvider()); viewer.setInput(getViewSite()); initTable(viewer); createActions(); initializeToolBar(); initializeMenu(); } private void initTable(final TableViewer viewer) { table = viewer.getTable(); TableLayout layout = new TableLayout(); table.setLayout(layout); table.setHeaderVisible(true); table.setLinesVisible(true); ColumnLayoutData[] columnLayouts = getColumnLayouts(); for (int i = 0; i < columnLayouts.length; i++) { layout.addColumnData(columnLayouts[i]); TableColumn tc = new TableColumn(table, SWT.NONE, i); tc.setText(VISIBLE_FIELDS[i].getColumnHeaderText()); tc.setImage(VISIBLE_FIELDS[i].getColumnHeaderImage()); // tc.addSelectionListener(headerListener); } } private ColumnLayoutData[] getColumnLayouts() { if (memento == null) { return DEFAULT_COLUMN_LAYOUTS; } // ↓実際はmementoからColumnLayoutData[]を構築して返す return DEFAULT_COLUMN_LAYOUTS; } private void createActions() { openBrowserAction = new Action("サイトにアクセスする") { public void run() { } }; doubleClickAction = new Action("") { public void run() { } }; } private void initializeToolBar() { IToolBarManager tbm = getViewSite().getActionBars().getToolBarManager(); } private void initializeMenu() { IMenuManager manager = getViewSite().getActionBars().getMenuManager(); } public void setFocus() { } } ***スクリーンショット [#ec539a86] 実際にビューを表示してみると、こんな感じです。 #ref(view.png) 次は [[検索ロジックをビューに実装>Eclipse/プラグイン開発のTIPS集/GooglePlugin/実際の検索ロジックをビューに実装]] します。 ---- この記事は #vote(おもしろかった[2],そうでもない[0]) #comment #topicpath SIZE(10){現在のアクセス:&counter;}