LabeledFactory.groovy
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.event.GroovyCallback
022 import groovyx.javafx.factory.AbstractNodeFactory
023 import javafx.beans.value.ChangeListener
024 import javafx.beans.value.ObservableValue
025 import javafx.collections.FXCollections
026 import javafx.collections.ObservableList
027 import javafx.scene.control.ButtonBase
028 import javafx.scene.control.ChoiceBox
029 import javafx.scene.control.ContextMenu
030 import javafx.scene.control.Tooltip
031 
032 import static griffon.builder.javafx.factory.ActionFactory.applyAction
033 import static griffon.builder.javafx.factory.ActionFactory.extractActionParams
034 
035 /**
036  *
037  @author jimclarke
038  */
039 class LabeledFactory extends AbstractNodeFactory {
040     LabeledFactory(Class beanClass) {
041         super(beanClass);
042     }
043 
044     Object newInstance(FactoryBuilderSupport builder, Object name, Object value, Map attributesthrows InstantiationException, IllegalAccessException {
045         JavaFXAction action = null
046         Map actionParams = [:]
047         if (value instanceof JavaFXAction) {
048             action = value
049             value = null
050             actionParams = extractActionParams(attributes)
051         }
052 
053         def control = super.newInstance(builder, name, value, attributes)
054 
055         if (control instanceof ButtonBase && action) {
056             applyAction(control, action, actionParams)
057         }
058 
059         if (value != null) {
060             control.text = value.toString()
061         }
062         control
063     }
064 
065     @Override
066     boolean onHandleNodeAttributes(FactoryBuilderSupport builder, Object node, Map attributes) {
067         if (node instanceof ChoiceBox) {
068             List items = attributes.remove("items");
069             if (items) {
070                 if (!(items instanceof ObservableList)) {
071                     items = FXCollections.observableArrayList(items)
072                 }
073 
074                 node.setItems(items);
075             }
076         }
077         return super.onHandleNodeAttributes(builder, node, attributes)
078     }
079 
080     void setChild(FactoryBuilderSupport builder, Object parent, Object child) {
081         switch (child) {
082             case Tooltip:
083                 parent.tooltip = child;
084                 break;
085 
086             case ContextMenu:
087                 parent.contextMenu = child;
088                 break;
089 
090             case Node:
091                 parent.graphic = child;
092                 break;
093 
094             case GroovyCallback:
095                 if ((parent instanceof ChoiceBox&& (child.property == "onSelect")) {
096                     parent.selectionModel.selectedItemProperty().addListener(new ChangeListener() {
097                         void changed(final ObservableValue observable, final Object oldValue, final Object newValue) {
098                             builder.defer({ child.closure.call(parent, newValue)});
099                         }
100                     });
101                 }
102                 break;
103 
104             default:
105                 super.setChild(builder, parent, child);
106         }
107     }
108 }