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