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.artifact.JavaFXGriffonView;
024 import griffon.javafx.support.ActionMatcher;
025 import griffon.javafx.support.GriffonBuilderFactory;
026 import griffon.javafx.support.JavaFXAction;
027 import griffon.javafx.support.JavaFXUtils;
028 import javafx.fxml.FXMLLoader;
029 import javafx.scene.Node;
030 import javafx.scene.Parent;
031 import org.codehaus.griffon.runtime.core.artifact.AbstractGriffonView;
032
033 import javax.annotation.Nonnull;
034 import javax.annotation.Nullable;
035 import javax.inject.Inject;
036 import java.io.IOException;
037 import java.net.URL;
038
039 import static griffon.util.ConfigUtils.stripFilenameExtension;
040 import static griffon.util.GriffonNameUtils.isBlank;
041 import static griffon.util.GriffonNameUtils.requireNonBlank;
042
043 /**
044 * JavaFX-friendly implementation of the GriffonView interface.
045 *
046 * @author Andres Almiray
047 * @since 2.0.0
048 */
049 public abstract class AbstractJavaFXGriffonView extends AbstractGriffonView implements JavaFXGriffonView {
050 private static final String FXML_SUFFIX = ".fxml";
051
052 @Inject
053 protected ActionMatcher actionMatcher;
054
055 public AbstractJavaFXGriffonView() {
056
057 }
058
059 /**
060 * Creates a new instance of this class.
061 *
062 * @param application the GriffonApplication that holds this artifact.
063 *
064 * @deprecated Griffon prefers field injection over constructor injector for artifacts as of 2.1.0
065 */
066 @Inject
067 @Deprecated
068 public AbstractJavaFXGriffonView(@Nonnull GriffonApplication application) {
069 super(application);
070 }
071
072 @Nonnull
073 protected Node loadFromFXML() {
074 return loadFromFXML(resolveBasename());
075 }
076
077 @Nonnull
078 protected Node loadFromFXML(@Nonnull String baseName) {
079 requireNonBlank(baseName, "Argument 'baseName' must not be blank");
080 if (baseName.endsWith(FXML_SUFFIX)) {
081 baseName = stripFilenameExtension(baseName);
082 }
083 baseName = baseName.replace('.', '/');
084 String viewName = baseName + FXML_SUFFIX;
085 String styleName = baseName + ".css";
086
087 URL viewResource = getResourceAsURL(viewName);
088 if (viewResource == null) {
089 return null;
090 }
091
092 FXMLLoader fxmlLoader = new FXMLLoader(viewResource);
093 fxmlLoader.setResources(getApplication().getMessageSource().asResourceBundle());
094 fxmlLoader.setBuilderFactory(new GriffonBuilderFactory(getApplication(), getMvcGroup()));
095 fxmlLoader.setClassLoader(getApplication().getApplicationClassLoader().get());
096 fxmlLoader.setControllerFactory(klass -> getMvcGroup().getView());
097
098 try {
099 fxmlLoader.load();
100 } catch (IOException e) {
101 throw new GriffonException(e);
102 }
103
104 Parent node = fxmlLoader.getRoot();
105
106 URL cssResource = getResourceAsURL(styleName);
107 if (cssResource != null) {
108 String uriToCss = cssResource.toExternalForm();
109 node.getStylesheets().add(uriToCss);
110 }
111
112 return node;
113 }
114
115 @Nonnull
116 protected String resolveBasename() {
117 GriffonClass griffonClass = getGriffonClass();
118 String packageName = griffonClass.getPackageName();
119 String baseName = griffonClass.getLogicalPropertyName();
120 if (!isBlank(packageName)) {
121 baseName = packageName + "." + baseName;
122 }
123 return baseName;
124 }
125
126 protected void connectActions(@Nonnull Object node, @Nonnull GriffonController controller) {
127 JavaFXUtils.connectActions(node, controller, actionMatcher);
128 }
129
130 protected void connectMessageSource(@Nonnull Object node) {
131 JavaFXUtils.connectMessageSource(node, getApplication());
132 }
133
134 @Nullable
135 protected JavaFXAction toolkitActionFor(@Nonnull GriffonController controller, @Nonnull String actionName) {
136 Action action = actionFor(controller, actionName);
137 return action != null ? (JavaFXAction) action.getToolkitAction() : null;
138 }
139 }
|