Skip to content

Commit

Permalink
Enable simple prototypical implementation of using an icon font inste…
Browse files Browse the repository at this point in the history
…ad of pngs
  • Loading branch information
simonharrer committed Oct 1, 2015
1 parent 05a30ed commit 869e5bb
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 11 deletions.
135 changes: 124 additions & 11 deletions src/main/java/net/sf/jabref/gui/IconTheme.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,133 @@
import net.sf.jabref.logic.l10n.Localization;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jdesktop.swingx.HorizontalLayout;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.*;

public class IconTheme {

public static Font FONT;
public static Font FONT_16;

static {
try {
FONT = Font.createFont(Font.TRUETYPE_FONT, FontBasedIcon.class.getResourceAsStream("/fonts/fontawesome-webfont.ttf"));
FONT_16 = FONT.deriveFont(16f);
} catch (FontFormatException | IOException e) {
// PROBLEM!
e.printStackTrace();
}
}

public enum JabRefIcon {

ADD("\uf067", Color.GREEN),
FOLDER("\uf07b"),
REMOVE("\uf068", Color.RED);

private final String code;
private final Color color;

JabRefIcon(String code) {
this(code, Color.BLACK);
}

JabRefIcon(String code, Color color) {
this.code = code;
this.color = color;
}

public FontBasedIcon getIcon() {
return new FontBasedIcon(this.code, this.color);
}

public ImageIcon getImageIcon() {
return new ImageIcon() {

private FontBasedIcon icon = getIcon();

@Override
public synchronized void paintIcon(Component c, Graphics g, int x, int y) {
icon.paintIcon(c, g, x, y);
}

@Override
public int getIconWidth() {
return icon.getIconWidth();
}

@Override
public int getIconHeight() {
return icon.getIconHeight();
}

@Override
public Image getImage() {
int w = icon.getIconWidth();
int h = icon.getIconHeight();
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gd.getDefaultConfiguration();
BufferedImage image = gc.createCompatibleImage(w, h);
Graphics2D g = image.createGraphics();
icon.paintIcon(null, g, 0, 0);
g.dispose();
return image;
}
};
}

public JLabel getLabel() {
JLabel label = new JLabel(this.code);
label.setForeground(this.color);
label.setFont(FONT_16);
return label;
}
}

public static class FontBasedIcon implements Icon {

private final String iconCode;
private final Color iconColor;

public FontBasedIcon(String code, Color iconColor) {
this.iconCode = code;
this.iconColor = iconColor;
}

@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
Graphics2D g2 = (Graphics2D) g.create();

g2.setFont(FONT_16);
g2.setColor(iconColor);
FontMetrics fm = g2.getFontMetrics();

g2.translate(x, y + fm.getAscent());
g2.drawString(iconCode, 0, 0);

g2.dispose();
}

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

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

private static final Log LOGGER = LogFactory.getLog(IconTheme.class);

private static final Map<String, String> KEY_TO_ICON = readIconThemeFile(IconTheme.class.getResource("/images/crystal_16/Icons.properties"), "/images/crystal_16/");
Expand All @@ -40,7 +152,8 @@ public static Map<String, String> getAllIcons() {
* @return The ImageIcon for the function.
*/
public static ImageIcon getImage(String name) {
return new ImageIcon(getIconUrl(name));
return JabRefIcon.values()[new Random().nextInt(JabRefIcon.values().length)].getImageIcon();
//return new ImageIcon(getIconUrl(name));
}

/**
Expand All @@ -52,7 +165,7 @@ public static ImageIcon getImage(String name) {
*/
private static URL getIconUrl(String name) {
String key = Objects.requireNonNull(name, "icon name");
if(!KEY_TO_ICON.containsKey(key)) {
if (!KEY_TO_ICON.containsKey(key)) {
LOGGER.warn("could not find icon url by name " + name + ", so falling back on default icon " + DEFAULT_ICON_PATH);
}
String path = KEY_TO_ICON.getOrDefault(key, DEFAULT_ICON_PATH);
Expand All @@ -64,7 +177,7 @@ private static URL getIconUrl(String name) {
* of the '=' character - it simply looks for the first '=' to determine where the key ends.
* Both the key and the value is trimmed for whitespace at the ends.
*
* @param url The URL to read information from.
* @param url The URL to read information from.
* @param prefix A String to prefix to all values read. Can represent e.g. the directory
* where icon files are to be found.
* @return A Map containing all key-value pairs found.
Expand All @@ -77,8 +190,8 @@ private static Map<String, String> readIconThemeFile(URL url, String prefix) {

try (BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()))) {
String line;
while((line = in.readLine()) != null) {
if(!line.contains("=")) {
while ((line = in.readLine()) != null) {
if (!line.contains("=")) {
continue;
}

Expand Down
Binary file added src/main/resources/fonts/fontawesome-webfont.ttf
Binary file not shown.

0 comments on commit 869e5bb

Please sign in to comment.