Top / Eclipse / プラグイン開発のTIPS集 / プラグインクラスに定義しておきたいメソッド

プラグインクラスに定義しておきたいメソッド

○○Pluginなど、プラグインクラスにはそのプラグインでやりたいビジネスロジックや、ログ出力などのロジックを記述します。ログ出力に関するメソッドはだいたい以下のようなメソッドを定義しておけばよいでしょう。

UI依存しないCoreなプラグインの場合

org.eclipse.core.runtime.Pluginを継承する、UI依存しないプラグインの場合はログ出力をするメソッドを定義します。

public static void log(String message, Exception e) {
    IStatus status = new Status(IStatus.ERROR, getPluginId(),
            IStatus.OK, message, e);
    getDefault().getLog().log(status);
}

public static void log(String message) {
    log(message, null);
}

public static void log(Exception e) {
    StringWriter stringWriter = new StringWriter();
    e.printStackTrace(new PrintWriter(stringWriter));
    String message = stringWriter.getBuffer().toString();
    log(message, e);
}

public static String getPluginId() {
    return getDefault().getBundle().getSymbolicName();
}

UI依存するプラグインの場合

org.eclipse.ui.plugin.AbstractUIPluginを継承するUI依存するプラグインの場合は、ダイアログを出すなどの制御を入れておくと便利ですね。

以下のコードは、org.eclipse.update.internal.ui.UpdateUI などが入ってるプラグインのログ出力のコードです。

 public static String getPluginId() {
  return getDefault().getBundle().getSymbolicName();
}

public static void logException(Throwable e) {
  logException(e, true);
}

public static void logException(Throwable e, boolean showErrorDialog) {
  if (e instanceof InvocationTargetException) {
    e = ((InvocationTargetException) e).getTargetException();
  }

  IStatus status = null;
  if (e instanceof CoreException) {
    status = ((CoreException) e).getStatus();
  } else {
    String message = e.getMessage();
    if (message == null)
      message = e.toString();
    status = new Status(IStatus.ERROR, getPluginId(), IStatus.OK,
        message, e);
  }
  log(status, showErrorDialog);
}

public static void log(IStatus status, boolean showErrorDialog) {
  Bundle bundle = Platform.getBundle("org.eclipse.update.ui"); //$NON-NLS-1$
  Platform.getLog(bundle).log(status);
  if (Display.getCurrent() == null || !showErrorDialog)
    return;
  if (status.getSeverity() != IStatus.INFO) {
    ErrorDialog.openError(getActiveWorkbenchShell(), null, null, status);
  } else {
    MessageDialog.openInformation(getActiveWorkbenchShell(), null, status.getMessage());
  }
}

/**
 * Returns the standard display to be used. The method first checks, if the
 * thread calling this method has an associated disaply. If so, this display
 * is returned. Otherwise the method returns the default display.
 */
public static Display getStandardDisplay() {
  Display display;
  display = Display.getCurrent();
  if (display == null)
    display = Display.getDefault();
  return display;
}

public static Shell getActiveWorkbenchShell() {
  IWorkbenchWindow window = getActiveWorkbenchWindow();
  return window != null ? window.getShell() : getStandardDisplay()
      .getActiveShell();
}

public static IWorkbenchWindow getActiveWorkbenchWindow() {
  return getDefault().getWorkbench().getActiveWorkbenchWindow();
}

この記事は

選択肢 投票
おもしろかった 5  
そうでもない 0  

Top / Eclipse / プラグイン開発のTIPS集 / プラグインクラスに定義しておきたいメソッド

現在のアクセス:9812


トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2019-09-07 (土) 12:36:57 (1686d)