ApplicationFactory.groovy
01 /*
02  * SPDX-License-Identifier: Apache-2.0
03  *
04  * Copyright 2008-2017 the original author or authors.
05  *
06  * Licensed under the Apache License, Version 2.0 (the "License");
07  * you may not use this file except in compliance with the License.
08  * You may obtain a copy of the License at
09  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 package griffon.builder.javafx.factory
19 
20 import groovyx.javafx.factory.StageFactory
21 import javafx.stage.Stage
22 import javafx.stage.StageStyle
23 import javafx.stage.Window
24 
25 /**
26  *
27  @author Dean Iverson
28  */
29 class ApplicationFactory extends StageFactory {
30     ApplicationFactory() {
31         super(Stage)
32     }
33 
34     Object newInstance(FactoryBuilderSupport builder, Object name, Object value, Map attributes) {
35         Window window = builder.application.createApplicationContainer([:])
36         String windowName = (attributes.remove('name') ?: attributes.id?: computeWindowName()
37         builder.application.windowManager.attach(windowName, window)
38         window
39     }
40 
41     private static int COUNT = 0
42 
43     private static String computeWindowName() {
44         'window' + (COUNT++)
45     }
46 
47     @Override
48     boolean onHandleNodeAttributes(FactoryBuilderSupport builder, Object node, Map attributes) {
49         def stage = node as Stage
50         // stage.width = 800
51         // stage.height = 600
52 
53         attributes.each key, value ->
54             if (key == "title")
55                 stage.title = value
56         }
57 
58         if (!stage.title && builder.application.configuration['application.title']) {
59             stage.title = builder.application.configuration['application.title']
60         }
61 
62         def style = attributes.remove("style")
63         if (style == null) {
64             style = StageStyle.DECORATED;
65         }
66         if (style instanceof String) {
67             style = StageStyle.valueOf(style.toUpperCase())
68         }
69         stage.style = style
70 
71         builder.context.put("sizeToScene", attributes.remove("sizeToScene"))
72         builder.context.put("centerOnScreen", attributes.remove("centerOnScreen"))
73 
74         return true
75     }
76 
77     void onNodeCompleted(FactoryBuilderSupport builder, Object parent, Object node) {
78         if (node instanceof Stage) {
79             if (builder.context.sizeToScene || node.getWidth() == -1) {
80                 node.sizeToScene()
81             }
82             if (builder.context.centerOnScreen) {
83                 node.centerOnScreen();
84             }
85         }
86     }
87 }