source: src/main/java/genius/gui/tree/DomainAndProfileEditorPanel.java@ 126

Last change on this file since 126 was 126, checked in by Aron Hammond, 6 years ago

Added function to calculate opposition to MultiLateralAnalysis.java

Moved code to add RLBOA listeners to RLBOAUtils is misc package

Added input for strategyParameters to SessionPanel (gui)

!! close SessionInfo after tournament; this caused /tmp/ to fill up with GeniusData files

Our own package:

  • Added opponents and strategies that are mentioned in the report
  • Change class hierarchy, agents can now extend from RLBOAagentBilateral to inherit RL functionality.
  • States extend from AbstractState
File size: 10.5 KB
Line 
1package genius.gui.tree;
2
3import java.awt.BorderLayout;
4import java.awt.Color;
5import java.awt.Dimension;
6import java.awt.event.MouseAdapter;
7import java.awt.event.MouseEvent;
8import java.io.IOException;
9
10import javax.swing.Icon;
11import javax.swing.ImageIcon;
12import javax.swing.JButton;
13import javax.swing.JLabel;
14import javax.swing.JMenu;
15import javax.swing.JMenuBar;
16import javax.swing.JOptionPane;
17import javax.swing.JPanel;
18import javax.swing.JScrollPane;
19import javax.swing.JTextField;
20import javax.swing.ListSelectionModel;
21import javax.swing.table.DefaultTableCellRenderer;
22import javax.swing.table.TableColumnModel;
23
24import genius.core.DomainImpl;
25import genius.core.issue.Issue;
26import genius.core.issue.Objective;
27import genius.core.jtreetable.JTreeTable;
28import genius.core.repository.DomainRepItem;
29import genius.core.utility.AdditiveUtilitySpace;
30import genius.core.utility.UncertainAdditiveUtilitySpace;
31import genius.gui.dialogs.EditIssueDialog;
32import genius.gui.dialogs.NewIssueDialog;
33
34/**
35 * Panel to edit a domain and a profile. It configures itself to domain or
36 * profile editor, depending on whether there are already profiles available for
37 * the domain.
38 */
39public class DomainAndProfileEditorPanel extends JPanel {
40
41 private static final long serialVersionUID = 9072786889017106286L;
42 // Attributes
43 private static final Color UNSELECTED = Color.WHITE;
44 private static final Color HIGHLIGHT = Color.YELLOW;
45 private JTreeTable treeTable;
46 private NegotiatorTreeTableModel model;
47 private JMenuBar menuBar;
48 private JMenu fileMenu;
49 private JMenu editMenu;
50 private DomainRepItem fDomainRepItem;
51 /** true iff no profiles for domain yet and the domain can be edited. */
52 private boolean hasNoProfiles;
53 private JTextField discount;
54 private JTextField reservationValue;
55
56 private UncertaintySettingsModel uncertaintySettings;
57
58 /**
59 * Create new profile
60 *
61 * @param domain
62 * @param hasNoProfiles
63 * if true, then there are no profiles for the domain yet and you
64 * can edit the domain.
65 */
66 public DomainAndProfileEditorPanel(DomainImpl domain,
67 boolean hasNoProfiles) {
68 this(new NegotiatorTreeTableModel(domain), hasNoProfiles);
69 }
70
71 /**
72 * Edit existing profile
73 *
74 * @param domain
75 * the domain of the profile
76 * @param utilitySpace
77 * the profile to edit
78 */
79 public DomainAndProfileEditorPanel(DomainImpl domain,
80 AdditiveUtilitySpace utilitySpace) {
81 this(new NegotiatorTreeTableModel(domain, utilitySpace), false);
82 }
83
84 public DomainAndProfileEditorPanel(NegotiatorTreeTableModel treeModel,
85 boolean hasNoProfiles) {
86 super();
87 this.hasNoProfiles = hasNoProfiles;
88 init(treeModel, null);
89 }
90
91 public void clearTreeTable(DomainImpl domain,
92 AdditiveUtilitySpace utilitySpace) {
93 init(new NegotiatorTreeTableModel(domain, utilitySpace),
94 this.getSize());
95 }
96
97 public boolean isDomain() {
98 return model.getUtilitySpace() == null;
99 }
100
101 public boolean hasNoProfiles() {
102 return hasNoProfiles;
103 }
104
105 public JTreeTable getTreeTable() {
106 return treeTable;
107 }
108
109 public NegotiatorTreeTableModel getNegotiatorTreeTableModel() {
110 return model;
111 }
112
113 public Objective getRoot() {
114 return (Objective) model.getRoot();
115 }
116
117 /*******************************************************/
118 /*********** PRIVATE CODE ******************************/
119 /*******************************************************/
120
121 private void init(NegotiatorTreeTableModel treeModel, Dimension size) {
122 final DomainAndProfileEditorPanel thisFrame = this;
123 model = treeModel;
124 setLayout(new BorderLayout());
125
126 initTable(model);
127 treeTable.addMouseListener(new MouseAdapter() {
128 @Override
129 public void mouseReleased(MouseEvent e) {
130
131 if (e.getClickCount() == 2) {
132 Object selected = treeTable.getTree()
133 .getLastSelectedPathComponent();
134
135 if (selected instanceof Issue) {
136 if (hasNoProfiles || !isDomain()) {
137 new EditIssueDialog(thisFrame, (Issue) selected);
138 } else {
139 showError(
140 "You may only edit the issues when there are no preference profiles.");
141 }
142 }
143 }
144 }
145 });
146 treeTable.setRowHeight(40);
147 // Initialize the Menu
148 initMenus();
149 JPanel bottomArea = new JPanel(new BorderLayout());
150
151 AdditiveUtilitySpace utilspace = model.getUtilitySpace();
152 if (utilspace != null) {
153 uncertaintySettings = new UncertaintySettingsModel(utilspace);
154 bottomArea.add(new UncertaintySettingsPanel(uncertaintySettings),
155 BorderLayout.CENTER);
156 }
157 bottomArea.add(savePanel(), BorderLayout.SOUTH);
158
159 add(bottomArea, BorderLayout.SOUTH);
160
161 if (size != null)
162 this.setSize(size);
163
164 }
165
166 private void showError(String error) {
167 JOptionPane.showMessageDialog(null, error, "Edit error", 0);
168
169 }
170
171 private JPanel savePanel() {
172 JButton saveButton = new JButton("Save changes");
173 Icon icon = new ImageIcon(getClass().getClassLoader()
174 .getResource("genius/gui/resources/save.png"));
175 saveButton.setPreferredSize(new Dimension(180, 60));
176 saveButton.setIcon(icon);
177 saveButton.setFont(saveButton.getFont().deriveFont(14.0f));
178 saveButton.addActionListener(new java.awt.event.ActionListener() {
179 @Override
180 public void actionPerformed(java.awt.event.ActionEvent evt) {
181 save();
182 }
183
184 });
185
186 JPanel simplePanel = new JPanel();
187 if (hasNoProfiles) {
188 addDomainEditButtons(simplePanel);
189 }
190 simplePanel.add(saveButton);
191
192 if (model.getUtilitySpace() != null) {
193 addProfileEditButtons(simplePanel);
194 }
195 return simplePanel;
196 }
197
198 private void addProfileEditButtons(JPanel simplePanel) {
199 simplePanel.add(new JLabel("Discount: "));
200 discount = new JTextField(
201 model.getUtilitySpace().getDiscountFactor() + "", 5);
202 simplePanel.add(discount);
203
204 simplePanel.add(new JLabel("Reservation value: "));
205 reservationValue = new JTextField(
206 model.getUtilitySpace().getReservationValueUndiscounted() + "",
207 5);
208 simplePanel.add(reservationValue);
209 }
210
211 private void addDomainEditButtons(JPanel simplePanel) {
212 final DomainAndProfileEditorPanel thisFrame = this;
213 JButton addIssue = new JButton("Add issue");
214 Icon icon2 = new ImageIcon(getClass().getClassLoader()
215 .getResource("genius/gui/resources/edit_add-32.png"));
216 addIssue.setPreferredSize(new Dimension(180, 60));
217 addIssue.setIcon(icon2);
218 addIssue.setFont(addIssue.getFont().deriveFont(18.0f));
219 addIssue.addActionListener(new java.awt.event.ActionListener() {
220 @Override
221 public void actionPerformed(java.awt.event.ActionEvent evt) {
222 new NewIssueDialog(thisFrame);
223 }
224 });
225 simplePanel.add(addIssue);
226
227 JButton removeIssue = new JButton("Remove issue");
228 Icon icon3 = new ImageIcon(getClass().getClassLoader()
229 .getResource("genius/gui/resources/edit_remove-32.png"));
230 removeIssue.setPreferredSize(new Dimension(180, 60));
231 removeIssue.setIcon(icon3);
232 removeIssue.setFont(removeIssue.getFont().deriveFont(18.0f));
233 removeIssue.addActionListener(new java.awt.event.ActionListener() {
234 @Override
235 public void actionPerformed(java.awt.event.ActionEvent evt) {
236 Object selected = treeTable.getTree()
237 .getLastSelectedPathComponent();
238
239 if (selected instanceof Issue) {
240 ((Issue) selected).removeFromParent();
241 // correct numbering
242 for (int i = 0; i < model.getDomain().getIssues()
243 .size(); i++) {
244 model.getDomain().getIssues().get(i).setNumber(i + 1); // +
245 // 1
246 // for
247 // root
248 }
249 treeTable.updateUI();
250 }
251 }
252 });
253 simplePanel.add(removeIssue);
254 }
255
256 private void save() {
257 if (model.getUtilitySpace() != null) {
258 saveProfile();
259 } else {
260 model.getDomain().toXML().saveToFile(model.getDomain().getName());
261 }
262 }
263
264 private void saveProfile() {
265 double newDiscount = 1.0;
266 AdditiveUtilitySpace uspace;
267 // create the right new class, depending on uncertainty setting
268 if (uncertaintySettings.getIsEnabled().getValue()) {
269 int comps = uncertaintySettings.getComparisons().getValue();
270 int errs = uncertaintySettings.getErrors().getValue();
271 uspace = new UncertainAdditiveUtilitySpace(model.getUtilitySpace(),
272 comps, errs,
273 uncertaintySettings.getIsExperimental().getValue());
274 } else {
275 uspace = new AdditiveUtilitySpace(model.getUtilitySpace());
276 }
277
278 // FIXME GUI should not allow to set illegal values in first place.
279 try {
280 newDiscount = Double.parseDouble(discount.getText());
281 if (newDiscount < 0 || newDiscount > 1) {
282 showError("The discount value is not valid.");
283 return;
284 }
285 } catch (Exception e) {
286 showError("The discount value is not valid.");
287 return;
288 }
289 double newRV = 1.0;
290 try {
291 newRV = Double.parseDouble(reservationValue.getText());
292 if (newRV < 0 || newRV > 1) {
293 showError("The reservation value is not valid.");
294 return;
295 }
296 } catch (Exception e) {
297 showError("The reservation value is not valid.");
298 return;
299 }
300 uspace.setDiscount(newDiscount);
301 uspace.setReservationValue(newRV);
302 try {
303 uspace.toXML().saveToFile(model.getUtilitySpace().getFileName());
304 } catch (IOException e) {
305 showError("Something went wrong while saving:" + e.getMessage());
306 e.printStackTrace();
307 }
308 }
309
310 private void initTable(NegotiatorTreeTableModel model) {
311 treeTable = new JTreeTable(model);
312 treeTable.setPreferredSize(new Dimension(1024, 800));
313 treeTable.setPreferredScrollableViewportSize(new Dimension(1024, 300));
314 treeTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
315 treeTable.setRowSelectionAllowed(true);
316 treeTable.setColumnSelectionAllowed(false);
317 treeTable.setCellSelectionEnabled(true);
318
319 TableColumnModel colModel = treeTable.getColumnModel();
320 if (treeTable.getColumnCount() > 3)
321 colModel.getColumn(3).setMinWidth(220); // Wouter: make it likely
322 // that Weight column is
323 // shown completely.
324
325 DefaultTableCellRenderer labelRenderer = new JLabelCellRenderer();
326 treeTable.setDefaultRenderer(JLabel.class, labelRenderer);
327 treeTable.setDefaultRenderer(JTextField.class, labelRenderer);
328
329 IssueValueCellEditor valueEditor = new IssueValueCellEditor(model);
330 treeTable.setDefaultRenderer(IssueValuePanel.class, valueEditor);
331 treeTable.setDefaultEditor(IssueValuePanel.class, valueEditor);
332
333 WeightSliderCellEditor cellEditor = new WeightSliderCellEditor(model);
334 treeTable.setDefaultRenderer(WeightSlider.class, cellEditor);
335 treeTable.setDefaultEditor(WeightSlider.class, cellEditor);
336 treeTable.setRowHeight(24);
337
338 JScrollPane treePane = new JScrollPane(treeTable);
339 treePane.setBackground(treeTable.getBackground());
340 add(treePane, BorderLayout.CENTER);
341 }
342
343 private void initMenus() {
344 menuBar = new JMenuBar();
345 fileMenu = new JMenu("File");
346 editMenu = new JMenu("Edit");
347 menuBar.add(fileMenu);
348 menuBar.add(editMenu);
349 }
350
351}
Note: See TracBrowser for help on using the repository browser.