| /******************************************************************************* |
| * Copyright (c) 2016 ALL4TEC & CEA LIST. |
| * 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: |
| * ALL4TEC & CEA LIST - initial API and implementation |
| ******************************************************************************/ |
| package org.polarsys.esf.globalanalysis.execution.ui; |
| |
| import java.util.ResourceBundle; |
| |
| import org.eclipse.core.runtime.Assert; |
| import org.eclipse.core.runtime.IAdaptable; |
| import org.eclipse.core.runtime.IStatus; |
| import org.eclipse.core.runtime.Platform; |
| import org.eclipse.core.runtime.PlatformObject; |
| import org.eclipse.core.runtime.Status; |
| import org.eclipse.emf.common.EMFPlugin; |
| import org.eclipse.emf.common.ui.EclipseUIPlugin; |
| import org.eclipse.emf.common.util.ResourceLocator; |
| import org.eclipse.jface.resource.ImageRegistry; |
| import org.osgi.framework.BundleContext; |
| import org.polarsys.esf.core.common.messages.Messages; |
| |
| /** |
| * The activator class controls the plug-in life cycle. |
| * |
| * @author $Author: ymunoz $ |
| * @version $Revision: 168 $ |
| */ |
| public class GlobalAnalysisExecutionUIActivator extends EMFPlugin { |
| |
| /** Keep track of the encapsulating singleton. */ |
| public static final GlobalAnalysisExecutionUIActivator INSTANCE = new GlobalAnalysisExecutionUIActivator(); |
| |
| /** Keep track of the implementation singleton. */ |
| private static Implementation sPlugin = null; |
| |
| /** Messages class used to find localised string. */ |
| private static Messages sMessages = new Messages(ResourceBundle.getBundle(Messages.BUNDLE_NAME)); |
| |
| /** |
| * Create the instance. |
| */ |
| public GlobalAnalysisExecutionUIActivator() { |
| super(new ResourceLocator[] {}); |
| } |
| |
| /** |
| * Create an Error status with the data given in parameter and log it. |
| * |
| * @param pMessage The message to log |
| * @param pException The exception to log |
| */ |
| public static void logError(final String pMessage, final Exception pException) { |
| // Create the Error status |
| IStatus vStatus = new Status(IStatus.ERROR, sPlugin.getSymbolicName(), IStatus.ERROR, pMessage, pException); |
| |
| // Log it |
| INSTANCE.log(vStatus); |
| } |
| |
| /** |
| * Create a Warning status with the data given in parameter and log it. |
| * |
| * @param pMessage The message to log |
| * @param pException The exception to log |
| */ |
| public static void logWarning(final String pMessage, final Exception pException) { |
| // Create the Warning status |
| IStatus vStatus = new Status(IStatus.WARNING, sPlugin.getSymbolicName(), IStatus.WARNING, pMessage, pException); |
| |
| // Log it |
| INSTANCE.log(vStatus); |
| } |
| |
| /** |
| * Create an Info status with the data given in parameter and log it. |
| * |
| * @param pMessage The message to log |
| * @param pException The exception to log |
| */ |
| public static void logInfo(final String pMessage, final Exception pException) { |
| // Create the Info status |
| IStatus vStatus = new Status(IStatus.INFO, sPlugin.getSymbolicName(), IStatus.INFO, pMessage, pException); |
| |
| // Log it |
| INSTANCE.log(vStatus); |
| } |
| |
| /** |
| * Returns the singleton instance of the Eclipse plugin. |
| * |
| * @return The singleton instance. |
| */ |
| @Override |
| public ResourceLocator getPluginResourceLocator() { |
| return sPlugin; |
| } |
| |
| /** |
| * @return The messages class used to return localised string |
| */ |
| public static Messages getMessages() { |
| return sMessages; |
| } |
| |
| /** |
| * If it is possible to adapt the given object to the given type, this |
| * returns the right adapter. |
| * |
| * Performs the following checks: |
| * <ol> |
| * <li>Returns <code>sourceObject</code> if it is an instance of the adapter type.</li> |
| * <li>If sourceObject implements IAdaptable, it is queried for adapters.</li> |
| * <li>If sourceObject is not an instance of PlatformObject, the adapter manager is queried for adapters</li> |
| * </ol> |
| * |
| * Otherwise returns <code>null</code>. |
| * |
| * @param pSourceObject Object to adapt, or null |
| * @param pAdapterClass Type to adapt to |
| * @param pActivatePlugins <code>true</code> if IAdapterManager.loadAdapter should be used |
| * (may trigger plugin activation) |
| * |
| * @return A representation of the source object that is assignable to the |
| * adapter type, or null if no such representation exists |
| * |
| * @see org.eclipse.ui.internal.views.ViewsPlugin.getAdapter |
| */ |
| @SuppressWarnings("restriction") |
| public static Object getAdapter( |
| final Object pSourceObject, |
| final Class<?> pAdapterClass, |
| final boolean pActivatePlugins) { |
| |
| Object vAdapterObject = null; |
| |
| Assert.isNotNull(pAdapterClass); |
| if (pSourceObject != null) { |
| |
| // If the source object is already an instance of the target type, |
| // return it directly |
| if (pAdapterClass.isInstance(pSourceObject)) { |
| vAdapterObject = pSourceObject; |
| |
| } else { |
| // Check if the source object is adaptable |
| // In this case, use its mechanism directly |
| if (pSourceObject instanceof IAdaptable) { |
| // Get the adapter using the source object method |
| vAdapterObject = ((IAdaptable) pSourceObject).getAdapter(pAdapterClass); |
| |
| // Sanity-check |
| if (vAdapterObject != null) { |
| Assert.isTrue(pAdapterClass.isInstance(vAdapterObject)); |
| } |
| } |
| |
| // Then, if the adapter object is still not found, check |
| // if the source object is an instance of PlatformObject to use |
| // its specific adaptation mechanism |
| if (vAdapterObject == null && !(pSourceObject instanceof PlatformObject)) { |
| // Try to find the adapter, with or without a load, according |
| // to the flag value |
| if (pActivatePlugins) { |
| vAdapterObject = Platform.getAdapterManager().loadAdapter( |
| pSourceObject, |
| pAdapterClass.getName()); |
| |
| } else { |
| vAdapterObject = Platform.getAdapterManager().getAdapter( |
| pSourceObject, |
| pAdapterClass); |
| } |
| } |
| } |
| } |
| |
| return vAdapterObject; |
| } |
| |
| /** |
| * Returns the singleton instance of the Eclipse plugin. |
| * |
| * @return The singleton instance. |
| */ |
| public static Implementation getPlugin() { |
| return sPlugin; |
| } |
| |
| /** |
| * The actual implementation of the Eclipse <b>UIPlugin</b>. |
| */ |
| public static class Implementation |
| extends EclipseUIPlugin { |
| |
| /** Key for run propagation icon. */ |
| public static final String ICON_PROPAGATION_KEY = "icon.propagation"; //$NON-NLS-1$ |
| |
| /** Key for wizard propagation image. */ |
| public static final String ICON_WIZARD_PROPAGATION_KEY = "icon.wizard.propagation"; //$NON-NLS-1$ |
| |
| /** |
| * Creates an instance. |
| */ |
| public Implementation() { |
| super(); |
| |
| // Remember the static instance. |
| sPlugin = this; |
| |
| } |
| |
| /** |
| * {@inheritDoc} |
| */ |
| @Override |
| public void start(final BundleContext pContext) throws Exception { |
| super.start(pContext); |
| } |
| |
| /** |
| * {@inheritDoc} |
| */ |
| @Override |
| protected void initializeImageRegistry(final ImageRegistry pReg) { |
| super.initializeImageRegistry(pReg); |
| |
| // Icon paths are not externalised as they are no reason to modify it or to access to it externally |
| String vSymbolicName = getPlugin().getSymbolicName(); |
| |
| pReg.put(ICON_PROPAGATION_KEY, |
| imageDescriptorFromPlugin(vSymbolicName, "src/main/resources/icons/icon_propagation.png")); //$NON-NLS-1$ |
| |
| pReg.put(ICON_WIZARD_PROPAGATION_KEY, |
| imageDescriptorFromPlugin(vSymbolicName, "src/main/resources/icons/wizard_propagation.png")); //$NON-NLS-1$ |
| } |
| |
| } |
| |
| } |