1 | package geniusweb.simplerunner.gui;
|
---|
2 |
|
---|
3 | import java.awt.BorderLayout;
|
---|
4 | import java.awt.Component;
|
---|
5 | import java.awt.TextField;
|
---|
6 | import java.net.URISyntaxException;
|
---|
7 | import java.util.Arrays;
|
---|
8 |
|
---|
9 | import javax.swing.BoxLayout;
|
---|
10 | import javax.swing.JButton;
|
---|
11 | import javax.swing.JComboBox;
|
---|
12 | import javax.swing.JLabel;
|
---|
13 | import javax.swing.JOptionPane;
|
---|
14 | import javax.swing.JPanel;
|
---|
15 | import javax.swing.JScrollPane;
|
---|
16 | import javax.swing.JSeparator;
|
---|
17 | import javax.swing.JSpinner;
|
---|
18 | import javax.swing.JTable;
|
---|
19 | import javax.swing.JTextArea;
|
---|
20 | import javax.swing.SpinnerModel;
|
---|
21 | import javax.swing.SpinnerNumberModel;
|
---|
22 |
|
---|
23 | import com.fasterxml.jackson.core.JsonProcessingException;
|
---|
24 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
25 |
|
---|
26 | import geniusweb.deadline.Deadline;
|
---|
27 | import geniusweb.deadline.DeadlineRounds;
|
---|
28 | import geniusweb.deadline.DeadlineTime;
|
---|
29 | import geniusweb.protocol.NegoSettings;
|
---|
30 | import geniusweb.protocol.session.TeamInfo;
|
---|
31 | import geniusweb.protocol.session.saop.SAOPSettings;
|
---|
32 | import geniusweb.references.Parameters;
|
---|
33 | import geniusweb.references.PartyRef;
|
---|
34 | import geniusweb.references.PartyWithParameters;
|
---|
35 | import geniusweb.references.PartyWithProfile;
|
---|
36 | import geniusweb.references.ProfileRef;
|
---|
37 |
|
---|
38 | public class SettingsPanel extends JPanel {
|
---|
39 | private final ObjectMapper jackson = new ObjectMapper();
|
---|
40 | private final JComboBox<String> protocolcombo = new JComboBox<>(
|
---|
41 | new String[] { "SAOP" });
|
---|
42 |
|
---|
43 | private final TimePanel timepanel = new TimePanel();
|
---|
44 | private final TextField nextparty = new TextField(30);
|
---|
45 | private final TextField nextparameters = new TextField(30);
|
---|
46 | private final MyFileChooser nextprofile = new MyFileChooser();
|
---|
47 | private final JButton addButton = new JButton("add");
|
---|
48 | private final SelectionModel selectedparties = new SelectionModel();
|
---|
49 |
|
---|
50 | public SettingsPanel() {
|
---|
51 | setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
|
---|
52 | add(new WithLabel("protocol", protocolcombo));
|
---|
53 | add(new WithLabel("deadline", timepanel));
|
---|
54 | add(new JSeparator());
|
---|
55 | add(new JLabel("participants"));
|
---|
56 | add(new WithLabel("party:", nextparty));
|
---|
57 | add(new WithLabel("parameters:", nextparameters));
|
---|
58 | add(new WithLabel("profile:", nextprofile));
|
---|
59 | add(new JSeparator());
|
---|
60 | add(addButton);
|
---|
61 | add(new JLabel("Selected Profiles, parties for the session"));
|
---|
62 | add(new JScrollPane(new JTable(selectedparties)));
|
---|
63 | addButton.addActionListener(evt -> addparty());
|
---|
64 | }
|
---|
65 |
|
---|
66 | private void addparty() {
|
---|
67 | try {
|
---|
68 | addparty1();
|
---|
69 | } catch (IllegalArgumentException e) {
|
---|
70 | JScrollPane message = new JScrollPane(
|
---|
71 | new JTextArea(e.getMessage(), 10, 40));
|
---|
72 | JOptionPane.showMessageDialog(this, message, "Failed to add party",
|
---|
73 | JOptionPane.WARNING_MESSAGE, null);
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | private void addparty1() {
|
---|
78 | PartyRef partyref;
|
---|
79 | try {
|
---|
80 | partyref = new PartyRef("classpath:" + nextparty.getText());
|
---|
81 | } catch (URISyntaxException e) {
|
---|
82 | throw new IllegalArgumentException("Party not set correctly:" + e);
|
---|
83 | }
|
---|
84 | Parameters parameters;
|
---|
85 | try {
|
---|
86 | parameters = jackson.readValue("{" + nextparameters.getText() + "}",
|
---|
87 | Parameters.class);
|
---|
88 | } catch (JsonProcessingException e) {
|
---|
89 | throw new IllegalArgumentException(
|
---|
90 | "Parameters not set correctly:" + e);
|
---|
91 | }
|
---|
92 | ProfileRef profile;
|
---|
93 | try {
|
---|
94 | profile = new ProfileRef("file:" + nextprofile.getFile());
|
---|
95 | } catch (URISyntaxException e) {
|
---|
96 | throw new IllegalArgumentException(
|
---|
97 | "Profile not set correctly:" + e);
|
---|
98 | }
|
---|
99 | PartyWithParameters partywithparams = new PartyWithParameters(partyref,
|
---|
100 | parameters);
|
---|
101 | PartyWithProfile partywithprofile = new PartyWithProfile(
|
---|
102 | partywithparams, profile);
|
---|
103 | TeamInfo teaminfo = new TeamInfo(Arrays.asList(partywithprofile));
|
---|
104 | selectedparties.addTeam(teaminfo);
|
---|
105 | }
|
---|
106 |
|
---|
107 | public NegoSettings getSettings() {
|
---|
108 | return new SAOPSettings(selectedparties.getTeams(),
|
---|
109 | timepanel.getDeadline());
|
---|
110 | }
|
---|
111 |
|
---|
112 | }
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * adds label to left of component
|
---|
116 | */
|
---|
117 | class WithLabel extends JPanel {
|
---|
118 | public WithLabel(String label, Component component) {
|
---|
119 | setLayout(new BorderLayout());
|
---|
120 | add(new JLabel(label), BorderLayout.WEST);
|
---|
121 | add(component, BorderLayout.CENTER);
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | class TimePanel extends JPanel {
|
---|
126 | private static final long DEFAULTDEADLINE = 10000;
|
---|
127 | JComboBox<String> unitcombo = new JComboBox<String>(
|
---|
128 | new String[] { "seconds", "rounds" });
|
---|
129 | private SpinnerModel time = new SpinnerNumberModel(10, 1, 1000, 1);
|
---|
130 |
|
---|
131 | public TimePanel() {
|
---|
132 | setLayout(new BorderLayout());
|
---|
133 | add(new JSpinner(time), BorderLayout.CENTER);
|
---|
134 | add(unitcombo, BorderLayout.EAST);
|
---|
135 | }
|
---|
136 |
|
---|
137 | public Deadline getDeadline() {
|
---|
138 | if (unitcombo.getSelectedIndex() == 0)
|
---|
139 | return new DeadlineTime((int) time.getValue() * 1000);
|
---|
140 | return new DeadlineRounds((int) time.getValue(), DEFAULTDEADLINE);
|
---|
141 | }
|
---|
142 | } |
---|