Skip to content

Commit

Permalink
Use Unicode symbol as Icon
Browse files Browse the repository at this point in the history
  • Loading branch information
mlt committed Feb 10, 2024
1 parent 35367a7 commit 99fcadf
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 12 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@
<groupId>sc.fiji</groupId>
<artifactId>fiji-lib</artifactId>
</dependency>
<dependency>
<groupId>com.formdev</groupId>
<artifactId>flatlaf</artifactId>
<scope>provided</scope>
</dependency>

<!-- ImgLib2 dependencies -->
<dependency>
Expand Down
83 changes: 83 additions & 0 deletions src/main/java/trainableSegmentation/SymbolIcon.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*-
* #%L
* Fiji distribution of ImageJ for the life sciences.
* %%
* Copyright (C) 2010 - 2024 Fiji developers.
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
package trainableSegmentation;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import javax.swing.Icon;
import javax.swing.UIManager;

import com.formdev.flatlaf.FlatLaf.DisabledIconProvider;;

/**
* Unicode symbol as Icon
*/
public class SymbolIcon implements Icon, DisabledIconProvider {
private String symbol = null;
private int size;

/**
*
*/
public SymbolIcon(String symbol) {
this(symbol, 16);
}

public SymbolIcon(String symbol, int size) {
this.symbol = symbol;
this.size = size;
}

@Override
public int getIconHeight() {
return size;
}

@Override
public int getIconWidth() {
return size;
}

@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
final Font oldFont = g.getFont();
final float fs = size/1.1f;
g.setFont(oldFont.deriveFont(fs));
final Color color = UIManager.getColor(c.isEnabled() ? "textText" : "textInactiveText");
g.setColor(color);
if (g instanceof Graphics2D)
((Graphics2D) g).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
g.drawString(symbol, x, y+(int)fs);
g.setFont(oldFont);
}

@Override
public Icon getDisabledIcon() {
return this;
}
}
17 changes: 5 additions & 12 deletions src/main/java/trainableSegmentation/Weka_Segmentation.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public class Weka_Segmentation implements PlugIn
private JButton [] addExampleButton = null;
private JButton[] addExampleExtras = null; // consider JideSplitButton?
private final JPopupMenu addExampleMenu = new JPopupMenu();
private final JMenuItem exampleColor = new JMenuItem(new AbstractAction("🎨 Change color…") {
private final JMenuItem exampleColor = new JMenuItem(new AbstractAction("Change color…", new SymbolIcon("🎨")) {
private static final long serialVersionUID = 1L;
{
putValue(Action.SHORT_DESCRIPTION, "Change class and traces color");
Expand All @@ -239,7 +239,7 @@ public void actionPerformed(ActionEvent e) {
IJ.showMessage("Failed to look up class " + label + " for color change");
}
});
private final JMenuItem exampleRename = new JMenuItem(new AbstractAction("✎ Rename class…") {
private final JMenuItem exampleRename = new JMenuItem(new AbstractAction("Rename class…", new SymbolIcon("✎")) {
private static final long serialVersionUID = 1L;
{
putValue(Action.SHORT_DESCRIPTION, "Change class label");
Expand All @@ -259,7 +259,7 @@ public void actionPerformed(ActionEvent e) {
IJ.showMessage("Failed to look up class " + label + " for label change");
}
});
private final JMenuItem exampleRemove = new JMenuItem(new AbstractAction("✘ Remove class") {
private final JMenuItem exampleRemove = new JMenuItem(new AbstractAction("Remove class", new SymbolIcon("✘")) {
private static final long serialVersionUID = 1L;
{
putValue(Action.SHORT_DESCRIPTION, "Remove class and all associated traces");
Expand All @@ -285,7 +285,7 @@ public void actionPerformed(ActionEvent e) {
});
/** Tag for class label name in addExampleExtras' client property bag */
private final Object labelTag = new Object();
private final JButton saveExamplesButton = new JButton(new AbstractAction("💾 Save…") {
private final JButton saveExamplesButton = new JButton(new AbstractAction("Save…", new SymbolIcon("💾")) {
private static final long serialVersionUID = 1L;
{
putValue(Action.SHORT_DESCRIPTION, "Save example ROI traces");
Expand All @@ -298,7 +298,7 @@ public void actionPerformed(ActionEvent e) {
}

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

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

// remove space for a menu item icon in default look and feel
// https://stackoverflow.com/a/64590818/673826
// https://github.com/JFormDesigner/FlatLaf/issues/328
final Insets insets = new Insets(2, -8, 2, 2);
exampleColor.setMargin(insets);
exampleRename.setMargin(insets);
exampleRemove.setMargin(insets);
addExampleMenu.add(exampleColor);
addExampleMenu.add(exampleRename);
addExampleMenu.addSeparator();
Expand Down

0 comments on commit 99fcadf

Please sign in to comment.