1 /* 2 * Copyright (c) 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.util.control.effectpicker.editors; 33 34 import com.oracle.javafx.scenebuilder.kit.util.control.effectpicker.EffectPickerController; 35 36 import java.io.IOException; 37 import java.io.InputStream; 38 import java.net.URL; 39 40 import javafx.beans.property.ObjectProperty; 41 import javafx.beans.property.SimpleObjectProperty; 42 import javafx.beans.value.ChangeListener; 43 import javafx.event.ActionEvent; 44 import javafx.event.Event; 45 import javafx.fxml.FXML; 46 import javafx.fxml.FXMLLoader; 47 import javafx.scene.Parent; 48 import javafx.scene.control.ChoiceBox; 49 import javafx.scene.control.Label; 50 import javafx.scene.layout.GridPane; 51 52 public class EnumControl<T> extends GridPane { 53 54 @FXML 55 private Label editor_label; 56 @FXML 57 private ChoiceBox<T> editor_choicebox; 58 59 private final ObjectProperty<T> value = new SimpleObjectProperty<>(); 60 private final EffectPickerController effectPickerController; 61 62 public EnumControl(EffectPickerController effectPickerController, 63 String label, T[] values, T initValue) { 64 this.effectPickerController = effectPickerController; 65 initialize(label, values, initValue); 66 } 67 68 public ObjectProperty<T> valueProperty() { 69 return value; 70 } 71 72 public T getValue() { 73 return value.get(); 74 } 75 76 public void setValue(T v) { 77 value.set(v); 78 } 79 80 private void initialize(String label, T[] values, T initValue) { 81 82 final URL layoutURL = EnumControl.class.getResource("EnumControl.fxml"); //NOI18N 83 try (InputStream is = layoutURL.openStream()) { 84 final FXMLLoader loader = new FXMLLoader(); 85 loader.setController(this); 86 loader.setRoot(this); 87 loader.setLocation(layoutURL); 88 final Parent p = (Parent) loader.load(is); 89 assert p == this; 90 } catch (IOException x) { 91 throw new RuntimeException(x); 92 } 93 94 editor_label.setText(label); 95 editor_choicebox.getItems().addAll(values); 96 editor_choicebox.setValue(initValue); 97 editor_choicebox.getSelectionModel().selectedItemProperty().addListener((ChangeListener<T>) (ov, t, t1) -> { 98 // First update the model 99 setValue(t1); 100 // Then notify the controller a change occured 101 effectPickerController.incrementRevision(); 102 }); 103 104 setValue(initValue); 105 106 editor_choicebox.addEventHandler(ActionEvent.ACTION, (Event event) -> { 107 event.consume(); 108 }); 109 } 110 }