- 追加された行はこの色です。
- 削除された行はこの色です。
[[Eclipse/プラグイン開発のTIPS集/GooglePlugin/実際の検索ロジックをビューに実装]]
// 下階層用テンプレート
#topicpath
----
//ここにコンテンツを記述します。
つぎは [[「検索ロジックをビューに実装」>Eclipse/プラグイン開発のTIPS集/GooglePlugin/実際の検索ロジックをビューに実装]] で実装したビューのメソッドを呼び出すポップアップメニューをJavaエディタに実装します。
エディタのポップアップにプラグインする方法などは[[メニューバーとか、ポップアップとかのまとめ>Eclipse/プラグイン開発のTIPS集/メニューバーとか、ポップアップとかのまとめ]]
***plugin.xml [#w13e0f16]
<extension point="org.eclipse.ui.popupMenus">
<viewerContribution
targetID="#CompilationUnitEditorContext"
id="nu.mine.kino.plugin.google.ui.contributions.CompilationUnitEditorContext">
<action
label="Googleで検索"
icon="icons/icon.gif"
class="nu.mine.kino.plugin.google.ui.popup.actions.CompilationUnitEditorContextAction"
menubarPath="additions"
id="nu.mine.kino.plugin.google.ui.popup.actions.CompilationUnitEditorContextAction">
<selection
class="org.eclipse.jface.text.ITextSelection">
</selection>
</action>
</viewerContribution>
</extension>
これでJavaエディタのポップアップメニューに「Googleで検索」というメニューが表示されます。
***スクリーンショット [#x2bc6c02]
#ref(menu.png)
***ソースコード [#ce995de6]
ソースコードは以下のようになりました。
package nu.mine.kino.plugin.google.ui.popup.actions;
import nu.mine.kino.plugin.google.ui.views.GoogleResultView;
import org.apache.log4j.Logger;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IEditorActionDelegate;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.texteditor.AbstractTextEditor;
/**
* @author Masatomi KINO
* @version $Revision$
*/
public class CompilationUnitEditorContextAction implements
IEditorActionDelegate {
private static final Logger logger = Logger
.getLogger(CompilationUnitEditorContextAction.class);
private ISelection selection;
private IEditorPart editorPart;
public void setActiveEditor(IAction action, IEditorPart targetEditor) {
this.editorPart = targetEditor;
}
public void selectionChanged(IAction action, ISelection selection) {
this.selection = selection;
}
public void run(IAction action) {
logger.debug("run(IAction) - start");
AbstractTextEditor editor = (AbstractTextEditor) editorPart;
final ITextSelection textSelection = (ITextSelection) selection;
logger.debug("選択されている文字列は" + textSelection.getText());
IWorkbenchPage workbenchPage = editor.getSite().getPage();
try {
logger.debug("Viewを取得します");
final GoogleResultView view = (GoogleResultView) workbenchPage
.showView("nu.mine.kino.plugin.google.ui.views.GoogleResultView");
logger.debug("Viewを取得しました");
logger.debug(view);
view.search(textSelection.getText());
} catch (PartInitException e) {
logger.error(e);
}
logger.debug("run(IAction) - end");
}
}
これでJavaエディタのポップアップメニューから「Googleで検索」を選択すると[[IEditorActionDelegate:http://www.docjar.com/docs/api/org/eclipse/ui/IEditorActionDelegate.html]]#run が実行されてビューに検索結果が表示されます。
#ref(result.png)
まだ[[TableViewer:http://www.docjar.com/docs/api/org/eclipse/jface/viewers/TableViewer.html]]に[[LabelProvider:http://www.docjar.com/docs/api/org/eclipse/jface/viewers/LabelProvider.html]]を設定していないため、結果表示も検索結果オブジェクト(nu.mine.kino.googleapis.ResultElement)のtoString()がそのまま表示されてしまいます。
次はこの[[LabelProviderを設定して、TableViewに情報を表示>Eclipse/プラグイン開発のTIPS集/GooglePlugin/LabelProviderで表示を制御する]]できるようにします。
----
この記事は
#vote(おもしろかった,そうでもない)
#vote(おもしろかった[2],そうでもない[0])
#comment
#topicpath
SIZE(10){現在のアクセス:&counter;}