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.editor.panel.inspector.editors; 33 34 import com.oracle.javafx.scenebuilder.kit.editor.i18n.I18N; 35 import com.oracle.javafx.scenebuilder.kit.metadata.property.ValuePropertyMetadata; 36 import com.oracle.javafx.scenebuilder.kit.metadata.property.value.CursorPropertyMetadata; 37 38 import java.util.Map; 39 import java.util.Set; 40 41 import javafx.event.ActionEvent; 42 import javafx.fxml.FXML; 43 import javafx.scene.Cursor; 44 import javafx.scene.ImageCursor; 45 import javafx.scene.Node; 46 import javafx.scene.Parent; 47 import javafx.scene.control.CheckMenuItem; 48 import javafx.scene.control.Label; 49 import javafx.scene.control.MenuButton; 50 import javafx.scene.control.MenuItem; 51 52 /** 53 * Insets editor (for top/right/bottom/left fields). 54 * 55 * 56 */ 57 public class CursorEditor extends PropertyEditor { 58 59 private Parent root; 60 61 @FXML 62 private MenuButton cursorMb; 63 @FXML 64 private CheckMenuItem inheritedMi; 65 @FXML 66 private Label inheritedLb; 67 68 private Cursor cursor = Cursor.DEFAULT; 69 private String inheritedText, inheritedParentText; 70 71 public CursorEditor(ValuePropertyMetadata propMeta, Set<Class<?>> selectedClasses) { 72 super(propMeta, selectedClasses); 73 initialize(); 74 } 75 76 // Separate method to please FindBugs 77 private void initialize() { 78 root = EditorUtils.loadFxml("CursorEditor.fxml", this); //NOI18N 79 int index = 0; 80 Map<Cursor, String> predefinedCursors = CursorPropertyMetadata.getCursorMap(); 81 // Order the cursors 82 Cursor[] cursorList = {Cursor.DEFAULT, Cursor.CLOSED_HAND, Cursor.OPEN_HAND, Cursor.HAND, Cursor.MOVE, Cursor.WAIT, 83 Cursor.TEXT, Cursor.V_RESIZE, Cursor.H_RESIZE, Cursor.N_RESIZE, Cursor.NE_RESIZE, Cursor.E_RESIZE, Cursor.SE_RESIZE, 84 Cursor.S_RESIZE, Cursor.SW_RESIZE, Cursor.W_RESIZE, Cursor.NW_RESIZE, 85 Cursor.CROSSHAIR, Cursor.NONE, Cursor.DISAPPEAR}; 86 for (Cursor cursorObj : cursorList) { 87 String cursorStr = predefinedCursors.get(cursorObj); 88 final Label cursorLabel = new Label(cursorStr); 89 cursorLabel.setCursor(cursorObj); 90 CheckMenuItem menuItem = new CheckMenuItem(); 91 menuItem.setGraphic(cursorLabel); 92 // add predefined cursors before "inherited" menu item 93 cursorMb.getItems().add(index++, menuItem); 94 menuItem.setOnAction(e -> { 95 selectCursor(cursorStr); 96 userUpdateValueProperty(getValue()); 97 }); 98 } 99 100 // "inherited" menu item 101 inheritedText = I18N.getString("inspector.cursor.inherited"); 102 inheritedParentText = I18N.getString("inspector.cursor.inheritedparent"); 103 inheritedLb.setText(inheritedParentText); 104 } 105 106 @Override 107 public Node getValueEditor() { 108 return super.handleGenericModes(root); 109 } 110 111 @Override 112 public Object getValue() { 113 return cursor; 114 } 115 116 @Override 117 public void setValue(Object value) { 118 setValueGeneric(value); 119 if (isSetValueDone()) { 120 return; 121 } 122 123 if (value == null) { 124 cursor = null; 125 selectCursor(inheritedParentText); 126 127 } else { 128 assert value instanceof Cursor; 129 if (value instanceof ImageCursor) { 130 // Custom cursor 131 selectCursor(""); //NOI18N 132 cursorMb.setText(I18N.getString("inspector.cursor.custom")); 133 } else { 134 // predefined cursor 135 // select the corresponding menu item 136 selectCursor(((Cursor) value).toString()); 137 } 138 } 139 } 140 141 @Override 142 public void reset(ValuePropertyMetadata propMeta, Set<Class<?>> selectedClasses) { 143 super.reset(propMeta, selectedClasses); 144 } 145 146 @Override 147 protected void valueIsIndeterminate() { 148 handleIndeterminate(cursorMb); 149 } 150 151 // 152 // FXML methods 153 // 154 @FXML 155 void inherited(ActionEvent event) { 156 cursor = null; 157 selectCursor(inheritedParentText); 158 userUpdateValueProperty(getValue()); 159 } 160 161 // Select the menu item corresponding to a cursor string. 162 private void selectCursor(String cursorStr) { 163 for (MenuItem menuItem : cursorMb.getItems()) { 164 if (!(menuItem instanceof CheckMenuItem)) { 165 // inherited action 166 continue; 167 } 168 CheckMenuItem checkMenuItem = (CheckMenuItem) menuItem; 169 assert checkMenuItem.getGraphic() instanceof Label; 170 if (cursorStr.equals(((Label) checkMenuItem.getGraphic()).getText())) { 171 checkMenuItem.setSelected(true); 172 // set the menu button text 173 if (cursorStr.equals(inheritedParentText)) { 174 // change the menu button text to be shorter in this case... 175 cursorMb.setText(inheritedText); 176 } else { 177 cursorMb.setText(cursorStr); 178 } 179 cursor = checkMenuItem.getGraphic().getCursor(); 180 } else { 181 checkMenuItem.setSelected(false); 182 } 183 } 184 } 185 186 @Override 187 public void requestFocus() { 188 EditorUtils.doNextFrame(() -> cursorMb.requestFocus()); 189 } 190 }