diff options
author | Lars Vogel | 2021-02-15 11:41:42 +0000 |
---|---|---|
committer | Lars Vogel | 2021-10-26 15:34:42 +0000 |
commit | 7cddf9b5fe0cbb3b29588276d552da6a75b4de3b (patch) | |
tree | bf5dd3df71135aa4123485e3c07e3298c18f8ce1 | |
parent | bfab362c5703231c6fd2677bef1128d7a1b9ed28 (diff) | |
download | eclipse.platform.ui-I20211030-1800.tar.gz eclipse.platform.ui-I20211030-1800.tar.xz eclipse.platform.ui-I20211030-1800.zip |
Bug 478220 - Remove activator from org.eclipse.e4.ui.css.swtI20211031-1800I20211030-1800I20211029-1800I20211028-1800I20211027-1800I20211026-1800
Activator was only used to access and track an OSGi immediate component
defined in the same bundle. Replace this by a simple static method.
Change-Id: I3c4e0514e07c9090d81dfe5eefe1b5320e3b66c8
Signed-off-by: Lars Vogel <Lars.Vogel@vogella.com>
Reviewed-on: https://git.eclipse.org/r/c/platform/eclipse.platform.ui/+/176261
Tested-by: Platform Bot <platform-bot@eclipse.org>
9 files changed, 81 insertions, 107 deletions
diff --git a/bundles/org.eclipse.e4.ui.css.swt/META-INF/MANIFEST.MF b/bundles/org.eclipse.e4.ui.css.swt/META-INF/MANIFEST.MF index 68ccec05b3e..163b421a60c 100644 --- a/bundles/org.eclipse.e4.ui.css.swt/META-INF/MANIFEST.MF +++ b/bundles/org.eclipse.e4.ui.css.swt/META-INF/MANIFEST.MF @@ -32,7 +32,6 @@ Require-Bundle: org.eclipse.e4.ui.css.core;bundle-version="0.12.200", org.w3c.css.sac;bundle-version="1.3.0", org.eclipse.core.runtime;bundle-version="[3.6.0,4.0.0)" Bundle-RequiredExecutionEnvironment: JavaSE-11 -Bundle-Activator: org.eclipse.e4.ui.internal.css.swt.CSSActivator Bundle-ActivationPolicy: lazy Import-Package: org.eclipse.jface.resource Automatic-Module-Name: org.eclipse.e4.ui.css.swt diff --git a/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/helpers/CSSSWTColorHelper.java b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/helpers/CSSSWTColorHelper.java index 8df4b6a9dda..33ffab6f257 100644 --- a/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/helpers/CSSSWTColorHelper.java +++ b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/helpers/CSSSWTColorHelper.java @@ -28,7 +28,7 @@ import org.eclipse.e4.ui.css.core.css2.CSS2ColorHelper; import org.eclipse.e4.ui.css.core.css2.CSS2RGBColorImpl; import org.eclipse.e4.ui.css.core.dom.properties.Gradient; import org.eclipse.e4.ui.css.core.engine.CSSEngine; -import org.eclipse.e4.ui.internal.css.swt.CSSActivator; +import org.eclipse.e4.ui.internal.css.swt.ColorAndFontUtil; import org.eclipse.e4.ui.internal.css.swt.definition.IColorAndFontProvider; import org.eclipse.swt.SWT; import org.eclipse.swt.custom.CTabFolder; @@ -339,7 +339,7 @@ public class CSSSWTColorHelper { } private static RGBA findColorByDefinition(String name) { - IColorAndFontProvider provider = CSSActivator.getDefault().getColorAndFontProvider(); + IColorAndFontProvider provider = ColorAndFontUtil.getColorAndFontProvider(); if (provider != null) { RGB rgb = provider.getColor(normalizeId(name.substring(1))); if (rgb != null) { diff --git a/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/helpers/CSSSWTFontHelper.java b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/helpers/CSSSWTFontHelper.java index 559c0c34bc4..bedb97a1cf3 100644 --- a/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/helpers/CSSSWTFontHelper.java +++ b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/helpers/CSSSWTFontHelper.java @@ -23,7 +23,7 @@ import org.eclipse.e4.ui.css.core.css2.CSS2PrimitiveValueImpl; import org.eclipse.e4.ui.css.core.dom.properties.css2.CSS2FontProperties; import org.eclipse.e4.ui.css.core.dom.properties.css2.CSS2FontPropertiesImpl; import org.eclipse.e4.ui.css.core.engine.CSSElementContext; -import org.eclipse.e4.ui.internal.css.swt.CSSActivator; +import org.eclipse.e4.ui.internal.css.swt.ColorAndFontUtil; import org.eclipse.e4.ui.internal.css.swt.definition.IColorAndFontProvider; import org.eclipse.swt.SWT; import org.eclipse.swt.custom.CTabItem; @@ -219,7 +219,7 @@ public class CSSSWTFontHelper { } private static FontData[] findFontDataByDefinition(CSSPrimitiveValue cssFontFamily) { - IColorAndFontProvider provider = CSSActivator.getDefault().getColorAndFontProvider(); + IColorAndFontProvider provider = ColorAndFontUtil.getColorAndFontProvider(); FontData[] result = new FontData[0]; if (provider != null) { FontData[] fontData = provider.getFont(normalizeId(cssFontFamily.getStringValue().substring(1))); diff --git a/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/internal/css/swt/CSSActivator.java b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/internal/css/swt/CSSActivator.java deleted file mode 100644 index 79225a19f4b..00000000000 --- a/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/internal/css/swt/CSSActivator.java +++ /dev/null @@ -1,60 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2010, 2019 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - *******************************************************************************/ -package org.eclipse.e4.ui.internal.css.swt; - -import org.eclipse.e4.ui.internal.css.swt.definition.IColorAndFontProvider; -import org.osgi.framework.BundleActivator; -import org.osgi.framework.BundleContext; -import org.osgi.util.tracker.ServiceTracker; - -public class CSSActivator implements BundleActivator { - - private static CSSActivator activator; - - private BundleContext context; - private ServiceTracker<IColorAndFontProvider, IColorAndFontProvider> colorAndFontProviderTracker; - - public static CSSActivator getDefault() { - return activator; - } - - @Override - public void start(BundleContext context) throws Exception { - activator = this; - this.context = context; - } - - @Override - public void stop(BundleContext context) throws Exception { - if (colorAndFontProviderTracker != null) { - colorAndFontProviderTracker.close(); - colorAndFontProviderTracker = null; - } - this.context = null; - } - - public IColorAndFontProvider getColorAndFontProvider() { - if (colorAndFontProviderTracker == null) { - if (context == null) { - return null; - } - colorAndFontProviderTracker = new ServiceTracker<>( - context, - IColorAndFontProvider.class.getName(), null); - colorAndFontProviderTracker.open(); - } - return colorAndFontProviderTracker.getService(); - } - -} diff --git a/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/internal/css/swt/ColorAndFontUtil.java b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/internal/css/swt/ColorAndFontUtil.java new file mode 100644 index 00000000000..67c6b85b4e8 --- /dev/null +++ b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/internal/css/swt/ColorAndFontUtil.java @@ -0,0 +1,51 @@ +/******************************************************************************* + * Copyright (c) 2010, 2019 IBM Corporation and others. + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.e4.ui.internal.css.swt; + +import org.eclipse.e4.ui.internal.css.swt.definition.IColorAndFontProvider; +import org.osgi.framework.FrameworkUtil; +import org.osgi.framework.ServiceReference; +import org.osgi.util.tracker.ServiceTracker; + +public class ColorAndFontUtil { + + static IColorAndFontProvider colorAndFontProvider = null; + + static { + if (FrameworkUtil.getBundle(ColorAndFontUtil.class).getBundleContext() != null) { + ServiceTracker<IColorAndFontProvider, IColorAndFontProvider> colorAndFontProviderTracker = new ServiceTracker<>( + FrameworkUtil.getBundle(ColorAndFontUtil.class).getBundleContext(), + IColorAndFontProvider.class.getName(), null) { + @Override + public IColorAndFontProvider addingService(ServiceReference<IColorAndFontProvider> reference) { + // this is needed so that the unit test can exchange the color and font provider + // with a mocked version + colorAndFontProvider = super.addingService(reference); + return colorAndFontProvider; + } + }; + colorAndFontProviderTracker.open(); + } + } + + /** + * Util method to access the OSGI immediate component IColorAndFontProvider + * defined in the same bundle as we do life in the same bundle we do not track + * its life-cycle (as it is the same as our life-cycle) + */ + public static IColorAndFontProvider getColorAndFontProvider() { + return colorAndFontProvider; + } + +} diff --git a/tests/org.eclipse.e4.ui.tests.css.swt/META-INF/MANIFEST.MF b/tests/org.eclipse.e4.ui.tests.css.swt/META-INF/MANIFEST.MF index 5b523e8fc0b..6aa6a4a2972 100644 --- a/tests/org.eclipse.e4.ui.tests.css.swt/META-INF/MANIFEST.MF +++ b/tests/org.eclipse.e4.ui.tests.css.swt/META-INF/MANIFEST.MF @@ -19,7 +19,8 @@ Bundle-ActivationPolicy: lazy Bundle-RequiredExecutionEnvironment: JavaSE-11 Import-Package: org.eclipse.core.runtime;version="3.5.0", org.osgi.framework;version="1.7.0";resolution:=optional, - org.osgi.service.event;version="1.3.0";resolution:=optional + org.osgi.service.event;version="1.3.0";resolution:=optional, + org.osgi.util.tracker;version="1.5.3" Eclipse-BundleShape: dir Automatic-Module-Name: org.eclipse.e4.ui.tests.css.swt Bundle-Vendor: %Bundle-Vendor diff --git a/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/css/swt/helpers/CSSSWTHelperTestCase.java b/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/css/swt/helpers/CSSSWTHelperTestCase.java index 4b1f82244d0..8b0f21e96ba 100644 --- a/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/css/swt/helpers/CSSSWTHelperTestCase.java +++ b/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/css/swt/helpers/CSSSWTHelperTestCase.java @@ -16,17 +16,16 @@ package org.eclipse.e4.ui.css.swt.helpers; import static org.eclipse.e4.ui.css.swt.helpers.CSSSWTColorHelper.COLOR_DEFINITION_MARKER; import static org.eclipse.e4.ui.css.swt.helpers.CSSSWTFontHelper.FONT_DEFINITION_MARKER; -import static org.junit.Assert.fail; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; import org.eclipse.e4.ui.css.core.css2.CSS2FontHelper; import org.eclipse.e4.ui.css.core.dom.properties.css2.CSS2FontProperties; import org.eclipse.e4.ui.css.core.impl.dom.CSSValueImpl; -import org.eclipse.e4.ui.internal.css.swt.CSSActivator; import org.eclipse.e4.ui.internal.css.swt.definition.IColorAndFontProvider; import org.eclipse.swt.graphics.FontData; import org.eclipse.swt.graphics.RGB; +import org.osgi.framework.FrameworkUtil; import org.w3c.dom.DOMException; import org.w3c.dom.css.CSSPrimitiveValue; import org.w3c.dom.css.CSSValue; @@ -52,17 +51,8 @@ public abstract class CSSSWTHelperTestCase { } private void registerProvider(final IColorAndFontProvider provider) { - try { - new CSSActivator() { - @Override - public IColorAndFontProvider getColorAndFontProvider() { - return provider; - } - }.start(null); - } catch (Exception e) { - fail(); - } - + FrameworkUtil.getBundle(getClass()).getBundleContext().registerService(IColorAndFontProvider.class, provider, + null); } protected CSS2FontProperties fontProperties(String family) { diff --git a/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/tests/css/swt/ColorDefinitionTest.java b/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/tests/css/swt/ColorDefinitionTest.java index 51b3dcd579a..bb81f067540 100644 --- a/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/tests/css/swt/ColorDefinitionTest.java +++ b/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/tests/css/swt/ColorDefinitionTest.java @@ -18,12 +18,12 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; +import java.util.Hashtable; + import org.eclipse.e4.ui.css.core.engine.CSSEngine; -import org.eclipse.e4.ui.internal.css.swt.CSSActivator; import org.eclipse.e4.ui.internal.css.swt.definition.IColorAndFontProvider; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Color; @@ -33,6 +33,7 @@ import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.internal.themes.ColorDefinition; import org.junit.Test; +import org.osgi.framework.FrameworkUtil; public class ColorDefinitionTest extends CSSSWTTestCase { @@ -169,17 +170,12 @@ public class ColorDefinitionTest extends CSSSWTTestCase { } private void registerColorProviderWith(final String symbolicName, final RGB rgb) { - try { - new CSSActivator() { - @Override - public IColorAndFontProvider getColorAndFontProvider() { - IColorAndFontProvider provider = mock(IColorAndFontProvider.class); - doReturn(rgb).when(provider).getColor(symbolicName); - return provider; - } - }.start(null); - } catch (Exception e) { - fail("Register color provider should not fail"); - } + IColorAndFontProvider provider = mock(IColorAndFontProvider.class); + doReturn(rgb).when(provider).getColor(symbolicName); + Hashtable<String, Object> properties = new Hashtable<>(); + properties.put("service.ranking", "1000"); + + FrameworkUtil.getBundle(getClass()).getBundleContext().registerService(IColorAndFontProvider.class, provider, + properties); } } diff --git a/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/tests/css/swt/FontDefinitionTest.java b/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/tests/css/swt/FontDefinitionTest.java index 2505e623ade..f14cbbe92e3 100644 --- a/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/tests/css/swt/FontDefinitionTest.java +++ b/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/tests/css/swt/FontDefinitionTest.java @@ -19,11 +19,11 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; -import org.eclipse.e4.ui.internal.css.swt.CSSActivator; +import java.util.Hashtable; + import org.eclipse.e4.ui.internal.css.swt.definition.IColorAndFontProvider; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Font; @@ -32,6 +32,7 @@ import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.internal.themes.FontDefinition; import org.junit.Test; +import org.osgi.framework.FrameworkUtil; public class FontDefinitionTest extends CSSSWTTestCase { @@ -142,17 +143,13 @@ public class FontDefinitionTest extends CSSSWTTestCase { } private void registerFontProviderWith(final String symbolicName, final FontData fontData) { - try { - new CSSActivator() { - @Override - public IColorAndFontProvider getColorAndFontProvider() { - IColorAndFontProvider provider = mock(IColorAndFontProvider.class); - doReturn(new FontData[] { fontData }).when(provider).getFont(symbolicName); - return provider; - } - }.start(null); - } catch (Exception e) { - fail("CssActivator start failed"); - } + IColorAndFontProvider provider = mock(IColorAndFontProvider.class); + doReturn(new FontData[] { fontData }).when(provider).getFont(symbolicName); + + Hashtable<String, Object> properties = new Hashtable<>(); + properties.put("service.ranking", "1000"); + + FrameworkUtil.getBundle(getClass()).getBundleContext().registerService(IColorAndFontProvider.class, provider, + null); } } |