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.metadata.property.ValuePropertyMetadata;
  35 
  36 import java.util.Set;
  37 
  38 import javafx.fxml.FXML;
  39 import javafx.scene.Node;
  40 import javafx.scene.Parent;
  41 import javafx.scene.control.ToggleButton;
  42 import javafx.scene.text.TextAlignment;
  43 
  44 /**
  45  * TextAlignment property editor (left/center/right/justify toggle buttons with
  46  * icons).
  47  *
  48  */
  49 public class TextAlignmentEditor extends PropertyEditor {
  50 
  51     private Parent root;
  52     @FXML
  53     private ToggleButton leftTb;
  54     @FXML
  55     private ToggleButton centerTb;
  56     @FXML
  57     private ToggleButton rightTb;
  58     @FXML
  59     private ToggleButton justifyTb;
  60 
  61     private final ToggleButton[] toggleButtons = new ToggleButton[4];
  62 
  63     public TextAlignmentEditor(ValuePropertyMetadata propMeta, Set<Class<?>> selectedClasses) {
  64         super(propMeta, selectedClasses);
  65         initialize();
  66     }
  67 
  68     //Method to please FindBugs
  69     private void initialize() {
  70         root = EditorUtils.loadFxml("TextAlignmentEditor.fxml", this); //NOI18N
  71 
  72         toggleButtons[0] = leftTb;
  73         toggleButtons[1] = centerTb;
  74         toggleButtons[2] = rightTb;
  75         toggleButtons[3] = justifyTb;
  76         for (ToggleButton tb : toggleButtons) {
  77             tb.setOnAction(t -> userUpdateValueProperty(getValue()));
  78             tb.disableProperty().bind(disableProperty());
  79         }
  80         setLayoutFormat(LayoutFormat.SIMPLE_LINE_BOTTOM);
  81     }
  82 
  83     @Override
  84     public Node getValueEditor() {
  85         return super.handleGenericModes(root);
  86     }
  87 
  88     @Override
  89     public Object getValue() {
  90         for (ToggleButton tb : toggleButtons) {
  91             if (tb.isSelected()) {
  92                 if (tb.equals(leftTb)) {
  93                     return TextAlignment.LEFT.toString();
  94                 } else if (tb.equals(centerTb)) {
  95                     return TextAlignment.CENTER.toString();
  96                 } else if (tb.equals(rightTb)) {
  97                     return TextAlignment.RIGHT.toString();
  98                 } else if (tb.equals(justifyTb)) {
  99                     return TextAlignment.JUSTIFY.toString();
 100                 }
 101             }
 102         }
 103         return getPropertyMeta().getDefaultValueObject();
 104     }
 105 
 106     @Override
 107     public void setValue(Object value) {
 108         setValueGeneric(value);
 109         if (isSetValueDone()) {
 110             return;
 111         }
 112 
 113         if (value == null) {
 114             value = getPropertyMeta().getDefaultValueObject();
 115         }
 116         assert value instanceof String;
 117         if (value.equals(TextAlignment.LEFT.toString())) {
 118             leftTb.setSelected(true);
 119         } else if (value.equals(TextAlignment.CENTER.toString())) {
 120             centerTb.setSelected(true);
 121         } else if (value.equals(TextAlignment.RIGHT.toString())) {
 122             rightTb.setSelected(true);
 123         } else if (value.equals(TextAlignment.JUSTIFY.toString())) {
 124             justifyTb.setSelected(true);
 125         }
 126     }
 127 
 128     @Override
 129     public void reset(ValuePropertyMetadata propMeta, Set<Class<?>> selectedClasses) {
 130         super.reset(propMeta, selectedClasses);
 131         setLayoutFormat(LayoutFormat.SIMPLE_LINE_BOTTOM);
 132     }
 133 
 134     @Override
 135     protected void valueIsIndeterminate() {
 136         for (ToggleButton tb : toggleButtons) {
 137             tb.setSelected(false);
 138         }
 139     }
 140 
 141     @Override
 142     public void requestFocus() {
 143         EditorUtils.doNextFrame(() -> leftTb.requestFocus());
 144     }
 145 }