/*
 * @(#)GOperation.java
 *
 * Copyright 2001 by Intelligent Technology Inc. All rights reserved.
 */

package jp.co.iti.fagot.gob;

import java.awt.Cursor;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;

import java.net.URL;

import java.util.Hashtable; 

import javax.swing.ImageIcon;

import jp.co.iti.fagot.util.ZResource;

import jp.co.iti.fagot.IZApp;
import jp.co.iti.fagot.ZApp;
/**
 * 操作の定義
 *
 * 移動、拡大・縮小など操作を定義
 * また、操作によるマウス形状の定義
 *
 * @author  福島康治
 * @version 1.01 2001/01/07
 */
public class GOperation {

	/**
	 * カーソル形状の取得
	 */
	static public Cursor getCursor( byte nMode ) {
	int nType =  Cursor.DEFAULT_CURSOR;
	boolean bCustom = false;
	String  szKey   = null;
	switch( nMode ) {
	  case MOVE:
		nType = Cursor.MOVE_CURSOR;
		break;
	  case ZOOMNE:
		nType = Cursor.NE_RESIZE_CURSOR;
		break;
	  case ZOOMNW:
		nType = Cursor.NW_RESIZE_CURSOR;
		break;
	  case ZOOMSE:
		nType = Cursor.SE_RESIZE_CURSOR;
		break;
	  case ZOOMSW:
		nType = Cursor.SW_RESIZE_CURSOR;
		break;
	  case ZOOMN:
		nType = Cursor.N_RESIZE_CURSOR;
		break;
	  case ZOOMS:
		nType = Cursor.S_RESIZE_CURSOR;
		break;
	  case ZOOME:
		nType = Cursor.E_RESIZE_CURSOR;
		break;
	  case ZOOMW:
		nType = Cursor.W_RESIZE_CURSOR;
		break;
	  case SELECTION:
	  case NEW:
	  case POINT:
		nType = Cursor.CROSSHAIR_CURSOR;
		break;
	  case HAND:
		bCustom = true;
		szKey   = "hand";
		break;
	  case HANDON:
		bCustom = true;
		szKey   = "handon";
		break;
	  case ZOOMIN:
		bCustom = true;
		szKey   = "zoomin";
		break;
	  case ZOOMOUT:
		bCustom = true;
		szKey   = "zoomout";
		break;
	  case ZOOMON:
		nType = Cursor.CROSSHAIR_CURSOR;
		break;
	  case PROP:
		bCustom = true;
		szKey   = "prop";
		break;
	}

	Cursor cur = null;
	if ( bCustom ) {
		cur = (Cursor)mCursors.get(szKey);
		if ( cur == null && getResource() != null ) {
			try {
				URL url = getResource().getURL("cursor."+szKey);
				Toolkit kit = Toolkit.getDefaultToolkit();
				Image img = kit.getImage(url);
//				javax.swing.ImageIcon icon = new javax.swing.ImageIcon(img);
//				System.out.println("Cursor Width="+icon.getIconWidth()+
//										" Height="+icon.getIconHeight());
				cur = kit.createCustomCursor(img,new Point(10,10),szKey);
				mCursors.put(szKey, cur);
			} catch ( IndexOutOfBoundsException iob ) {
				cur = null;
			}
		}
		// リソースがない。またはカーソル作成に失敗
		if ( cur == null ) {
			cur = new Cursor(Cursor.CROSSHAIR_CURSOR);
		}
/*
		カスタムカーソルの描画
		cur = public Cursor createCustomCursor(Image cursor,
                                 Point hotSpot,
                                 String name)
                          throws IndexOutOfBoundsException
*/
	} else {
		cur = new Cursor( nType );
	}
	return cur;
	}

	/**
	 * リソースを取得する内部関数
	 */
	static protected ZResource getResource() {
	ZResource res = null;
	IZApp app = ZApp.getTheApp();
	if ( app != null ) {
		res = app.getResource();
	}
	return res;
	}

	//## アトリビュート################################################
	/**
	 * 状態：なし
	 */
	static public final byte NONE		= 0 ;

	/**
	 * 状態：移動
	 */
	static public final byte MOVE		= 1 ;

	/**
	 * 状態：拡大の基準インデックス
	 */
	static public final byte ZOOMBASE	= 2 ;

	/**
	 * 状態：拡大：北東
	 */
	static public final byte ZOOMNE		= ZOOMBASE ;

	/**
	 * 状態：拡大：北西
	 */
	static public final byte ZOOMNW		= ZOOMBASE+1 ;

	/**
	 * 状態：拡大：南東
	 */
	static public final byte ZOOMSE		= ZOOMBASE+2 ;

	/**
	 * 状態：拡大：南西
	 */
	static public final byte ZOOMSW		= ZOOMBASE+3 ;

	/**
	 * 状態：拡大：北
	 */
	static public final byte ZOOMN		= ZOOMBASE+4 ;

	/**
	 * 状態：拡大：南
	 */
	static public final byte ZOOMS		= ZOOMBASE+5 ;

	/**
	 * 状態：拡大：東
	 */
	static public final byte ZOOME		= ZOOMBASE+6 ;

	/**
	 * 状態：拡大：西
	 */
	static public final byte ZOOMW		= ZOOMBASE+7 ;

	/**
	 * 状態：拡大の最大インデックス
	 */
	static public final byte ZOOMMAX	= ZOOMBASE+7 ;

	/**
	 * 状態：範囲指定
	 */
	static public final byte SELECTION	= 10;

	/**
	 * 状態：新規作成
	 */
	static public final byte NEW		= 11;

	/**
	 * 状態：ポイント移動
	 */
	static public final byte POINT		= 12;

	/**
	 * 状態：手づかみ移動前
	 */
	static public final byte HAND		= 13;

	/**
	 * 状態：手づかみ移動中
	 */
	static public final byte HANDON		= 14;

	/**
	 * 状態：拡大前
	 */
	static public final byte ZOOMIN		= 15;

	/**
	 * 状態：縮小前
	 */
	static public final byte ZOOMOUT	= 16;

	/**
	 * 状態：拡大中
	 */
	static public final byte ZOOMON		= 17;

	/**
	 * 状態：プロパティ
	 */
	static public final byte PROP		= 18;

	/**
	 * カーソルマップ
	 */
	static protected Hashtable	mCursors	 = new Hashtable();
}
