1 /******************************************************************************* 2 * Copyright (c) 2008, 2019 Code 9 Corporation and others. 3 * 4 * This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License 2.0 6 * which accompanies this distribution, and is available at 7 * https://www.eclipse.org/legal/epl-2.0/ 8 * 9 * SPDX-License-Identifier: EPL-2.0 10 * 11 * Contributors: 12 * Code 9 Corporation - initial API and implementation 13 * Rafael Oliveira Nobrega <rafael.oliveira@gmail.com> - bug 242028 14 * Alexander Fedorov <alexander.fedorov@arsysop.ru> - Bug 549441, Bug 489181 15 *******************************************************************************/ 16 package org.eclipse.pde.internal.ds.ui; 17 18 import java.lang.reflect.InvocationTargetException; 19 20 import org.eclipse.core.resources.ResourcesPlugin; 21 import org.eclipse.core.runtime.CoreException; 22 import org.eclipse.core.runtime.IStatus; 23 import org.eclipse.core.runtime.Status; 24 import org.eclipse.jface.dialogs.ErrorDialog; 25 import org.eclipse.jface.resource.ImageRegistry; 26 import org.eclipse.jface.resource.ResourceLocator; 27 import org.eclipse.swt.widgets.Display; 28 import org.eclipse.swt.widgets.Shell; 29 import org.eclipse.ui.IWorkbenchWindow; 30 import org.eclipse.ui.PlatformUI; 31 import org.eclipse.ui.plugin.AbstractUIPlugin; 32 import org.osgi.framework.BundleContext; 33 34 35 /** 36 * The activator class controls the plug-in life cycle 37 */ 38 public class Activator extends AbstractUIPlugin { 39 40 // The plug-in ID 41 public static final String PLUGIN_ID = "org.eclipse.pde.ds.ui"; //$NON-NLS-1$ 42 43 // The shared instance 44 private static Activator plugin; 45 46 /** 47 * The constructor 48 */ Activator()49 public Activator() { 50 } 51 52 @Override start(BundleContext context)53 public void start(BundleContext context) throws Exception { 54 super.start(context); 55 plugin = this; 56 } 57 58 @Override stop(BundleContext context)59 public void stop(BundleContext context) throws Exception { 60 plugin = null; 61 super.stop(context); 62 } 63 64 /** 65 * Returns the shared instance 66 * 67 * @return the shared instance 68 */ getDefault()69 public static Activator getDefault() { 70 return plugin; 71 } 72 73 @Override initializeImageRegistry(ImageRegistry registry)74 protected void initializeImageRegistry(ImageRegistry registry) { 75 registerImageDescriptor(registry, SharedImages.DESC_IMPLEMENTATION); 76 registerImageDescriptor(registry, SharedImages.DESC_PROPERTIES); 77 registerImageDescriptor(registry, SharedImages.DESC_PROPERTY); 78 registerImageDescriptor(registry, SharedImages.DESC_PROVIDE); 79 registerImageDescriptor(registry, SharedImages.DESC_REFERENCE); 80 registerImageDescriptor(registry, SharedImages.DESC_REFERENCE_ZERO_N); 81 registerImageDescriptor(registry, SharedImages.DESC_REFERENCE_ZERO_ONE); 82 registerImageDescriptor(registry, SharedImages.DESC_REFERENCE_ONE_N); 83 registerImageDescriptor(registry, SharedImages.DESC_ROOT); 84 registerImageDescriptor(registry, SharedImages.DESC_SERVICE); 85 registerImageDescriptor(registry, SharedImages.DESC_SERVICES); 86 registerImageDescriptor(registry, SharedImages.DESC_DS); 87 registerImageDescriptor(registry, SharedImages.DESC_ATTR); 88 registerImageDescriptor(registry, SharedImages.OVR_DYNAMIC); 89 registerImageDescriptor(registry, SharedImages.DESC_DETAILS); 90 registerImageDescriptor(registry, SharedImages.DESC_DS_WIZ); 91 } 92 registerImageDescriptor(ImageRegistry registry, String id)93 private void registerImageDescriptor(ImageRegistry registry, String id) { 94 ResourceLocator.imageDescriptorFromBundle(PLUGIN_ID, id).ifPresent(d -> registry.put(id, d)); 95 } 96 getActiveWorkbenchShell()97 public static Shell getActiveWorkbenchShell() { 98 IWorkbenchWindow window = getActiveWorkbenchWindow(); 99 if (window != null) { 100 return window.getShell(); 101 } 102 return null; 103 } 104 getActiveWorkbenchWindow()105 public static IWorkbenchWindow getActiveWorkbenchWindow() { 106 return PlatformUI.getWorkbench().getActiveWorkbenchWindow(); 107 } 108 logException(Throwable e, final String title, String message)109 public static void logException(Throwable e, final String title, 110 String message) { 111 if (e instanceof InvocationTargetException) { 112 e = ((InvocationTargetException) e).getTargetException(); 113 } 114 IStatus status = null; 115 if (e instanceof CoreException) 116 status = ((CoreException) e).getStatus(); 117 else { 118 if (message == null) 119 message = e.getMessage(); 120 if (message == null) 121 message = e.toString(); 122 status = new Status(IStatus.ERROR, PLUGIN_ID, IStatus.OK, 123 message, e); 124 } 125 ResourcesPlugin.getPlugin().getLog().log(status); 126 Display display = Display.getCurrent() != null ? Display.getCurrent() 127 : Display.getDefault(); 128 final IStatus fstatus = status; 129 display.asyncExec(() -> ErrorDialog.openError(null, title, null, fstatus)); 130 } 131 logException(Throwable e)132 public static void logException(Throwable e) { 133 logException(e, null, null); 134 } 135 136 137 } 138