Package org.vietspider.generic

Source Code of org.vietspider.generic.CursorCache

/*******************************************************************************
* Copyright (c) Emil Crumhorn - Hexapixel.com - emil.crumhorn@gmail.com
* 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:
*    emil.crumhorn@gmail.com - initial API and implementation
*******************************************************************************/

package org.vietspider.generic;

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

import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.widgets.Display;

public class CursorCache {

  private static HashMap<Integer, Cursor> map = new HashMap<Integer, Cursor>();

  /**
   * Returns a cursor that is also cached as to not create more handles for each time the cursor type is fetched.
   *
   * @param type Cursor Type to fetch
   * @return Cursor
   */
  public static Cursor getCursor(int type) {
    if (map.get(new Integer(type)) != null) {
      return map.get(new Integer(type));
    }
    Cursor c = new Cursor(Display.getDefault(), type);
    map.put(new Integer(type), c);
    return c;

  }

  /**
   * Disposes all cursors held in the cache.
   * <p>
   * <b>IMPORTANT: ONLY CALL WHEN YOU WANT TO DISPOSE ALL CACHED CURSORS!</b>
   *
   */
  public static void dispose() {
    if (map != null && map.keySet() != null) {
      Iterator<Integer> keys = map.keySet().iterator();
      while (keys.hasNext()) {
        Object key = keys.next();
        (map.get(key)).dispose();
      }
    }
    map = null;
  }
}
TOP

Related Classes of org.vietspider.generic.CursorCache

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.