From daf0fd5a9ff403ca020d95f50b94b6e1c75a569a Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Fri, 17 May 2019 23:30:28 +0200 Subject: [PATCH] ConfigItem: better boolean, better help icon --- src/be/nikiroo/utils/Image.java | 7 ++-- src/be/nikiroo/utils/ui/ConfigItem.java | 52 +++++++++++++++++++++---- 2 files changed, 49 insertions(+), 10 deletions(-) diff --git a/src/be/nikiroo/utils/Image.java b/src/be/nikiroo/utils/Image.java index 64f7b5a..3e65364 100644 --- a/src/be/nikiroo/utils/Image.java +++ b/src/be/nikiroo/utils/Image.java @@ -60,8 +60,9 @@ public class Image implements Closeable, Serializable { /** * Create an image from Base64 encoded data. * - * @deprecated Please use {@link Image#Image(InputStream)} instead, with a - * {@link Base64InputStream} + *

+ * Please use {@link Image#Image(InputStream)} when possible instead, with a + * {@link Base64InputStream}; it can be much more efficient. * * @param base64EncodedData * the Base64 encoded data as a String @@ -69,7 +70,6 @@ public class Image implements Closeable, Serializable { * @throws IOException * in case of I/O error or badly formated Base64 */ - @Deprecated public Image(String base64EncodedData) throws IOException { this(new Base64InputStream(new ByteArrayInputStream( StringUtils.getBytes(base64EncodedData)), false)); @@ -235,6 +235,7 @@ public class Image implements Closeable, Serializable { * @throws ClassNotFoundException * will not be thrown by this method */ + @SuppressWarnings("unused") private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { data = getTemporaryFile(); diff --git a/src/be/nikiroo/utils/ui/ConfigItem.java b/src/be/nikiroo/utils/ui/ConfigItem.java index 9769ab1..3185f13 100644 --- a/src/be/nikiroo/utils/ui/ConfigItem.java +++ b/src/be/nikiroo/utils/ui/ConfigItem.java @@ -2,12 +2,16 @@ package be.nikiroo.utils.ui; import java.awt.BorderLayout; import java.awt.Color; +import java.awt.Cursor; import java.awt.Dimension; import java.awt.Graphics2D; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; import java.awt.image.BufferedImage; import java.io.File; +import java.io.IOException; import javax.swing.Icon; import javax.swing.ImageIcon; @@ -25,6 +29,7 @@ import javax.swing.JPasswordField; import javax.swing.JTextField; import javax.swing.plaf.basic.BasicArrowButton; +import be.nikiroo.utils.Image; import be.nikiroo.utils.StringUtils; import be.nikiroo.utils.StringUtils.Alignment; import be.nikiroo.utils.resources.Bundle; @@ -47,6 +52,18 @@ import be.nikiroo.utils.resources.MetaInfo; public class ConfigItem> extends JPanel { private static final long serialVersionUID = 1L; + /** A small (?) blue in PNG, base64 encoded. */ + private static String infoImage64 = // + "" + + "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABmJLR0QA/wD/AP+gvaeTAAAACXBI" + + "WXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4wURFRg6IrtcdgAAATdJREFUOMvtkj8sQ1EUxr9z/71G" + + "m1RDogYxq7WDDYMYTSajSG4n6YRYzSaSLibWbiaDIGwdiLIYDFKDNJEgKu969xi8UNHy7H7LPcN3" + + "v/Odcy+hG9oOIeIcBCJS9MAvlZtOMtHxsrFrJHGqe0RVGnHAHpcIbPlng8BS3HmKBJYzabGUzcrJ" + + "XK+ckIrqANYR2JEv2nYDEVck0WKGfHzyq82Go+btxoX3XAcAIqTj8wPqOH6mtMeM4bGCLhyfhTMA" + + "qlLhKHqujCfaweCAmV0p50dPzsNpEKpK01V/n55HIvTnfDC2odKlfeYadZN/T+AqDACUsnkhqaU1" + + "LRIVuX1x7ciuSWQxVIrunONrfq3dI6oh+T94Z8453vEem/HTqT8ZpFJ0qDXtGkPbAGAMeSRngQCA" + + "eUvgn195AwlZWyvjtQdhAAAAAElFTkSuQmCC"; + /** * Create a new {@link ConfigItem} for the given {@link MetaInfo}. * @@ -156,7 +173,7 @@ public class ConfigItem> extends JPanel { } }); - field.setText(info.getName()); + this.add(label(info), BorderLayout.WEST); this.add(field, BorderLayout.CENTER); } @@ -384,7 +401,7 @@ public class ConfigItem> extends JPanel { } int w = ps.width; - int step = 80; + int step = 150; for (int i = 2 * step; i < 10 * step; i += step) { if (w < i) { w = i; @@ -392,11 +409,9 @@ public class ConfigItem> extends JPanel { } } - // TODO: image - JButton help = new JButton("?"); - help.addActionListener(new ActionListener() { + final Runnable showInfo = new Runnable() { @Override - public void actionPerformed(ActionEvent e) { + public void run() { StringBuilder builder = new StringBuilder(); String text = info.getDescription().replace("\\n", "\n"); for (String line : StringUtils.justifyText(text, 80, @@ -407,7 +422,30 @@ public class ConfigItem> extends JPanel { builder.append(line); } text = builder.toString(); - JOptionPane.showMessageDialog(ConfigItem.this, text); + JOptionPane.showMessageDialog(ConfigItem.this, text, + info.getName(), JOptionPane.INFORMATION_MESSAGE); + } + }; + + JLabel help = new JLabel(""); + help.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); + try { + Image img = new Image(infoImage64); + try { + BufferedImage bImg = ImageUtilsAwt.fromImage(img); + help.setIcon(new ImageIcon(bImg)); + } finally { + img.close(); + } + } catch (IOException e) { + // This is an hard-coded image, should not happen + help.setText("?"); + } + + help.addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent e) { + showInfo.run(); } }); -- 2.27.0