1 /* 2 * Copyright (c) 2011, 2016, 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.javafx.tk.Toolkit; 29 import com.sun.webkit.CursorManager; 30 import com.sun.webkit.graphics.WCGraphicsManager; 31 import com.sun.webkit.graphics.WCImage; 32 import java.util.HashMap; 33 import java.util.Locale; 34 import java.util.Map; 35 import java.util.MissingResourceException; 36 import java.util.ResourceBundle; 37 import javafx.scene.Cursor; 38 import javafx.scene.ImageCursor; 39 import javafx.scene.image.Image; 40 41 public final class CursorManagerImpl extends CursorManager<Cursor> { 42 43 private final Map<String, Cursor> map = new HashMap<String, Cursor>(); 44 private ResourceBundle bundle; 45 46 @Override protected Cursor getCustomCursor(WCImage image, int hotspotX, int hotspotY) { 47 return new ImageCursor( 48 Toolkit.getImageAccessor().fromPlatformImage( 49 WCGraphicsManager.getGraphicsManager().toPlatformImage(image)), 50 hotspotX, hotspotY); 51 } 52 53 @Override protected Cursor getPredefinedCursor(int type) { 54 switch (type) { 55 default: 56 case POINTER: return Cursor.DEFAULT; 57 case CROSS: return Cursor.CROSSHAIR; 58 case HAND: return Cursor.HAND; 59 case MOVE: return Cursor.MOVE; 60 case TEXT: return Cursor.TEXT; 61 case WAIT: return Cursor.WAIT; 62 case HELP: return getCustomCursor("help", Cursor.DEFAULT); 63 case EAST_RESIZE: return Cursor.E_RESIZE; 64 case NORTH_RESIZE: return Cursor.N_RESIZE; 65 case NORTH_EAST_RESIZE: return Cursor.NE_RESIZE; 66 case NORTH_WEST_RESIZE: return Cursor.NW_RESIZE; 67 case SOUTH_RESIZE: return Cursor.S_RESIZE; 68 case SOUTH_EAST_RESIZE: return Cursor.SE_RESIZE; 69 case SOUTH_WEST_RESIZE: return Cursor.SW_RESIZE; 70 case WEST_RESIZE: return Cursor.W_RESIZE; 71 case NORTH_SOUTH_RESIZE: return Cursor.V_RESIZE; 72 case EAST_WEST_RESIZE: return Cursor.H_RESIZE; 73 case NORTH_EAST_SOUTH_WEST_RESIZE: return getCustomCursor("resize.nesw", Cursor.DEFAULT); 74 case NORTH_WEST_SOUTH_EAST_RESIZE: return getCustomCursor("resize.nwse", Cursor.DEFAULT); 75 case COLUMN_RESIZE: return getCustomCursor("resize.column", Cursor.H_RESIZE); 76 case ROW_RESIZE: return getCustomCursor("resize.row", Cursor.V_RESIZE); 77 case MIDDLE_PANNING: return getCustomCursor("panning.middle", Cursor.DEFAULT); 78 case EAST_PANNING: return getCustomCursor("panning.east", Cursor.DEFAULT); 79 case NORTH_PANNING: return getCustomCursor("panning.north", Cursor.DEFAULT); 80 case NORTH_EAST_PANNING: return getCustomCursor("panning.ne", Cursor.DEFAULT); 81 case NORTH_WEST_PANNING: return getCustomCursor("panning.nw", Cursor.DEFAULT); 82 case SOUTH_PANNING: return getCustomCursor("panning.south", Cursor.DEFAULT); 83 case SOUTH_EAST_PANNING: return getCustomCursor("panning.se", Cursor.DEFAULT); 84 case SOUTH_WEST_PANNING: return getCustomCursor("panning.sw", Cursor.DEFAULT); 85 case WEST_PANNING: return getCustomCursor("panning.west", Cursor.DEFAULT); 86 case VERTICAL_TEXT: return getCustomCursor("vertical.text", Cursor.DEFAULT); 87 case CELL: return getCustomCursor("cell", Cursor.DEFAULT); 88 case CONTEXT_MENU: return getCustomCursor("context.menu", Cursor.DEFAULT); 89 case NO_DROP: return getCustomCursor("no.drop", Cursor.DEFAULT); 90 case NOT_ALLOWED: return getCustomCursor("not.allowed", Cursor.DEFAULT); 91 case PROGRESS: return getCustomCursor("progress", Cursor.WAIT); 92 case ALIAS: return getCustomCursor("alias", Cursor.DEFAULT); 93 case ZOOM_IN: return getCustomCursor("zoom.in", Cursor.DEFAULT); 94 case ZOOM_OUT: return getCustomCursor("zoom.out", Cursor.DEFAULT); 95 case COPY: return getCustomCursor("copy", Cursor.DEFAULT); 96 case NONE: return Cursor.NONE; 97 case GRAB: return getCustomCursor("grab", Cursor.OPEN_HAND); 98 case GRABBING: return getCustomCursor("grabbing", Cursor.CLOSED_HAND); 99 } 100 } 101 102 private Cursor getCustomCursor(String name, Cursor predefined) { 103 Cursor cursor = this.map.get(name); 104 if (cursor == null) { 105 try { 106 if (bundle == null) { 107 bundle = ResourceBundle.getBundle("com.sun.javafx.webkit.Cursors", Locale.getDefault()); 108 } 109 if (bundle != null) { 110 String resource = bundle.getString(name + ".file"); 111 Image image = new Image(CursorManagerImpl.class.getResourceAsStream(resource)); 112 113 resource = bundle.getString(name + ".hotspotX"); 114 int hotspotX = Integer.parseInt(resource); 115 116 resource = bundle.getString(name + ".hotspotY"); 117 int hotspotY = Integer.parseInt(resource); 118 119 cursor = new ImageCursor(image, hotspotX, hotspotY); 120 } 121 } catch (MissingResourceException e) { 122 // ignore, treat cursor as missing, use predefined instead 123 } 124 if (cursor == null) { 125 cursor = predefined; 126 } 127 this.map.put(name, cursor); 128 } 129 return cursor; 130 } 131 }