org.mailster.gui
Class SWTHelper

java.lang.Object
  extended by org.mailster.gui.SWTHelper

public class SWTHelper
extends java.lang.Object

---
Mailster (C) 2007-2009 De Oliveira Edouard

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

See  Mailster Web Site
---

SWTHelper.java - A helper class that handles all the OS associated resources. Resources are cached and the helper provides a disposeAll() method that allows to free all system resources acquired till there. The class also register a JVM shutdown hook to ensure it won't be forgotten.

Version:
$Revision: 1.11 $, $Date: 2009/02/07 19:45:52 $
Author:
Edouard De Oliveira

Field Summary
static java.lang.String DESC_IMAGES_DIRECTORY
          The directory in which images and icons are stored.
static java.lang.String GRAY_IMAGE_SUFFIX
          The suffix added to the key name of the gray version of a image.
static java.lang.String IMAGES_DIRECTORY
           
private static java.util.Map<org.eclipse.swt.graphics.Image,java.util.HashMap<org.eclipse.swt.graphics.Image,org.eclipse.swt.graphics.Image>> imageToDecoratorMap
          Images decorator map.
private static org.slf4j.Logger log
           
private static java.util.Map<java.lang.String,org.eclipse.swt.graphics.Resource> resourceTracker
          The resource tracker.
static org.eclipse.swt.graphics.Font SYSTEM_FONT
          The system font.
static org.eclipse.swt.graphics.Font SYSTEM_FONT_BOLD
          The system font in bold style.
private static java.lang.String tempDir
          The temporary directory path.
 
Constructor Summary
SWTHelper()
           
 
Method Summary
static void collapseAll(org.eclipse.swt.widgets.Tree tree)
           
static org.eclipse.swt.graphics.Color createColor(int r, int g, int b)
          Creates a Color object and register in the resource tracker for later disposal.
static org.eclipse.swt.graphics.Font createFont(org.eclipse.swt.graphics.FontData data)
          Creates a font that is registered in the resource tracker.
static org.eclipse.swt.graphics.Font createFont(org.eclipse.swt.graphics.FontData[] data)
          Creates a font that is registered in the resource tracker.
static org.eclipse.swt.widgets.ToolItem createToolItem(org.eclipse.swt.widgets.ToolBar parent, int type, java.lang.String text, java.lang.String toolTipText, java.lang.String imageName, boolean createHotImage)
          Helper function to create a tool item.
static org.eclipse.swt.graphics.Image decorateImage(org.eclipse.swt.graphics.Image baseImage, org.eclipse.swt.graphics.Image decorator, int corner)
          Returns an image composed of a base image decorated by another image.
static org.eclipse.swt.graphics.Image decorateImage(java.lang.String baseImage, java.lang.String decorator, int corner)
          Returns an image composed of a base image decorated by another image.
static void disposeAll()
          Dispose all registered OS resources.
static void expandAll(org.eclipse.swt.widgets.Tree tree)
           
static org.eclipse.swt.widgets.Display getDisplay()
          Try to return the current display.
static org.eclipse.swt.graphics.Color[] getGradientColors(int steps, org.eclipse.swt.graphics.Color start, org.eclipse.swt.graphics.Color end)
          Computes the gradient between two colors.
static org.eclipse.jface.resource.ImageDescriptor getImageDescriptor(java.lang.String fileName)
          Returns an ImageDescriptor from the application's image directory but DOES NOT register it with the resource tracker.
static java.lang.String getImageURL(java.lang.String fileName)
          Returns the file URL of the image on the local temporary directory.
static java.io.InputStream getResourceAsStream(java.lang.String fileName)
          Loads an image from the application's image directory and register it with a resource tracker for later disposal by the disposeAll() function.
static org.eclipse.swt.graphics.Image loadGrayImage(java.lang.String fileName)
          Loads an image from the application's image directory, then computes the correszponding gray image and finally register the grayed image with a resource tracker for later disposal by the disposeAll() function.
static org.eclipse.swt.graphics.Image loadImage(java.lang.String fileName)
          Loads an image from the application's image directory and register it with a resource tracker for later disposal by the disposeAll() function.
protected static org.eclipse.swt.graphics.Image loadImage(java.lang.String fileName, boolean register)
          Loads an image from the application's image directory and register it with a resource tracker for later disposal by the disposeAll() function if the register parameter is set to true.
static org.eclipse.swt.graphics.Font makeBoldFont(org.eclipse.swt.graphics.Font sourceFont)
          Turns the specified Font bold.
private static void setNodesExpandedState(org.eclipse.swt.widgets.TreeItem current, boolean expand)
           
private static boolean writeImageToTempDirectory(java.lang.String fileName)
          Writes the resource image to the temporary directory.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

private static final org.slf4j.Logger log

DESC_IMAGES_DIRECTORY

public static final java.lang.String DESC_IMAGES_DIRECTORY
The directory in which images and icons are stored.

See Also:
Constant Field Values

IMAGES_DIRECTORY

public static final java.lang.String IMAGES_DIRECTORY
See Also:
Constant Field Values

GRAY_IMAGE_SUFFIX

public static final java.lang.String GRAY_IMAGE_SUFFIX
The suffix added to the key name of the gray version of a image.

See Also:
Constant Field Values

tempDir

private static final java.lang.String tempDir
The temporary directory path.


resourceTracker

private static java.util.Map<java.lang.String,org.eclipse.swt.graphics.Resource> resourceTracker
The resource tracker.


SYSTEM_FONT

public static final org.eclipse.swt.graphics.Font SYSTEM_FONT
The system font. Note: This font should not be freed because it's allocated by the system itself.


SYSTEM_FONT_BOLD

public static final org.eclipse.swt.graphics.Font SYSTEM_FONT_BOLD
The system font in bold style.


imageToDecoratorMap

private static java.util.Map<org.eclipse.swt.graphics.Image,java.util.HashMap<org.eclipse.swt.graphics.Image,org.eclipse.swt.graphics.Image>> imageToDecoratorMap
Images decorator map.

Constructor Detail

SWTHelper

public SWTHelper()
Method Detail

makeBoldFont

public static org.eclipse.swt.graphics.Font makeBoldFont(org.eclipse.swt.graphics.Font sourceFont)
Turns the specified Font bold.

Parameters:
sourceFont - the source Font object
Returns:
Font the Font in Bold

createFont

public static org.eclipse.swt.graphics.Font createFont(org.eclipse.swt.graphics.FontData data)
Creates a font that is registered in the resource tracker.

Parameters:
data - the FontData to create the font
Returns:
Font the new font

createFont

public static org.eclipse.swt.graphics.Font createFont(org.eclipse.swt.graphics.FontData[] data)
Creates a font that is registered in the resource tracker.

Parameters:
data - the FontData to create the font
Returns:
Font the new font

getImageDescriptor

public static org.eclipse.jface.resource.ImageDescriptor getImageDescriptor(java.lang.String fileName)
Returns an ImageDescriptor from the application's image directory but DOES NOT register it with the resource tracker.

Parameters:
fileName - the image file name
Returns:
the image object

loadImage

public static org.eclipse.swt.graphics.Image loadImage(java.lang.String fileName)
Loads an image from the application's image directory and register it with a resource tracker for later disposal by the disposeAll() function.

Parameters:
fileName - the image file name
Returns:
the image object

getResourceAsStream

public static java.io.InputStream getResourceAsStream(java.lang.String fileName)
Loads an image from the application's image directory and register it with a resource tracker for later disposal by the disposeAll() function.

Parameters:
fileName - the image file name
Returns:
the image as an InputStream

loadImage

protected static org.eclipse.swt.graphics.Image loadImage(java.lang.String fileName,
                                                          boolean register)
Loads an image from the application's image directory and register it with a resource tracker for later disposal by the disposeAll() function if the register parameter is set to true.

Parameters:
fileName - the image file name
register - if true image is registered in the resource tracker
Returns:
the image object

loadGrayImage

public static org.eclipse.swt.graphics.Image loadGrayImage(java.lang.String fileName)
Loads an image from the application's image directory, then computes the correszponding gray image and finally register the grayed image with a resource tracker for later disposal by the disposeAll() function.

Parameters:
fileName - the image file name
Returns:
the image object

disposeAll

public static void disposeAll()
Dispose all registered OS resources.


createToolItem

public static org.eclipse.swt.widgets.ToolItem createToolItem(org.eclipse.swt.widgets.ToolBar parent,
                                                              int type,
                                                              java.lang.String text,
                                                              java.lang.String toolTipText,
                                                              java.lang.String imageName,
                                                              boolean createHotImage)
Helper function to create a tool item. The behaviour of the toolitem is set to use a grayed image when not hovered and to use the normal image when mouse is over the toolitem. Image objects are registered in the resource tracker.

Returns:
ToolItem

getGradientColors

public static org.eclipse.swt.graphics.Color[] getGradientColors(int steps,
                                                                 org.eclipse.swt.graphics.Color start,
                                                                 org.eclipse.swt.graphics.Color end)
Computes the gradient between two colors. Color objects are registered in the resource tracker.

Parameters:
steps - the number of steps for the color transition in the gradient
start - the color at the start of the gradient
end - the color at the end of the gradient
Returns:
an array of the colors for the gradient

createColor

public static org.eclipse.swt.graphics.Color createColor(int r,
                                                         int g,
                                                         int b)
Creates a Color object and register in the resource tracker for later disposal.

Parameters:
r - the red composant of the color
g - the green composant of the color
b - the blue composant of the color
Returns:
the generated color object

getImageURL

public static java.lang.String getImageURL(java.lang.String fileName)
Returns the file URL of the image on the local temporary directory. If image isn't already created it creates it before.

Parameters:
fileName - the image file name
Returns:
the url if file exists or "file:///" if file couldn't be written to temporary directory

writeImageToTempDirectory

private static boolean writeImageToTempDirectory(java.lang.String fileName)
Writes the resource image to the temporary directory.

Parameters:
fileName - the image file name
Returns:
true if write succeeded

getDisplay

public static org.eclipse.swt.widgets.Display getDisplay()
Try to return the current display. If no display is tied to current thread then it returns the default Display a.k.a the first created as described in Display.getDefault().

Returns:
the current or default display

decorateImage

public static org.eclipse.swt.graphics.Image decorateImage(java.lang.String baseImage,
                                                           java.lang.String decorator,
                                                           int corner)
Returns an image composed of a base image decorated by another image.

Parameters:
baseImage - The filename of the base image that should be decorated
decorator - The filename of the image used to decorate the base image
corner - The corner to place decorator image
Returns:
Image The resulting decorated image

decorateImage

public static org.eclipse.swt.graphics.Image decorateImage(org.eclipse.swt.graphics.Image baseImage,
                                                           org.eclipse.swt.graphics.Image decorator,
                                                           int corner)
Returns an image composed of a base image decorated by another image.

Parameters:
baseImage - Image The base image that should be decorated
decorator - Image The image used to decorate the base image
corner - The corner to place decorator image
Returns:
Image The resulting decorated image

expandAll

public static void expandAll(org.eclipse.swt.widgets.Tree tree)

collapseAll

public static void collapseAll(org.eclipse.swt.widgets.Tree tree)

setNodesExpandedState

private static void setNodesExpandedState(org.eclipse.swt.widgets.TreeItem current,
                                          boolean expand)