Browse Source

Refactored AllIcons to make use of ComponentDemo.

pull/139/head
weisj 5 years ago
parent
commit
0e101edde0
  1. 119
      core/src/test/java/icon/AllIcons.java

119
core/src/test/java/icon/AllIcons.java

@ -24,7 +24,6 @@
package icon;
import com.github.weisj.darklaf.DarkLaf;
import com.github.weisj.darklaf.LafManager;
import com.github.weisj.darklaf.components.OverlayScrollPane;
import com.github.weisj.darklaf.icons.IconLoader;
import com.github.weisj.darklaf.icons.ThemedSVGIcon;
@ -47,7 +46,7 @@ import java.util.List;
import java.util.*;
import java.util.stream.Stream;
public class AllIcons {
public class AllIcons implements ComponentDemo {
private static final String[] FOLDERS = new String[]{
"icons/control", "icons/dialog", "icons/files", "icons/indicator", "icons/menu", "icons/misc",
@ -55,72 +54,69 @@ public class AllIcons {
};
public static void main(final String[] args) {
SwingUtilities.invokeLater(() -> {
LafManager.install(ComponentDemo.getTheme());
try {
JFrame frame = new JFrame("Icons");
JList<Pair<String, Icon>> list = new JList<>(new ListModel<Pair<String, Icon>>() {
final List<Pair<String, Icon>> elements = loadIcons();
@Override
public int getSize() {
return elements.size();
}
@Override
public Pair<String, Icon> getElementAt(final int index) {
return elements.get(index);
}
@Override
public void addListDataListener(final ListDataListener l) {
}
@Override
public void removeListDataListener(final ListDataListener l) {
}
});
list.setLayoutOrientation(JList.VERTICAL);
list.setCellRenderer(new IconListRenderer());
frame.setContentPane(new OverlayScrollPane(list));
frame.pack();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (URISyntaxException | IOException e) {
e.printStackTrace();
ComponentDemo.showDemo(new AllIcons());
}
@Override
public JComponent createComponent() {
JList<Pair<String, Icon>> list = new JList<>(new ListModel<Pair<String, Icon>>() {
final List<Pair<String, Icon>> elements = loadIcons();
@Override
public int getSize() {
return elements.size();
}
@Override
public Pair<String, Icon> getElementAt(final int index) {
return elements.get(index);
}
@Override
public void addListDataListener(final ListDataListener l) {
}
@Override
public void removeListDataListener(final ListDataListener l) {
}
});
list.setLayoutOrientation(JList.VERTICAL);
list.setCellRenderer(new IconListRenderer());
return new OverlayScrollPane(list);
}
private static List<Pair<String, Icon>> loadIcons() throws URISyntaxException, IOException {
private List<Pair<String, Icon>> loadIcons() {
List<Pair<String, Icon>> list = new ArrayList<>();
for (String folder : FOLDERS) {
Pair<Stream<Path>, Optional<FileSystem>> files = walk(folder, DarkLaf.class);
try (FileSystem fs = files.getSecond().isPresent() ? files.getSecond().get() : null) {
files.getFirst().forEach(p -> {
if (p.getFileName().toString().endsWith(".svg")) {
int SIZE = 30;
ThemedSVGIcon icon = (ThemedSVGIcon) IconLoader.get(DarkLaf.class)
.loadSVGIcon(folder + "/" + p.getFileName(),
SIZE, SIZE, true);
SVGIcon svgIcon = icon.getSVGIcon();
int autosize = svgIcon.getAutosize();
svgIcon.setAutosize(SVGIcon.AUTOSIZE_NONE);
int width = Math.min(svgIcon.getIconWidth() * 2, 100);
int height = (int) (((double) width / svgIcon.getIconWidth()) * svgIcon.getIconHeight());
icon.setDisplaySize(width, height);
svgIcon.setAutosize(autosize);
list.add(new Pair<>(p.getFileName().toString(), icon));
}
});
try {
for (String folder : FOLDERS) {
Pair<Stream<Path>, Optional<FileSystem>> files = walk(folder, DarkLaf.class);
try (FileSystem fs = files.getSecond().isPresent() ? files.getSecond().get() : null) {
files.getFirst().forEach(p -> {
if (p.getFileName().toString().endsWith(".svg")) {
int SIZE = 30;
ThemedSVGIcon icon = (ThemedSVGIcon) IconLoader.get(DarkLaf.class)
.loadSVGIcon(folder + "/" + p.getFileName(),
SIZE, SIZE, true);
SVGIcon svgIcon = icon.getSVGIcon();
int autosize = svgIcon.getAutosize();
svgIcon.setAutosize(SVGIcon.AUTOSIZE_NONE);
int width = Math.min(svgIcon.getIconWidth() * 2, 100);
int height = (int) (((double) width / svgIcon.getIconWidth()) * svgIcon.getIconHeight());
icon.setDisplaySize(width, height);
svgIcon.setAutosize(autosize);
list.add(new Pair<>(p.getFileName().toString(), icon));
}
});
}
}
} catch (final IOException | URISyntaxException e) {
e.printStackTrace();
}
return list;
}
public static Pair<Stream<Path>, Optional<FileSystem>> walk(final String path, final Class<?> clazz)
public Pair<Stream<Path>, Optional<FileSystem>> walk(final String path, final Class<?> clazz)
throws URISyntaxException, IOException {
URI uri = clazz.getResource(path).toURI();
if ("jar".equals(uri.getScheme())) {
@ -134,8 +130,17 @@ public class AllIcons {
}
}
@Override
public String getTitle() {
return "All Icons";
}
private static final class IconListRenderer extends JLabel implements ListCellRenderer<Pair<String, Icon>> {
private IconListRenderer() {
setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
}
@Override
public Component getListCellRendererComponent(final JList<? extends Pair<String, Icon>> list,
final Pair<String, Icon> value, final int index,

Loading…
Cancel
Save