Add GUI support

This commit is contained in:
2021-10-28 16:30:07 +02:00
parent 03b882635e
commit 7536ae4db3
7 changed files with 421 additions and 3 deletions

View File

@ -0,0 +1,208 @@
package io.reisub.dreambot.util.ui;
import org.dreambot.api.methods.MethodProvider;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class GUI extends JFrame {
private final GridBagLayout layout;
private final GridBagConstraints constraints;
private final Map<String, Component> componentMap;
private final Map<String, java.util.List<Option>> optionMap;
private volatile boolean ready;
public GUI(String title) {
super(title);
layout = new GridBagLayout();
constraints = new GridBagConstraints();
constraints.insets = new Insets(6, 6, 6, 6);
componentMap = new HashMap<>();
optionMap = new HashMap<>();
}
public void init() {
setUndecorated(true);
Container contentPane = getContentPane();
contentPane.setLayout(layout);
addStart();
pack();
setLocationRelativeTo(getOwner());
setVisible(true);
}
private void addStart() {
JButton start = new JButton("Start");
start.setPreferredSize(new Dimension(100, 40));
constraints.fill = GridBagConstraints.VERTICAL;
constraints.weighty = 2.0;
layout.setConstraints(start, constraints);
getContentPane().add(start);
start.addActionListener((e) -> {
for (String key : componentMap.keySet()) {
Component component = componentMap.get(key);
java.util.List<Option> options = new ArrayList<>();
if (component instanceof JComboBox) {
Option option = (Option) ((JComboBox<?>) component).getSelectedItem();
options.add(option);
} else if (component instanceof JList) {
DefaultListModel<Option> listModel = (DefaultListModel<Option>) ((JList<?>) component).getModel();
for (int i = 0; i < listModel.size(); i++) {
options.add(listModel.get(i));
}
}
optionMap.put(key, options);
}
ready = true;
setVisible(false);
dispose();
});
}
public void addComboBox(String name, Option[] options, Parameter... parameters) {
addComboBox(name, options, false, parameters);
}
public void addComboBox(String name, Option[] options, boolean multiple, Parameter... parameters) {
JComboBox<Option> comboBox = new JComboBox<>();
for (Option option : options) {
comboBox.addItem(option);
}
JLabel label = new JLabel(name);
label.setLabelFor(comboBox);
JPanel parametersPanel = new JPanel();
GridBagLayout parametersPanelLayout = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(0, 6, 6, 6);
parametersPanel.setLayout(parametersPanelLayout);
DefaultListModel<Option> optionsList = new DefaultListModel<>();
JList<Option> optionsJList = new JList<>(optionsList);
for (Parameter parameter : parameters) {
JLabel pLabel = new JLabel(parameter.getName());
pLabel.setLabelFor(parameter.getComponent());
componentMap.put(name + "." + parameter.getName(), parameter.getComponent());
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1;
c.gridwidth = 1;
parametersPanelLayout.setConstraints(pLabel, c);
parametersPanel.add(pLabel);
c.gridwidth = GridBagConstraints.REMAINDER;
parametersPanelLayout.setConstraints(parameter.getComponent(), c);
parametersPanel.add(parameter.getComponent());
}
if (multiple) {
c.gridwidth = 2;
JButton button = new JButton("Add");
button.addActionListener((e) -> {
Option option = (Option) comboBox.getSelectedItem();
assert option != null;
option = option.clone();
if (option.getName().equals("custom")) {
JTextField customField = (JTextField) componentMap.get(name + ".Custom");
MethodProvider.log(customField);
if (customField != null) {
option.setCustomName(customField.getText());
}
}
for (Parameter parameter : parameters) {
Component component = parameter.getComponent();
String value = "";
if (component instanceof JTextField) {
value = ((JTextField) component).getText();
} else if (component instanceof JCheckBox) {
if (((JCheckBox) component).isSelected()) {
value = "true";
} else {
value = "false";
}
}
option.setParameter(parameter.getName(), value);
}
optionsList.addElement(option);
});
parametersPanelLayout.setConstraints(button, c);
parametersPanel.add(button);
}
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.anchor = GridBagConstraints.NORTH;
constraints.gridwidth = 1;
layout.setConstraints(label, constraints);
getContentPane().add(label);
if (multiple) {
JPanel panel = new JPanel();
GridBagLayout panelLayout = new GridBagLayout();
GridBagConstraints panelConstraints = new GridBagConstraints();
panel.setLayout(panelLayout);
panelConstraints.weightx = 1;
panelConstraints.fill = GridBagConstraints.HORIZONTAL;
panelConstraints.gridwidth = GridBagConstraints.REMAINDER;
panelLayout.setConstraints(comboBox, panelConstraints);
panel.add(comboBox);
panelConstraints.fill = GridBagConstraints.BOTH;
panelLayout.setConstraints(optionsJList, panelConstraints);
panel.add(optionsJList);
layout.setConstraints(panel, constraints);
getContentPane().add(panel);
} else {
layout.setConstraints(comboBox, constraints);
getContentPane().add(comboBox);
}
constraints.gridwidth = GridBagConstraints.REMAINDER;
layout.setConstraints(parametersPanel, constraints);
getContentPane().add(parametersPanel);
if (multiple) {
componentMap.put(name, optionsJList);
} else {
componentMap.put(name, comboBox);
}
}
public boolean isReady() {
return ready;
}
public Option getOption(String name) {
return optionMap.get(name).get(0);
}
public java.util.List<Option> getOptions(String name) {
return optionMap.get(name);
}
}

View File

@ -0,0 +1,55 @@
package io.reisub.dreambot.util.ui;
import java.util.HashMap;
import java.util.Map;
public class Option implements Cloneable {
private final String name, info;
private final Map<String, String> parameters;
private String customName = "";
public Option(String name) {
this(name, "");
}
public Option(String name, String info) {
this.name = name;
this.info = info;
parameters = new HashMap<>();
}
public String getName() {
return name;
}
@Override
public String toString() {
if (!customName.equals("")) {
return customName;
}
if (info.equals("")) {
return name;
} else {
return name + " (" + info + ")";
}
}
public void setCustomName(String customName) {
this.customName = customName;
}
public void setParameter(String key, String value) {
parameters.put(key, value);
}
@Override
public Option clone() {
try {
return (Option) super.clone();
} catch (CloneNotSupportedException e) {
throw new AssertionError();
}
}
}

View File

@ -0,0 +1,29 @@
package io.reisub.dreambot.util.ui;
import javax.swing.*;
import java.awt.*;
public class Parameter {
private final String name;
private final Component component;
public Parameter(String name) {
JTextField textField = new JTextField(10);
this.name = name;
this.component = textField;
}
public Parameter(String name, Component component) {
this.name = name;
this.component = component;
}
public String getName() {
return name;
}
public Component getComponent() {
return component;
}
}