From 555ca7f6d1017e0cbe8709129d0e05dd51f33b17 Mon Sep 17 00:00:00 2001 From: Skiphs Date: Wed, 4 Dec 2013 22:50:45 -0800 Subject: [PATCH] Added biome color profile folder and shortcut support. --- src/amidst/Amidst.java | 2 +- src/amidst/gui/AmidstMenu.java | 45 +++++++++++++++---- src/amidst/preferences/BiomeColorProfile.java | 20 +++++++-- 3 files changed, 54 insertions(+), 13 deletions(-) diff --git a/src/amidst/Amidst.java b/src/amidst/Amidst.java index 380aa4fa..d6a12586 100644 --- a/src/amidst/Amidst.java +++ b/src/amidst/Amidst.java @@ -24,7 +24,7 @@ public static void main(String args[]) { Google.startTracking(); Google.track("Run"); - BiomeColorProfile.scanAndLoad(); + BiomeColorProfile.scan(); new VersionSelectWindow(); } diff --git a/src/amidst/gui/AmidstMenu.java b/src/amidst/gui/AmidstMenu.java index a19d45cd..5c96c4a9 100644 --- a/src/amidst/gui/AmidstMenu.java +++ b/src/amidst/gui/AmidstMenu.java @@ -22,6 +22,7 @@ import java.awt.event.KeyEvent; import java.awt.image.BufferedImage; import java.io.File; +import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; import java.util.Random; @@ -347,6 +348,7 @@ private OptionsMenu() { setMnemonic(KeyEvent.VK_M); } private class BiomeColorMenu extends JMenu { + private ArrayList profileCheckboxes = new ArrayList();; private class BiomeProfileActionListener implements ActionListener { private BiomeColorProfile profile; private ArrayList profileCheckboxes; @@ -366,18 +368,43 @@ public void actionPerformed(ActionEvent e) { } private BiomeColorMenu() { super("Biome profile"); - ArrayList profileCheckboxes = new ArrayList(); - for (int i = 0; i < BiomeColorProfile.profiles.size(); i++) { - BiomeColorProfile profile = BiomeColorProfile.profiles.get(i); - JCheckBoxMenuItem menuItem = new JCheckBoxMenuItem(profile.name); - menuItem.addActionListener(new BiomeProfileActionListener(profile, menuItem, profileCheckboxes)); - profileCheckboxes.add( - menuItem); - add(menuItem); - } + + Log.i("Checking for additional biome color profiles."); + File colorProfileFolder = new File("./biome"); + scanAndLoad(colorProfileFolder, this); profileCheckboxes.get(0).setSelected(true); } + private boolean scanAndLoad(File folder, JMenu menu) { + File[] files = folder.listFiles(); + BiomeColorProfile profile; + boolean foundProfiles = false; + for (int i = 0; i < files.length; i++) { + if (files[i].isFile()) { + if ((profile = BiomeColorProfile.createFromFile(files[i])) != null) { + JCheckBoxMenuItem menuItem = new JCheckBoxMenuItem(profile.name); + menuItem.addActionListener(new BiomeProfileActionListener(profile, menuItem, profileCheckboxes)); + if (profile.shortcut != null) { + KeyStroke accelerator = KeyStroke.getKeyStroke(profile.shortcut); + if (accelerator != null) + menuItem.setAccelerator(accelerator); + else + Log.i("Unable to create keyboard shortcut from: " + profile.shortcut); + } + menu.add(menuItem); + profileCheckboxes.add(menuItem); + foundProfiles = true; + } + } else { + JMenu subMenu = new JMenu(files[i].getName()); + if (scanAndLoad(files[i], subMenu)) { + menu.add(subMenu); + } + } + } + return foundProfiles; + } + } private class MapOptionsMenu extends JMenu { private MapOptionsMenu() { diff --git a/src/amidst/preferences/BiomeColorProfile.java b/src/amidst/preferences/BiomeColorProfile.java index e10048a7..d74bf2c1 100644 --- a/src/amidst/preferences/BiomeColorProfile.java +++ b/src/amidst/preferences/BiomeColorProfile.java @@ -37,11 +37,11 @@ public int toColorInt() { } }; public static boolean isEnabled = false; - public static ArrayList profiles = new ArrayList(); public HashMap colorMap = new HashMap(); public int colorArray[] = new int[Biome.length << 1]; public String name; + public String shortcut; public BiomeColorProfile() { name = "default"; @@ -113,7 +113,7 @@ public void activate() { } - public static void scanAndLoad() { + public static void scan() { Log.i("Searching for biome color profiles."); File colorProfileFolder = new File("./biome"); @@ -127,6 +127,7 @@ public static void scanAndLoad() { if (!Options.instance.biomeColorProfile.save(defaultProfileFile)) Log.i("Attempted to save default biome color profile, but encountered an error."); + /* File[] colorProfiles = colorProfileFolder.listFiles(); for (int i = 0; i < colorProfiles.length; i++) { if (colorProfiles[i].exists() && colorProfiles[i].isFile()) { @@ -138,7 +139,20 @@ public static void scanAndLoad() { Log.i("Unable to load file: " + colorProfiles[i]); } } - } + }*/ isEnabled = true; } + + public static BiomeColorProfile createFromFile(File file) { + BiomeColorProfile profile = null; + if (file.exists() && file.isFile()) { + try { + profile = Util.readObject(file, BiomeColorProfile.class); + profile.fillColorArray(); + } catch (FileNotFoundException e) { + Log.i("Unable to load file: " + file); + } + } + return profile; + } }