/******************************************************************************* * Copyright (c) 2006, 2007 Spring IDE Developers * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Spring IDE Developers - initial API and implementation *******************************************************************************/ package org.springframework.ide.eclipse.beans.ui.search; import java.text.MessageFormat; import java.util.MissingResourceException; import java.util.ResourceBundle; import org.eclipse.core.resources.IWorkspace; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Platform; import org.eclipse.core.runtime.Status; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.IWorkbenchPage; import org.eclipse.ui.IWorkbenchWindow; import org.eclipse.ui.plugin.AbstractUIPlugin; /** * Central access point for the Spring Framework Search UI plug-in * (id <code>"org.springframework.ide.eclipse.beans.ui.search"</code>). * @author Torsten Juergeleit * @author Christian Dupuis */ public class BeansSearchPlugin extends AbstractUIPlugin { /** * Plugin identifier for Spring Beans UI (value * <code>org.springframework.ide.eclipse.beans.ui.search</code>). */ public static final String PLUGIN_ID = "org.springframework.ide.eclipse.beans.ui.search"; private static final String RESOURCE_NAME = PLUGIN_ID + ".messages"; /** The shared instance.*/ private static BeansSearchPlugin plugin; private ResourceBundle resourceBundle; /** * Creates the Spring Beans Search UI plug-in. * <p> * The plug-in instance is created automatically by the Eclipse platform. * Clients must not call. */ public BeansSearchPlugin() { plugin = this; try { resourceBundle = ResourceBundle.getBundle(RESOURCE_NAME); } catch (MissingResourceException e) { log(e); resourceBundle = null; } } public static BeansSearchPlugin getDefault() { return plugin; } public ResourceBundle getResourceBundle() { return resourceBundle; } public static IWorkspace getWorkspace() { return ResourcesPlugin.getWorkspace(); } public static IWorkbenchWindow getActiveWorkbenchWindow() { return getDefault().getWorkbench().getActiveWorkbenchWindow(); } public static Shell getActiveWorkbenchShell() { return getActiveWorkbenchWindow().getShell(); } public static IWorkbenchPage getActiveWorkbenchPage() { return getActiveWorkbenchWindow().getActivePage(); } /** * Returns the string from the plugin's resource bundle, * or 'key' if not found. */ public static String getResourceString(String key) { String bundleString; ResourceBundle bundle = getDefault().getResourceBundle(); if (bundle != null) { try { bundleString = bundle.getString(key); } catch (MissingResourceException e) { log(e); bundleString = "!" + key + "!"; } } else { bundleString = "!" + key + "!"; } return bundleString; } public static boolean isDebug(String option) { String value = Platform.getDebugOption(option); return (value != null && value.equalsIgnoreCase("true") ? true : false); } public static void log(IStatus status) { getDefault().getLog().log(status); } /** * Writes the message to the plug-in's log * * @param message the text to write to the log */ public static void log(String message, Throwable exception) { IStatus status = createErrorStatus(message, exception); getDefault().getLog().log(status); } public static void log(Throwable exception) { getDefault().getLog().log(createErrorStatus( getResourceString("Plugin.internal_error"), exception)); } /** * Returns a new {@link IStatus} for this plug-in */ public static IStatus createErrorStatus(String message, Throwable exception) { if (message == null) { message = ""; } return new Status(IStatus.ERROR, PLUGIN_ID, 0, message, exception); } public static String getFormattedMessage(String key, Object... args) { return MessageFormat.format(getResourceString(key), args); } }