| 
001 /*002  * Copyright 2008-2017 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.builder.javafx.factory
 017
 018 import griffon.javafx.support.JavaFXAction
 019 import groovyx.javafx.factory.AbstractFXBeanFactory
 020 import javafx.beans.value.ChangeListener
 021 import javafx.beans.value.ObservableValue
 022 import javafx.scene.control.Tooltip
 023
 024 import static griffon.javafx.support.JavaFXUtils.resolveIcon
 025
 026 /**
 027  *
 028  * @author Andres Almiray
 029  */
 030 class ActionFactory extends AbstractFXBeanFactory {
 031     ActionFactory() {
 032         super(JavaFXAction, false)
 033     }
 034
 035     @Override
 036     boolean onHandleNodeAttributes(FactoryBuilderSupport builder, Object node, Map attributes) {
 037         attributes.remove('id')
 038         return super.onHandleNodeAttributes(builder, node, attributes)
 039     }
 040
 041     static Map extractActionParams(Map attributes) {
 042         Map actionParams = [:]
 043
 044         actionParams.skipOnAction = attributes.remove('skipOnAction')
 045         actionParams.skipName = attributes.remove('skipName')
 046         actionParams.skipDescription = attributes.remove('skipDescription')
 047         actionParams.skipAccelerator = attributes.remove('skipAccelerator')
 048         actionParams.skipIcon = attributes.remove('skipIcon')
 049         actionParams.skipSelected = attributes.remove('skipSelected')
 050         actionParams.skipEnabled = attributes.remove('skipEnabled')
 051
 052         actionParams
 053     }
 054
 055     static void applyAction(control, JavaFXAction action, Map actionParams) {
 056         MetaClass mc = control.metaClass
 057
 058         if (!actionParams.skipOnAction && mc.respondsTo(control, "onActionProperty")) {
 059             action.onActionProperty().addListener(new ChangeListener() {
 060                 void changed(ObservableValue observableValue, Object oldValue, Object newValue) {
 061                     control.onActionProperty().set(newValue)
 062                 }
 063             })
 064             control.onActionProperty().set(action.onAction)
 065         }
 066         if (!actionParams.skipName && mc.respondsTo(control, "textProperty")) {
 067             action.nameProperty().addListener(new ChangeListener() {
 068                 void changed(ObservableValue observableValue, Object oldValue, Object newValue) {
 069                     control.textProperty().set(newValue)
 070                 }
 071             })
 072             control.textProperty().set(action.name)
 073         }
 074         if (!actionParams.skipDescription && mc.respondsTo(control, "tooltipProperty")) {
 075             action.descriptionProperty().addListener(new ChangeListener() {
 076                 void changed(ObservableValue observableValue, Object oldValue, Object newValue) {
 077                     setTooltip(control, newValue)
 078                 }
 079             })
 080             if (action.description) setTooltip(control, action.description)
 081         }
 082         if (!actionParams.skipAccelerator && mc.respondsTo(control, "acceleratorProperty")) {
 083             action.acceleratorProperty().addListener(new ChangeListener() {
 084                 void changed(ObservableValue observableValue, Object oldValue, Object newValue) {
 085                     control.acceleratorProperty().set(newValue)
 086                 }
 087             })
 088             if (action.accelerator != null) control.acceleratorProperty().set(action.accelerator)
 089         }
 090         if (mc.respondsTo(control, "graphicProperty")) {
 091             if (!actionParams.skipIcon) {
 092                 action.iconProperty().addListener(new ChangeListener() {
 093                     void changed(ObservableValue observableValue, Object oldValue, Object newValue) {
 094                         setIcon(control, newValue)
 095                     }
 096                 })
 097                 if (action.icon) setIcon(control, action.icon)
 098             }
 099         }
 100         if (!actionParams.skipSelected && mc.respondsTo(control, "selectedProperty")) {
 101             action.selectedProperty().addListener(new ChangeListener() {
 102                 void changed(ObservableValue observableValue, Object oldValue, Object newValue) {
 103                     control.selectedProperty().set(newValue)
 104                 }
 105             })
 106             control.selectedProperty().set(action.selected)
 107         }
 108         if (!actionParams.skipEnabled && mc.respondsTo(control, "disableProperty")) {
 109             action.enabledProperty().addListener(new ChangeListener() {
 110                 void changed(ObservableValue observableValue, Object oldValue, Object newValue) {
 111                     control.disableProperty().set(!newValue)
 112                 }
 113             })
 114             control.disableProperty().set(!action.enabled)
 115         }
 116     }
 117
 118     static void setIcon(node, String iconUrl) {
 119         node.graphicProperty().set(resolveIcon(iconUrl))
 120     }
 121
 122     static void setTooltip(node, String text) {
 123         Tooltip tooltip = node.tooltipProperty().get()
 124         if (!tooltip) {
 125             tooltip = new Tooltip()
 126             node.tooltipProperty().set(tooltip)
 127         }
 128         tooltip.text = text
 129     }
 130 }
 |