Adapt views plugins to TMF
[deliverable/tracecompass.git] / tmf / org.lttng.scope.tmf2.views.ui / src / org / lttng / scope / tmf2 / views / ui / ScopeUIActivator.java
CommitLineData
4290d358
AM
1/*
2 * Copyright (C) 2017 EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com>
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 */
9
10package org.lttng.scope.tmf2.views.ui;
11
12import static java.util.Objects.requireNonNull;
13import static org.eclipse.tracecompass.common.core.NonNullUtils.nullToEmptyString;
14
15import java.util.Collections;
16import java.util.HashMap;
17import java.util.Map;
18
19import org.eclipse.core.runtime.IStatus;
20import org.eclipse.core.runtime.Status;
21import org.eclipse.jdt.annotation.Nullable;
22import org.eclipse.jface.resource.ImageDescriptor;
23import org.eclipse.swt.graphics.Image;
24import org.eclipse.ui.plugin.AbstractUIPlugin;
25import org.osgi.framework.BundleContext;
26
27/**
28 * The activator class controls the plug-in life cycle
29 *
30 * @author Alexandre Montplaisir
31 */
32public abstract class ScopeUIActivator extends AbstractUIPlugin {
33
34 // ------------------------------------------------------------------------
35 // Attributes
36 // ------------------------------------------------------------------------
37
38 /** Map of all the registered activators, indexed by activator classes */
39 private static final Map<Class<? extends ScopeUIActivator>, ScopeUIActivator> UI_ACTIVATORS =
40 Collections.synchronizedMap(new HashMap<Class<? extends ScopeUIActivator>, ScopeUIActivator>());
41
42 /** This instance's plug-in ID */
43 private final String fPluginId;
44
45 // ------------------------------------------------------------------------
46 // Constructors
47 // ------------------------------------------------------------------------
48
49 /**
50 * Constructor
51 */
52 public ScopeUIActivator() {
53 fPluginId = requireNonNull(getBundle().getSymbolicName());
54 }
55
56 // ------------------------------------------------------------------------
57 // Accessors
58 // ------------------------------------------------------------------------
59
60 /**
61 * Return this plug-in's ID.
62 *
63 * @return The plug-in ID
64 */
65 public String getPluginId() {
66 return fPluginId;
67 }
68
69 /**
70 * Get a registered activator. Subclasses should implement their own public
71 * getInstance() method, which returns the result of this.
72 *
73 * @param activatorClass
74 * The activator's runtime class
75 * @return The corresponding activator
76 */
77 protected static <T extends ScopeUIActivator> T getInstance(Class<T> activatorClass) {
78 ScopeUIActivator activator = UI_ACTIVATORS.get(activatorClass);
79 if (activator == null) {
80 /* The activator should be registered at this point! */
81 throw new IllegalStateException();
82 }
83 /*
84 * We inserted the corresponding class into the map ourselves, cast
85 * should always be safe.
86 */
87 @SuppressWarnings("unchecked")
88 T ret = (T) activator;
89 return ret;
90 }
91
92 /**
93 * Get an {@link Image} from a path within the plugin.
94 *
95 * @param path
96 * The path to the image
97 * @return The image object, or null if it could not be found
98 */
99 public @Nullable Image getImageFromPath(String path) {
100 ImageDescriptor id = getImageDescripterFromPath(path);
101 if (id == null) {
102 return null;
103 }
104 return id.createImage();
105 }
106
107 /**
108 * Get the image descriptor from a path within the plugin.
109 *
110 * @param path
111 * The path to the image
112 *
113 * @return The corresponding image descriptor, or null if the image is not
114 * found
115 */
116 public @Nullable ImageDescriptor getImageDescripterFromPath(String path) {
117 return AbstractUIPlugin.imageDescriptorFromPlugin(fPluginId, path);
118 }
119
120 // ------------------------------------------------------------------------
121 // Abstract methods
122 // ------------------------------------------------------------------------
123
124 /**
125 * Additional actions to run at the plug-in startup
126 */
127 protected abstract void startActions();
128
129 /**
130 * Additional actions to run at the plug-in shtudown
131 */
132 protected abstract void stopActions();
133
134 // ------------------------------------------------------------------------
135 // ore.eclipse.core.runtime.Plugin
136 // ------------------------------------------------------------------------
137
138 @Override
139 public final void start(@Nullable BundleContext context) throws Exception {
140 super.start(context);
141 Class<? extends ScopeUIActivator> activatorClass = this.getClass();
142 synchronized (UI_ACTIVATORS) {
143 if (UI_ACTIVATORS.containsKey(activatorClass)) {
144 logError("Duplicate Activator : " + activatorClass.getCanonicalName()); //$NON-NLS-1$
145 }
146 UI_ACTIVATORS.put(activatorClass, this);
147 }
148 startActions();
149 }
150
151 @Override
152 public final void stop(@Nullable BundleContext context) throws Exception {
153 stopActions();
154 UI_ACTIVATORS.remove(this.getClass());
155 super.stop(context);
156 }
157
158 // ------------------------------------------------------------------------
159 // Logging helpers
160 // ------------------------------------------------------------------------
161
162 /**
163 * Log a message with severity INFO.
164 *
165 * @param message
166 * The message to log
167 * @param exception
168 * Optional exception to attach to the message
169 */
170 public void logInfo(@Nullable String message, Throwable... exception) {
171 if (exception.length < 1) {
172 getLog().log(new Status(IStatus.INFO, fPluginId, nullToEmptyString(message)));
173 } else {
174 getLog().log(new Status(IStatus.INFO, fPluginId, nullToEmptyString(message), exception[0]));
175 }
176 }
177
178
179 /**
180 * Log a message with severity WARNING.
181 *
182 * @param message
183 * The message to log
184 * @param exception
185 * Optional exception to attach to the message
186 */
187 public void logWarning(@Nullable String message, Throwable... exception) {
188 if (exception.length < 1) {
189 getLog().log(new Status(IStatus.WARNING, fPluginId, nullToEmptyString(message)));
190 } else {
191 getLog().log(new Status(IStatus.WARNING, fPluginId, nullToEmptyString(message), exception[0]));
192 }
193 }
194
195 /**
196 * Log a message with severity ERROR.
197 *
198 * @param message
199 * The message to log
200 * @param exception
201 * Optional exception to attach to the message
202 */
203 public void logError(@Nullable String message, Throwable... exception) {
204 if (exception.length < 1) {
205 getLog().log(new Status(IStatus.ERROR, fPluginId, nullToEmptyString(message)));
206 } else {
207 getLog().log(new Status(IStatus.ERROR, fPluginId, nullToEmptyString(message), exception[0]));
208 }
209 }
210
211}
This page took 0.031088 seconds and 5 git commands to generate.