Swing checkbox now extends Swing MenuItem. Fixed size of checkmark
This commit is contained in:
parent
f64feecaed
commit
fd492a74e9
|
@ -17,7 +17,6 @@ package dorkbox.systemTray.swingUI;
|
||||||
|
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionEvent;
|
||||||
import java.awt.event.ActionListener;
|
import java.awt.event.ActionListener;
|
||||||
import java.io.File;
|
|
||||||
|
|
||||||
import javax.swing.ImageIcon;
|
import javax.swing.ImageIcon;
|
||||||
import javax.swing.JMenuItem;
|
import javax.swing.JMenuItem;
|
||||||
|
@ -26,41 +25,47 @@ import dorkbox.systemTray.Checkbox;
|
||||||
import dorkbox.systemTray.Entry;
|
import dorkbox.systemTray.Entry;
|
||||||
import dorkbox.systemTray.SystemTray;
|
import dorkbox.systemTray.SystemTray;
|
||||||
import dorkbox.systemTray.peer.CheckboxPeer;
|
import dorkbox.systemTray.peer.CheckboxPeer;
|
||||||
import dorkbox.systemTray.util.ImageUtils;
|
import dorkbox.systemTray.util.HeavyCheckMark;
|
||||||
|
import dorkbox.util.FontUtil;
|
||||||
import dorkbox.util.SwingUtil;
|
import dorkbox.util.SwingUtil;
|
||||||
|
|
||||||
class SwingMenuItemCheckbox implements CheckboxPeer {
|
class SwingMenuItemCheckbox extends SwingMenuItem implements CheckboxPeer {
|
||||||
|
|
||||||
private final SwingMenu parent;
|
|
||||||
private final JMenuItem _native = new JMenuItem();
|
|
||||||
|
|
||||||
// these have to be volatile, because they can be changed from any thread
|
// these have to be volatile, because they can be changed from any thread
|
||||||
private volatile ActionListener callback;
|
|
||||||
private volatile boolean isChecked = false;
|
private volatile boolean isChecked = false;
|
||||||
|
|
||||||
private static ImageIcon checkedIcon;
|
private static volatile ImageIcon checkedIcon;
|
||||||
private static ImageIcon uncheckedIcon;
|
|
||||||
|
|
||||||
// this is ALWAYS called on the EDT.
|
// this is ALWAYS called on the EDT.
|
||||||
SwingMenuItemCheckbox(final SwingMenu parent, final Entry entry) {
|
SwingMenuItemCheckbox(final SwingMenu parent, final Entry entry) {
|
||||||
this.parent = parent;
|
super(parent, entry);
|
||||||
|
|
||||||
if (SystemTray.SWING_UI != null) {
|
|
||||||
_native.setUI(SystemTray.SWING_UI.getItemUI(_native, entry));
|
|
||||||
}
|
|
||||||
|
|
||||||
parent._native.add(_native);
|
|
||||||
|
|
||||||
if (checkedIcon == null) {
|
if (checkedIcon == null) {
|
||||||
// from Brankic1979, public domain
|
try {
|
||||||
File checkedFile = ImageUtils.resizeAndCache(ImageUtils.ENTRY_SIZE, ImageUtils.class.getResource("checked_32.png"));
|
JMenuItem jMenuItem = new JMenuItem();
|
||||||
checkedIcon = new ImageIcon(checkedFile.getAbsolutePath());
|
|
||||||
|
|
||||||
File uncheckedFile = ImageUtils.getTransparentImage(ImageUtils.ENTRY_SIZE);
|
// do the same modifications that would also happen (if specified) for the actual displayed menu items
|
||||||
uncheckedIcon = new ImageIcon(uncheckedFile.getAbsolutePath());
|
if (SystemTray.SWING_UI != null) {
|
||||||
|
jMenuItem.setUI(SystemTray.SWING_UI.getItemUI(jMenuItem, null));
|
||||||
|
}
|
||||||
|
|
||||||
|
// this is the largest size of an image used in a JMenuItem, before the size of the JMenuItem is forced to be larger
|
||||||
|
int menuImageSize = SystemTray.get()
|
||||||
|
.getMenuImageSize();
|
||||||
|
|
||||||
|
// Having the checkmark size the same size as the letter X is a reasonably nice size.
|
||||||
|
int size = FontUtil.getFontHeight(jMenuItem.getFont(), "X");
|
||||||
|
|
||||||
|
// we have to to the left/right as well, otherwise the checkmark will (possibly) not be centered
|
||||||
|
int sizePadding = (menuImageSize - size) / 2;
|
||||||
|
|
||||||
|
|
||||||
|
checkedIcon = new ImageIcon(HeavyCheckMark.get(jMenuItem.getForeground(), size,
|
||||||
|
0, sizePadding, 0, sizePadding));
|
||||||
|
} catch(Exception e) {
|
||||||
|
SystemTray.logger.error("Error creating check-mark image.", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_native.setIcon(uncheckedIcon);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -154,29 +159,10 @@ class SwingMenuItemCheckbox implements CheckboxPeer {
|
||||||
_native.setIcon(checkedIcon);
|
_native.setIcon(checkedIcon);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
_native.setIcon(uncheckedIcon);
|
_native.setIcon(SwingMenuItem.transparentIcon);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public
|
|
||||||
void remove() {
|
|
||||||
//noinspection Duplicates
|
|
||||||
SwingUtil.invokeLater(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public
|
|
||||||
void run() {
|
|
||||||
if (callback != null) {
|
|
||||||
_native.removeActionListener(callback);
|
|
||||||
callback = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
parent._native.remove(_native);
|
|
||||||
_native.removeAll();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user