source: src/main/java/genius/gui/dialogs/NewObjectiveDialog.java

Last change on this file was 83, checked in by Wouter Pasman, 6 years ago

#28 added first version of uncertainty panel. Not yet attached to save

File size: 5.6 KB
Line 
1package genius.gui.dialogs;
2
3import java.awt.BorderLayout;
4import java.awt.Component;
5import java.awt.event.ActionEvent;
6import java.awt.event.ActionListener;
7
8import javax.swing.BorderFactory;
9import javax.swing.BoxLayout;
10import javax.swing.JButton;
11import javax.swing.JDialog;
12import javax.swing.JLabel;
13import javax.swing.JOptionPane;
14import javax.swing.JPanel;
15import javax.swing.JTextField;
16
17import genius.core.issue.Objective;
18import genius.core.utility.AdditiveUtilitySpace;
19import genius.core.utility.EvaluatorObjective;
20import genius.gui.tree.NegotiatorTreeTableModel;
21import genius.gui.tree.DomainAndProfileEditorPanel;
22
23/**
24 * A dialog allowing the user to add a new Objective
25 */
26
27public class NewObjectiveDialog extends JDialog implements ActionListener {
28
29 private static final long serialVersionUID = 4665460273597895313L;
30
31 protected JButton okButton;
32 protected JButton cancelButton;
33 protected JLabel nameLabel;
34 protected JLabel numberLabel;
35 protected JTextField nameField;
36 protected JTextField numberField;
37 protected DomainAndProfileEditorPanel treeFrame;
38
39 public NewObjectiveDialog(DomainAndProfileEditorPanel owner) {
40 this(owner, false);
41 }
42
43 /**
44 *
45 * @param owner
46 * @param modal
47 * true if multiple dialogs can be open at once, false if not.
48 */
49 public NewObjectiveDialog(DomainAndProfileEditorPanel owner, boolean modal) {
50 this(owner, modal, "Create new Objective");
51 }
52
53 public NewObjectiveDialog(DomainAndProfileEditorPanel owner, boolean modal, String name) {
54 super();
55 this.treeFrame = owner;
56
57 initPanels();
58
59 this.pack();
60 this.setVisible(true);
61 }
62
63 protected void initPanels() {
64 this.setLayout(new BorderLayout());
65
66 this.add(constructBasicPropertyPanel(), BorderLayout.NORTH);
67 this.add(constructButtonPanel(), BorderLayout.SOUTH);
68 }
69
70 private JPanel constructBasicPropertyPanel() {
71 // Initialize the labels
72 nameLabel = new JLabel("Name:");
73 nameLabel.setAlignmentX(Component.RIGHT_ALIGNMENT);
74 numberLabel = new JLabel("Number:");
75 numberLabel.setAlignmentX(Component.RIGHT_ALIGNMENT);
76
77 // Initialize the fields
78 nameField = new JTextField();
79 nameField.setAlignmentX(Component.LEFT_ALIGNMENT);
80 numberField = new JTextField();
81 numberField.setAlignmentX(Component.LEFT_ALIGNMENT);
82
83 JPanel labelPanel = new JPanel();
84 labelPanel.setLayout(new BoxLayout(labelPanel, BoxLayout.PAGE_AXIS));
85
86 labelPanel.add(new JLabel("Name:"));
87
88 JPanel fieldPanel = new JPanel();
89 fieldPanel.setLayout(new BoxLayout(fieldPanel, BoxLayout.PAGE_AXIS));
90
91 fieldPanel.add(nameField);
92
93 JPanel basicPropertyPanel = new JPanel();
94 basicPropertyPanel.setBorder(
95 BorderFactory.createTitledBorder("Basic Properties"));
96 basicPropertyPanel.setLayout(new BorderLayout());
97 basicPropertyPanel.add(labelPanel, BorderLayout.LINE_START);
98 basicPropertyPanel.add(fieldPanel, BorderLayout.CENTER);
99
100 return basicPropertyPanel;
101 }
102
103 /**
104 * Initializes the buttons, and returns a panel containing them.
105 *
106 * @return a JPanel with the buttons.
107 */
108 private JPanel constructButtonPanel() {
109 // Initialize the buttons
110 okButton = new JButton("Ok");
111 okButton.addActionListener(this);
112 cancelButton = new JButton("Cancel");
113 cancelButton.addActionListener(this);
114
115 JPanel buttonPanel = new JPanel();
116 buttonPanel.add(okButton);
117 buttonPanel.add(cancelButton);
118
119 return buttonPanel;
120 }
121
122 protected String getObjectiveName() throws InvalidInputException {
123 return nameField.getText();
124 }
125
126 protected int getObjectiveNumber() throws InvalidInputException {
127 return (((NegotiatorTreeTableModel) treeFrame.getTreeTable().getTree()
128 .getModel()).getHighestObjectiveNr() + 1);
129 }
130
131 protected String getObjectiveDescription() throws InvalidInputException {
132 return "";
133 }
134
135 // overridden by issues
136 protected boolean getWeightCheck() {
137 return false;
138 }
139
140 protected Objective constructObjective() {
141 String name;
142 int number;
143 Objective selected; // The Objective that is seleced in the tree, which
144 // will be the new Objective's parent.
145 try {
146 name = getObjectiveName();
147 number = treeFrame.getNegotiatorTreeTableModel()
148 .getHighestObjectiveNr() + 1;
149 } catch (InvalidInputException e) {
150 JOptionPane.showMessageDialog(this, e.getMessage());
151 return null;
152 }
153 try {
154 selected = (Objective) treeFrame.getTreeTable().getTree()
155 .getLastSelectedPathComponent();
156 if (selected == null) {
157 JOptionPane.showMessageDialog(this,
158 "There is no valid parent selected for this objective.");
159 return null;
160 }
161 } catch (Exception e) {
162 JOptionPane.showMessageDialog(this,
163 "There is no valid parent selected for this objective.");
164 return null;
165 }
166 Objective objective = new Objective(selected, name, number);
167
168 selected.addChild(objective);
169 AdditiveUtilitySpace utilspace = treeFrame.getNegotiatorTreeTableModel()
170 .getUtilitySpace();
171 if (utilspace != null) {
172 EvaluatorObjective ev = new EvaluatorObjective();
173 ev.setHasWeight(getWeightCheck());
174 utilspace.addEvaluator(objective, ev);
175 }
176
177 return objective;
178 }
179
180 @Override
181 public void actionPerformed(ActionEvent e) {
182 if (e.getSource() == okButton) {
183 Objective objective = constructObjective();
184 if (objective == null)
185 return;
186
187 NegotiatorTreeTableModel model = treeFrame
188 .getNegotiatorTreeTableModel();
189 model.treeStructureChanged(this, treeFrame.getTreeTable().getTree()
190 .getSelectionPath().getPath());
191
192 this.dispose();
193
194 } else if (e.getSource() == cancelButton) {
195 this.dispose();
196 }
197 }
198
199 protected class InvalidInputException extends Exception {
200
201 private static final long serialVersionUID = 866805428763450366L;
202
203 protected InvalidInputException() {
204 super();
205 }
206
207 protected InvalidInputException(String message) {
208 super(message);
209 }
210 }
211}
Note: See TracBrowser for help on using the repository browser.