Merge pull request #2263 from Owain94/enumsets

config: render EnumSet as checkboxes
This commit is contained in:
Owain van Brakel
2020-01-21 02:50:51 +01:00
committed by GitHub
4 changed files with 96 additions and 70 deletions

View File

@@ -69,13 +69,6 @@ public @interface ConfigItem
boolean parse() default false;
/**
* For Config items that have a value of multiple enums,
*
* @return the number of rows that are display in the item without having to scroll.
*/
int displayRows() default 2;
Class<?> clazz() default void.class;
String method() default "";

View File

@@ -534,7 +534,14 @@ public class ConfigManager
EnumSet enumSet = EnumSet.noneOf(enumClass);
for (String s : splitStr)
{
enumSet.add(Enum.valueOf(enumClass, s.replace("[", "").replace("]", "")));
try
{
enumSet.add(Enum.valueOf(enumClass, s.replace("[", "").replace("]", "")));
}
catch (IllegalArgumentException ignore)
{
return EnumSet.noneOf(enumClass);
}
}
return enumSet;
}
@@ -617,6 +624,11 @@ public class ConfigManager
}
if (object instanceof EnumSet)
{
if (((EnumSet) object).size() == 0)
{
return getElementType((EnumSet) object).getCanonicalName() + "{}";
}
return ((EnumSet) object).toArray()[0].getClass().getCanonicalName() + "{" + object.toString() + "}";
}
if (object instanceof Number)
@@ -626,6 +638,15 @@ public class ConfigManager
return object.toString();
}
public static <T extends Enum<T>> Class<T> getElementType(EnumSet<T> enumSet)
{
if (enumSet.isEmpty())
{
enumSet = EnumSet.complementOf(enumSet);
}
return enumSet.iterator().next().getDeclaringClass();
}
public void sendConfig()
{
boolean changed;

View File

@@ -26,11 +26,11 @@ package net.runelite.client.plugins.config;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
@@ -65,7 +65,6 @@ import javax.swing.JSpinner;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JToggleButton;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
@@ -361,7 +360,6 @@ class ConfigPanel extends PluginPanel
}
private Boolean parse(ConfigItem item, String value)
{
try
@@ -843,9 +841,6 @@ class ConfigPanel extends PluginPanel
}
else if (cid.getType() == EnumSet.class)
{
int displayRows = cid.getItem().displayRows();
Class enumType = cid.getItem().enumClass();
EnumSet enumSet = configManager.getConfiguration(cd.getGroup().value(),
@@ -854,37 +849,25 @@ class ConfigPanel extends PluginPanel
{
enumSet = EnumSet.noneOf(enumType);
}
JList jList = new JList(enumType.getEnumConstants());
jList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
if (!enumSet.isEmpty() && enumSet.size() > 1)
JPanel enumsetLayout = new JPanel(new GridLayout(0, 2));
List<JCheckBox> jcheckboxes = new ArrayList<>();
for (Object obj : enumType.getEnumConstants())
{
int[] selected = new int[enumSet.size()];
for (int i = 0; i < enumSet.size(); i++)
{
if (enumSet.contains(EnumSet.allOf(enumType).toArray()[i]))
{
selected[i] = Lists.newArrayList(EnumSet.allOf(enumType)).indexOf(enumSet.toArray()[i]);
}
}
jList.setSelectedIndices(selected);
}
if (enumSet.size() == 1)
{
enumSet.forEach(anObject -> jList.setSelectedValue(anObject, true));
}
jList.setVisibleRowCount(displayRows);
jList.setPrototypeCellValue("XXXXXXXXXX");
jList.setCellRenderer(new ComboBoxListRenderer());
jList.setLayoutOrientation(JList.VERTICAL);
jList.setSelectionBackground(Color.decode("708090"));
jList.addListSelectionListener(e -> changeConfiguration(jList, cd, cid));
JScrollPane jScrollPane = new JScrollPane();
jScrollPane.setViewportView(jList);
jScrollPane.setViewportBorder(BorderFactory.createLoweredSoftBevelBorder());
String option = String.valueOf(obj).toLowerCase().replace("_", " ");
item.add(jScrollPane, BorderLayout.SOUTH);
JCheckBox checkbox = new JCheckBox(option);
checkbox.setBackground(ColorScheme.LIGHT_GRAY_COLOR);
checkbox.setSelected(enumSet.contains(obj));
jcheckboxes.add(checkbox);
enumsetLayout.add(checkbox);
}
jcheckboxes.forEach(checkbox -> checkbox.addActionListener(ae -> changeConfiguration(jcheckboxes, cd, cid)));
item.add(enumsetLayout, BorderLayout.SOUTH);
}
JPanel titleSection = titleSectionWidgets.get(cid.getItem().titleSection());
@@ -938,6 +921,32 @@ class ConfigPanel extends PluginPanel
revalidate();
}
private void changeConfiguration(List<JCheckBox> components, ConfigDescriptor cd, ConfigItemDescriptor cid)
{
Class<? extends Enum> enumType = cid.getItem().enumClass();
EnumSet enumSet = configManager.getConfiguration(cd.getGroup().value(),
cid.getItem().keyName(), EnumSet.class);
if (enumSet == null)
{
//noinspection unchecked
enumSet = EnumSet.noneOf(enumType);
}
enumSet.clear();
EnumSet finalEnumSet = enumSet;
//noinspection unchecked
components.forEach(value ->
{
if (value.isSelected())
{
finalEnumSet.add(Enum.valueOf(cid.getItem().enumClass(), String.valueOf(value.getText()).toUpperCase().replace(" ", "_")));
}
});
configManager.setConfiguration(cd.getGroup().value(), cid.getItem().keyName(), finalEnumSet);
}
private void changeConfiguration(Component component, ConfigDescriptor cd, ConfigItemDescriptor cid)
{
final ConfigItem configItem = cid.getItem();
@@ -985,27 +994,6 @@ class ConfigPanel extends PluginPanel
JComboBox jComboBox = (JComboBox) component;
configManager.setConfiguration(cd.getGroup().value(), cid.getItem().keyName(), ((Enum) jComboBox.getSelectedItem()).name());
}
else if (component instanceof JList)
{
JList jList = (JList) component;
Class<? extends Enum> enumType = cid.getItem().enumClass();
EnumSet enumSet = configManager.getConfiguration(cd.getGroup().value(),
cid.getItem().keyName(), EnumSet.class);
if (enumSet == null)
{
//noinspection unchecked
enumSet = EnumSet.noneOf(enumType);
}
enumSet.clear();
EnumSet finalEnumSet = enumSet;
//noinspection unchecked
jList.getSelectedValuesList().forEach(value ->
finalEnumSet.add(Enum.valueOf(cid.getItem().enumClass(), value.toString())));
configManager.setConfiguration(cd.getGroup().value(), cid.getItem().keyName(), finalEnumSet);
}
else if (component instanceof HotkeyButton)
{
HotkeyButton hotkeyButton = (HotkeyButton) component;

View File

@@ -482,42 +482,60 @@ public class PlayerIndicatorsPlugin extends Plugin
if (this.highlightOwnPlayer)
{
relationColorHashMap.put(PlayerRelation.SELF, config.getOwnPlayerColor());
locationHashMap.put(PlayerRelation.SELF, EnumSet.copyOf(config.selfIndicatorModes()).toArray());
if (config.selfIndicatorModes() != null)
{
locationHashMap.put(PlayerRelation.SELF, EnumSet.copyOf(config.selfIndicatorModes()).toArray());
}
}
this.highlightFriends = config.highlightFriends();
if (this.highlightFriends)
{
relationColorHashMap.put(PlayerRelation.FRIEND, config.getFriendColor());
locationHashMap.put(PlayerRelation.FRIEND, config.friendIndicatorMode().toArray());
if (config.friendIndicatorMode() != null)
{
locationHashMap.put(PlayerRelation.FRIEND, config.friendIndicatorMode().toArray());
}
}
this.highlightClan = config.highlightClan();
if (this.highlightClan)
{
relationColorHashMap.put(PlayerRelation.CLAN, config.getClanColor());
locationHashMap.put(PlayerRelation.CLAN, config.clanIndicatorModes().toArray());
if (config.clanIndicatorModes() != null)
{
locationHashMap.put(PlayerRelation.CLAN, config.clanIndicatorModes().toArray());
}
}
this.highlightTeam = config.highlightTeamMembers();
if (this.highlightTeam)
{
relationColorHashMap.put(PlayerRelation.TEAM, config.getTeamcolor());
locationHashMap.put(PlayerRelation.TEAM, config.teamIndicatorModes().toArray());
if (config.teamIndicatorModes() != null)
{
locationHashMap.put(PlayerRelation.TEAM, config.teamIndicatorModes().toArray());
}
}
this.highlightOther = config.highlightOtherPlayers();
if (this.highlightOther)
{
relationColorHashMap.put(PlayerRelation.OTHER, config.getOtherColor());
locationHashMap.put(PlayerRelation.OTHER, EnumSet.copyOf(config.otherIndicatorModes()).toArray());
if (config.otherIndicatorModes() != null)
{
locationHashMap.put(PlayerRelation.OTHER, EnumSet.copyOf(config.otherIndicatorModes()).toArray());
}
}
this.highlightTargets = config.highlightTargets();
if (this.highlightTargets)
{
relationColorHashMap.put(PlayerRelation.TARGET, config.getTargetsColor());
locationHashMap.put(PlayerRelation.TARGET, config.targetsIndicatorModes().toArray());
if (config.targetsIndicatorModes() != null)
{
locationHashMap.put(PlayerRelation.TARGET, config.targetsIndicatorModes().toArray());
}
}
this.highlightCallers = config.highlightCallers();
@@ -527,7 +545,10 @@ public class PlayerIndicatorsPlugin extends Plugin
this.configCallers = config.callers();
relationColorHashMap.put(PlayerRelation.CALLER, config.callerColor());
locationHashMap.put(PlayerRelation.CALLER, config.callerHighlightOptions().toArray());
if (config.callerHighlightOptions() != null)
{
locationHashMap.put(PlayerRelation.CALLER, config.callerHighlightOptions().toArray());
}
getCallerList();
}
@@ -535,7 +556,10 @@ public class PlayerIndicatorsPlugin extends Plugin
if (this.highlightCallerTargets)
{
relationColorHashMap.put(PlayerRelation.CALLER_TARGET, config.callerTargetColor());
locationHashMap.put(PlayerRelation.CALLER_TARGET, config.callerTargetHighlightOptions().toArray());
if (config.callerTargetHighlightOptions() != null)
{
locationHashMap.put(PlayerRelation.CALLER_TARGET, config.callerTargetHighlightOptions().toArray());
}
}
this.showClanRanks = config.showClanRanks();