38
38
import org .slf4j .Logger ;
39
39
import org .slf4j .LoggerFactory ;
40
40
41
+ import javax .swing .Icon ;
42
+ import javax .swing .ImageIcon ;
41
43
import javax .swing .JMenu ;
42
44
import javax .swing .JMenuBar ;
43
45
import javax .swing .JMenuItem ;
44
46
import javax .swing .JSeparator ;
45
47
import java .awt .Component ;
48
+ import java .net .URL ;
46
49
import java .util .Arrays ;
47
50
import java .util .List ;
48
51
import java .util .stream .Collectors ;
@@ -75,8 +78,9 @@ static public void addCommandToBdvHandleMenu(BdvHandle bdvh, Context ctx,
75
78
Class <? extends Command > commandClass , int skipTopLevels , Object ... args )
76
79
{
77
80
Plugin plugin = commandClass .getDeclaredAnnotation (Plugin .class );
81
+
78
82
addActionToBdvHandleMenu (bdvh , plugin .menuPath (), skipTopLevels , () -> ctx
79
- .getService (CommandService .class ).run (commandClass , true , args ));
83
+ .getService (CommandService .class ).run (commandClass , true , args ), plugin . iconPath (), plugin . description () );
80
84
}
81
85
82
86
static public void addSeparator (BdvHandle bdvh , String pathHierarchy ) {
@@ -106,12 +110,20 @@ static public void addSeparator(BdvHandle bdvh, String pathHierarchy) {
106
110
static public void addCommandToBdvHandleMenu (BdvHandle bdvh , Context ctx , String path ,
107
111
Class <? extends Command > commandClass , Object ... args )
108
112
{
113
+
114
+ Plugin plugin = commandClass .getDeclaredAnnotation (Plugin .class );
115
+
109
116
addActionToBdvHandleMenu (bdvh , path , 0 , () -> ctx
110
- .getService (CommandService .class ).run (commandClass , true , args ));
117
+ .getService (CommandService .class ).run (commandClass , true , args ), plugin .iconPath (), plugin .description ());
118
+ }
119
+
120
+ static public void addActionToBdvHandleMenu (BdvHandle bdvh ,
121
+ String pathHierarchy , int skipTopLevels , Runnable runnable ) {
122
+ addActionToBdvHandleMenu (bdvh , pathHierarchy , skipTopLevels , runnable , null , null );
111
123
}
112
124
113
125
static public void addActionToBdvHandleMenu (BdvHandle bdvh ,
114
- String pathHierarchy , int skipTopLevels , Runnable runnable )
126
+ String pathHierarchy , int skipTopLevels , Runnable runnable , String iconPath , String description )
115
127
{
116
128
if (bdvh instanceof BdvHandleFrame ) {
117
129
final JMenuBar bdvMenuBar = ((BdvHandleFrame ) bdvh ).getBigDataViewer ()
@@ -126,6 +138,18 @@ static public void addActionToBdvHandleMenu(BdvHandle bdvh,
126
138
JMenuItem jMenuItemRoot = findOrCreateJMenu (bdvMenuBar , path );
127
139
if (jMenuItemRoot != null ) {
128
140
final JMenuItem jMenuItem = new JMenuItem (path .get (path .size () - 1 ));
141
+
142
+ if ((iconPath !=null ) && (!iconPath .isEmpty ())) {
143
+ URL iconURL = BdvScijavaHelper .class .getResource (iconPath );
144
+ ImageIcon iconRotX = new ImageIcon (iconURL );
145
+ Icon icon = new ImageIcon ( iconRotX .getImage ().getScaledInstance ( 32 , 32 , java .awt .Image .SCALE_SMOOTH ) );
146
+ jMenuItem .setIcon (icon );
147
+ }
148
+
149
+ if ((description !=null ) && (!description .isEmpty ())) {
150
+ jMenuItem .setToolTipText (description );
151
+ }
152
+
129
153
jMenuItem .addActionListener (e -> runnable .run ());
130
154
jMenuItemRoot .add (jMenuItem );
131
155
bdvMenuBar .updateUI ();
0 commit comments