プラグインクラスにロジックを実装する
まずはプラグインクラスで、先ほど作ったcoreプラグイン機能のGUIラッパを作成します。具体的には、
- プログレスバー上で、coreプラグインのスペルミスチェッククラスを使って、スペルミスをチェックするメソッド。
- スペルミスがあった場合は正しいスペルに変更して検索するかを尋ねるダイアログを表示するスレッドクラス。
などを作成します。1つめのメソッドgetSpellingSuggestionは、後に作成するバックグラウンドジョブのダイアログを表示する「org.eclipse.core.runtime.jobs.Job」(のサブクラス)のrun(IProgressMonitor monitor)メソッドから起動されます。2つめのスレッドは、そのメソッド内でさらに別のダイアログ(スペルミス補正ダイアログ)を出すために呼ばれるスレッドです。
プラグインクラスGooglePluginのソースコード
上記2つのビジネスメソッドを、プラグインクラス「GooglePlugin」に次のように定義しました。
package nu.mine.kino.plugin.google.ui; import nu.mine.kino.plugin.google.core.GoogleCorePlugin; import nu.mine.kino.plugin.google.ui.preferences.PreferenceConstants; import org.apache.log4j.Logger; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.jface.dialogs.MessageDialogWithToggle; import org.eclipse.swt.widgets.Display; import org.eclipse.ui.IWorkbench; import org.eclipse.ui.IWorkbenchWindow; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.plugin.AbstractUIPlugin; import org.osgi.framework.BundleContext; /**
* GoogleのWEBサービスAPIを用いて検索を行うプラグインです。
*
* @author Masatomi KINO
* @version $Revision: 1.1 $
*/ public class GooglePlugin extends AbstractUIPlugin { /**
* Logger for this class
*/ private static final Logger logger = Logger.getLogger(GooglePlugin.class); // The plug-in ID public static final String PLUGIN_ID = "nu.mine.kino.plugin.google.ui"; // The shared instance private static GooglePlugin plugin; /**
* The constructor
*/ public GooglePlugin() { plugin = this; } /*
* (non-Javadoc)
*
* @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext) */ public void start(BundleContext context) throws Exception { super.start(context); } /*
* (non-Javadoc)
*
* @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext) */ public void stop(BundleContext context) throws Exception { plugin = null; super.stop(context); } /**
* Returns the shared instance
*
* @return the shared instance
*/ public static GooglePlugin getDefault() { return plugin; } /**
* 引数の文字列でGoogleにリクエストし、
* 単語の誤りをチェックします。
* 候補があった場合、候補が返ります。候補がなかった場合、
* 入力した単語が返ります。
*
* @param text
* @param monitor
* @return
*/ public String getSpellingSuggestion(final String text, IProgressMonitor monitor) { String searchText; String suggestionText = null; // 設定画面より、Google Keyを取得 String myGoogleKey = GooglePlugin.getDefault().getPreferenceStore() .getString(PreferenceConstants.GOOGLE_KEY); // Googleにリクエスト suggestionText = GoogleCorePlugin.getDefault().getSpellingSuggestion( myGoogleKey, text); // nullでない場合は候補が返ってきた if (suggestionText != null) { CheckThread dialog = new CheckThread(text, suggestionText); // ユーザーに変更するかを選ばせる checkSyncExec(dialog); // dialogより結果を取得する if (dialog.isOK()) { // 変更する searchText = suggestionText; logger.debug("スペルミスの候補に変更します"); if (monitor != null) { monitor.setTaskName("「" + suggestionText + "」に変更してGoogleを検索中..."); } } else { // 変更しない searchText = text; logger.debug( "スペルミスの候補に変更しません(ユーザーがキャンセル)"); //$NON-NLS-1$ } } else { logger.debug("スペルミスの候補がありませんでした"); //$NON-NLS-1$ // 例外が発生した場合もこのケース searchText = text; } return searchText; } /**
* プログレスバー上でダイアログを出す際に使用するスレッドです。
*
* @author Masatomi KINO
* @see GoogleResultView#checkSyncExec(Runnable)
*/ private class CheckThread implements Runnable { /**
* Logger for this class
*/ // private final Logger logger = Logger.getLogger(CheckThread.class); // ダイアログでユーザーがどちらを押したかを判定するフラグ // 0 : する // 1 : しない private int returnCode; private String text; private String suggestionText; // private boolean returnFlag; /**
* @param text
* @param suggestionText
*/ public CheckThread(String text, String suggestionText) { this.text = text; this.suggestionText = suggestionText; } public void run() { StringBuffer message = new StringBuffer(); message.append("検索文字列を補正しますか? "); message.append(text); message.append(" -> "); message.append(suggestionText); IWorkbench workbench = PlatformUI.getWorkbench(); IWorkbenchWindow window = workbench.getActiveWorkbenchWindow(); MessageDialog dialog = new MessageDialog( window.getShell(), "スペルミスしていませんか?", null, new String(message), MessageDialog.QUESTION, new String[] { "する", "しない" }, 1); // 戻り値の数値は選択されたボタンのインデックス returnCode = dialog.open(); } /**
* ダイアログでユーザーがどちらを押したかを判定します。
*
* @return 0 : する 1 : しない
*/ public boolean isOK() { if (returnCode == 0) { return true; } return false; // return returnFlag; } } private boolean checkSyncExec(Runnable thread) { if (!Display.getDefault().isDisposed()) { Display.getDefault().syncExec(thread); return true; } else { return false; } } }

