From 0c072215632c9877c0655f49423d83d21cdbd0dd Mon Sep 17 00:00:00 2001 From: Eike Stepper Date: Tue, 6 Nov 2012 18:59:52 +0100 Subject: Offline Example --- .../examples/client/offline/ClientPerspective.java | 28 ++ .../client/offline/FailoverMonitorView.java | 19 +- .../client/offline/NormalRepositoryView.java | 23 + .../client/offline/nodes/NodeManagerDialog.java | 4 +- .../examples/client/offline/nodes/NodeType.java | 35 +- .../org/eclipse/wb/swt/ExampleResourceManager.java | 511 +++++++++++++++++++++ .../src/org/eclipse/wb/swt/ResourceManager.java | 511 --------------------- 7 files changed, 596 insertions(+), 535 deletions(-) create mode 100644 plugins/org.eclipse.emf.cdo.examples.client.offline/src/org/eclipse/emf/cdo/examples/client/offline/ClientPerspective.java create mode 100644 plugins/org.eclipse.emf.cdo.examples.client.offline/src/org/eclipse/wb/swt/ExampleResourceManager.java delete mode 100644 plugins/org.eclipse.emf.cdo.examples.client.offline/src/org/eclipse/wb/swt/ResourceManager.java (limited to 'plugins/org.eclipse.emf.cdo.examples.client.offline/src/org/eclipse') diff --git a/plugins/org.eclipse.emf.cdo.examples.client.offline/src/org/eclipse/emf/cdo/examples/client/offline/ClientPerspective.java b/plugins/org.eclipse.emf.cdo.examples.client.offline/src/org/eclipse/emf/cdo/examples/client/offline/ClientPerspective.java new file mode 100644 index 0000000000..3c579117c0 --- /dev/null +++ b/plugins/org.eclipse.emf.cdo.examples.client.offline/src/org/eclipse/emf/cdo/examples/client/offline/ClientPerspective.java @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others. + * 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: + * Eike Stepper - initial API and implementation + */ +package org.eclipse.emf.cdo.examples.client.offline; + +import org.eclipse.ui.IPageLayout; +import org.eclipse.ui.IPerspectiveFactory; + +/** + * @author Eike Stepper + */ +public class ClientPerspective implements IPerspectiveFactory +{ + public void createInitialLayout(IPageLayout layout) + { + layout.setFixed(true); + layout.addView("org.eclipse.emf.cdo.ui.CDOSessionsView", IPageLayout.LEFT, 0.5f, IPageLayout.ID_EDITOR_AREA); + layout.addView("org.eclipse.ui.views.PropertySheet", IPageLayout.BOTTOM, 0.5f, + "org.eclipse.emf.cdo.ui.CDOSessionsView"); + } +} diff --git a/plugins/org.eclipse.emf.cdo.examples.client.offline/src/org/eclipse/emf/cdo/examples/client/offline/FailoverMonitorView.java b/plugins/org.eclipse.emf.cdo.examples.client.offline/src/org/eclipse/emf/cdo/examples/client/offline/FailoverMonitorView.java index 3f4fb8f16c..046a8d05b1 100644 --- a/plugins/org.eclipse.emf.cdo.examples.client.offline/src/org/eclipse/emf/cdo/examples/client/offline/FailoverMonitorView.java +++ b/plugins/org.eclipse.emf.cdo.examples.client.offline/src/org/eclipse/emf/cdo/examples/client/offline/FailoverMonitorView.java @@ -103,7 +103,7 @@ public class FailoverMonitorView extends AbstractView */ public static class OScope extends Canvas implements Runnable, PaintListener, ControlListener { - public static final int[] PEAK = { 25, 50, 100, 80, 100, 50, 25, 10, 5 }; + public static final int[] PEAK = { 10, 95, 80, 90, 50, 25, 10, 5 }; private static final String INITIAL = "#INITIAL#"; @@ -135,8 +135,6 @@ public class FailoverMonitorView extends AbstractView private int channelHeight; - private int x; - private boolean resizing; private Color black; @@ -230,8 +228,6 @@ public class FailoverMonitorView extends AbstractView return; } - x = Math.min(x, width); - int oldChannelHeight = channelHeight; if (oldChannelHeight == 0) { @@ -277,11 +273,6 @@ public class FailoverMonitorView extends AbstractView redraw(); - if (++x >= width) - { - x = 0; - } - getDisplay().timerExec(getTimerExecMillis(), this); } @@ -311,6 +302,8 @@ public class FailoverMonitorView extends AbstractView private boolean headNegate; + private int x; + private int y; public Channel(String name) @@ -367,6 +360,7 @@ public class FailoverMonitorView extends AbstractView } values = newValues; + x = Math.min(x, width); this.y = y; } @@ -389,6 +383,11 @@ public class FailoverMonitorView extends AbstractView gc.setForeground(black); gc.setLineWidth(2); gc.drawRectangle(x, fx, 2, 2); + + if (++x >= width) + { + x = 0; + } } void setName(String name) diff --git a/plugins/org.eclipse.emf.cdo.examples.client.offline/src/org/eclipse/emf/cdo/examples/client/offline/NormalRepositoryView.java b/plugins/org.eclipse.emf.cdo.examples.client.offline/src/org/eclipse/emf/cdo/examples/client/offline/NormalRepositoryView.java index 10e6689b27..919ce13cb7 100644 --- a/plugins/org.eclipse.emf.cdo.examples.client.offline/src/org/eclipse/emf/cdo/examples/client/offline/NormalRepositoryView.java +++ b/plugins/org.eclipse.emf.cdo.examples.client.offline/src/org/eclipse/emf/cdo/examples/client/offline/NormalRepositoryView.java @@ -22,7 +22,11 @@ import org.eclipse.jface.viewers.TreeViewer; import org.eclipse.swt.SWT; import org.eclipse.swt.custom.SashForm; import org.eclipse.swt.custom.ScrolledComposite; +import org.eclipse.swt.graphics.Image; import org.eclipse.swt.widgets.Composite; +import org.eclipse.wb.swt.ExampleResourceManager; + +import java.util.concurrent.ExecutorService; /** * @author Eike Stepper @@ -47,6 +51,25 @@ public class NormalRepositoryView extends AbstractView { itemProvider = new ContainerItemProvider>() { + private Image bean = ExampleResourceManager.getPluginImage(Application.PLUGIN_ID, "icons/Bean.gif"); + + @Override + public String getText(Object obj) + { + if (obj instanceof ExecutorService) + { + return "ExecutorService"; + } + + return super.getText(obj); + } + + @Override + public Image getImage(Object obj) + { + return bean; + } + @Override protected void handleElementEvent(final IEvent event) { diff --git a/plugins/org.eclipse.emf.cdo.examples.client.offline/src/org/eclipse/emf/cdo/examples/client/offline/nodes/NodeManagerDialog.java b/plugins/org.eclipse.emf.cdo.examples.client.offline/src/org/eclipse/emf/cdo/examples/client/offline/nodes/NodeManagerDialog.java index cb025c7102..3348436bb1 100644 --- a/plugins/org.eclipse.emf.cdo.examples.client.offline/src/org/eclipse/emf/cdo/examples/client/offline/nodes/NodeManagerDialog.java +++ b/plugins/org.eclipse.emf.cdo.examples.client.offline/src/org/eclipse/emf/cdo/examples/client/offline/nodes/NodeManagerDialog.java @@ -34,7 +34,7 @@ import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Tree; -import org.eclipse.wb.swt.ResourceManager; +import org.eclipse.wb.swt.ExampleResourceManager; import java.util.Properties; @@ -104,7 +104,7 @@ public class NodeManagerDialog extends TitleAreaDialog }; setTitle("Node Manager"); - setTitleImage(ResourceManager.getPluginImage(Application.PLUGIN_ID, "icons/NodeManager.gif")); + setTitleImage(ExampleResourceManager.getPluginImage(Application.PLUGIN_ID, "icons/NodeManager.gif")); setMessage("Select an existing node or create a new one."); Composite area = (Composite)super.createDialogArea(parent); diff --git a/plugins/org.eclipse.emf.cdo.examples.client.offline/src/org/eclipse/emf/cdo/examples/client/offline/nodes/NodeType.java b/plugins/org.eclipse.emf.cdo.examples.client.offline/src/org/eclipse/emf/cdo/examples/client/offline/nodes/NodeType.java index 5c04b5acc3..9af2251769 100644 --- a/plugins/org.eclipse.emf.cdo.examples.client.offline/src/org/eclipse/emf/cdo/examples/client/offline/nodes/NodeType.java +++ b/plugins/org.eclipse.emf.cdo.examples.client.offline/src/org/eclipse/emf/cdo/examples/client/offline/nodes/NodeType.java @@ -61,7 +61,7 @@ import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Text; import org.eclipse.ui.application.IWorkbenchWindowConfigurer; -import org.eclipse.wb.swt.ResourceManager; +import org.eclipse.wb.swt.ExampleResourceManager; import org.h2.jdbcx.JdbcDataSource; @@ -169,12 +169,12 @@ public abstract class NodeType extends SetContainer implements IElement public Image getImage() { - return ResourceManager.getPluginImage(Application.PLUGIN_ID, "icons/Folder.gif"); + return ExampleResourceManager.getPluginImage(Application.PLUGIN_ID, "icons/Folder.gif"); } public Image getInstanceImage() { - return ResourceManager.getPluginImage(Application.PLUGIN_ID, "icons/" + getTypeName() + ".gif"); + return ExampleResourceManager.getPluginImage(Application.PLUGIN_ID, "icons/" + getTypeName() + ".gif"); } public String getTypeName() @@ -512,8 +512,11 @@ public abstract class NodeType extends SetContainer implements IElement { super.start(node); + // IConnector connector = (IConnector)IPluginContainer.INSTANCE.getElement("org.eclipse.net4j.connectors", "jvm", + // "example"); + CDOSession session = (CDOSession)IPluginContainer.INSTANCE.getElement("org.eclipse.emf.cdo.sessions", "cdo", - "jvm://example"); + "jvm://example?repositoryName=" + REPOSITORY_NAME); node.getObjects().put(CDOSession.class, session); } @@ -530,25 +533,33 @@ public abstract class NodeType extends SetContainer implements IElement protected IRepository createRepository(Node node, IStore store, Map props) { String serverName = node.getSetting(SERVER_PROPERTY); - Node serverNode = getManager().getNode(serverName); + final Node serverNode = getManager().getNode(serverName); if (serverNode == null) { throw new IllegalStateException("Server not found: " + serverName); } - final String serverAddress = "localhost:" + serverNode.getSetting(PORT_PROPERTY); - CDOSessionConfigurationFactory factory = new CDOSessionConfigurationFactory() { public CDONet4jSessionConfiguration createSessionConfiguration() { - IConnector connector = Net4jUtil.getConnector(IPluginContainer.INSTANCE, "tcp", serverAddress); + String serverAddress = "localhost:" + serverNode.getSetting(PORT_PROPERTY); - CDONet4jSessionConfiguration configuration = CDONet4jUtil.createNet4jSessionConfiguration(); - configuration.setConnector(connector); - configuration.setRepositoryName(REPOSITORY_NAME); - configuration.setRevisionManager(CDORevisionUtil.createRevisionManager(CDORevisionCache.NOOP)); + CDONet4jSessionConfiguration configuration; + if (serverNode.getType() instanceof FailoverMonitor) + { + configuration = CDONet4jUtil.createFailoverSessionConfiguration(serverAddress, serverNode.getName()); + } + else + { + IConnector connector = Net4jUtil.getConnector(IPluginContainer.INSTANCE, "tcp", serverAddress); + configuration = CDONet4jUtil.createNet4jSessionConfiguration(); + configuration.setConnector(connector); + configuration.setRepositoryName(REPOSITORY_NAME); + } + + configuration.setRevisionManager(CDORevisionUtil.createRevisionManager(CDORevisionCache.NOOP)); return configuration; } }; diff --git a/plugins/org.eclipse.emf.cdo.examples.client.offline/src/org/eclipse/wb/swt/ExampleResourceManager.java b/plugins/org.eclipse.emf.cdo.examples.client.offline/src/org/eclipse/wb/swt/ExampleResourceManager.java new file mode 100644 index 0000000000..4fa47d02e8 --- /dev/null +++ b/plugins/org.eclipse.emf.cdo.examples.client.offline/src/org/eclipse/wb/swt/ExampleResourceManager.java @@ -0,0 +1,511 @@ +/******************************************************************************* + * Copyright (c) 2011 Google, Inc. + * 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: + * Google, Inc. - initial API and implementation + *******************************************************************************/ +package org.eclipse.wb.swt; + +import org.eclipse.core.runtime.Platform; +import org.eclipse.jface.resource.CompositeImageDescriptor; +import org.eclipse.jface.resource.ImageDescriptor; +import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.graphics.Point; +import org.eclipse.swt.graphics.Rectangle; + +import org.osgi.framework.Bundle; + +import java.io.File; +import java.io.InputStream; +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +/** + * Utility class for managing OS resources associated with SWT/JFace controls such as colors, fonts, images, + * etc. + * + * !!! IMPORTANT !!! Application code must explicitly invoke the dispose() method to release the + * operating system resources managed by cached objects when those objects and OS resources are no longer + * needed (e.g. on application shutdown) + * + * This class may be freely distributed as part of any application or plugin. + *

+ * + * @author scheglov_ke + * @author Dan Rubel + */ +public class ExampleResourceManager extends SWTResourceManager +{ + // ////////////////////////////////////////////////////////////////////////// + // + // Image + // + // ////////////////////////////////////////////////////////////////////////// + private static Map m_descriptorImageMap = new HashMap(); + + /** + * Returns an {@link ImageDescriptor} stored in the file at the specified path relative to the specified + * class. + * + * @param clazz + * the {@link Class} relative to which to find the image descriptor. + * @param path + * the path to the image file. + * @return the {@link ImageDescriptor} stored in the file at the specified path. + */ + public static ImageDescriptor getImageDescriptor(Class clazz, String path) + { + return ImageDescriptor.createFromFile(clazz, path); + } + + /** + * Returns an {@link ImageDescriptor} stored in the file at the specified path. + * + * @param path + * the path to the image file. + * @return the {@link ImageDescriptor} stored in the file at the specified path. + */ + public static ImageDescriptor getImageDescriptor(String path) + { + try + { + return ImageDescriptor.createFromURL(new File(path).toURI().toURL()); + } + catch (MalformedURLException e) + { + return null; + } + } + + /** + * Returns an {@link Image} based on the specified {@link ImageDescriptor}. + * + * @param descriptor + * the {@link ImageDescriptor} for the {@link Image}. + * @return the {@link Image} based on the specified {@link ImageDescriptor}. + */ + public static Image getImage(ImageDescriptor descriptor) + { + if (descriptor == null) + { + return null; + } + Image image = m_descriptorImageMap.get(descriptor); + if (image == null) + { + image = descriptor.createImage(); + m_descriptorImageMap.put(descriptor, image); + } + return image; + } + + /** + * Maps images to decorated images. + */ + @SuppressWarnings("unchecked") + private static Map>[] m_decoratedImageMap = new Map[LAST_CORNER_KEY]; + + /** + * Returns an {@link Image} composed of a base image decorated by another image. + * + * @param baseImage + * the base {@link Image} that should be decorated. + * @param decorator + * the {@link Image} to decorate the base image. + * @return {@link Image} The resulting decorated image. + */ + public static Image decorateImage(Image baseImage, Image decorator) + { + return decorateImage(baseImage, decorator, BOTTOM_RIGHT); + } + + /** + * Returns an {@link Image} composed of a base image decorated by another image. + * + * @param baseImage + * the base {@link Image} that should be decorated. + * @param decorator + * the {@link Image} to decorate the base image. + * @param corner + * the corner to place decorator image. + * @return the resulting decorated {@link Image}. + */ + public static Image decorateImage(final Image baseImage, final Image decorator, final int corner) + { + if (corner <= 0 || corner >= LAST_CORNER_KEY) + { + throw new IllegalArgumentException("Wrong decorate corner"); + } + Map> cornerDecoratedImageMap = m_decoratedImageMap[corner]; + if (cornerDecoratedImageMap == null) + { + cornerDecoratedImageMap = new HashMap>(); + m_decoratedImageMap[corner] = cornerDecoratedImageMap; + } + Map decoratedMap = cornerDecoratedImageMap.get(baseImage); + if (decoratedMap == null) + { + decoratedMap = new HashMap(); + cornerDecoratedImageMap.put(baseImage, decoratedMap); + } + // + Image result = decoratedMap.get(decorator); + if (result == null) + { + final Rectangle bib = baseImage.getBounds(); + final Rectangle dib = decorator.getBounds(); + final Point baseImageSize = new Point(bib.width, bib.height); + CompositeImageDescriptor compositImageDesc = new CompositeImageDescriptor() + { + @Override + protected void drawCompositeImage(int width, int height) + { + drawImage(baseImage.getImageData(), 0, 0); + if (corner == TOP_LEFT) + { + drawImage(decorator.getImageData(), 0, 0); + } + else if (corner == TOP_RIGHT) + { + drawImage(decorator.getImageData(), bib.width - dib.width, 0); + } + else if (corner == BOTTOM_LEFT) + { + drawImage(decorator.getImageData(), 0, bib.height - dib.height); + } + else if (corner == BOTTOM_RIGHT) + { + drawImage(decorator.getImageData(), bib.width - dib.width, bib.height - dib.height); + } + } + + @Override + protected Point getSize() + { + return baseImageSize; + } + }; + // + result = compositImageDesc.createImage(); + decoratedMap.put(decorator, result); + } + return result; + } + + /** + * Dispose all of the cached images. + */ + public static void disposeImages() + { + SWTResourceManager.disposeImages(); + // dispose ImageDescriptor images + { + for (Iterator I = m_descriptorImageMap.values().iterator(); I.hasNext();) + { + I.next().dispose(); + } + m_descriptorImageMap.clear(); + } + // dispose decorated images + for (int i = 0; i < m_decoratedImageMap.length; i++) + { + Map> cornerDecoratedImageMap = m_decoratedImageMap[i]; + if (cornerDecoratedImageMap != null) + { + for (Map decoratedMap : cornerDecoratedImageMap.values()) + { + for (Image image : decoratedMap.values()) + { + image.dispose(); + } + decoratedMap.clear(); + } + cornerDecoratedImageMap.clear(); + } + } + // dispose plugin images + { + for (Iterator I = m_URLImageMap.values().iterator(); I.hasNext();) + { + I.next().dispose(); + } + m_URLImageMap.clear(); + } + } + + // ////////////////////////////////////////////////////////////////////////// + // + // Plugin images support + // + // ////////////////////////////////////////////////////////////////////////// + /** + * Maps URL to images. + */ + private static Map m_URLImageMap = new HashMap(); + + /** + * Provider for plugin resources, used by WindowBuilder at design time. + */ + public interface PluginResourceProvider + { + URL getEntry(String symbolicName, String path); + } + + /** + * Instance of {@link PluginResourceProvider}, used by WindowBuilder at design time. + */ + private static PluginResourceProvider m_designTimePluginResourceProvider = null; + + /** + * Returns an {@link Image} based on a plugin and file path. + * + * @param plugin + * the plugin {@link Object} containing the image + * @param name + * the path to the image within the plugin + * @return the {@link Image} stored in the file at the specified path + * + * @deprecated Use {@link #getPluginImage(String, String)} instead. + */ + @Deprecated + public static Image getPluginImage(Object plugin, String name) + { + try + { + URL url = getPluginImageURL(plugin, name); + if (url != null) + { + return getPluginImageFromUrl(url); + } + } + catch (Throwable e) + { + // Ignore any exceptions + } + return null; + } + + /** + * Returns an {@link Image} based on a {@link Bundle} and resource entry path. + * + * @param symbolicName + * the symbolic name of the {@link Bundle}. + * @param path + * the path of the resource entry. + * @return the {@link Image} stored in the file at the specified path. + */ + public static Image getPluginImage(String symbolicName, String path) + { + try + { + URL url = getPluginImageURL(symbolicName, path); + if (url != null) + { + return getPluginImageFromUrl(url); + } + } + catch (Throwable e) + { + // Ignore any exceptions + } + return null; + } + + /** + * Returns an {@link Image} based on given {@link URL}. + */ + private static Image getPluginImageFromUrl(URL url) + { + try + { + try + { + String key = url.toExternalForm(); + Image image = m_URLImageMap.get(key); + if (image == null) + { + InputStream stream = url.openStream(); + try + { + image = getImage(stream); + m_URLImageMap.put(key, image); + } + finally + { + stream.close(); + } + } + return image; + } + catch (Throwable e) + { + // Ignore any exceptions + } + } + catch (Throwable e) + { + // Ignore any exceptions + } + return null; + } + + /** + * Returns an {@link ImageDescriptor} based on a plugin and file path. + * + * @param plugin + * the plugin {@link Object} containing the image. + * @param name + * the path to th eimage within the plugin. + * @return the {@link ImageDescriptor} stored in the file at the specified path. + * + * @deprecated Use {@link #getPluginImageDescriptor(String, String)} instead. + */ + @Deprecated + public static ImageDescriptor getPluginImageDescriptor(Object plugin, String name) + { + try + { + try + { + URL url = getPluginImageURL(plugin, name); + return ImageDescriptor.createFromURL(url); + } + catch (Throwable e) + { + // Ignore any exceptions + } + } + catch (Throwable e) + { + // Ignore any exceptions + } + return null; + } + + /** + * Returns an {@link ImageDescriptor} based on a {@link Bundle} and resource entry path. + * + * @param symbolicName + * the symbolic name of the {@link Bundle}. + * @param path + * the path of the resource entry. + * @return the {@link ImageDescriptor} based on a {@link Bundle} and resource entry path. + */ + public static ImageDescriptor getPluginImageDescriptor(String symbolicName, String path) + { + try + { + URL url = getPluginImageURL(symbolicName, path); + if (url != null) + { + return ImageDescriptor.createFromURL(url); + } + } + catch (Throwable e) + { + // Ignore any exceptions + } + return null; + } + + /** + * Returns an {@link URL} based on a {@link Bundle} and resource entry path. + */ + private static URL getPluginImageURL(String symbolicName, String path) + { + // try runtime plugins + { + Bundle bundle = Platform.getBundle(symbolicName); + if (bundle != null) + { + return bundle.getEntry(path); + } + } + // try design time provider + if (m_designTimePluginResourceProvider != null) + { + return m_designTimePluginResourceProvider.getEntry(symbolicName, path); + } + // no such resource + return null; + } + + /** + * Returns an {@link URL} based on a plugin and file path. + * + * @param plugin + * the plugin {@link Object} containing the file path. + * @param name + * the file path. + * @return the {@link URL} representing the file at the specified path. + * @throws Exception + */ + private static URL getPluginImageURL(Object plugin, String name) throws Exception + { + // try to work with 'plugin' as with OSGI BundleContext + try + { + Class BundleClass = Class.forName("org.osgi.framework.Bundle"); //$NON-NLS-1$ + Class BundleContextClass = Class.forName("org.osgi.framework.BundleContext"); //$NON-NLS-1$ + if (BundleContextClass.isAssignableFrom(plugin.getClass())) + { + Method getBundleMethod = BundleContextClass.getMethod("getBundle", new Class[0]); //$NON-NLS-1$ + Object bundle = getBundleMethod.invoke(plugin, new Object[0]); + // + Class PathClass = Class.forName("org.eclipse.core.runtime.Path"); //$NON-NLS-1$ + Constructor pathConstructor = PathClass.getConstructor(new Class[] { String.class }); + Object path = pathConstructor.newInstance(new Object[] { name }); + // + Class IPathClass = Class.forName("org.eclipse.core.runtime.IPath"); //$NON-NLS-1$ + Class PlatformClass = Class.forName("org.eclipse.core.runtime.Platform"); //$NON-NLS-1$ + Method findMethod = PlatformClass.getMethod("find", new Class[] { BundleClass, IPathClass }); //$NON-NLS-1$ + return (URL)findMethod.invoke(null, new Object[] { bundle, path }); + } + } + catch (Throwable e) + { + // Ignore any exceptions + } + // else work with 'plugin' as with usual Eclipse plugin + { + Class PluginClass = Class.forName("org.eclipse.core.runtime.Plugin"); //$NON-NLS-1$ + if (PluginClass.isAssignableFrom(plugin.getClass())) + { + // + Class PathClass = Class.forName("org.eclipse.core.runtime.Path"); //$NON-NLS-1$ + Constructor pathConstructor = PathClass.getConstructor(new Class[] { String.class }); + Object path = pathConstructor.newInstance(new Object[] { name }); + // + Class IPathClass = Class.forName("org.eclipse.core.runtime.IPath"); //$NON-NLS-1$ + Method findMethod = PluginClass.getMethod("find", new Class[] { IPathClass }); //$NON-NLS-1$ + return (URL)findMethod.invoke(plugin, new Object[] { path }); + } + } + return null; + } + + // ////////////////////////////////////////////////////////////////////////// + // + // General + // + // ////////////////////////////////////////////////////////////////////////// + /** + * Dispose of cached objects and their underlying OS resources. This should only be called when the cached + * objects are no longer needed (e.g. on application shutdown). + */ + public static void dispose() + { + disposeColors(); + disposeFonts(); + disposeImages(); + } +} diff --git a/plugins/org.eclipse.emf.cdo.examples.client.offline/src/org/eclipse/wb/swt/ResourceManager.java b/plugins/org.eclipse.emf.cdo.examples.client.offline/src/org/eclipse/wb/swt/ResourceManager.java deleted file mode 100644 index 42b9dc18f4..0000000000 --- a/plugins/org.eclipse.emf.cdo.examples.client.offline/src/org/eclipse/wb/swt/ResourceManager.java +++ /dev/null @@ -1,511 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2011 Google, Inc. - * 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: - * Google, Inc. - initial API and implementation - *******************************************************************************/ -package org.eclipse.wb.swt; - -import org.eclipse.core.runtime.Platform; -import org.eclipse.jface.resource.CompositeImageDescriptor; -import org.eclipse.jface.resource.ImageDescriptor; -import org.eclipse.swt.graphics.Image; -import org.eclipse.swt.graphics.Point; -import org.eclipse.swt.graphics.Rectangle; - -import org.osgi.framework.Bundle; - -import java.io.File; -import java.io.InputStream; -import java.lang.reflect.Constructor; -import java.lang.reflect.Method; -import java.net.MalformedURLException; -import java.net.URL; -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; - -/** - * Utility class for managing OS resources associated with SWT/JFace controls such as colors, fonts, images, - * etc. - * - * !!! IMPORTANT !!! Application code must explicitly invoke the dispose() method to release the - * operating system resources managed by cached objects when those objects and OS resources are no longer - * needed (e.g. on application shutdown) - * - * This class may be freely distributed as part of any application or plugin. - *

- * - * @author scheglov_ke - * @author Dan Rubel - */ -public class ResourceManager extends SWTResourceManager -{ - // ////////////////////////////////////////////////////////////////////////// - // - // Image - // - // ////////////////////////////////////////////////////////////////////////// - private static Map m_descriptorImageMap = new HashMap(); - - /** - * Returns an {@link ImageDescriptor} stored in the file at the specified path relative to the specified - * class. - * - * @param clazz - * the {@link Class} relative to which to find the image descriptor. - * @param path - * the path to the image file. - * @return the {@link ImageDescriptor} stored in the file at the specified path. - */ - public static ImageDescriptor getImageDescriptor(Class clazz, String path) - { - return ImageDescriptor.createFromFile(clazz, path); - } - - /** - * Returns an {@link ImageDescriptor} stored in the file at the specified path. - * - * @param path - * the path to the image file. - * @return the {@link ImageDescriptor} stored in the file at the specified path. - */ - public static ImageDescriptor getImageDescriptor(String path) - { - try - { - return ImageDescriptor.createFromURL(new File(path).toURI().toURL()); - } - catch (MalformedURLException e) - { - return null; - } - } - - /** - * Returns an {@link Image} based on the specified {@link ImageDescriptor}. - * - * @param descriptor - * the {@link ImageDescriptor} for the {@link Image}. - * @return the {@link Image} based on the specified {@link ImageDescriptor}. - */ - public static Image getImage(ImageDescriptor descriptor) - { - if (descriptor == null) - { - return null; - } - Image image = m_descriptorImageMap.get(descriptor); - if (image == null) - { - image = descriptor.createImage(); - m_descriptorImageMap.put(descriptor, image); - } - return image; - } - - /** - * Maps images to decorated images. - */ - @SuppressWarnings("unchecked") - private static Map>[] m_decoratedImageMap = new Map[LAST_CORNER_KEY]; - - /** - * Returns an {@link Image} composed of a base image decorated by another image. - * - * @param baseImage - * the base {@link Image} that should be decorated. - * @param decorator - * the {@link Image} to decorate the base image. - * @return {@link Image} The resulting decorated image. - */ - public static Image decorateImage(Image baseImage, Image decorator) - { - return decorateImage(baseImage, decorator, BOTTOM_RIGHT); - } - - /** - * Returns an {@link Image} composed of a base image decorated by another image. - * - * @param baseImage - * the base {@link Image} that should be decorated. - * @param decorator - * the {@link Image} to decorate the base image. - * @param corner - * the corner to place decorator image. - * @return the resulting decorated {@link Image}. - */ - public static Image decorateImage(final Image baseImage, final Image decorator, final int corner) - { - if (corner <= 0 || corner >= LAST_CORNER_KEY) - { - throw new IllegalArgumentException("Wrong decorate corner"); - } - Map> cornerDecoratedImageMap = m_decoratedImageMap[corner]; - if (cornerDecoratedImageMap == null) - { - cornerDecoratedImageMap = new HashMap>(); - m_decoratedImageMap[corner] = cornerDecoratedImageMap; - } - Map decoratedMap = cornerDecoratedImageMap.get(baseImage); - if (decoratedMap == null) - { - decoratedMap = new HashMap(); - cornerDecoratedImageMap.put(baseImage, decoratedMap); - } - // - Image result = decoratedMap.get(decorator); - if (result == null) - { - final Rectangle bib = baseImage.getBounds(); - final Rectangle dib = decorator.getBounds(); - final Point baseImageSize = new Point(bib.width, bib.height); - CompositeImageDescriptor compositImageDesc = new CompositeImageDescriptor() - { - @Override - protected void drawCompositeImage(int width, int height) - { - drawImage(baseImage.getImageData(), 0, 0); - if (corner == TOP_LEFT) - { - drawImage(decorator.getImageData(), 0, 0); - } - else if (corner == TOP_RIGHT) - { - drawImage(decorator.getImageData(), bib.width - dib.width, 0); - } - else if (corner == BOTTOM_LEFT) - { - drawImage(decorator.getImageData(), 0, bib.height - dib.height); - } - else if (corner == BOTTOM_RIGHT) - { - drawImage(decorator.getImageData(), bib.width - dib.width, bib.height - dib.height); - } - } - - @Override - protected Point getSize() - { - return baseImageSize; - } - }; - // - result = compositImageDesc.createImage(); - decoratedMap.put(decorator, result); - } - return result; - } - - /** - * Dispose all of the cached images. - */ - public static void disposeImages() - { - SWTResourceManager.disposeImages(); - // dispose ImageDescriptor images - { - for (Iterator I = m_descriptorImageMap.values().iterator(); I.hasNext();) - { - I.next().dispose(); - } - m_descriptorImageMap.clear(); - } - // dispose decorated images - for (int i = 0; i < m_decoratedImageMap.length; i++) - { - Map> cornerDecoratedImageMap = m_decoratedImageMap[i]; - if (cornerDecoratedImageMap != null) - { - for (Map decoratedMap : cornerDecoratedImageMap.values()) - { - for (Image image : decoratedMap.values()) - { - image.dispose(); - } - decoratedMap.clear(); - } - cornerDecoratedImageMap.clear(); - } - } - // dispose plugin images - { - for (Iterator I = m_URLImageMap.values().iterator(); I.hasNext();) - { - I.next().dispose(); - } - m_URLImageMap.clear(); - } - } - - // ////////////////////////////////////////////////////////////////////////// - // - // Plugin images support - // - // ////////////////////////////////////////////////////////////////////////// - /** - * Maps URL to images. - */ - private static Map m_URLImageMap = new HashMap(); - - /** - * Provider for plugin resources, used by WindowBuilder at design time. - */ - public interface PluginResourceProvider - { - URL getEntry(String symbolicName, String path); - } - - /** - * Instance of {@link PluginResourceProvider}, used by WindowBuilder at design time. - */ - private static PluginResourceProvider m_designTimePluginResourceProvider = null; - - /** - * Returns an {@link Image} based on a plugin and file path. - * - * @param plugin - * the plugin {@link Object} containing the image - * @param name - * the path to the image within the plugin - * @return the {@link Image} stored in the file at the specified path - * - * @deprecated Use {@link #getPluginImage(String, String)} instead. - */ - @Deprecated - public static Image getPluginImage(Object plugin, String name) - { - try - { - URL url = getPluginImageURL(plugin, name); - if (url != null) - { - return getPluginImageFromUrl(url); - } - } - catch (Throwable e) - { - // Ignore any exceptions - } - return null; - } - - /** - * Returns an {@link Image} based on a {@link Bundle} and resource entry path. - * - * @param symbolicName - * the symbolic name of the {@link Bundle}. - * @param path - * the path of the resource entry. - * @return the {@link Image} stored in the file at the specified path. - */ - public static Image getPluginImage(String symbolicName, String path) - { - try - { - URL url = getPluginImageURL(symbolicName, path); - if (url != null) - { - return getPluginImageFromUrl(url); - } - } - catch (Throwable e) - { - // Ignore any exceptions - } - return null; - } - - /** - * Returns an {@link Image} based on given {@link URL}. - */ - private static Image getPluginImageFromUrl(URL url) - { - try - { - try - { - String key = url.toExternalForm(); - Image image = m_URLImageMap.get(key); - if (image == null) - { - InputStream stream = url.openStream(); - try - { - image = getImage(stream); - m_URLImageMap.put(key, image); - } - finally - { - stream.close(); - } - } - return image; - } - catch (Throwable e) - { - // Ignore any exceptions - } - } - catch (Throwable e) - { - // Ignore any exceptions - } - return null; - } - - /** - * Returns an {@link ImageDescriptor} based on a plugin and file path. - * - * @param plugin - * the plugin {@link Object} containing the image. - * @param name - * the path to th eimage within the plugin. - * @return the {@link ImageDescriptor} stored in the file at the specified path. - * - * @deprecated Use {@link #getPluginImageDescriptor(String, String)} instead. - */ - @Deprecated - public static ImageDescriptor getPluginImageDescriptor(Object plugin, String name) - { - try - { - try - { - URL url = getPluginImageURL(plugin, name); - return ImageDescriptor.createFromURL(url); - } - catch (Throwable e) - { - // Ignore any exceptions - } - } - catch (Throwable e) - { - // Ignore any exceptions - } - return null; - } - - /** - * Returns an {@link ImageDescriptor} based on a {@link Bundle} and resource entry path. - * - * @param symbolicName - * the symbolic name of the {@link Bundle}. - * @param path - * the path of the resource entry. - * @return the {@link ImageDescriptor} based on a {@link Bundle} and resource entry path. - */ - public static ImageDescriptor getPluginImageDescriptor(String symbolicName, String path) - { - try - { - URL url = getPluginImageURL(symbolicName, path); - if (url != null) - { - return ImageDescriptor.createFromURL(url); - } - } - catch (Throwable e) - { - // Ignore any exceptions - } - return null; - } - - /** - * Returns an {@link URL} based on a {@link Bundle} and resource entry path. - */ - private static URL getPluginImageURL(String symbolicName, String path) - { - // try runtime plugins - { - Bundle bundle = Platform.getBundle(symbolicName); - if (bundle != null) - { - return bundle.getEntry(path); - } - } - // try design time provider - if (m_designTimePluginResourceProvider != null) - { - return m_designTimePluginResourceProvider.getEntry(symbolicName, path); - } - // no such resource - return null; - } - - /** - * Returns an {@link URL} based on a plugin and file path. - * - * @param plugin - * the plugin {@link Object} containing the file path. - * @param name - * the file path. - * @return the {@link URL} representing the file at the specified path. - * @throws Exception - */ - private static URL getPluginImageURL(Object plugin, String name) throws Exception - { - // try to work with 'plugin' as with OSGI BundleContext - try - { - Class BundleClass = Class.forName("org.osgi.framework.Bundle"); //$NON-NLS-1$ - Class BundleContextClass = Class.forName("org.osgi.framework.BundleContext"); //$NON-NLS-1$ - if (BundleContextClass.isAssignableFrom(plugin.getClass())) - { - Method getBundleMethod = BundleContextClass.getMethod("getBundle", new Class[0]); //$NON-NLS-1$ - Object bundle = getBundleMethod.invoke(plugin, new Object[0]); - // - Class PathClass = Class.forName("org.eclipse.core.runtime.Path"); //$NON-NLS-1$ - Constructor pathConstructor = PathClass.getConstructor(new Class[] { String.class }); - Object path = pathConstructor.newInstance(new Object[] { name }); - // - Class IPathClass = Class.forName("org.eclipse.core.runtime.IPath"); //$NON-NLS-1$ - Class PlatformClass = Class.forName("org.eclipse.core.runtime.Platform"); //$NON-NLS-1$ - Method findMethod = PlatformClass.getMethod("find", new Class[] { BundleClass, IPathClass }); //$NON-NLS-1$ - return (URL)findMethod.invoke(null, new Object[] { bundle, path }); - } - } - catch (Throwable e) - { - // Ignore any exceptions - } - // else work with 'plugin' as with usual Eclipse plugin - { - Class PluginClass = Class.forName("org.eclipse.core.runtime.Plugin"); //$NON-NLS-1$ - if (PluginClass.isAssignableFrom(plugin.getClass())) - { - // - Class PathClass = Class.forName("org.eclipse.core.runtime.Path"); //$NON-NLS-1$ - Constructor pathConstructor = PathClass.getConstructor(new Class[] { String.class }); - Object path = pathConstructor.newInstance(new Object[] { name }); - // - Class IPathClass = Class.forName("org.eclipse.core.runtime.IPath"); //$NON-NLS-1$ - Method findMethod = PluginClass.getMethod("find", new Class[] { IPathClass }); //$NON-NLS-1$ - return (URL)findMethod.invoke(plugin, new Object[] { path }); - } - } - return null; - } - - // ////////////////////////////////////////////////////////////////////////// - // - // General - // - // ////////////////////////////////////////////////////////////////////////// - /** - * Dispose of cached objects and their underlying OS resources. This should only be called when the cached - * objects are no longer needed (e.g. on application shutdown). - */ - public static void dispose() - { - disposeColors(); - disposeFonts(); - disposeImages(); - } -} -- cgit v1.2.3