Package com.positive.charts.util

Source Code of com.positive.charts.util.DrawingAssets

package com.positive.charts.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Resource;

/**
* Holds the string-keyed map of drawing assets (colors, images, patterns, etc)
* and disposes them when no longer needed.
*/
public class DrawingAssets {

  /**
   * Checks the key to be non-<code>null</code> and throws an
   * {@link IllegalArgumentException} if it is <code>null</code>.
   *
   * @param key
   */
  private static void checkKey(final String key) {
    if (key == null) {
      throw new IllegalArgumentException(
          "DrawingAssets: key cannot be null");
    }
  }

  /** A map of colors. */
  private final Map colors;

  /** A map of strokes. Doesn't need to be disposed. */
  private final Map strokes;

  /** A map of cursors. */
  private final Map cursors;

  /** A map of fonts. */
  private final Map fonts;

  /** A map of images. */
  private final Map images;

  /** A map of paths. */
  private final Map paths;

  /** A map of patterns. */
  private final Map patterns;

  /** A map of regions. */
  private final Map regions;

  /** A map of text layouts. */
  private final Map textLayouts;

  /** A map of transforms. */
  private final Map transforms;

  /**
   * Constructs a new instance of this class bound to a given device. All new
   * assets instantiated with the use of this class will be created on this
   * device.
   *
   * @param device
   *            device to use for creating drawing assets
   */
  public DrawingAssets() {
    this.colors = new HashMap();
    // colors.put(key, value)
    this.strokes = new HashMap();
    this.cursors = new HashMap();
    this.fonts = new HashMap();
    this.images = new HashMap();
    this.paths = new HashMap();
    this.patterns = new HashMap();
    this.regions = new HashMap();
    this.textLayouts = new HashMap();
    this.transforms = new HashMap();
  }

  /**
   * Disposes all allocated drawing resources. The resources will no longer be
   * available after this operation.
   */
  public void dispose() {
    this.dispose(this.colors);
    this.dispose(this.cursors);
    this.dispose(this.fonts);
    this.dispose(this.images);
    this.dispose(this.paths);
    this.dispose(this.patterns);
    this.dispose(this.regions);
    this.dispose(this.textLayouts);
    this.dispose(this.transforms);
  }

  /**
   * Disposes all resources in a map and clears it.
   *
   * @param resources
   *            map of resources to dispose
   */
  private void dispose(final Map resources) {
    for (final Iterator iter = resources.values().iterator(); iter
        .hasNext();) {
      final Resource resource = (Resource) iter.next();
      if (resource != null) {
        resource.dispose();
      }
    }
    resources.clear();
  }

  /**
   * Returns the color which maps to a given key, or <code>null</code> if no
   * color is associated with this key.
   *
   * @param key
   *            a name of the color
   * @return the value to which this map maps the specified key, or
   *         <code>null</code> if the map contains no mapping for this key.
   */
  public Color getColor(final String key) {
    return (Color) this.colors.get(key);
  }

  public Stroke getStroke(final String key) {
    return (Stroke) this.strokes.get(key);
  }

  /**
   * Registers a color with a given key. If a color with such key is already
   * registered, it is disposed. If the <code>color</code> is
   * <code>null</code>, then the color is unregistered.
   *
   * @param key
   *            name of a color
   * @param color
   *            color to register, or null to clear the old value.
   */
  public void setColor(final String key, final Color color) {
    checkKey(key);
    if (this.colors.containsKey(key)) {
      ((Color) this.colors.get(key)).dispose();
    }
    if (color != null) {
      this.colors.put(key, color);
    } else {
      this.colors.remove(key);
    }
  }

  public void setStroke(final String key, final Stroke stroke) {
    checkKey(key);
    if (stroke != null) {
      this.strokes.put(key, stroke);
    } else {
      this.strokes.remove(key);
    }
  }

}
TOP

Related Classes of com.positive.charts.util.DrawingAssets

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.