1 /* 2 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. 3 * All rights reserved. Use is subject to license terms. 4 * 5 * This file is available and licensed under the following license: 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * - Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * - Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the distribution. 16 * - Neither the name of Oracle Corporation nor the names of its 17 * contributors may be used to endorse or promote products derived 18 * from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 package com.oracle.javafx.scenebuilder.kit.metadata.property.value; 33 34 import com.oracle.javafx.scenebuilder.kit.fxom.FXOMDocument; 35 import com.oracle.javafx.scenebuilder.kit.fxom.FXOMInstance; 36 import com.oracle.javafx.scenebuilder.kit.metadata.util.DesignImage; 37 import com.oracle.javafx.scenebuilder.kit.metadata.util.InspectorPath; 38 import com.oracle.javafx.scenebuilder.kit.metadata.util.PropertyName; 39 import java.util.Collections; 40 import java.util.HashMap; 41 import java.util.Map; 42 import javafx.scene.Cursor; 43 import javafx.scene.ImageCursor; 44 45 /** 46 * 47 * 48 */ 49 public class CursorPropertyMetadata extends ComplexPropertyMetadata<Cursor> { 50 51 private final DoublePropertyMetadata hotspotXMetadata 52 = new DoublePropertyMetadata(new PropertyName("hotspotX"), 53 DoublePropertyMetadata.DoubleKind.COORDINATE, true, 0.0, InspectorPath.UNUSED); 54 private final DoublePropertyMetadata hotspotYMetadata 55 = new DoublePropertyMetadata(new PropertyName("hotspotY"), 56 DoublePropertyMetadata.DoubleKind.COORDINATE, true, 0.0, InspectorPath.UNUSED); 57 private final ImagePropertyMetadata imageMetadata 58 = new ImagePropertyMetadata(new PropertyName("image"), 59 true, null, InspectorPath.UNUSED); 60 61 private static Map<Cursor, String> cursorMap; 62 63 public CursorPropertyMetadata(PropertyName name, boolean readWrite, 64 Cursor defaultValue, InspectorPath inspectorPath) { 65 super(name, Cursor.class, readWrite, defaultValue, inspectorPath); 66 } 67 68 public static synchronized Map<Cursor, String> getCursorMap() { 69 if (cursorMap == null) { 70 cursorMap = new HashMap<>(); 71 cursorMap.put(Cursor.CLOSED_HAND, "CLOSED_HAND" ); 72 cursorMap.put(Cursor.CROSSHAIR, "CROSSHAIR" ); 73 cursorMap.put(Cursor.DEFAULT, "DEFAULT" ); 74 cursorMap.put(Cursor.DISAPPEAR, "DISAPPEAR" ); 75 cursorMap.put(Cursor.E_RESIZE, "E_RESIZE" ); 76 cursorMap.put(Cursor.HAND, "HAND" ); 77 cursorMap.put(Cursor.H_RESIZE, "H_RESIZE" ); 78 cursorMap.put(Cursor.MOVE, "MOVE" ); 79 cursorMap.put(Cursor.NE_RESIZE, "NE_RESIZE" ); 80 cursorMap.put(Cursor.NONE, "NONE" ); 81 cursorMap.put(Cursor.NW_RESIZE, "NW_RESIZE" ); 82 cursorMap.put(Cursor.N_RESIZE, "N_RESIZE" ); 83 cursorMap.put(Cursor.OPEN_HAND, "OPEN_HAND" ); 84 cursorMap.put(Cursor.SE_RESIZE, "SE_RESIZE" ); 85 cursorMap.put(Cursor.SW_RESIZE, "SW_RESIZE" ); 86 cursorMap.put(Cursor.S_RESIZE, "S_RESIZE" ); 87 cursorMap.put(Cursor.TEXT, "TEXT" ); 88 cursorMap.put(Cursor.V_RESIZE, "V_RESIZE" ); 89 cursorMap.put(Cursor.WAIT, "WAIT" ); 90 cursorMap.put(Cursor.W_RESIZE, "W_RESIZE" ); 91 cursorMap = Collections.unmodifiableMap(cursorMap); 92 } 93 94 return cursorMap; 95 } 96 97 98 /* 99 * ComplexPropertyMetadata 100 */ 101 @Override 102 public FXOMInstance makeFxomInstanceFromValue(Cursor value, FXOMDocument fxomDocument) { 103 final FXOMInstance result; 104 105 final String cursorName = getCursorMap().get(value); 106 if (cursorName != null) { 107 // It's a standard cursor 108 result = new FXOMInstance(fxomDocument, Cursor.class); 109 result.setFxConstant(cursorName); 110 } else if (value instanceof ImageCursor) { 111 final ImageCursor imageCursor = (ImageCursor) value; 112 result = new FXOMInstance(fxomDocument, ImageCursor.class); 113 hotspotXMetadata.setValue(result, imageCursor.getHotspotX()); 114 hotspotYMetadata.setValue(result, imageCursor.getHotspotY()); 115 imageMetadata.setValue(result, new DesignImage(imageCursor.getImage())); 116 } else { 117 // Emergency code 118 assert false; 119 result = new FXOMInstance(fxomDocument, Cursor.class); 120 result.setFxConstant(getCursorMap().get(Cursor.DEFAULT)); 121 } 122 123 return result; 124 } 125 } 126