- 追加された行はこの色です。
- 削除された行はこの色です。
// 下階層用テンプレート
#topicpath
----
//ここにコンテンツを記述します。
**プラグインクラスに定義しておきたいメソッド [#wffd52d9]
○○Pluginなど、プラグインクラスにはそのプラグインでやりたいビジネスロジックや、ログ出力などのロジックを記述します。ログ出力に関するメソッドはだいたい以下のようなメソッドを定義しておけばよいでしょう。
***UI依存しないCoreなプラグインの場合 [#a496e8f5]
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依存するプラグインの場合 [#f86377bb]
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) {
if (showErrorDialog)
ErrorDialog.openError(getActiveWorkbenchShell(), null, null,
status);
// ResourcesPlugin.getPlugin().getLog().log(status);
// Should log on the update plugin's log
// Platform.getPlugin("org.eclipse.core.runtime").getLog().log(status);
Bundle bundle = Platform.getBundle("org.eclipse.update.ui"); //$NON-NLS-1$
Platform.getLog(bundle).log(status);
ErrorDialog.openError(getActiveWorkbenchShell(), null, null, status);
} else {
MessageDialog.openInformation(getActiveWorkbenchShell(), null,
status.getMessage());
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();
}
----
この記事は
#vote(おもしろかった[4],そうでもない[0])
#comment
#topicpath
SIZE(10){現在のアクセス:&counter;}