01 /*
02 * SPDX-License-Identifier: Apache-2.0
03 *
04 * Copyright 2008-2017 the original author or authors.
05 *
06 * Licensed under the Apache License, Version 2.0 (the "License");
07 * you may not use this file except in compliance with the License.
08 * You may obtain a copy of the License at
09 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 package griffon.javafx.support;
19
20 import javafx.scene.Node;
21 import javafx.scene.control.MenuItem;
22
23 import javax.annotation.Nonnull;
24 import java.util.Collection;
25
26 import static griffon.javafx.support.JavaFXUtils.configureControl;
27 import static griffon.javafx.support.JavaFXUtils.findElement;
28 import static griffon.javafx.support.JavaFXUtils.findElements;
29 import static griffon.javafx.support.JavaFXUtils.getGriffonActionId;
30
31 /**
32 * Strategy for matching controller actions to widgets defined in FXML.
33 *
34 * @author Andres Almiray
35 * @since 2.10.0
36 */
37 public interface ActionMatcher {
38 /**
39 * Matches a widget whose action id has been defined by using the default convention.
40 * Either the action id was defined using {@code JavaFXUtils.griffonActionId} or it
41 * has "ActionTarget" as a suffix. Examples:
42 * <p>
43 * <pre>
44 * <Button JavaFXUtils.griffonActionId="copy"/>
45 * <Button fx:id="copyActionTarget"/>
46 * </pre>
47 */
48 ActionMatcher DEFAULT = new ActionMatcher() {
49 @Override
50 public void match(@Nonnull Object node, @Nonnull String actionName, @Nonnull JavaFXAction action) {
51 Collection<Object> controls = findElements(node, arg -> {
52 if (arg instanceof Node) {
53 return actionName.equals(getGriffonActionId((Node) arg));
54 } else if (arg instanceof MenuItem) {
55 return actionName.equals(getGriffonActionId((MenuItem) arg));
56 }
57 return false;
58 });
59
60 for (Object control : controls) {
61 configureControl(control, action);
62 }
63
64 Object control = findElement(node, actionName + "ActionTarget");
65 if (control != null && !controls.contains(control)) {
66 configureControl(control, action);
67 }
68 }
69 };
70
71 void match(@Nonnull Object node, @Nonnull String actionName, @Nonnull JavaFXAction action);
72 }
|