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