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

Last change on this file since 209 was 155, checked in by Tim Baarslag, 6 years ago

Fixed refactor TODO of createExperimentalOutcomeComparisonUserModel

Made fixed seed a checkbox

User guide fixes

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