1
1
package org .jabref .gui .actions ;
2
2
3
+ import java .lang .reflect .InvocationTargetException ;
4
+ import java .lang .reflect .Method ;
3
5
import java .util .Objects ;
4
6
5
7
import javafx .scene .Node ;
6
8
import javafx .scene .control .Button ;
7
9
import javafx .scene .control .ButtonBase ;
8
10
import javafx .scene .control .CheckMenuItem ;
11
+ import javafx .scene .control .Label ;
9
12
import javafx .scene .control .Menu ;
10
13
import javafx .scene .control .MenuItem ;
14
+ import javafx .scene .control .Tooltip ;
11
15
12
16
import org .jabref .gui .keyboard .KeyBindingRepository ;
17
+ import org .jabref .model .strings .StringUtil ;
13
18
19
+ import com .sun .javafx .scene .control .skin .ContextMenuContent ;
14
20
import de .saxsys .mvvmfx .utils .commands .Command ;
15
21
import org .controlsfx .control .action .ActionUtils ;
22
+ import org .fxmisc .easybind .EasyBind ;
23
+ import org .slf4j .Logger ;
24
+ import org .slf4j .LoggerFactory ;
16
25
17
26
/**
18
27
* Helper class to create and style controls according to an {@link Action}.
19
28
*/
20
29
public class ActionFactory {
21
30
31
+ private static final Logger LOGGER = LoggerFactory .getLogger (ActionFactory .class );
32
+
22
33
private final KeyBindingRepository keyBindingRepository ;
23
34
24
35
public ActionFactory (KeyBindingRepository keyBindingRepository ) {
@@ -33,14 +44,64 @@ private static void setGraphic(MenuItem node, Action action) {
33
44
action .getIcon ().ifPresent (icon -> node .setGraphic (icon .getGraphicNode ()));
34
45
}
35
46
36
- public MenuItem configureMenuItem (Action action , Command command , MenuItem menuItem ) {
37
- return ActionUtils .configureMenuItem (new JabRefAction (action , command , keyBindingRepository ), menuItem );
47
+ /*
48
+ * Returns MenuItemContainer node associated with this menu item
49
+ * which can contain:
50
+ * 1. label node of type Label for displaying menu item text,
51
+ * 2. right node of type Label for displaying accelerator text,
52
+ * or an arrow if it's a Menu,
53
+ * 3. graphic node for displaying menu item icon, and
54
+ * 4. left node for displaying either radio button or check box.
55
+ *
56
+ * This is basically rewritten impl_styleableGetNode() which
57
+ * should not be used since it's marked as deprecated.
58
+ */
59
+ private static Label getAssociatedNode (MenuItem menuItem ) {
60
+ ContextMenuContent .MenuItemContainer container = (ContextMenuContent .MenuItemContainer ) menuItem .impl_styleableGetNode ();
61
+
62
+ if (container == null ) {
63
+ return null ;
64
+ } else {
65
+ // We have to use reflection to get the associated label
66
+ try {
67
+ Method getLabel = ContextMenuContent .MenuItemContainer .class .getDeclaredMethod ("getLabel" );
68
+ getLabel .setAccessible (true );
69
+ return (Label ) getLabel .invoke (container );
70
+ } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e ) {
71
+ LOGGER .warn ("Could not get label of menu item" , e );
72
+ }
73
+ }
74
+ return null ;
38
75
}
39
76
40
- public MenuItem createMenuItem (Action action , Command command ) {
41
- MenuItem menuItem = ActionUtils .createMenuItem (new JabRefAction (action , command , keyBindingRepository ));
77
+ public MenuItem configureMenuItem (Action action , Command command , MenuItem menuItem ) {
78
+ ActionUtils .configureMenuItem (new JabRefAction (action , command , keyBindingRepository ), menuItem );
42
79
setGraphic (menuItem , action );
43
80
81
+ // Show tooltips
82
+ if (command instanceof SimpleCommand ) {
83
+ EasyBind .subscribe (
84
+ ((SimpleCommand ) command ).statusMessageProperty (),
85
+ message -> {
86
+ Label label = getAssociatedNode (menuItem );
87
+ if (label != null ) {
88
+ label .setMouseTransparent (false );
89
+ if (StringUtil .isBlank (message )) {
90
+ label .setTooltip (null );
91
+ } else {
92
+ label .setTooltip (new Tooltip (message ));
93
+ }
94
+ }
95
+ }
96
+ );
97
+ }
98
+
99
+ return menuItem ;
100
+ }
101
+
102
+ public MenuItem createMenuItem (Action action , Command command ) {
103
+ MenuItem menuItem = new MenuItem ();
104
+ configureMenuItem (action , command , menuItem );
44
105
return menuItem ;
45
106
}
46
107
0 commit comments