JavaFXUtils.java
001 /*
002  * Copyright 2008-2014 the original author or authors.
003  *
004  * Licensed under the Apache License, Version 2.0 (the "License");
005  * you may not use this file except in compliance with the License.
006  * You may obtain a copy of the License at
007  *
008  *     http://www.apache.org/licenses/LICENSE-2.0
009  *
010  * Unless required by applicable law or agreed to in writing, software
011  * distributed under the License is distributed on an "AS IS" BASIS,
012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013  * See the License for the specific language governing permissions and
014  * limitations under the License.
015  */
016 package griffon.javafx.support;
017 
018 import javafx.beans.value.ChangeListener;
019 import javafx.beans.value.ObservableValue;
020 import javafx.event.ActionEvent;
021 import javafx.event.EventHandler;
022 import javafx.scene.Node;
023 import javafx.scene.Parent;
024 import javafx.scene.control.ButtonBase;
025 import javafx.scene.control.Control;
026 import javafx.scene.control.Tooltip;
027 import javafx.scene.image.Image;
028 import javafx.scene.image.ImageView;
029 
030 import javax.annotation.Nonnull;
031 import javax.annotation.Nullable;
032 import java.net.URL;
033 
034 import static griffon.util.GriffonNameUtils.isBlank;
035 import static griffon.util.GriffonNameUtils.requireNonBlank;
036 import static java.util.Objects.requireNonNull;
037 
038 /**
039  @author Andres Almiray
040  */
041 public final class JavaFXUtils {
042     private static final String ERROR_CONTROL_NULL = "Argument 'control' must not be null";
043 
044     private JavaFXUtils() {
045 
046     }
047 
048     public static void configure(final @Nonnull ButtonBase control, final @Nonnull JavaFXAction action) {
049         requireNonNull(control, "Argument 'control' must not be null");
050         requireNonNull(action, "Argument 'action' must not be null");
051 
052         action.onActionProperty().addListener(new ChangeListener<EventHandler<ActionEvent>>() {
053             @Override
054             public void changed(ObservableValue<? extends EventHandler<ActionEvent>> observableValue, EventHandler<ActionEvent> oldValue, EventHandler<ActionEvent> newValue) {
055                 control.onActionProperty().set(newValue);
056             }
057         });
058         control.onActionProperty().set(action.getOnAction());
059 
060         action.nameProperty().addListener(new ChangeListener<String>() {
061             @Override
062             public void changed(ObservableValue<? extends String> observableValue, String oldValue, String newValue) {
063                 control.textProperty().set(newValue);
064             }
065         });
066         control.textProperty().set(action.getName());
067 
068         action.descriptionProperty().addListener(new ChangeListener<String>() {
069             @Override
070             public void changed(ObservableValue<? extends String> observableValue, String oldValue, String newValue) {
071                 setTooltip(control, newValue);
072             }
073         });
074         setTooltip(control, action.getDescription());
075 
076         action.iconProperty().addListener(new ChangeListener<String>() {
077             @Override
078             public void changed(ObservableValue<? extends String> observableValue, String oldValue, String newValue) {
079                 setIcon(control, newValue);
080             }
081         });
082         if (!isBlank(action.getIcon())) {
083             setIcon(control, action.getIcon());
084         }
085 
086         action.enabledProperty().addListener(new ChangeListener<Boolean>() {
087             @Override
088             public void changed(ObservableValue<? extends Boolean> observableValue, Boolean oldValue, Boolean newValue) {
089                 control.setDisable(!newValue);
090             }
091         });
092         control.setDisable(!action.getEnabled());
093     }
094 
095     public static void setTooltip(@Nonnull Control control, @Nullable String text) {
096         if (isBlank(text)) {
097             return;
098         }
099         requireNonNull(control, ERROR_CONTROL_NULL);
100 
101         Tooltip tooltip = control.tooltipProperty().get();
102         if (tooltip == null) {
103             tooltip = new Tooltip();
104             control.tooltipProperty().set(tooltip);
105         }
106         tooltip.setText(text);
107     }
108 
109     public static void setIcon(@Nonnull ButtonBase control, @Nonnull String iconUrl) {
110         requireNonNull(control, ERROR_CONTROL_NULL);
111         requireNonBlank(iconUrl, "Argument 'iconUrl' must not be blank");
112 
113         URL resource = Thread.currentThread().getContextClassLoader().getResource(iconUrl);
114         if (resource != null) {
115             Image image = new Image(resource.toString());
116             control.graphicProperty().set(new ImageView(image));
117         }
118     }
119 
120     @Nullable
121     public static Node findNode(@Nonnull Node root, @Nonnull String id) {
122         requireNonNull(root, "Argument 'root' must not be null");
123         requireNonBlank(id, "Argument 'id' must not be blank");
124 
125         if (id.equals(root.getId())) return root;
126 
127         if (root instanceof Parent) {
128             Parent parent = (Parentroot;
129             for (Node child : parent.getChildrenUnmodifiable()) {
130                 Node found = findNode(child, id);
131                 if (found != nullreturn found;
132             }
133         }
134 
135         return null;
136     }
137 }