Skip to content

Commit 99fcadf

Browse files
committed
Use Unicode symbol as Icon
1 parent 35367a7 commit 99fcadf

3 files changed

Lines changed: 93 additions & 12 deletions

File tree

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,11 @@
162162
<groupId>sc.fiji</groupId>
163163
<artifactId>fiji-lib</artifactId>
164164
</dependency>
165+
<dependency>
166+
<groupId>com.formdev</groupId>
167+
<artifactId>flatlaf</artifactId>
168+
<scope>provided</scope>
169+
</dependency>
165170

166171
<!-- ImgLib2 dependencies -->
167172
<dependency>
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*-
2+
* #%L
3+
* Fiji distribution of ImageJ for the life sciences.
4+
* %%
5+
* Copyright (C) 2010 - 2024 Fiji developers.
6+
* %%
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as
9+
* published by the Free Software Foundation, either version 3 of the
10+
* License, or (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public
18+
* License along with this program. If not, see
19+
* <http://www.gnu.org/licenses/gpl-3.0.html>.
20+
* #L%
21+
*/
22+
package trainableSegmentation;
23+
24+
import java.awt.Color;
25+
import java.awt.Component;
26+
import java.awt.Font;
27+
import java.awt.Graphics;
28+
import java.awt.Graphics2D;
29+
import java.awt.RenderingHints;
30+
31+
import javax.swing.Icon;
32+
import javax.swing.UIManager;
33+
34+
import com.formdev.flatlaf.FlatLaf.DisabledIconProvider;;
35+
36+
/**
37+
* Unicode symbol as Icon
38+
*/
39+
public class SymbolIcon implements Icon, DisabledIconProvider {
40+
private String symbol = null;
41+
private int size;
42+
43+
/**
44+
*
45+
*/
46+
public SymbolIcon(String symbol) {
47+
this(symbol, 16);
48+
}
49+
50+
public SymbolIcon(String symbol, int size) {
51+
this.symbol = symbol;
52+
this.size = size;
53+
}
54+
55+
@Override
56+
public int getIconHeight() {
57+
return size;
58+
}
59+
60+
@Override
61+
public int getIconWidth() {
62+
return size;
63+
}
64+
65+
@Override
66+
public void paintIcon(Component c, Graphics g, int x, int y) {
67+
final Font oldFont = g.getFont();
68+
final float fs = size/1.1f;
69+
g.setFont(oldFont.deriveFont(fs));
70+
final Color color = UIManager.getColor(c.isEnabled() ? "textText" : "textInactiveText");
71+
g.setColor(color);
72+
if (g instanceof Graphics2D)
73+
((Graphics2D) g).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
74+
RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
75+
g.drawString(symbol, x, y+(int)fs);
76+
g.setFont(oldFont);
77+
}
78+
79+
@Override
80+
public Icon getDisabledIcon() {
81+
return this;
82+
}
83+
}

src/main/java/trainableSegmentation/Weka_Segmentation.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ public class Weka_Segmentation implements PlugIn
221221
private JButton [] addExampleButton = null;
222222
private JButton[] addExampleExtras = null; // consider JideSplitButton?
223223
private final JPopupMenu addExampleMenu = new JPopupMenu();
224-
private final JMenuItem exampleColor = new JMenuItem(new AbstractAction("🎨 Change color…") {
224+
private final JMenuItem exampleColor = new JMenuItem(new AbstractAction("Change color…", new SymbolIcon("🎨")) {
225225
private static final long serialVersionUID = 1L;
226226
{
227227
putValue(Action.SHORT_DESCRIPTION, "Change class and traces color");
@@ -239,7 +239,7 @@ public void actionPerformed(ActionEvent e) {
239239
IJ.showMessage("Failed to look up class " + label + " for color change");
240240
}
241241
});
242-
private final JMenuItem exampleRename = new JMenuItem(new AbstractAction("✎ Rename class…") {
242+
private final JMenuItem exampleRename = new JMenuItem(new AbstractAction("Rename class…", new SymbolIcon("✎")) {
243243
private static final long serialVersionUID = 1L;
244244
{
245245
putValue(Action.SHORT_DESCRIPTION, "Change class label");
@@ -259,7 +259,7 @@ public void actionPerformed(ActionEvent e) {
259259
IJ.showMessage("Failed to look up class " + label + " for label change");
260260
}
261261
});
262-
private final JMenuItem exampleRemove = new JMenuItem(new AbstractAction("✘ Remove class") {
262+
private final JMenuItem exampleRemove = new JMenuItem(new AbstractAction("Remove class", new SymbolIcon("✘")) {
263263
private static final long serialVersionUID = 1L;
264264
{
265265
putValue(Action.SHORT_DESCRIPTION, "Remove class and all associated traces");
@@ -285,7 +285,7 @@ public void actionPerformed(ActionEvent e) {
285285
});
286286
/** Tag for class label name in addExampleExtras' client property bag */
287287
private final Object labelTag = new Object();
288-
private final JButton saveExamplesButton = new JButton(new AbstractAction("💾 Save…") {
288+
private final JButton saveExamplesButton = new JButton(new AbstractAction("Save…", new SymbolIcon("💾")) {
289289
private static final long serialVersionUID = 1L;
290290
{
291291
putValue(Action.SHORT_DESCRIPTION, "Save example ROI traces");
@@ -298,7 +298,7 @@ public void actionPerformed(ActionEvent e) {
298298
}
299299

300300
});
301-
private final JButton loadExamplesButton = new JButton(new AbstractAction("📂 Load…") {
301+
private final JButton loadExamplesButton = new JButton(new AbstractAction("Load…", new SymbolIcon("📂")) {
302302
private static final long serialVersionUID = 1L;
303303
{
304304
putValue(Action.SHORT_DESCRIPTION, "Load example ROI traces");
@@ -798,13 +798,6 @@ private class CustomWindow extends StackWindow
798798

799799
setTitle( Weka_Segmentation.PLUGIN_NAME + " " + Weka_Segmentation.PLUGIN_VERSION );
800800

801-
// remove space for a menu item icon in default look and feel
802-
// https://stackoverflow.com/a/64590818/673826
803-
// https://github.com/JFormDesigner/FlatLaf/issues/328
804-
final Insets insets = new Insets(2, -8, 2, 2);
805-
exampleColor.setMargin(insets);
806-
exampleRename.setMargin(insets);
807-
exampleRemove.setMargin(insets);
808801
addExampleMenu.add(exampleColor);
809802
addExampleMenu.add(exampleRename);
810803
addExampleMenu.addSeparator();

0 commit comments

Comments
 (0)