GriffonBuilderFactory.java
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.javafx.support;
19 
20 import griffon.core.GriffonApplication;
21 import griffon.core.artifact.GriffonView;
22 import griffon.core.mvc.MVCGroup;
23 import griffon.javafx.artifact.ContentProvider;
24 import javafx.fxml.JavaFXBuilderFactory;
25 import javafx.util.Builder;
26 import javafx.util.BuilderFactory;
27 
28 import javax.annotation.Nonnull;
29 import java.util.LinkedHashMap;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.UUID;
33 
34 import static griffon.util.GriffonNameUtils.isBlank;
35 import static griffon.util.GriffonNameUtils.requireNonBlank;
36 import static java.util.Objects.requireNonNull;
37 
38 /**
39  @author Andres Almiray
40  @since 2.12.0
41  */
42 public class GriffonBuilderFactory implements BuilderFactory {
43     protected final GriffonApplication application;
44     protected final MVCGroup mvcGroup;
45     protected final BuilderFactory delegate;
46 
47     public GriffonBuilderFactory(@Nonnull GriffonApplication application, @Nonnull MVCGroup mvcGroup) {
48         this(application, mvcGroup, application.getApplicationClassLoader().get());
49     }
50 
51     public GriffonBuilderFactory(@Nonnull GriffonApplication application, @Nonnull MVCGroup mvcGroup, @Nonnull ClassLoader classLoader) {
52         this.application = requireNonNull(application, "Argument 'application' must not be null");
53         this.mvcGroup = requireNonNull(mvcGroup, "Argument 'mvcGroup' must not be null");
54         this.delegate = new JavaFXBuilderFactory(requireNonNull(classLoader, "Argument 'classLoader' must not be null"));
55     }
56 
57     @Override
58     public Builder<?> getBuilder(Class<?> type) {
59         if (type == MetaComponent.class) {
60             return new MetaComponentBuilder(application, mvcGroup);
61         }
62         return delegate.getBuilder(type);
63     }
64 
65     private static class MetaComponentBuilder extends MetaComponent implements Builder<Object> {
66         private final GriffonApplication application;
67         private final MVCGroup mvcGroup;
68 
69         public MetaComponentBuilder(@Nonnull GriffonApplication application, @Nonnull MVCGroup mvcGroup) {
70             this.application = requireNonNull(application, "Argument 'application' must not be null");
71             this.mvcGroup = requireNonNull(mvcGroup, "Argument 'mvcGroup' must not be null");
72         }
73 
74         @Override
75         public Object build() {
76             String mvcType = requireNonBlank(getMvcType()"Must define a value for 'mvcType'");
77             String mvcId = getMcvId();
78             if (isBlank(mvcId|| application.getMvcGroupManager().findGroup(mvcId!= null) {
79                 mvcId = mvcType + "-" + UUID.randomUUID().toString();
80             }
81 
82             MVCGroup group = mvcGroup.createMVCGroup(mvcType, mvcId, toMap(getMvcArgs()));
83             GriffonView view = group.getView();
84             if (view instanceof ContentProvider) {
85                 return ((ContentProviderview).getContent();
86             }
87             return group.getContext().get(group.getMvcId() "-rootNode");
88         }
89 
90         @Nonnull
91         private Map<String, Object> toMap(@Nonnull List<MvcArg> mvcArgs) {
92             Map<String, Object> args = new LinkedHashMap<>();
93             mvcArgs.forEach(arg -> args.put(arg.getName(), arg.getValue()));
94             return args;
95         }
96     }
97 }