001 /*
002 * SPDX-License-Identifier: Apache-2.0
003 *
004 * Copyright 2008-2017 the original author or authors.
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018 package griffon.builder.javafx.factory
019
020 import griffon.javafx.support.JavaFXAction
021 import groovyx.javafx.factory.AbstractFXBeanFactory
022 import javafx.beans.value.ChangeListener
023 import javafx.beans.value.ObservableValue
024 import javafx.scene.control.Tooltip
025
026 import static griffon.javafx.support.JavaFXUtils.resolveIcon
027
028 /**
029 *
030 * @author Andres Almiray
031 */
032 class ActionFactory extends AbstractFXBeanFactory {
033 ActionFactory() {
034 super(JavaFXAction, false)
035 }
036
037 @Override
038 boolean onHandleNodeAttributes(FactoryBuilderSupport builder, Object node, Map attributes) {
039 attributes.remove('id')
040 return super.onHandleNodeAttributes(builder, node, attributes)
041 }
042
043 static Map extractActionParams(Map attributes) {
044 Map actionParams = [:]
045
046 actionParams.skipOnAction = attributes.remove('skipOnAction')
047 actionParams.skipName = attributes.remove('skipName')
048 actionParams.skipDescription = attributes.remove('skipDescription')
049 actionParams.skipAccelerator = attributes.remove('skipAccelerator')
050 actionParams.skipIcon = attributes.remove('skipIcon')
051 actionParams.skipSelected = attributes.remove('skipSelected')
052 actionParams.skipEnabled = attributes.remove('skipEnabled')
053
054 actionParams
055 }
056
057 static void applyAction(control, JavaFXAction action, Map actionParams) {
058 MetaClass mc = control.metaClass
059
060 if (!actionParams.skipOnAction && mc.respondsTo(control, "onActionProperty")) {
061 action.onActionProperty().addListener(new ChangeListener() {
062 void changed(ObservableValue observableValue, Object oldValue, Object newValue) {
063 control.onActionProperty().set(newValue)
064 }
065 })
066 control.onActionProperty().set(action.onAction)
067 }
068 if (!actionParams.skipName && mc.respondsTo(control, "textProperty")) {
069 action.nameProperty().addListener(new ChangeListener() {
070 void changed(ObservableValue observableValue, Object oldValue, Object newValue) {
071 control.textProperty().set(newValue)
072 }
073 })
074 control.textProperty().set(action.name)
075 }
076 if (!actionParams.skipDescription && mc.respondsTo(control, "tooltipProperty")) {
077 action.descriptionProperty().addListener(new ChangeListener() {
078 void changed(ObservableValue observableValue, Object oldValue, Object newValue) {
079 setTooltip(control, newValue)
080 }
081 })
082 if (action.description) setTooltip(control, action.description)
083 }
084 if (!actionParams.skipAccelerator && mc.respondsTo(control, "acceleratorProperty")) {
085 action.acceleratorProperty().addListener(new ChangeListener() {
086 void changed(ObservableValue observableValue, Object oldValue, Object newValue) {
087 control.acceleratorProperty().set(newValue)
088 }
089 })
090 if (action.accelerator != null) control.acceleratorProperty().set(action.accelerator)
091 }
092 if (mc.respondsTo(control, "graphicProperty")) {
093 if (!actionParams.skipIcon) {
094 action.iconProperty().addListener(new ChangeListener() {
095 void changed(ObservableValue observableValue, Object oldValue, Object newValue) {
096 setIcon(control, newValue)
097 }
098 })
099 if (action.icon) setIcon(control, action.icon)
100 }
101 }
102 if (!actionParams.skipSelected && mc.respondsTo(control, "selectedProperty")) {
103 action.selectedProperty().addListener(new ChangeListener() {
104 void changed(ObservableValue observableValue, Object oldValue, Object newValue) {
105 control.selectedProperty().set(newValue)
106 }
107 })
108 control.selectedProperty().set(action.selected)
109 }
110 if (!actionParams.skipEnabled && mc.respondsTo(control, "disableProperty")) {
111 action.enabledProperty().addListener(new ChangeListener() {
112 void changed(ObservableValue observableValue, Object oldValue, Object newValue) {
113 control.disableProperty().set(!newValue)
114 }
115 })
116 control.disableProperty().set(!action.enabled)
117 }
118 }
119
120 static void setIcon(node, String iconUrl) {
121 node.graphicProperty().set(resolveIcon(iconUrl))
122 }
123
124 static void setTooltip(node, String text) {
125 Tooltip tooltip = node.tooltipProperty().get()
126 if (!tooltip) {
127 tooltip = new Tooltip()
128 node.tooltipProperty().set(tooltip)
129 }
130 tooltip.text = text
131 }
132 }
|