001 /*
002 * SPDX-License-Identifier: Apache-2.0
003 *
004 * Copyright 2008-2017 the original author or authors.
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018 package org.codehaus.griffon.runtime.javafx.artifact;
019
020 import griffon.core.GriffonApplication;
021 import griffon.core.artifact.GriffonClass;
022 import griffon.core.artifact.GriffonController;
023 import griffon.core.controller.Action;
024 import griffon.exceptions.GriffonException;
025 import griffon.javafx.artifact.JavaFXGriffonView;
026 import griffon.javafx.support.ActionMatcher;
027 import griffon.javafx.support.GriffonBuilderFactory;
028 import griffon.javafx.support.JavaFXAction;
029 import griffon.javafx.support.JavaFXUtils;
030 import javafx.fxml.FXMLLoader;
031 import javafx.scene.Node;
032 import javafx.scene.Parent;
033 import org.codehaus.griffon.runtime.core.artifact.AbstractGriffonView;
034
035 import javax.annotation.Nonnull;
036 import javax.annotation.Nullable;
037 import javax.inject.Inject;
038 import java.io.IOException;
039 import java.net.URL;
040
041 import static griffon.util.ConfigUtils.stripFilenameExtension;
042 import static griffon.util.GriffonNameUtils.isNotBlank;
043 import static griffon.util.GriffonNameUtils.requireNonBlank;
044
045 /**
046 * JavaFX-friendly implementation of the GriffonView interface.
047 *
048 * @author Andres Almiray
049 * @since 2.0.0
050 */
051 public abstract class AbstractJavaFXGriffonView extends AbstractGriffonView implements JavaFXGriffonView {
052 private static final String FXML_SUFFIX = ".fxml";
053
054 @Inject
055 protected ActionMatcher actionMatcher;
056
057 public AbstractJavaFXGriffonView() {
058
059 }
060
061 /**
062 * Creates a new instance of this class.
063 *
064 * @param application the GriffonApplication that holds this artifact.
065 *
066 * @deprecated Griffon prefers field injection over constructor injector for artifacts as of 2.1.0
067 */
068 @Inject
069 @Deprecated
070 public AbstractJavaFXGriffonView(@Nonnull GriffonApplication application) {
071 super(application);
072 }
073
074 @Nonnull
075 protected Node loadFromFXML() {
076 return loadFromFXML(resolveBasename());
077 }
078
079 @Nonnull
080 protected Node loadFromFXML(@Nonnull String baseName) {
081 requireNonBlank(baseName, "Argument 'baseName' must not be blank");
082 if (baseName.endsWith(FXML_SUFFIX)) {
083 baseName = stripFilenameExtension(baseName);
084 }
085 baseName = baseName.replace('.', '/');
086 String viewName = baseName + FXML_SUFFIX;
087 String styleName = baseName + ".css";
088
089 URL viewResource = getResourceAsURL(viewName);
090 if (viewResource == null) {
091 throw new IllegalStateException("resource " + viewName + " not found");
092 }
093
094 FXMLLoader fxmlLoader = createFxmlLoader(viewResource);
095 configureFxmlLoader(fxmlLoader);
096
097 try {
098 fxmlLoader.load();
099 } catch (IOException e) {
100 throw new GriffonException(e);
101 }
102
103 Parent node = fxmlLoader.getRoot();
104
105 URL cssResource = getResourceAsURL(styleName);
106 if (cssResource != null) {
107 String uriToCss = cssResource.toExternalForm();
108 node.getStylesheets().add(uriToCss);
109 }
110
111 return node;
112 }
113
114 @Nonnull
115 protected FXMLLoader createFxmlLoader(@Nonnull URL viewResource) {
116 return new FXMLLoader(viewResource);
117 }
118
119 protected void configureFxmlLoader(@Nonnull FXMLLoader fxmlLoader) {
120 fxmlLoader.setBuilderFactory(new GriffonBuilderFactory(getApplication(), getMvcGroup()));
121 fxmlLoader.setResources(getApplication().getMessageSource().asResourceBundle());
122 fxmlLoader.setClassLoader(getApplication().getApplicationClassLoader().get());
123 fxmlLoader.setControllerFactory(klass -> getMvcGroup().getView());
124 }
125
126 @Nonnull
127 protected String resolveBasename() {
128 GriffonClass griffonClass = getGriffonClass();
129 String packageName = griffonClass.getPackageName();
130 String baseName = griffonClass.getLogicalPropertyName();
131 if (isNotBlank(packageName)) {
132 baseName = packageName + "." + baseName;
133 }
134 return baseName;
135 }
136
137 protected void connectActions(@Nonnull Object node, @Nonnull GriffonController controller) {
138 JavaFXUtils.connectActions(node, controller, actionMatcher);
139 }
140
141 protected void connectMessageSource(@Nonnull Object node) {
142 JavaFXUtils.connectMessageSource(node, getApplication());
143 }
144
145 @Nullable
146 protected JavaFXAction toolkitActionFor(@Nonnull GriffonController controller, @Nonnull String actionName) {
147 Action action = actionFor(controller, actionName);
148 return action != null ? (JavaFXAction) action.getToolkitAction() : null;
149 }
150 }
|