1 | package genius.gui.tree;
|
---|
2 |
|
---|
3 | import java.awt.BorderLayout;
|
---|
4 | import java.awt.Color;
|
---|
5 | import java.awt.Dimension;
|
---|
6 | import java.awt.event.MouseAdapter;
|
---|
7 | import java.awt.event.MouseEvent;
|
---|
8 | import java.io.IOException;
|
---|
9 | import java.util.Enumeration;
|
---|
10 |
|
---|
11 | import javax.swing.Icon;
|
---|
12 | import javax.swing.ImageIcon;
|
---|
13 | import javax.swing.JButton;
|
---|
14 | import javax.swing.JLabel;
|
---|
15 | import javax.swing.JMenu;
|
---|
16 | import javax.swing.JMenuBar;
|
---|
17 | import javax.swing.JOptionPane;
|
---|
18 | import javax.swing.JPanel;
|
---|
19 | import javax.swing.JScrollPane;
|
---|
20 | import javax.swing.JTextField;
|
---|
21 | import javax.swing.ListSelectionModel;
|
---|
22 | import javax.swing.table.DefaultTableCellRenderer;
|
---|
23 | import javax.swing.table.TableColumnModel;
|
---|
24 |
|
---|
25 | import genius.core.DomainImpl;
|
---|
26 | import genius.core.issue.Issue;
|
---|
27 | import genius.core.issue.Objective;
|
---|
28 | import genius.core.jtreetable.JTreeTable;
|
---|
29 | import genius.core.repository.DomainRepItem;
|
---|
30 | import genius.core.utility.AdditiveUtilitySpace;
|
---|
31 | import genius.gui.dialogs.EditIssueDialog;
|
---|
32 | import 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 | */
|
---|
39 | public 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 | uncertaintySettings = new UncertaintySettingsModel(
|
---|
88 | treeModel.getDomain().getNumberOfPossibleBids());
|
---|
89 | this.hasNoProfiles = hasNoProfiles;
|
---|
90 | init(treeModel, null);
|
---|
91 | }
|
---|
92 |
|
---|
93 | public void clearTreeTable(DomainImpl domain,
|
---|
94 | AdditiveUtilitySpace utilitySpace) {
|
---|
95 | init(new NegotiatorTreeTableModel(domain, utilitySpace),
|
---|
96 | this.getSize());
|
---|
97 | }
|
---|
98 |
|
---|
99 | public boolean isDomain() {
|
---|
100 | return model.getUtilitySpace() == null;
|
---|
101 | }
|
---|
102 |
|
---|
103 | private void init(NegotiatorTreeTableModel treeModel, Dimension size) {
|
---|
104 | final DomainAndProfileEditorPanel thisFrame = this;
|
---|
105 | model = treeModel;
|
---|
106 | setLayout(new BorderLayout());
|
---|
107 | // Initialize the table
|
---|
108 | initTable(model);
|
---|
109 | treeTable.addMouseListener(new MouseAdapter() {
|
---|
110 | @Override
|
---|
111 | public void mouseReleased(MouseEvent e) {
|
---|
112 |
|
---|
113 | if (e.getClickCount() == 2) {
|
---|
114 | Object selected = treeTable.getTree()
|
---|
115 | .getLastSelectedPathComponent();
|
---|
116 |
|
---|
117 | if (selected instanceof Issue) {
|
---|
118 | if (hasNoProfiles || !isDomain()) {
|
---|
119 | new EditIssueDialog(thisFrame, (Issue) selected);
|
---|
120 | } else {
|
---|
121 | JOptionPane.showMessageDialog(null,
|
---|
122 | "You may only edit the issues when there are no preference profiles.",
|
---|
123 | "Edit error", 0);
|
---|
124 | }
|
---|
125 | }
|
---|
126 | }
|
---|
127 | }
|
---|
128 | });
|
---|
129 | treeTable.setRowHeight(40);
|
---|
130 | // Initialize the Menu
|
---|
131 | initMenus();
|
---|
132 | JPanel bottomArea = new JPanel(new BorderLayout());
|
---|
133 |
|
---|
134 | bottomArea.add(new UncertaintySettingsPanel(uncertaintySettings),
|
---|
135 | BorderLayout.CENTER);
|
---|
136 | bottomArea.add(savePanel(), BorderLayout.SOUTH);
|
---|
137 |
|
---|
138 | add(bottomArea, BorderLayout.SOUTH);
|
---|
139 |
|
---|
140 | if (size != null)
|
---|
141 | this.setSize(size);
|
---|
142 |
|
---|
143 | }
|
---|
144 |
|
---|
145 | private JPanel savePanel() {
|
---|
146 | JButton saveButton = new JButton("Save changes");
|
---|
147 | Icon icon = new ImageIcon(getClass().getClassLoader()
|
---|
148 | .getResource("genius/gui/resources/save.png"));
|
---|
149 | saveButton.setPreferredSize(new Dimension(180, 60));
|
---|
150 | saveButton.setIcon(icon);
|
---|
151 | saveButton.setFont(saveButton.getFont().deriveFont(14.0f));
|
---|
152 | saveButton.addActionListener(new java.awt.event.ActionListener() {
|
---|
153 | @Override
|
---|
154 | public void actionPerformed(java.awt.event.ActionEvent evt) {
|
---|
155 | save();
|
---|
156 | }
|
---|
157 |
|
---|
158 | });
|
---|
159 |
|
---|
160 | JPanel simplePanel = new JPanel();
|
---|
161 | if (hasNoProfiles) {
|
---|
162 | addDomainEditButtons(simplePanel);
|
---|
163 | }
|
---|
164 | simplePanel.add(saveButton);
|
---|
165 |
|
---|
166 | if (model.getUtilitySpace() != null) {
|
---|
167 | addProfileEditButtons(simplePanel);
|
---|
168 | }
|
---|
169 | return simplePanel;
|
---|
170 | }
|
---|
171 |
|
---|
172 | private void addProfileEditButtons(JPanel simplePanel) {
|
---|
173 | simplePanel.add(new JLabel("Discount: "));
|
---|
174 | discount = new JTextField(
|
---|
175 | model.getUtilitySpace().getDiscountFactor() + "", 5);
|
---|
176 | simplePanel.add(discount);
|
---|
177 |
|
---|
178 | simplePanel.add(new JLabel("Reservation value: "));
|
---|
179 | reservationValue = new JTextField(
|
---|
180 | model.getUtilitySpace().getReservationValueUndiscounted() + "",
|
---|
181 | 5);
|
---|
182 | simplePanel.add(reservationValue);
|
---|
183 | }
|
---|
184 |
|
---|
185 | private void addDomainEditButtons(JPanel simplePanel) {
|
---|
186 | final DomainAndProfileEditorPanel thisFrame = this;
|
---|
187 | JButton addIssue = new JButton("Add issue");
|
---|
188 | Icon icon2 = new ImageIcon(getClass().getClassLoader()
|
---|
189 | .getResource("genius/gui/resources/edit_add-32.png"));
|
---|
190 | addIssue.setPreferredSize(new Dimension(180, 60));
|
---|
191 | addIssue.setIcon(icon2);
|
---|
192 | addIssue.setFont(addIssue.getFont().deriveFont(18.0f));
|
---|
193 | addIssue.addActionListener(new java.awt.event.ActionListener() {
|
---|
194 | @Override
|
---|
195 | public void actionPerformed(java.awt.event.ActionEvent evt) {
|
---|
196 | new NewIssueDialog(thisFrame);
|
---|
197 | }
|
---|
198 | });
|
---|
199 | simplePanel.add(addIssue);
|
---|
200 |
|
---|
201 | JButton removeIssue = new JButton("Remove issue");
|
---|
202 | Icon icon3 = new ImageIcon(getClass().getClassLoader()
|
---|
203 | .getResource("genius/gui/resources/edit_remove-32.png"));
|
---|
204 | removeIssue.setPreferredSize(new Dimension(180, 60));
|
---|
205 | removeIssue.setIcon(icon3);
|
---|
206 | removeIssue.setFont(removeIssue.getFont().deriveFont(18.0f));
|
---|
207 | removeIssue.addActionListener(new java.awt.event.ActionListener() {
|
---|
208 | @Override
|
---|
209 | public void actionPerformed(java.awt.event.ActionEvent evt) {
|
---|
210 | Object selected = treeTable.getTree()
|
---|
211 | .getLastSelectedPathComponent();
|
---|
212 |
|
---|
213 | if (selected instanceof Issue) {
|
---|
214 | ((Issue) selected).removeFromParent();
|
---|
215 | // correct numbering
|
---|
216 | for (int i = 0; i < model.getDomain().getIssues()
|
---|
217 | .size(); i++) {
|
---|
218 | model.getDomain().getIssues().get(i).setNumber(i + 1); // +
|
---|
219 | // 1
|
---|
220 | // for
|
---|
221 | // root
|
---|
222 | }
|
---|
223 | treeTable.updateUI();
|
---|
224 | }
|
---|
225 | }
|
---|
226 | });
|
---|
227 | simplePanel.add(removeIssue);
|
---|
228 | }
|
---|
229 |
|
---|
230 | private void save() {
|
---|
231 | if (model.getUtilitySpace() != null) {
|
---|
232 | double newDiscount = 1.0;
|
---|
233 | try {
|
---|
234 | newDiscount = Double.parseDouble(discount.getText());
|
---|
235 | if (newDiscount < 0 || newDiscount > 1) {
|
---|
236 | JOptionPane.showMessageDialog(null,
|
---|
237 | "The discount value is not valid.");
|
---|
238 | return;
|
---|
239 | }
|
---|
240 | } catch (Exception e) {
|
---|
241 | JOptionPane.showMessageDialog(null,
|
---|
242 | "The discount value is not valid.");
|
---|
243 | return;
|
---|
244 | }
|
---|
245 | double newRV = 1.0;
|
---|
246 | try {
|
---|
247 | newRV = Double.parseDouble(reservationValue.getText());
|
---|
248 | if (newRV < 0 || newRV > 1) {
|
---|
249 | JOptionPane.showMessageDialog(null,
|
---|
250 | "The reservation value is not valid.");
|
---|
251 | return;
|
---|
252 | }
|
---|
253 | } catch (Exception e) {
|
---|
254 | JOptionPane.showMessageDialog(null,
|
---|
255 | "The reservation value is not valid.");
|
---|
256 | return;
|
---|
257 | }
|
---|
258 | model.getUtilitySpace().setDiscount(newDiscount);
|
---|
259 | model.getUtilitySpace().setReservationValue(newRV);
|
---|
260 | try {
|
---|
261 | model.getUtilitySpace().toXML()
|
---|
262 | .saveToFile(model.getUtilitySpace().getFileName());
|
---|
263 | } catch (IOException e) {
|
---|
264 | e.printStackTrace();
|
---|
265 | }
|
---|
266 | } else { // this is a domain
|
---|
267 | model.getDomain().toXML().saveToFile(model.getDomain().getName());
|
---|
268 | }
|
---|
269 | }
|
---|
270 |
|
---|
271 | public boolean hasNoProfiles() {
|
---|
272 | return hasNoProfiles;
|
---|
273 | }
|
---|
274 |
|
---|
275 | private void initTable(NegotiatorTreeTableModel model) {
|
---|
276 | treeTable = new JTreeTable(model);
|
---|
277 | treeTable.setPreferredSize(new Dimension(1024, 800));
|
---|
278 | treeTable.setPreferredScrollableViewportSize(new Dimension(1024, 300));
|
---|
279 | treeTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
---|
280 | treeTable.setRowSelectionAllowed(true);
|
---|
281 | treeTable.setColumnSelectionAllowed(false);
|
---|
282 | treeTable.setCellSelectionEnabled(true);
|
---|
283 |
|
---|
284 | TableColumnModel colModel = treeTable.getColumnModel();
|
---|
285 | if (treeTable.getColumnCount() > 3)
|
---|
286 | colModel.getColumn(3).setMinWidth(220); // Wouter: make it likely
|
---|
287 | // that Weight column is
|
---|
288 | // shown completely.
|
---|
289 |
|
---|
290 | DefaultTableCellRenderer labelRenderer = new JLabelCellRenderer();
|
---|
291 | treeTable.setDefaultRenderer(JLabel.class, labelRenderer);
|
---|
292 | treeTable.setDefaultRenderer(JTextField.class, labelRenderer);
|
---|
293 |
|
---|
294 | IssueValueCellEditor valueEditor = new IssueValueCellEditor(model);
|
---|
295 | treeTable.setDefaultRenderer(IssueValuePanel.class, valueEditor);
|
---|
296 | treeTable.setDefaultEditor(IssueValuePanel.class, valueEditor);
|
---|
297 |
|
---|
298 | WeightSliderCellEditor cellEditor = new WeightSliderCellEditor(model);
|
---|
299 | treeTable.setDefaultRenderer(WeightSlider.class, cellEditor);
|
---|
300 | treeTable.setDefaultEditor(WeightSlider.class, cellEditor);
|
---|
301 | treeTable.setRowHeight(24);
|
---|
302 |
|
---|
303 | JScrollPane treePane = new JScrollPane(treeTable);
|
---|
304 | treePane.setBackground(treeTable.getBackground());
|
---|
305 | add(treePane, BorderLayout.CENTER);
|
---|
306 | }
|
---|
307 |
|
---|
308 | private void initMenus() {
|
---|
309 | menuBar = new JMenuBar();
|
---|
310 | fileMenu = new JMenu("File");
|
---|
311 | editMenu = new JMenu("Edit");
|
---|
312 | menuBar.add(fileMenu);
|
---|
313 | menuBar.add(editMenu);
|
---|
314 | }
|
---|
315 |
|
---|
316 | public JTreeTable getTreeTable() {
|
---|
317 | return treeTable;
|
---|
318 | }
|
---|
319 |
|
---|
320 | public NegotiatorTreeTableModel getNegotiatorTreeTableModel() {
|
---|
321 | return model;
|
---|
322 | }
|
---|
323 |
|
---|
324 | protected void updateHighlights(Objective selected) {
|
---|
325 | Objective parent = null;
|
---|
326 | if (selected != null) {
|
---|
327 | parent = selected.getParent();
|
---|
328 | }
|
---|
329 | Enumeration<Objective> treeEnum = ((Objective) model.getRoot())
|
---|
330 | .getPreorderEnumeration();
|
---|
331 | while (treeEnum.hasMoreElements()) {
|
---|
332 | Objective obj = treeEnum.nextElement();
|
---|
333 | if (selected == null || parent == null) {
|
---|
334 | setRowBackground(obj, UNSELECTED);
|
---|
335 | } else if (parent.isParent(obj)) {
|
---|
336 | setRowBackground(obj, HIGHLIGHT);
|
---|
337 | } else {
|
---|
338 | setRowBackground(obj, UNSELECTED);
|
---|
339 | }
|
---|
340 | }
|
---|
341 | }
|
---|
342 |
|
---|
343 | public Objective getRoot() {
|
---|
344 | return (Objective) model.getRoot();
|
---|
345 | }
|
---|
346 |
|
---|
347 | protected void setRowBackground(Objective node, Color color) {
|
---|
348 | model.getNameField(node).setBackground(color);
|
---|
349 | model.getTypeField(node).setBackground(color);
|
---|
350 | model.getNumberField(node).setBackground(color);
|
---|
351 | model.getIssueValuePanel(node).setBackground(color);
|
---|
352 | }
|
---|
353 |
|
---|
354 | public DomainRepItem getDomainRepItem() {
|
---|
355 | return fDomainRepItem;
|
---|
356 | }
|
---|
357 | } |
---|