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

Last change on this file since 346 was 227, checked in by Tim Baarslag, 5 years ago

Added elicitationCost GUI, using DoubleModel

File size: 11.0 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 double eli = uncertaintySettings.getElicitationCost().getValue();
272 boolean fixedseed = uncertaintySettings.getIsFixedSeed().getValue();
273 uspace = new UncertainAdditiveUtilitySpace(model.getUtilitySpace(),
274 comps, errs, eli, fixedseed,
275 uncertaintySettings.getIsExperimental().getValue());
276 } else {
277 uspace = new AdditiveUtilitySpace(model.getUtilitySpace());
278 }
279
280 // FIXME GUI should not allow to set illegal values in first place.
281 try {
282 newDiscount = Double.parseDouble(discount.getText());
283 if (newDiscount < 0 || newDiscount > 1) {
284 showError("The discount value is not valid.");
285 return;
286 }
287 } catch (Exception e) {
288 showError("The discount value is not valid.");
289 return;
290 }
291 double newRV = 1.0;
292 try {
293 newRV = Double.parseDouble(reservationValue.getText());
294 if (newRV < 0 || newRV > 1) {
295 showError("The reservation value is not valid.");
296 return;
297 }
298 } catch (Exception e) {
299 showError("The reservation value is not valid.");
300 return;
301 }
302 uspace.setDiscount(newDiscount);
303 uspace.setReservationValue(newRV);
304 try {
305 uspace.toXML().saveToFile(model.getUtilitySpace().getFileName());
306 } catch (IOException e) {
307 showError("Something went wrong while saving:" + e.getMessage());
308 e.printStackTrace();
309 }
310 }
311
312 private void initTable(NegotiatorTreeTableModel model) {
313 treeTable = new JTreeTable(model);
314 treeTable.setPreferredSize(new Dimension(1024, 800));
315 treeTable.setPreferredScrollableViewportSize(new Dimension(1024, 300));
316 treeTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
317 treeTable.setRowSelectionAllowed(true);
318 treeTable.setColumnSelectionAllowed(false);
319 treeTable.setCellSelectionEnabled(true);
320
321 TableColumnModel colModel = treeTable.getColumnModel();
322 if (treeTable.getColumnCount() > 3)
323 colModel.getColumn(3).setMinWidth(220); // Wouter: make it likely
324 // that Weight column is
325 // shown completely.
326
327 DefaultTableCellRenderer labelRenderer = new JLabelCellRenderer();
328 treeTable.setDefaultRenderer(JLabel.class, labelRenderer);
329 treeTable.setDefaultRenderer(JTextField.class, labelRenderer);
330
331 IssueValueCellEditor valueEditor = new IssueValueCellEditor(model);
332 treeTable.setDefaultRenderer(IssueValuePanel.class, valueEditor);
333 treeTable.setDefaultEditor(IssueValuePanel.class, valueEditor);
334
335 WeightSliderCellEditor cellEditor = new WeightSliderCellEditor(model);
336 treeTable.setDefaultRenderer(WeightSlider.class, cellEditor);
337 treeTable.setDefaultEditor(WeightSlider.class, cellEditor);
338 treeTable.setRowHeight(24);
339
340 JScrollPane treePane = new JScrollPane(treeTable);
341 treePane.setBackground(treeTable.getBackground());
342 add(treePane, BorderLayout.CENTER);
343 }
344
345 private void initMenus() {
346 menuBar = new JMenuBar();
347 fileMenu = new JMenu("File");
348 editMenu = new JMenu("Edit");
349 menuBar.add(fileMenu);
350 menuBar.add(editMenu);
351 }
352
353}
Note: See TracBrowser for help on using the repository browser.