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