source: simplerunner/src/main/java/geniusweb/simplerunner/gui/SettingsPanel.java@ 52

Last change on this file since 52 was 52, checked in by ruud, 14 months ago

Fixed small issues in domaineditor.

File size: 4.5 KB
Line 
1package geniusweb.simplerunner.gui;
2
3import java.awt.BorderLayout;
4import java.awt.Component;
5import java.awt.TextField;
6import java.net.URISyntaxException;
7import java.util.Arrays;
8
9import javax.swing.BoxLayout;
10import javax.swing.JButton;
11import javax.swing.JComboBox;
12import javax.swing.JLabel;
13import javax.swing.JOptionPane;
14import javax.swing.JPanel;
15import javax.swing.JScrollPane;
16import javax.swing.JSeparator;
17import javax.swing.JSpinner;
18import javax.swing.JTable;
19import javax.swing.JTextArea;
20import javax.swing.SpinnerModel;
21import javax.swing.SpinnerNumberModel;
22
23import com.fasterxml.jackson.core.JsonProcessingException;
24import com.fasterxml.jackson.databind.ObjectMapper;
25
26import geniusweb.deadline.Deadline;
27import geniusweb.deadline.DeadlineRounds;
28import geniusweb.deadline.DeadlineTime;
29import geniusweb.protocol.NegoSettings;
30import geniusweb.protocol.session.TeamInfo;
31import geniusweb.protocol.session.saop.SAOPSettings;
32import geniusweb.references.Parameters;
33import geniusweb.references.PartyRef;
34import geniusweb.references.PartyWithParameters;
35import geniusweb.references.PartyWithProfile;
36import geniusweb.references.ProfileRef;
37
38public 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 */
117class 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
125class 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}
Note: See TracBrowser for help on using the repository browser.