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