001 /*
002 * Copyright 2008-2014 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.core.controller.ActionManager;
023 import griffon.exceptions.GriffonException;
024 import griffon.javafx.support.JavaFXAction;
025 import javafx.event.ActionEvent;
026 import javafx.fxml.FXMLLoader;
027 import javafx.fxml.JavaFXBuilderFactory;
028 import javafx.scene.Node;
029 import javafx.scene.Parent;
030 import javafx.util.Callback;
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 import java.util.Map;
039
040 import static griffon.javafx.support.JavaFXUtils.findNode;
041 import static griffon.util.ConfigUtils.stripFilenameExtension;
042 import static griffon.util.GriffonNameUtils.isBlank;
043 import static griffon.util.GriffonNameUtils.requireNonBlank;
044 import static java.util.Objects.requireNonNull;
045
046 /**
047 * JavaFX-friendly implementation of the GriffonView interface.
048 *
049 * @author Andres Almiray
050 * @since 2.0.0
051 */
052 public abstract class AbstractJavaFXGriffonView extends AbstractGriffonView {
053 private static final String ACTION_TARGET_SUFFIX = "ActionTarget";
054 private static final String FXML_SUFFIX = ".fxml";
055
056 public AbstractJavaFXGriffonView() {
057
058 }
059
060 /**
061 * Creates a new instance of this class.
062 *
063 * @param application the GriffonApplication that holds this artifact.
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 @Nullable
073 protected Node loadFromFXML() {
074 return loadFromFXML(resolveBasename());
075 }
076
077 @Nullable
078 protected Node loadFromFXML(@Nonnull String baseName) {
079 requireNonBlank(baseName, "Argument 'baseName' cannot 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 JavaFXBuilderFactory(getApplication().getApplicationClassLoader().get()));
095 fxmlLoader.setClassLoader(getApplication().getApplicationClassLoader().get());
096 fxmlLoader.setControllerFactory(new Callback<Class<?>, Object>() {
097 @Override
098 public Object call(Class<?> aClass) {
099 return getMvcGroup().getView();
100 }
101 });
102
103 try {
104 fxmlLoader.load();
105 } catch (IOException e) {
106 throw new GriffonException(e);
107 }
108
109 Parent node = fxmlLoader.getRoot();
110
111 URL cssResource = getResourceAsURL(styleName);
112 if (cssResource != null) {
113 String uriToCss = cssResource.toExternalForm();
114 node.getStylesheets().add(uriToCss);
115 }
116
117 return node;
118 }
119
120 protected String resolveBasename() {
121 GriffonClass griffonClass = getGriffonClass();
122 String packageName = griffonClass.getPackageName();
123 String baseName = griffonClass.getLogicalPropertyName();
124 if (!isBlank(packageName)) {
125 baseName = packageName + "." + baseName;
126 }
127 return baseName;
128 }
129
130 protected void connectActions(@Nonnull Node node, @Nonnull GriffonController controller) {
131 requireNonNull(node, "Argument 'node' cannot be null");
132 requireNonNull(controller, "Argument 'controller' cannot be null");
133 ActionManager actionManager = getApplication().getActionManager();
134 for (Map.Entry<String, Action> e : actionManager.actionsFor(controller).entrySet()) {
135 String actionTargetName = actionManager.normalizeName(e.getKey()) + ACTION_TARGET_SUFFIX;
136 Node control = findNode(node, actionTargetName);
137 if (control == null) continue;
138 JavaFXAction action = (JavaFXAction) e.getValue().getToolkitAction();
139 control.addEventHandler(ActionEvent.ACTION, action.getOnAction());
140 }
141 }
142
143 @Nullable
144 protected JavaFXAction toolkitActionFor(@Nonnull GriffonController controller, @Nonnull String actionName) {
145 Action action = actionFor(controller, actionName);
146 return action != null ? (JavaFXAction) action.getToolkitAction() : null;
147 }
148 }
|