use of net.runelite.client.config.ConfigDescriptor in project runelite by runelite.
the class ConfigPanel method buildConfigButton.
private JButton buildConfigButton(Config config) {
// Create edit config button and disable it by default
final JButton editConfigButton = new JButton(new ImageIcon(CONFIG_ICON));
editConfigButton.setPreferredSize(new Dimension(32, 0));
editConfigButton.setEnabled(false);
// If we have configuration proxy enable the button and add edit config listener
if (config != null) {
final ConfigDescriptor configDescriptor = configManager.getConfigDescriptor(config);
final boolean configEmpty = configDescriptor.getItems().stream().allMatch(item -> item.getItem().hidden());
if (!configEmpty) {
editConfigButton.addActionListener(ae -> openGroupConfigPanel(config, configDescriptor, configManager));
editConfigButton.setEnabled(true);
editConfigButton.setToolTipText("Edit plugin configuration");
}
}
return editConfigButton;
}
use of net.runelite.client.config.ConfigDescriptor in project runelite by runelite.
the class ConfigPanel method openGroupConfigPanel.
private void openGroupConfigPanel(Config config, ConfigDescriptor cd, ConfigManager configManager) {
scrollBarPosition = getScrollPane().getVerticalScrollBar().getValue();
removeAll();
String name = cd.getGroup().name() + " Configuration";
JLabel title = new JLabel(name);
title.setToolTipText(cd.getGroup().description());
add(title, SwingConstants.CENTER);
for (ConfigItemDescriptor cid : cd.getItems()) {
if (cid.getItem().hidden()) {
continue;
}
JPanel item = new JPanel();
item.setLayout(new BorderLayout());
name = cid.getItem().name();
JLabel configEntryName = new JLabel(name);
configEntryName.setToolTipText("<html>" + name + ":<br>" + cid.getItem().description() + "</html>");
item.add(configEntryName, BorderLayout.CENTER);
if (cid.getType() == boolean.class) {
JCheckBox checkbox = new JCheckBox();
checkbox.setSelected(Boolean.parseBoolean(configManager.getConfiguration(cd.getGroup().keyName(), cid.getItem().keyName())));
checkbox.addActionListener(ae -> changeConfiguration(checkbox, cd, cid));
item.add(checkbox, BorderLayout.EAST);
}
if (cid.getType() == int.class) {
int value = Integer.parseInt(configManager.getConfiguration(cd.getGroup().keyName(), cid.getItem().keyName()));
SpinnerModel model = new SpinnerNumberModel(value, 0, Integer.MAX_VALUE, 1);
JSpinner spinner = new JSpinner(model);
Component editor = spinner.getEditor();
JFormattedTextField spinnerTextField = ((JSpinner.DefaultEditor) editor).getTextField();
spinnerTextField.setColumns(SPINNER_FIELD_WIDTH);
spinner.addChangeListener(ce -> changeConfiguration(spinner, cd, cid));
item.add(spinner, BorderLayout.EAST);
}
if (cid.getType() == String.class) {
JTextField textField = new JTextField("", TEXT_FIELD_WIDTH);
textField.setText(configManager.getConfiguration(cd.getGroup().keyName(), cid.getItem().keyName()));
textField.addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent e) {
}
@Override
public void focusLost(FocusEvent e) {
changeConfiguration(textField, cd, cid);
textField.setToolTipText(textField.getText());
}
});
textField.setToolTipText(textField.getText());
item.add(textField, BorderLayout.EAST);
}
if (cid.getType() == Color.class) {
JButton colorPicker = new JButton("Pick a color");
colorPicker.setFocusable(false);
colorPicker.setBackground(Color.decode(configManager.getConfiguration(cd.getGroup().keyName(), cid.getItem().keyName())));
colorPicker.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
final JFrame parent = new JFrame();
JColorChooser jColorChooser = new JColorChooser(Color.decode(configManager.getConfiguration(cd.getGroup().keyName(), cid.getItem().keyName())));
jColorChooser.getSelectionModel().addChangeListener(e1 -> colorPicker.setBackground(jColorChooser.getColor()));
parent.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
changeConfiguration(jColorChooser, cd, cid);
}
});
parent.add(jColorChooser);
parent.pack();
parent.setVisible(true);
}
});
item.add(colorPicker, BorderLayout.EAST);
}
if (cid.getType() == Dimension.class) {
JPanel dimensionPanel = new JPanel();
dimensionPanel.setLayout(new BorderLayout());
String str = configManager.getConfiguration(cd.getGroup().keyName(), cid.getItem().keyName());
String[] splitStr = str.split("x");
int width = Integer.parseInt(splitStr[0]);
int height = Integer.parseInt(splitStr[1]);
SpinnerModel widthModel = new SpinnerNumberModel(width, 0, Integer.MAX_VALUE, 1);
JSpinner widthSpinner = new JSpinner(widthModel);
Component widthEditor = widthSpinner.getEditor();
JFormattedTextField widthSpinnerTextField = ((JSpinner.DefaultEditor) widthEditor).getTextField();
widthSpinnerTextField.setColumns(4);
SpinnerModel heightModel = new SpinnerNumberModel(height, 0, Integer.MAX_VALUE, 1);
JSpinner heightSpinner = new JSpinner(heightModel);
Component heightEditor = heightSpinner.getEditor();
JFormattedTextField heightSpinnerTextField = ((JSpinner.DefaultEditor) heightEditor).getTextField();
heightSpinnerTextField.setColumns(4);
ChangeListener listener = e -> configManager.setConfiguration(cd.getGroup().keyName(), cid.getItem().keyName(), widthSpinner.getValue() + "x" + heightSpinner.getValue());
widthSpinner.addChangeListener(listener);
heightSpinner.addChangeListener(listener);
dimensionPanel.add(widthSpinner, BorderLayout.WEST);
dimensionPanel.add(new JLabel(" x "), BorderLayout.CENTER);
dimensionPanel.add(heightSpinner, BorderLayout.EAST);
item.add(dimensionPanel, BorderLayout.EAST);
}
if (cid.getType().isEnum()) {
Class<? extends Enum> type = (Class<? extends Enum>) cid.getType();
JComboBox box = new JComboBox(type.getEnumConstants());
box.setFocusable(false);
// sorry but this is the way to keep the size of the combobox in check.
box.setPrototypeDisplayValue("XXXXXXXX");
try {
Enum selectedItem = Enum.valueOf(type, configManager.getConfiguration(cd.getGroup().keyName(), cid.getItem().keyName()));
box.setSelectedItem(selectedItem);
box.setToolTipText(selectedItem.toString());
} catch (IllegalArgumentException ex) {
log.debug("invalid seleced item", ex);
}
box.addItemListener(e -> {
if (e.getStateChange() == ItemEvent.SELECTED) {
changeConfiguration(box, cd, cid);
box.setToolTipText(box.getSelectedItem().toString());
}
});
item.add(box, BorderLayout.EAST);
}
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");
backButton.addActionListener(e -> openConfigList());
add(backButton);
revalidate();
getScrollPane().getVerticalScrollBar().setValue(0);
}
Aggregations