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.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 import java.util.Map;
038
039 import static griffon.javafx.support.JavaFXUtils.findNode;
040 import static griffon.util.ConfigUtils.stripFilenameExtension;
041 import static griffon.util.GriffonNameUtils.isBlank;
042 import static griffon.util.GriffonNameUtils.requireNonBlank;
043 import static java.util.Objects.requireNonNull;
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 {
052 private static final String ACTION_TARGET_SUFFIX = "ActionTarget";
053 private static final String FXML_SUFFIX = ".fxml";
054
055 @Inject
056 public AbstractJavaFXGriffonView(@Nonnull GriffonApplication application) {
057 super(application);
058 }
059
060 @Nullable
061 protected Node loadFromFXML() {
062 return loadFromFXML(resolveBasename());
063 }
064
065 @Nullable
066 protected Node loadFromFXML(@Nonnull String baseName) {
067 requireNonBlank(baseName, "Argument 'baseName' cannot be blank");
068 if (baseName.endsWith(FXML_SUFFIX)) {
069 baseName = stripFilenameExtension(baseName);
070 }
071 baseName = baseName.replace('.', '/');
072 String viewName = baseName + FXML_SUFFIX;
073 String styleName = baseName + ".css";
074
075 URL viewResource = getResourceAsURL(viewName);
076 if (viewResource == null) {
077 return null;
078 }
079
080 FXMLLoader fxmlLoader = new FXMLLoader(viewResource);
081 fxmlLoader.setResources(getApplication().getMessageSource().asResourceBundle());
082 fxmlLoader.setControllerFactory(new Callback<Class<?>, Object>() {
083 @Override
084 public Object call(Class<?> aClass) {
085 return getMvcGroup().getView();
086 }
087 });
088
089 try {
090 fxmlLoader.load();
091 } catch (IOException e) {
092 throw new GriffonException(e);
093 }
094
095 Parent node = fxmlLoader.getRoot();
096
097 URL cssResource = getResourceAsURL(styleName);
098 if (cssResource != null) {
099 String uriToCss = cssResource.toExternalForm();
100 node.getStylesheets().add(uriToCss);
101 }
102
103 return node;
104 }
105
106 protected String resolveBasename() {
107 GriffonClass griffonClass = getGriffonClass();
108 String packageName = griffonClass.getPackageName();
109 String baseName = griffonClass.getLogicalPropertyName();
110 if (!isBlank(packageName)) {
111 baseName = packageName + "." + baseName;
112 }
113 return baseName;
114 }
115
116 protected void connectActions(@Nonnull Node node, @Nonnull GriffonController controller) {
117 requireNonNull(node, "Argument 'node' cannot be null");
118 requireNonNull(controller, "Argument 'controller' cannot be null");
119 ActionManager actionManager = getApplication().getActionManager();
120 for (Map.Entry<String, Action> e : actionManager.actionsFor(controller).entrySet()) {
121 String actionTargetName = actionManager.normalizeName(e.getKey()) + ACTION_TARGET_SUFFIX;
122 Node control = findNode(node, actionTargetName);
123 if (control == null) continue;
124 JavaFXAction action = (JavaFXAction) e.getValue().getToolkitAction();
125 control.addEventHandler(ActionEvent.ACTION, action.getOnAction());
126 }
127 }
128
129 @Nullable
130 protected JavaFXAction toolkitActionFor(@Nonnull GriffonController controller, @Nonnull String actionName) {
131 Action action = actionFor(controller, actionName);
132 return action != null ? (JavaFXAction) action.getToolkitAction() : null;
133 }
134 }
|