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