1 /*
   2  * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.javafx.webkit;
  27 
  28 import com.sun.webkit.CursorManager;
  29 import com.sun.webkit.graphics.WCGraphicsManager;
  30 import com.sun.webkit.graphics.WCImage;
  31 import java.util.HashMap;
  32 import java.util.Locale;
  33 import java.util.Map;
  34 import java.util.MissingResourceException;
  35 import java.util.ResourceBundle;
  36 import javafx.scene.Cursor;
  37 import javafx.scene.ImageCursor;
  38 import javafx.scene.image.Image;
  39 
  40 public final class CursorManagerImpl extends CursorManager<Cursor> {
  41 
  42     private final Map<String, Cursor> map = new HashMap<String, Cursor>();
  43     private ResourceBundle bundle;
  44 
  45     @Override protected Cursor getCustomCursor(WCImage image, int hotspotX, int hotspotY) {
  46         return new ImageCursor(
  47                 Image.impl_fromPlatformImage(
  48                     WCGraphicsManager.getGraphicsManager().toPlatformImage(image)),
  49                 hotspotX, hotspotY);
  50     }
  51 
  52     @Override protected Cursor getPredefinedCursor(int type) {
  53         switch (type) {
  54             default:
  55             case POINTER:                      return                                   Cursor.DEFAULT;
  56             case CROSS:                        return                                   Cursor.CROSSHAIR;
  57             case HAND:                         return                                   Cursor.HAND;
  58             case MOVE:                         return                                   Cursor.MOVE;
  59             case TEXT:                         return                                   Cursor.TEXT;
  60             case WAIT:                         return                                   Cursor.WAIT;
  61             case HELP:                         return getCustomCursor("help",           Cursor.DEFAULT);
  62             case EAST_RESIZE:                  return                                   Cursor.E_RESIZE;
  63             case NORTH_RESIZE:                 return                                   Cursor.N_RESIZE;
  64             case NORTH_EAST_RESIZE:            return                                   Cursor.NE_RESIZE;
  65             case NORTH_WEST_RESIZE:            return                                   Cursor.NW_RESIZE;
  66             case SOUTH_RESIZE:                 return                                   Cursor.S_RESIZE;
  67             case SOUTH_EAST_RESIZE:            return                                   Cursor.SE_RESIZE;
  68             case SOUTH_WEST_RESIZE:            return                                   Cursor.SW_RESIZE;
  69             case WEST_RESIZE:                  return                                   Cursor.W_RESIZE;
  70             case NORTH_SOUTH_RESIZE:           return                                   Cursor.V_RESIZE;
  71             case EAST_WEST_RESIZE:             return                                   Cursor.H_RESIZE;
  72             case NORTH_EAST_SOUTH_WEST_RESIZE: return getCustomCursor("resize.nesw",    Cursor.DEFAULT);
  73             case NORTH_WEST_SOUTH_EAST_RESIZE: return getCustomCursor("resize.nwse",    Cursor.DEFAULT);
  74             case COLUMN_RESIZE:                return getCustomCursor("resize.column",  Cursor.H_RESIZE);
  75             case ROW_RESIZE:                   return getCustomCursor("resize.row",     Cursor.V_RESIZE);
  76             case MIDDLE_PANNING:               return getCustomCursor("panning.middle", Cursor.DEFAULT);
  77             case EAST_PANNING:                 return getCustomCursor("panning.east",   Cursor.DEFAULT);
  78             case NORTH_PANNING:                return getCustomCursor("panning.north",  Cursor.DEFAULT);
  79             case NORTH_EAST_PANNING:           return getCustomCursor("panning.ne",     Cursor.DEFAULT);
  80             case NORTH_WEST_PANNING:           return getCustomCursor("panning.nw",     Cursor.DEFAULT);
  81             case SOUTH_PANNING:                return getCustomCursor("panning.south",  Cursor.DEFAULT);
  82             case SOUTH_EAST_PANNING:           return getCustomCursor("panning.se",     Cursor.DEFAULT);
  83             case SOUTH_WEST_PANNING:           return getCustomCursor("panning.sw",     Cursor.DEFAULT);
  84             case WEST_PANNING:                 return getCustomCursor("panning.west",   Cursor.DEFAULT);
  85             case VERTICAL_TEXT:                return getCustomCursor("vertical.text",  Cursor.DEFAULT);
  86             case CELL:                         return getCustomCursor("cell",           Cursor.DEFAULT);
  87             case CONTEXT_MENU:                 return getCustomCursor("context.menu",   Cursor.DEFAULT);
  88             case NO_DROP:                      return getCustomCursor("no.drop",        Cursor.DEFAULT);
  89             case NOT_ALLOWED:                  return getCustomCursor("not.allowed",    Cursor.DEFAULT);
  90             case PROGRESS:                     return getCustomCursor("progress",       Cursor.WAIT);
  91             case ALIAS:                        return getCustomCursor("alias",          Cursor.DEFAULT);
  92             case ZOOM_IN:                      return getCustomCursor("zoom.in",        Cursor.DEFAULT);
  93             case ZOOM_OUT:                     return getCustomCursor("zoom.out",       Cursor.DEFAULT);
  94             case COPY:                         return getCustomCursor("copy",           Cursor.DEFAULT);
  95             case NONE:                         return                                   Cursor.NONE;
  96             case GRAB:                         return getCustomCursor("grab",           Cursor.OPEN_HAND);
  97             case GRABBING:                     return getCustomCursor("grabbing",       Cursor.CLOSED_HAND);
  98         }
  99     }
 100 
 101     private Cursor getCustomCursor(String name, Cursor predefined) {
 102         Cursor cursor = this.map.get(name);
 103         if (cursor == null) {
 104             try {
 105                 if (bundle == null) {
 106                     bundle = ResourceBundle.getBundle("com.sun.javafx.webkit.Cursors", Locale.getDefault());
 107                 }
 108                 if (bundle != null) {
 109                     String resource = bundle.getString(name + ".file");
 110                     Image image = new Image(CursorManagerImpl.class.getResourceAsStream(resource));
 111 
 112                     resource = bundle.getString(name + ".hotspotX");
 113                     int hotspotX = Integer.parseInt(resource);
 114 
 115                     resource = bundle.getString(name + ".hotspotY");
 116                     int hotspotY = Integer.parseInt(resource);
 117 
 118                     cursor = new ImageCursor(image, hotspotX, hotspotY);
 119                 }
 120             } catch (MissingResourceException e) {
 121                 // ignore, treat cursor as missing, use predefined instead
 122             }
 123             if (cursor == null) {
 124                 cursor = predefined;
 125             }
 126             this.map.put(name, cursor);
 127         }
 128         return cursor;
 129     }
 130 }