Java tutorial
/******************************************************************************* * Copyright (c) 2014 Joachim Tessmer. * 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: * Joachim Tessmer - initial API and implementation ******************************************************************************/ package de.instanttouch.ui.scaffolding.swt; import org.eclipse.jface.resource.ImageDescriptor; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.ImageData; import org.eclipse.ui.plugin.AbstractUIPlugin; import org.osgi.framework.BundleContext; import de.instantouch.model.base.SnakeBoolean; import de.instantouch.model.base.SnakeByteArray; import de.instantouch.model.base.SnakeDate; import de.instantouch.model.base.SnakeDouble; import de.instantouch.model.base.SnakeInteger; import de.instantouch.model.base.SnakeLong; import de.instantouch.model.base.SnakeString; import de.instantouch.model.base.SnakeType; import de.instantouch.model.base.ref.SnakeFile; import de.instantouch.model.base.ref.SnakeFolder; import de.instantouch.model.base.ref.SnakeLink; import de.instantouch.model.collections.SnakeEntity; import de.instantouch.model.collections.SnakeList; import de.instantouch.model.collections.SnakeMap; import de.instantouch.model.collections.SnakeMapEntry; import de.instanttouch.ui.scaffolding.swt.SnakeImageRegistry.IImagePathCreator; /** * The activator class controls the plug-in life cycle */ public class Activator extends AbstractUIPlugin { // The plug-in ID public static final String PLUGIN_ID = "de.instanttouch.ui.scaffolding.swt"; //$NON-NLS-1$ // The shared instance private static Activator plugin; /** * The constructor */ public Activator() { } /* * (non-Javadoc) * * @see * org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext * ) */ @Override public void start(BundleContext context) throws Exception { super.start(context); plugin = this; registerImages(); } /* * (non-Javadoc) * * @see * org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext * ) */ @Override public void stop(BundleContext context) throws Exception { plugin = null; super.stop(context); } /** * Returns the shared instance * * @return the shared instance */ public static Activator getDefault() { return plugin; } /** * Returns an image descriptor for the image file at the given plug-in * relative path * * @param path * the path * @return the image descriptor */ public static ImageDescriptor getImageDescriptor(String path) { return imageDescriptorFromPlugin(PLUGIN_ID, path); } /** * Returns an image descriptor for the image file at the given plug-in * relative path * * @param path * the path * @return the image descriptor */ public static ImageDescriptor getImageDescriptor(String bundle, String path) { return imageDescriptorFromPlugin(bundle, path); } /** * Gets an image from this plugin's image registry. If the image is not yet * part of the registry, it will be added. * * @param path * of the image to retrieve * @return An image from the registry */ public static Image getImage(String bundle, String path) { Image image = getDefault().getImageRegistry().get(bundle + ":" + path); if (image == null) { ImageDescriptor descriptor = getImageDescriptor(bundle, path); if (descriptor != null) { image = descriptor.createImage(); getDefault().getImageRegistry().put(bundle + ":" + path, descriptor); descriptor.getImageData(); } } return image; } /** * get registered image with a special height * * @param bundle * @param path * @param width * @return */ public static Image getImage(String bundle, String path, int prefWidth, int prefHeight) { Image image = getDefault().getImageRegistry().get(bundle + ":" + path + ":" + prefWidth + "," + prefHeight); if (image == null) { ImageDescriptor descriptor = getImageDescriptor(bundle, path); if (descriptor != null) { image = descriptor.createImage(); double width = image.getBounds().width; double height = image.getBounds().height; double scaleX = prefWidth / width; double scaleY = prefHeight / height; double scale = Math.min(scaleX, scaleY); double minWidth = width * scale; double minHeight = height * scale; ImageData imageData = image.getImageData(); ImageData newData = imageData.scaledTo((int) minWidth, (int) minHeight); image = new Image(image.getDevice(), newData); getDefault().putImage(image, bundle, path, prefWidth, prefHeight); } } return image; } /** * get registered image with a special height * * @param bundle * @param path * @param height2 * @param width * @return */ public static void putImage(Image image, String bundle, String path, int width, int height) { ImageDescriptor descriptor = ImageDescriptor.createFromImage(image); image = descriptor.createImage(); getDefault().getImageRegistry().put(bundle + ":" + path + ":" + width + "," + height, descriptor); } /** * Gets an image from this plugin's image registry. If the image is not yet * part of the registry, it will be added. * * @param path * of the image to retrieve * @return An image from the registry */ public static Image getImage(String path) { return getImage(PLUGIN_ID, path); } private void registerImages() { SnakeImageRegistry imageRegistry = SnakeImageRegistry.getInstance(); if (imageRegistry != null) { imageRegistry.registerImage(SnakeMapEntry.class, "icons/type_net.png"); imageRegistry.registerImage(SnakeList.class, "icons/list.png"); imageRegistry.registerImage(SnakeEntity.class, "icons/entity.png"); imageRegistry.registerImage(SnakeMap.class, "icons/model_icon.png"); imageRegistry.registerImage(SnakeByteArray.class, "icons/binary-icon.png"); imageRegistry.registerImage(SnakeInteger.class, "icons/number2.png"); imageRegistry.registerImage(SnakeLong.class, "icons/number2.png"); imageRegistry.registerImage(SnakeDouble.class, "icons/number2.png"); imageRegistry.registerImage(SnakeDate.class, "icons/clock-icon.png"); imageRegistry.registerImageCreator(SnakeBoolean.class, new IImagePathCreator() { @Override public String getOverlayPath(SnakeType type) { return null; } @Override public String getImagePath(SnakeType type) { String path = "icons/bool_disable.png"; if (type instanceof SnakeBoolean) { SnakeBoolean asBool = (SnakeBoolean) type; if (!asBool.isNull() && asBool.get()) { path = "icons/bool.png"; } } return path; } }); imageRegistry.registerImage(SnakeLink.class, "icons/link-icon.png"); imageRegistry.registerImage(SnakeFolder.class, "icons/folder_icon.png"); imageRegistry.registerImage(SnakeFile.class, "icons/file-icon.png"); imageRegistry.registerImage(SnakeString.class, "icons/pencil-icon.png"); imageRegistry.registerImage(SnakeType.class, "icons/type.png"); } } }