MCPcopy Create free account
hub / github.com/dorkbox/SystemTray / SwingMenuItem

Class SwingMenuItem

src/dorkbox/systemTray/ui/swing/SwingMenuItem.java:34–170  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

32import dorkbox.util.SwingUtil;
33
34class SwingMenuItem implements MenuItemPeer {
35 // necessary to have the correct scaling + padding for the menu entries.
36 static ImageIcon transparentIcon = null;
37
38 /**
39 * This should ONLY be called by _SwingTray!
40 *
41 * @param menuImageSize this is the largest size of an image used in a JMenuItem, before the size of the JMenuItem is forced to be larger
42 */
43 static
44 void createTransparentIcon(int menuImageSize, ImageResizeUtil imageResizeUtil) {
45 if (transparentIcon == null) {
46 try {
47 JMenuItem jMenuItem = new JMenuItem();
48
49 // do the same modifications that would also happen (if specified) for the actual displayed menu items
50 if (SystemTray.SWING_UI != null) {
51 jMenuItem.setUI(SystemTray.SWING_UI.getItemUI(jMenuItem, null));
52 }
53
54 transparentIcon = new ImageIcon(imageResizeUtil.getTransparentImage(menuImageSize)
55 .getAbsolutePath());
56 } catch (Exception e) {
57 SystemTray.logger.error("Error creating transparent image.", e);
58 }
59 }
60 }
61
62
63 protected final SwingMenu parent;
64 protected final JMenuItem _native = new JMenuItem();
65
66 protected volatile ActionListener callback;
67
68
69 // this is ALWAYS called on the EDT.
70 SwingMenuItem(final SwingMenu parent, final Entry entry, final int index) {
71 this.parent = parent;
72
73 if (SystemTray.SWING_UI != null) {
74 _native.setUI(SystemTray.SWING_UI.getItemUI(_native, entry));
75 }
76
77 _native.setHorizontalAlignment(SwingConstants.LEFT);
78 parent._native.add(_native, index);
79
80 _native.setIcon(transparentIcon);
81 }
82
83 @Override
84 public
85 void setImage(final MenuItem menuItem) {
86 SwingUtil.INSTANCE.invokeLater(()->{
87 File imageFile = menuItem.getImage();
88 if (imageFile != null) {
89 ImageIcon origIcon = new ImageIcon(imageFile.getAbsolutePath());
90 _native.setIcon(origIcon);
91 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected