[1] | 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 | * Frame from a domain.
|
---|
| 36 | *
|
---|
| 37 | * @author Wouter Pasman, Mark Hendrikx
|
---|
| 38 | */
|
---|
| 39 | public class TreeFrame 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 | // as we can't use this in a listener
|
---|
| 52 | private TreeFrame thisFrame;
|
---|
| 53 | private boolean hasNoProfiles;
|
---|
| 54 | private JTextField discount;
|
---|
| 55 | private JTextField reservationValue;
|
---|
| 56 |
|
---|
| 57 | // Constructors
|
---|
| 58 | public TreeFrame(DomainImpl domain, boolean hasNoProfiles) {
|
---|
| 59 | this(new NegotiatorTreeTableModel(domain), hasNoProfiles);
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | public TreeFrame(DomainImpl domain, AdditiveUtilitySpace utilitySpace) {
|
---|
| 63 | this(new NegotiatorTreeTableModel(domain, utilitySpace), false);
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | public TreeFrame(NegotiatorTreeTableModel treeModel, boolean hasNoProfiles) {
|
---|
| 67 | super();
|
---|
| 68 | this.hasNoProfiles = hasNoProfiles;
|
---|
| 69 | init(treeModel, null);
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | public void clearTreeTable(DomainImpl domain,
|
---|
| 73 | AdditiveUtilitySpace utilitySpace) {
|
---|
| 74 | init(new NegotiatorTreeTableModel(domain, utilitySpace), this.getSize());
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | public boolean isDomain() {
|
---|
| 78 | return model.getUtilitySpace() == null;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | private void init(NegotiatorTreeTableModel treeModel, Dimension size) {
|
---|
| 82 | thisFrame = this;
|
---|
| 83 | model = treeModel;
|
---|
| 84 | setLayout(new BorderLayout());
|
---|
| 85 | // Initialize the table
|
---|
| 86 | initTable(model);
|
---|
| 87 | treeTable.addMouseListener(new MouseAdapter() {
|
---|
| 88 | @Override
|
---|
| 89 | public void mouseReleased(MouseEvent e) {
|
---|
| 90 |
|
---|
| 91 | if (e.getClickCount() == 2) {
|
---|
| 92 | Object selected = treeTable.getTree()
|
---|
| 93 | .getLastSelectedPathComponent();
|
---|
| 94 |
|
---|
| 95 | if (selected instanceof Issue) {
|
---|
| 96 | if (hasNoProfiles || !isDomain()) {
|
---|
| 97 | new EditIssueDialog(thisFrame, (Issue) selected);
|
---|
| 98 | } else {
|
---|
| 99 | JOptionPane
|
---|
| 100 | .showMessageDialog(
|
---|
| 101 | null,
|
---|
| 102 | "You may only edit the issues when there are no preference profiles.",
|
---|
| 103 | "Edit error", 0);
|
---|
| 104 | }
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
| 108 | });
|
---|
| 109 | treeTable.setRowHeight(40);
|
---|
| 110 | // Initialize the Menu
|
---|
| 111 | initMenus();
|
---|
| 112 | JPanel simplePanel = new JPanel();
|
---|
| 113 |
|
---|
| 114 | JButton saveButton = new JButton("Save changes");
|
---|
| 115 | Icon icon = new ImageIcon(getClass().getClassLoader().getResource(
|
---|
| 116 | "genius/gui/resources/save.png"));
|
---|
| 117 | saveButton.setPreferredSize(new Dimension(180, 60));
|
---|
| 118 | saveButton.setIcon(icon);
|
---|
| 119 | saveButton.setFont(saveButton.getFont().deriveFont(18.0f));
|
---|
| 120 | saveButton.addActionListener(new java.awt.event.ActionListener() {
|
---|
| 121 | public void actionPerformed(java.awt.event.ActionEvent evt) {
|
---|
| 122 | if (model.getUtilitySpace() != null) {
|
---|
| 123 | double newDiscount = 1.0;
|
---|
| 124 | try {
|
---|
| 125 | newDiscount = Double.parseDouble(discount.getText());
|
---|
| 126 | if (newDiscount < 0 || newDiscount > 1) {
|
---|
| 127 | JOptionPane.showMessageDialog(null,
|
---|
| 128 | "The discount value is not valid.");
|
---|
| 129 | return;
|
---|
| 130 | }
|
---|
| 131 | } catch (Exception e) {
|
---|
| 132 | JOptionPane.showMessageDialog(null,
|
---|
| 133 | "The discount value is not valid.");
|
---|
| 134 | return;
|
---|
| 135 | }
|
---|
| 136 | double newRV = 1.0;
|
---|
| 137 | try {
|
---|
| 138 | newRV = Double.parseDouble(reservationValue.getText());
|
---|
| 139 | if (newRV < 0 || newRV > 1) {
|
---|
| 140 | JOptionPane.showMessageDialog(null,
|
---|
| 141 | "The reservation value is not valid.");
|
---|
| 142 | return;
|
---|
| 143 | }
|
---|
| 144 | } catch (Exception e) {
|
---|
| 145 | JOptionPane.showMessageDialog(null,
|
---|
| 146 | "The reservation value is not valid.");
|
---|
| 147 | return;
|
---|
| 148 | }
|
---|
| 149 | model.getUtilitySpace().setDiscount(newDiscount);
|
---|
| 150 | model.getUtilitySpace().setReservationValue(newRV);
|
---|
| 151 | try {
|
---|
| 152 | model.getUtilitySpace()
|
---|
| 153 | .toXML()
|
---|
| 154 | .saveToFile(
|
---|
| 155 | model.getUtilitySpace().getFileName());
|
---|
| 156 | } catch (IOException e) {
|
---|
| 157 | e.printStackTrace();
|
---|
| 158 | }
|
---|
| 159 | } else { // this is a domain
|
---|
| 160 | model.getDomain().toXML()
|
---|
| 161 | .saveToFile(model.getDomain().getName());
|
---|
| 162 | }
|
---|
| 163 | }
|
---|
| 164 | });
|
---|
| 165 |
|
---|
| 166 | if (hasNoProfiles) {
|
---|
| 167 |
|
---|
| 168 | JButton addIssue = new JButton("Add issue");
|
---|
| 169 | Icon icon2 = new ImageIcon(getClass().getClassLoader().getResource(
|
---|
| 170 | "genius/gui/resources/edit_add-32.png"));
|
---|
| 171 | addIssue.setPreferredSize(new Dimension(180, 60));
|
---|
| 172 | addIssue.setIcon(icon2);
|
---|
| 173 | addIssue.setFont(addIssue.getFont().deriveFont(18.0f));
|
---|
| 174 | addIssue.addActionListener(new java.awt.event.ActionListener() {
|
---|
| 175 | public void actionPerformed(java.awt.event.ActionEvent evt) {
|
---|
| 176 | new NewIssueDialog(thisFrame);
|
---|
| 177 | }
|
---|
| 178 | });
|
---|
| 179 | simplePanel.add(addIssue);
|
---|
| 180 |
|
---|
| 181 | JButton removeIssue = new JButton("Remove issue");
|
---|
| 182 | Icon icon3 = new ImageIcon(getClass().getClassLoader().getResource(
|
---|
| 183 | "genius/gui/resources/edit_remove-32.png"));
|
---|
| 184 | removeIssue.setPreferredSize(new Dimension(180, 60));
|
---|
| 185 | removeIssue.setIcon(icon3);
|
---|
| 186 | removeIssue.setFont(removeIssue.getFont().deriveFont(18.0f));
|
---|
| 187 | removeIssue.addActionListener(new java.awt.event.ActionListener() {
|
---|
| 188 | public void actionPerformed(java.awt.event.ActionEvent evt) {
|
---|
| 189 | Object selected = treeTable.getTree()
|
---|
| 190 | .getLastSelectedPathComponent();
|
---|
| 191 |
|
---|
| 192 | if (selected instanceof Issue) {
|
---|
| 193 | ((Issue) selected).removeFromParent();
|
---|
| 194 | // correct numbering
|
---|
| 195 | for (int i = 0; i < model.getDomain().getIssues()
|
---|
| 196 | .size(); i++) {
|
---|
| 197 | model.getDomain().getIssues().get(i)
|
---|
| 198 | .setNumber(i + 1); // + 1 for root
|
---|
| 199 | }
|
---|
| 200 | treeTable.updateUI();
|
---|
| 201 | }
|
---|
| 202 | }
|
---|
| 203 | });
|
---|
| 204 | simplePanel.add(removeIssue);
|
---|
| 205 | }
|
---|
| 206 | simplePanel.add(saveButton);
|
---|
| 207 |
|
---|
| 208 | if (model.getUtilitySpace() != null) {
|
---|
| 209 | simplePanel.add(new JLabel("Discount: "));
|
---|
| 210 | discount = new JTextField(model.getUtilitySpace()
|
---|
| 211 | .getDiscountFactor() + "", 5);
|
---|
| 212 | simplePanel.add(discount);
|
---|
| 213 |
|
---|
| 214 | simplePanel.add(new JLabel("Reservation value: "));
|
---|
| 215 | reservationValue = new JTextField(model.getUtilitySpace()
|
---|
| 216 | .getReservationValueUndiscounted() + "", 5);
|
---|
| 217 | simplePanel.add(reservationValue);
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | add(simplePanel, BorderLayout.SOUTH);
|
---|
| 221 |
|
---|
| 222 | if (size != null)
|
---|
| 223 | this.setSize(size);
|
---|
| 224 |
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | public boolean hasNoProfiles() {
|
---|
| 228 | return hasNoProfiles;
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | private void initTable(NegotiatorTreeTableModel model) {
|
---|
| 232 | treeTable = new JTreeTable(model);
|
---|
| 233 | treeTable.setPreferredSize(new Dimension(1024, 800));
|
---|
| 234 | treeTable.setPreferredScrollableViewportSize(new Dimension(1024, 300));
|
---|
| 235 | treeTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
---|
| 236 | treeTable.setRowSelectionAllowed(true);
|
---|
| 237 | treeTable.setColumnSelectionAllowed(false);
|
---|
| 238 | treeTable.setCellSelectionEnabled(true);
|
---|
| 239 |
|
---|
| 240 | TableColumnModel colModel = treeTable.getColumnModel();
|
---|
| 241 | if (treeTable.getColumnCount() > 3)
|
---|
| 242 | colModel.getColumn(3).setMinWidth(220); // Wouter: make it likely
|
---|
| 243 | // that Weight column is
|
---|
| 244 | // shown completely.
|
---|
| 245 |
|
---|
| 246 | DefaultTableCellRenderer labelRenderer = new JLabelCellRenderer();
|
---|
| 247 | treeTable.setDefaultRenderer(JLabel.class, labelRenderer);
|
---|
| 248 | treeTable.setDefaultRenderer(JTextField.class, labelRenderer);
|
---|
| 249 |
|
---|
| 250 | IssueValueCellEditor valueEditor = new IssueValueCellEditor(model);
|
---|
| 251 | treeTable.setDefaultRenderer(IssueValuePanel.class, valueEditor);
|
---|
| 252 | treeTable.setDefaultEditor(IssueValuePanel.class, valueEditor);
|
---|
| 253 |
|
---|
| 254 | WeightSliderCellEditor cellEditor = new WeightSliderCellEditor(model);
|
---|
| 255 | treeTable.setDefaultRenderer(WeightSlider.class, cellEditor);
|
---|
| 256 | treeTable.setDefaultEditor(WeightSlider.class, cellEditor);
|
---|
| 257 | treeTable.setRowHeight(24);
|
---|
| 258 |
|
---|
| 259 | JScrollPane treePane = new JScrollPane(treeTable);
|
---|
| 260 | treePane.setBackground(treeTable.getBackground());
|
---|
| 261 | add(treePane, BorderLayout.CENTER);
|
---|
| 262 | }
|
---|
| 263 |
|
---|
| 264 | private void initMenus() {
|
---|
| 265 | menuBar = new JMenuBar();
|
---|
| 266 | fileMenu = new JMenu("File");
|
---|
| 267 | editMenu = new JMenu("Edit");
|
---|
| 268 | menuBar.add(fileMenu);
|
---|
| 269 | menuBar.add(editMenu);
|
---|
| 270 | }
|
---|
| 271 |
|
---|
| 272 | public JTreeTable getTreeTable() {
|
---|
| 273 | return treeTable;
|
---|
| 274 | }
|
---|
| 275 |
|
---|
| 276 | public NegotiatorTreeTableModel getNegotiatorTreeTableModel() {
|
---|
| 277 | return model;
|
---|
| 278 | }
|
---|
| 279 |
|
---|
| 280 | protected void updateHighlights(Objective selected) {
|
---|
| 281 | Objective parent = null;
|
---|
| 282 | if (selected != null) {
|
---|
| 283 | parent = selected.getParent();
|
---|
| 284 | }
|
---|
| 285 | Enumeration<Objective> treeEnum = ((Objective) model.getRoot())
|
---|
| 286 | .getPreorderEnumeration();
|
---|
| 287 | while (treeEnum.hasMoreElements()) {
|
---|
| 288 | Objective obj = treeEnum.nextElement();
|
---|
| 289 | if (selected == null || parent == null) {
|
---|
| 290 | setRowBackground(obj, UNSELECTED);
|
---|
| 291 | } else if (parent.isParent(obj)) {
|
---|
| 292 | setRowBackground(obj, HIGHLIGHT);
|
---|
| 293 | } else {
|
---|
| 294 | setRowBackground(obj, UNSELECTED);
|
---|
| 295 | }
|
---|
| 296 | }
|
---|
| 297 | }
|
---|
| 298 |
|
---|
| 299 | public Objective getRoot() {
|
---|
| 300 | return (Objective) model.getRoot();
|
---|
| 301 | }
|
---|
| 302 |
|
---|
| 303 | protected void setRowBackground(Objective node, Color color) {
|
---|
| 304 | model.getNameField(node).setBackground(color);
|
---|
| 305 | model.getTypeField(node).setBackground(color);
|
---|
| 306 | model.getNumberField(node).setBackground(color);
|
---|
| 307 | model.getIssueValuePanel(node).setBackground(color);
|
---|
| 308 | }
|
---|
| 309 |
|
---|
| 310 | public DomainRepItem getDomainRepItem() {
|
---|
| 311 | return fDomainRepItem;
|
---|
| 312 | }
|
---|
| 313 | } |
---|