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

Class SwingMenu

src/dorkbox/systemTray/ui/swing/SwingMenu.java:36–180  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

34
35// this is a weird composite class, because it must be a Menu, but ALSO a Entry -- so it has both (and duplicate code)
36public
37class SwingMenu implements MenuPeer {
38 private final SwingMenu parent;
39
40 final JComponent _native;
41
42 // called by the system tray constructors
43 // This is NOT a copy constructor!
44 @SuppressWarnings("IncompleteCopyConstructor")
45 SwingMenu(final String trayName) {
46 this.parent = null;
47
48 TrayPopup trayPopup = new TrayPopup(trayName);
49 if (SystemTray.SWING_UI != null) {
50 trayPopup.setUI(SystemTray.SWING_UI.getMenuUI(trayPopup, null));
51 }
52 this._native = trayPopup;
53 }
54
55 // This is NOT a copy constructor!
56 @SuppressWarnings("IncompleteCopyConstructor")
57 public
58 SwingMenu(final SwingMenu parent, final Menu entry, final int index) {
59 this.parent = parent;
60
61 JMenu jMenu = new JMenu();
62 JPopupMenu popupMenu = jMenu.getPopupMenu(); // ensure the popup menu is created
63
64 if (SystemTray.SWING_UI != null) {
65 jMenu.setUI(SystemTray.SWING_UI.getItemUI(jMenu, entry));
66 popupMenu.setUI(SystemTray.SWING_UI.getMenuUI(popupMenu, entry));
67 }
68
69 this._native = jMenu;
70 parent._native.add(jMenu, index);
71 }
72
73 @Override
74 public
75 void add(final Menu parentMenu, final Entry entry, final int index) {
76 // must always be called on the EDT
77 SwingUtil.INSTANCE.invokeAndWaitQuietly(()->{
78 // don't add this entry if it's already been added via another method. Because of threading via swing/gtk, entries can
79 // POSSIBLY get added twice. Once via add() and once via bind().
80 if (entry.hasPeer()) {
81 return;
82 }
83
84 if (entry instanceof Menu) {
85 SwingMenu swingMenu = new SwingMenu(SwingMenu.this, (Menu) entry, index);
86 ((Menu) entry).bind(swingMenu, parentMenu, parentMenu.getImageResizeUtil());
87 }
88 else if (entry instanceof Separator) {
89 SwingMenuItemSeparator item = new SwingMenuItemSeparator(SwingMenu.this, index);
90 entry.bind(item, parentMenu, parentMenu.getImageResizeUtil());
91 }
92 else if (entry instanceof Checkbox) {
93 SwingMenuItemCheckbox item = new SwingMenuItemCheckbox(SwingMenu.this, entry, index);

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected