Package hidb2.gui.ressources

Source Code of hidb2.gui.ressources.RscMan

/**
* This file is part of HIDB2.
*
* HIDB2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HIDB2 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 Lesser Public License for more details.
*
* You should have received a copy of the GNU Lesser Public License
* along with HIDB2.  If not, see <http://www.gnu.org/licenses/>.
*/
package hidb2.gui.ressources;

import hidb2.Activator;

import java.util.HashMap;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;

/**
* Ressource Management class.
*
*/
public class RscMan
  {
  //-----------------------------------------------------------------------------
  // Icon file names
  //-----------------------------------------------------------------------------
  public final static String IN_TABLE = "Table-16x16.png";

  public final static String IN_BRUSH = "Brush-16x16.png";

  public final static String IN_FOLDER = "Folder-16x16.png";

  public final static String IN_TARGET = "Target-16x16.png";

  public final static String IN_EQUIPEMENT = "Equipment-16x16.png";

  public final static String IN_EQUIPEMENTS = "Des-Equipment-16x16.png";

  public final static String IN_SCENARIO = "Des-Scenario-16x16.png";

  public final static String IN_BOSS = "Boss-16x16.png";

  public final static String IN_GREENBOOKMARK = "Green bookmark-16x16.png";

  public final static String IN_REDBOOKMARK = "Red bookmark-16x16.png";

  public final static String IN_FILM = "Film-16x16.png";

  public final static String IN_LIST = "List-16x16.png";

  public final static String IN_IMAGE = "App-66-16x16.png";

  public final static String IN_ADD_GREEN = "App-60-16x16.png";

  public final static String IN_ADD = "Des-Add-16x16.png";

  public final static String IN_OPEN_STD = "Des-Open-16x16.png";
 
  public final static String IN_SEARCH = "Search-16x16.png";
 
  public final static String IN_NOFILE = "Alert-NoFile-512x512.png";
 
  //-----------------------------------------------------------------------------
  private static HashMap<String, Object> resources = new HashMap<String, Object>();

  private static RscMan instance = new RscMan();

  /**
   * construit la fonte demand�e
   * @param name
   * @param size
   * @param style
   * @return
   */
  public static Font getFont(String name, int size, int style)
    {
    return getFont(name, size, style, false, false);
    }

  /**
   * Construit la fonte demande
   * @param name
   * @param size
   * @param style
   * @param strikeout
   * @param underline
   * @return
   */
  public static Font getFont(String name, int size, int style, boolean strikeout, boolean underline)
    {
    String fontName = name + "|" + size + "|" + style + "|" + strikeout + "|" + underline;
    if (resources.containsKey(fontName))
      return (Font) resources.get(fontName);
    FontData fd = new FontData(name, size, style);
    if (strikeout || underline)
      {
      try
        {
        Class<?> lfCls = Class.forName("org.eclipse.swt.internal.win32.LOGFONT");
        Object lf = FontData.class.getField("data").get(fd);
        if (lf != null && lfCls != null)
          {
          if (strikeout)
            lfCls.getField("lfStrikeOut").set(lf, new Byte((byte) 1));
          if (underline)
            lfCls.getField("lfUnderline").set(lf, new Byte((byte) 1));
          }
        }
      catch (Throwable e)
        {
        System.err.println("Unable to set underline or strikeout" + " (probably on a non-Windows platform). " + e);
        }
      }
    Font font = new Font(Display.getDefault(), fd);
    resources.put(fontName, font);
    return font;
    }

  /**
   * Retourne l'image demande
   * @param url
   * @param widget
   * @return
   */
  public static Image getImage(String url, Control widget)
    {
    Image img = getImage(url);
    if (img != null && widget != null)
      img.setBackground(widget.getBackground());
    return img;
    }

  /**
   *
   * @param url
   * @return l'image voulu
   */
  public static Image getImage(String url)
    {
    try
      {
      if ((url == null) || (url.equals("")))
        {
        return PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_TOOL_NEW_WIZARD);
        }

      if (url.endsWith(".png") || url.endsWith(".gif") || url.endsWith(".jpg") || url.endsWith(".bmp"))
        {
        url = instance.getClass().getResource(url).toString();

        if (resources.containsKey(url))
          return (Image) resources.get(url);

        ImageDescriptor desc = Activator.getImageDescriptor(url);
        Image img = desc.createImage();
        if (img != null)
          resources.put(url, img);
        return img;
        }
      else
        {
        return PlatformUI.getWorkbench().getSharedImages().getImage(url);
        }
      }

    catch (Exception e)
      {
      System.err.println("ResourceManager.getImage: Error getting image " + url + ", " + e);
      return null;
      }
    }

  /**
   *
   * @param url
   * @return The descriptor of the desired image OR Shared Image
   */
  public static ImageDescriptor getImageDescriptor(String surl)
    {
    String url;
    try
      {
      ImageDescriptor desc;

      if (surl.endsWith(".png") || surl.endsWith(".gif") || surl.endsWith(".jpg") || surl.endsWith(".bmp"))
        {
        url = instance.getClass().getResource(surl).toString();
        String cle = "ImageDescriptor" + url;

        if (resources.containsKey(cle))
          return (ImageDescriptor) resources.get(cle);

        desc = Activator.getImageDescriptor(url);

        if (desc != null)
          resources.put(cle, desc);
        }
      else
        {
        desc = PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(surl);
        }

      return desc;
      }

    catch (Exception e)
      {
      System.err.println("ResourceManager.getImageDescriptor: Error getting image " + surl + ", " + e);
      return null;
      }
    }

  /**
   *
   * @param red
   * @param green
   * @param blue
   * @return la couleur voulue
   */
  public static Color getColor(int red, int green, int blue)
    {
    String name = "COLOR:" + red + "," + green + "," + blue;
    if (resources.containsKey(name))
      return (Color) resources.get(name);
    Color color = new Color(Display.getDefault(), red, green, blue);
    resources.put(name, color);
    return color;
    }

  /**
   *
   * @param rgb : la description de la couleur
   * @return la couleur voulue
   */
  public static Color getColor(RGB rgb)
    {
    String name = "COLOR:" + rgb.red + "," + rgb.green + "," + rgb.blue;
    if (resources.containsKey(name))
      return (Color) resources.get(name);
    Color color = new Color(Display.getDefault(), rgb);
    resources.put(name, color);
    return color;
    }

  /**
   *
   * @param type
   * @return le curseur souhait�
   */
  public static Cursor getCursor(int type)
    {
    String name = "CURSOR:" + type;
    if (resources.containsKey(name))
      return (Cursor) resources.get(name);
    Cursor cursor = new Cursor(Display.getDefault(), type);
    resources.put(name, cursor);
    return cursor;
    }

  }
TOP

Related Classes of hidb2.gui.ressources.RscMan

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.