Merge pull request #398 from UniquePassive/reset-plugins

Add a Reset button to config panels for resetting to defaults
This commit is contained in:
Adam
2018-01-25 19:03:11 -05:00
committed by GitHub
2 changed files with 35 additions and 12 deletions

View File

@@ -112,7 +112,7 @@ public class ConfigManager
{ {
for (Object config : getConfigProxies()) for (Object config : getConfigProxies())
{ {
setDefaultConfiguration(config); setDefaultConfiguration(config, false);
} }
} }
@@ -352,7 +352,7 @@ public class ConfigManager
* Initialize the configuration from the default settings * Initialize the configuration from the default settings
* @param proxy * @param proxy
*/ */
public void setDefaultConfiguration(Object proxy) public void setDefaultConfiguration(Object proxy, boolean override)
{ {
Class<?> clazz = proxy.getClass().getInterfaces()[0]; Class<?> clazz = proxy.getClass().getInterfaces()[0];
ConfigGroup group = clazz.getAnnotation(ConfigGroup.class); ConfigGroup group = clazz.getAnnotation(ConfigGroup.class);
@@ -371,10 +371,13 @@ public class ConfigManager
continue; continue;
} }
String current = getConfiguration(group.keyName(), item.keyName()); if (!override)
if (current != null)
{ {
continue; // something else is already set String current = getConfiguration(group.keyName(), item.keyName());
if (current != null)
{
continue; // something else is already set
}
} }
Object defaultValue; Object defaultValue;

View File

@@ -36,6 +36,8 @@ import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent; import java.awt.event.WindowEvent;
import java.util.Map; import java.util.Map;
import java.util.TreeMap; import java.util.TreeMap;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.swing.JButton; import javax.swing.JButton;
import javax.swing.JCheckBox; import javax.swing.JCheckBox;
import javax.swing.JColorChooser; import javax.swing.JColorChooser;
@@ -58,6 +60,7 @@ import javax.swing.SwingConstants;
import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener; import javax.swing.event.DocumentListener;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import net.runelite.client.config.Config;
import net.runelite.client.config.ConfigDescriptor; import net.runelite.client.config.ConfigDescriptor;
import net.runelite.client.config.ConfigItem; import net.runelite.client.config.ConfigItem;
import net.runelite.client.config.ConfigItemDescriptor; import net.runelite.client.config.ConfigItemDescriptor;
@@ -108,12 +111,18 @@ public class ConfigPanel extends PluginPanel
final void rebuildPluginList() final void rebuildPluginList()
{ {
Map<String, JPanel> newChildren = new TreeMap<>(); Map<String, JPanel> newChildren = new TreeMap<>();
configManager.getConfigProxies().stream() configManager.getConfigProxies()
.map(configManager::getConfigDescriptor) .stream()
.filter(configDescriptor -> configDescriptor.getItems().stream() // Config cannot be key because Proxy does not implement hashCode
.anyMatch(cid -> !cid.getItem().hidden())) .collect(Collectors.toMap(configManager::getConfigDescriptor, Function.identity()))
.forEach(cd -> .entrySet().stream()
.filter(e -> e.getKey().getItems().stream()
.anyMatch(cid -> !cid.getItem().hidden()))
.forEach(e ->
{ {
ConfigDescriptor cd = e.getKey();
Config config = e.getValue();
String groupName = cd.getGroup().name(); String groupName = cd.getGroup().name();
if (children.containsKey(groupName)) if (children.containsKey(groupName))
{ {
@@ -123,7 +132,7 @@ public class ConfigPanel extends PluginPanel
JPanel groupPanel = new JPanel(); JPanel groupPanel = new JPanel();
groupPanel.setLayout(new BorderLayout()); groupPanel.setLayout(new BorderLayout());
JButton viewGroupItemsButton = new JButton(groupName); JButton viewGroupItemsButton = new JButton(groupName);
viewGroupItemsButton.addActionListener(ae -> openGroupConfigPanel(cd, configManager)); viewGroupItemsButton.addActionListener(ae -> openGroupConfigPanel(config, cd, configManager));
groupPanel.add(viewGroupItemsButton); groupPanel.add(viewGroupItemsButton);
newChildren.put(groupName, groupPanel); newChildren.put(groupName, groupPanel);
}); });
@@ -223,7 +232,7 @@ public class ConfigPanel extends PluginPanel
} }
} }
private void openGroupConfigPanel(ConfigDescriptor cd, ConfigManager configManager) private void openGroupConfigPanel(Config config, ConfigDescriptor cd, ConfigManager configManager)
{ {
scrollBarPosition = getScrollPane().getVerticalScrollBar().getValue(); scrollBarPosition = getScrollPane().getVerticalScrollBar().getValue();
removeAll(); removeAll();
@@ -350,9 +359,20 @@ public class ConfigPanel extends PluginPanel
add(item); add(item);
} }
JButton resetButton = new JButton("Reset");
resetButton.addActionListener((e) ->
{
configManager.setDefaultConfiguration(config, true);
// Reload configuration panel
openGroupConfigPanel(config, cd, configManager);
});
add(resetButton);
JButton backButton = new JButton("Back"); JButton backButton = new JButton("Back");
backButton.addActionListener(e -> openConfigList()); backButton.addActionListener(e -> openConfigList());
add(backButton); add(backButton);
revalidate(); revalidate();
getScrollPane().getVerticalScrollBar().setValue(0); getScrollPane().getVerticalScrollBar().setValue(0);
} }