001 /*
002 * Copyright 2008-2015 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.JavaFXAction;
024 import griffon.javafx.support.JavaFXUtils;
025 import javafx.fxml.FXMLLoader;
026 import javafx.fxml.JavaFXBuilderFactory;
027 import javafx.scene.Node;
028 import javafx.scene.Parent;
029 import javafx.util.Callback;
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 public AbstractJavaFXGriffonView() {
052
053 }
054
055 /**
056 * Creates a new instance of this class.
057 *
058 * @param application the GriffonApplication that holds this artifact.
059 * @deprecated Griffon prefers field injection over constructor injector for artifacts as of 2.1.0
060 */
061 @Inject
062 @Deprecated
063 public AbstractJavaFXGriffonView(@Nonnull GriffonApplication application) {
064 super(application);
065 }
066
067 @Nullable
068 protected Node loadFromFXML() {
069 return loadFromFXML(resolveBasename());
070 }
071
072 @Nullable
073 protected Node loadFromFXML(@Nonnull String baseName) {
074 requireNonBlank(baseName, "Argument 'baseName' must not be blank");
075 if (baseName.endsWith(FXML_SUFFIX)) {
076 baseName = stripFilenameExtension(baseName);
077 }
078 baseName = baseName.replace('.', '/');
079 String viewName = baseName + FXML_SUFFIX;
080 String styleName = baseName + ".css";
081
082 URL viewResource = getResourceAsURL(viewName);
083 if (viewResource == null) {
084 return null;
085 }
086
087 FXMLLoader fxmlLoader = new FXMLLoader(viewResource);
088 fxmlLoader.setResources(getApplication().getMessageSource().asResourceBundle());
089 fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory(getApplication().getApplicationClassLoader().get()));
090 fxmlLoader.setClassLoader(getApplication().getApplicationClassLoader().get());
091 fxmlLoader.setControllerFactory(new Callback<Class<?>, Object>() {
092 @Override
093 public Object call(Class<?> aClass) {
094 return getMvcGroup().getView();
095 }
096 });
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 Node node, @Nonnull GriffonController controller) {
127 JavaFXUtils.connectActions(node, controller);
128 }
129
130 @Nullable
131 protected JavaFXAction toolkitActionFor(@Nonnull GriffonController controller, @Nonnull String actionName) {
132 Action action = actionFor(controller, actionName);
133 return action != null ? (JavaFXAction) action.getToolkitAction() : null;
134 }
135 }
|