1+ package edofro.menuomatic
2+
3+ import java.awt.Component
4+ import java.awt.Point
5+ import java.awt.Window
6+ import javax.swing.SwingUtilities
7+ import javax.swing.JComponent
8+
9+ import org.freeplane.plugin.script.proxy.ScriptUtils
10+ import org.freeplane.core.util.TextUtils as textUtils
11+ import org.freeplane.core.util.LogUtils as logger
12+
13+
14+ class MenuAction {
15+
16+ static final int maxTextLength = {
17+ try {
18+ edofro.menuomatic.PackMenu . maxTextLen
19+ }
20+ catch (e){
21+ 50
22+ }
23+ }()
24+
25+ static final c = ScriptUtils . c()
26+
27+ def static menuCommandToNode (n ){
28+ def nodo
29+ def action
30+ try {
31+ JComponent component = (JComponent ) getComponent()
32+ action = getAction(component)
33+ } catch (e){
34+ nodo = n. createChild(e. toString())
35+ nodo. note = e. printStackTrace()
36+ }
37+ if (action){
38+ nodo = n. createChild(getLabelText(action))
39+ nodo. link. text = " menuitem:_${ action.key} "
40+ } else {
41+ c. statusInfo = ' No menu or toolbar command encountered under mouse pointer'
42+ }
43+ }
44+
45+ def static getLabelText (action ){
46+ return getActionText(action. key,' text' )?: getActionText(action. key,' tooltip' )?: action. rawText?: action. key
47+ }
48+
49+ def static getActionText (acc ,tipo ){
50+ def texto = textUtils. getText(" ${ acc} .${ tipo} " , null )
51+ texto = (texto && texto!= ' null' )? textUtils. getShortText(texto, maxTextLength,' .' ): null
52+ return texto
53+ }
54+
55+ def static getAction (component ){
56+ if (component. properties. containsKey(' action' ) && component. action!= null ){
57+ def accion
58+ if (component. action. properties. containsKey(' originalAction' )){
59+ accion = component. action. originalAction
60+ }else {
61+ accion = component. action
62+ }
63+ return accion
64+ }else {
65+ return null
66+ }
67+ }
68+
69+ def static getComponent (){
70+ for (Window window : Window . getWindows()) {
71+ Point mousePositionA = window. getMousePosition(true )
72+ if (! mousePositionA)continue
73+ def compo = window. getLayeredPane()
74+ Point mousePosition
75+ try {
76+ mousePosition = SwingUtilities . convertPoint(window,mousePositionA,compo)
77+ } catch (e){
78+ logger. warn(' menuAction' ,e)
79+ logger. warn(' window: ' + window. toString())
80+ logger. warn(' mousePosition: ' + mousePositionA. toString())
81+ logger. warn(' LayeredPane: ' + compo. toString())
82+ throw e
83+ }
84+ if (mousePosition != null ) {
85+ Component componentUnderMouse = SwingUtilities . getDeepestComponentAt(compo, (int ) mousePosition. x,
86+ (int ) mousePosition. y);
87+ return componentUnderMouse
88+ }
89+ }
90+ }
91+
92+ }
0 commit comments