1 | package de.ugoe.cs.eventbench.swing;
|
---|
2 |
|
---|
3 | import java.awt.BorderLayout;
|
---|
4 | import java.util.ArrayList;
|
---|
5 | import java.util.List;
|
---|
6 |
|
---|
7 | import de.ugoe.cs.eventbench.assertions.AssertEvent;
|
---|
8 | import de.ugoe.cs.eventbench.assertions.FileEqualsReplay;
|
---|
9 | import de.ugoe.cs.eventbench.assertions.TextEqualsReplay;
|
---|
10 | import de.ugoe.cs.eventbench.data.Event;
|
---|
11 | import de.ugoe.cs.eventbench.data.GlobalDataContainer;
|
---|
12 | import de.ugoe.cs.util.console.Console;
|
---|
13 |
|
---|
14 | import javax.swing.JButton;
|
---|
15 | import javax.swing.JDialog;
|
---|
16 | import javax.swing.JPanel;
|
---|
17 | import javax.swing.border.EmptyBorder;
|
---|
18 | import javax.swing.JComboBox;
|
---|
19 | import javax.swing.JLabel;
|
---|
20 | import javax.swing.JTextField;
|
---|
21 | import javax.swing.DefaultComboBoxModel;
|
---|
22 | import javax.swing.JOptionPane;
|
---|
23 | import javax.swing.JFileChooser;
|
---|
24 | import java.awt.event.MouseAdapter;
|
---|
25 | import java.awt.event.MouseEvent;
|
---|
26 | import java.io.File;
|
---|
27 | import java.awt.event.ActionListener;
|
---|
28 | import java.awt.event.ActionEvent;
|
---|
29 | import javax.swing.JScrollPane;
|
---|
30 | import javax.swing.JList;
|
---|
31 | import javax.swing.border.EtchedBorder;
|
---|
32 | import javax.swing.JTree;
|
---|
33 | import javax.swing.tree.DefaultTreeModel;
|
---|
34 | import javax.swing.tree.DefaultMutableTreeNode;
|
---|
35 |
|
---|
36 | public class DlgInsert extends JDialog {
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Id for object serialization.
|
---|
40 | */
|
---|
41 | private static final long serialVersionUID = 1L;
|
---|
42 |
|
---|
43 | private final JPanel contentPanel = new JPanel();
|
---|
44 | private JTextField textFieldExpectedValue;
|
---|
45 | private JTextField textFieldActualFile;
|
---|
46 | private JTextField textFieldExpectedFile;
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Launch the application.
|
---|
50 | */
|
---|
51 | public static void showDialog(List<Event<?>> sequences, int selectedIndex,
|
---|
52 | final boolean insertBefore) {
|
---|
53 | try {
|
---|
54 | DlgInsert dialog = new DlgInsert(sequences, selectedIndex,
|
---|
55 | insertBefore);
|
---|
56 | dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
|
---|
57 | dialog.setVisible(true);
|
---|
58 | } catch (Exception e) {
|
---|
59 | e.printStackTrace();
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Create the dialog.
|
---|
65 | */
|
---|
66 | @SuppressWarnings("unchecked")
|
---|
67 | public DlgInsert(final List<Event<?>> sequences, final int selectedIndex,
|
---|
68 | final boolean insertBefore) {
|
---|
69 |
|
---|
70 | final javax.swing.DefaultListModel modelListTargets = new javax.swing.DefaultListModel();
|
---|
71 |
|
---|
72 | setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
|
---|
73 | setTitle("Insert testcase");
|
---|
74 |
|
---|
75 | setModal(true);
|
---|
76 | setBounds(100, 100, 530, 631);
|
---|
77 | getContentPane().setLayout(new BorderLayout());
|
---|
78 | contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
|
---|
79 | getContentPane().add(contentPanel, BorderLayout.CENTER);
|
---|
80 | contentPanel.setLayout(null);
|
---|
81 | final JComboBox comboBoxTestcase = new JComboBox();
|
---|
82 | final JPanel panelTextEquals = new JPanel();
|
---|
83 | panelTextEquals.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
|
---|
84 | final JPanel panelFileEquals = new JPanel();
|
---|
85 | panelFileEquals.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
|
---|
86 |
|
---|
87 | // JComboBox: comboBoxTestcase
|
---|
88 | comboBoxTestcase.addActionListener(new ActionListener() {
|
---|
89 | public void actionPerformed(ActionEvent arg0) {
|
---|
90 | if ("comboBoxChanged".equals(arg0.getActionCommand())) {
|
---|
91 |
|
---|
92 | // TextEquals
|
---|
93 | if (comboBoxTestcase.getSelectedIndex() == 0) {
|
---|
94 | panelTextEquals.setVisible(true);
|
---|
95 | panelFileEquals.setVisible(false);
|
---|
96 | }
|
---|
97 | // FileEquals
|
---|
98 | else if (comboBoxTestcase.getSelectedIndex() == 1) {
|
---|
99 | panelTextEquals.setVisible(false);
|
---|
100 | panelFileEquals.setVisible(true);
|
---|
101 | }
|
---|
102 | }
|
---|
103 | }
|
---|
104 | });
|
---|
105 | comboBoxTestcase.setModel(new DefaultComboBoxModel(new String[] {
|
---|
106 | "TextEquals", "OutputFileEquals" }));
|
---|
107 | comboBoxTestcase.setSelectedIndex(0);
|
---|
108 | comboBoxTestcase.setBounds(91, 11, 178, 20);
|
---|
109 | contentPanel.add(comboBoxTestcase);
|
---|
110 | // ***
|
---|
111 |
|
---|
112 | // JLabel
|
---|
113 | JLabel label = new JLabel("Testcase:");
|
---|
114 | label.setBounds(12, 14, 86, 14);
|
---|
115 | contentPanel.add(label);
|
---|
116 |
|
---|
117 | JLabel label_1 = new JLabel("Expected value:");
|
---|
118 | label_1.setBounds(10, 11, 96, 14);
|
---|
119 |
|
---|
120 | JLabel label_2 = new JLabel("Target:");
|
---|
121 | label_2.setBounds(10, 38, 86, 14);
|
---|
122 | // ***
|
---|
123 |
|
---|
124 | // JTextField: textFieldExpectedValue
|
---|
125 | textFieldExpectedValue = new JTextField();
|
---|
126 | textFieldExpectedValue.setColumns(10);
|
---|
127 | textFieldExpectedValue.setBounds(116, 8, 368, 20);
|
---|
128 | // ***
|
---|
129 |
|
---|
130 | // JPanel: panel
|
---|
131 | panelTextEquals.setLayout(null);
|
---|
132 | panelTextEquals.setBounds(10, 39, 494, 350);
|
---|
133 | contentPanel.add(panelTextEquals);
|
---|
134 | panelTextEquals.add(label_1);
|
---|
135 | panelTextEquals.add(label_2);
|
---|
136 | panelTextEquals.add(textFieldExpectedValue);
|
---|
137 |
|
---|
138 | JScrollPane scrollPane = new JScrollPane();
|
---|
139 | scrollPane.setBounds(421, 36, 63, 14);
|
---|
140 | panelTextEquals.add(scrollPane);
|
---|
141 |
|
---|
142 | final JList listTargets = new JList(modelListTargets);
|
---|
143 | scrollPane.setViewportView(listTargets);
|
---|
144 |
|
---|
145 | JScrollPane scrollPane_1 = new JScrollPane();
|
---|
146 | scrollPane_1.setBounds(10, 63, 474, 276);
|
---|
147 | panelTextEquals.add(scrollPane_1);
|
---|
148 |
|
---|
149 | //JTree to hold the targets
|
---|
150 | JTree tree = new JTree();
|
---|
151 | tree.setModel(new DefaultTreeModel(
|
---|
152 | new DefaultMutableTreeNode("Targets") {
|
---|
153 | {
|
---|
154 | List<DefaultMutableTreeNode> nodes = new ArrayList<DefaultMutableTreeNode>();
|
---|
155 | List<String> listTargets = new ArrayList<String>();
|
---|
156 | List<String> sortedTargets = new ArrayList<String>();
|
---|
157 |
|
---|
158 | try {
|
---|
159 | listTargets = (List<String>) GlobalDataContainer.getInstance().getData("ListTargets");
|
---|
160 | }
|
---|
161 | catch (ClassCastException e) {
|
---|
162 | Console.println("Not able to cast data in GlobalDataContainer to List of Strings");
|
---|
163 | }
|
---|
164 |
|
---|
165 | int parts = 1;
|
---|
166 | while(sortedTargets.size() < listTargets.size()) {
|
---|
167 | for(int i=0; i<listTargets.size(); i++) {
|
---|
168 | String splitted[] = listTargets.get(i).split("/>");
|
---|
169 | if(splitted.length == parts) {
|
---|
170 | sortedTargets.add(listTargets.get(i));
|
---|
171 | modelListTargets.addElement(listTargets.get(i));
|
---|
172 |
|
---|
173 | //insert in tree
|
---|
174 | boolean inserted = false;
|
---|
175 | for(int j=parts-2; j>=0 && !inserted; j--) {
|
---|
176 | splitted[j] = splitted[j] + "/>";
|
---|
177 | for(int k=0; k<nodes.size(); k++) {
|
---|
178 | if(nodes.get(k).toString().compareTo(splitted[j]) == 0) {
|
---|
179 | DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(splitted[j+1]);
|
---|
180 | nodes.add(newNode);
|
---|
181 | nodes.get(k).add(newNode);
|
---|
182 | inserted = true;
|
---|
183 | break;
|
---|
184 | }
|
---|
185 | }
|
---|
186 | }
|
---|
187 |
|
---|
188 | if(!inserted) {
|
---|
189 | DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(listTargets.get(i).toString());
|
---|
190 | nodes.add(newNode);
|
---|
191 | add(newNode);
|
---|
192 | }
|
---|
193 | }
|
---|
194 | }
|
---|
195 |
|
---|
196 | parts++;
|
---|
197 | }
|
---|
198 | }
|
---|
199 | }
|
---|
200 | ));
|
---|
201 | scrollPane_1.setViewportView(tree);
|
---|
202 |
|
---|
203 |
|
---|
204 | if (listTargets.getComponentCount() > 0)
|
---|
205 | listTargets.setSelectedIndex(0);
|
---|
206 |
|
---|
207 | panelFileEquals.setBounds(12, 400, 494, 120);
|
---|
208 | contentPanel.add(panelFileEquals);
|
---|
209 | panelFileEquals.setLayout(null);
|
---|
210 | panelFileEquals.setVisible(false);
|
---|
211 |
|
---|
212 | JLabel lblNewLabel = new JLabel("Actual file:");
|
---|
213 | lblNewLabel.setBounds(10, 11, 89, 14);
|
---|
214 | panelFileEquals.add(lblNewLabel);
|
---|
215 |
|
---|
216 | textFieldActualFile = new JTextField();
|
---|
217 | textFieldActualFile.setBounds(10, 30, 474, 20);
|
---|
218 | panelFileEquals.add(textFieldActualFile);
|
---|
219 | textFieldActualFile.setColumns(10);
|
---|
220 |
|
---|
221 | JButton btnNewButton = new JButton("Search file");
|
---|
222 | btnNewButton.addMouseListener(new MouseAdapter() {
|
---|
223 | public void mouseClicked(MouseEvent arg0) {
|
---|
224 | final JFileChooser fc = new JFileChooser();
|
---|
225 | if (fc.showOpenDialog(contentPanel) == 0) {
|
---|
226 | textFieldExpectedFile.setText(fc.getSelectedFile()
|
---|
227 | .getAbsolutePath());
|
---|
228 | }
|
---|
229 | }
|
---|
230 | });
|
---|
231 | btnNewButton.setBounds(93, 61, 89, 23);
|
---|
232 | panelFileEquals.add(btnNewButton);
|
---|
233 |
|
---|
234 | JLabel lblNewLabel_1 = new JLabel("Expected file:");
|
---|
235 | lblNewLabel_1.setBounds(10, 70, 89, 14);
|
---|
236 | panelFileEquals.add(lblNewLabel_1);
|
---|
237 |
|
---|
238 | textFieldExpectedFile = new JTextField();
|
---|
239 | textFieldExpectedFile.setColumns(10);
|
---|
240 | textFieldExpectedFile.setBounds(10, 88, 474, 20);
|
---|
241 | panelFileEquals.add(textFieldExpectedFile);
|
---|
242 | {
|
---|
243 | JPanel buttonPane = new JPanel();
|
---|
244 | buttonPane.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
|
---|
245 | buttonPane.setBounds(12, 531, 494, 51);
|
---|
246 | contentPanel.add(buttonPane);
|
---|
247 | {
|
---|
248 | // JButton: okButton
|
---|
249 | JButton okButton = new JButton("Insert");
|
---|
250 | okButton.setBounds(349, 11, 135, 31);
|
---|
251 | okButton.addMouseListener(new MouseAdapter() {
|
---|
252 | public void mouseClicked(MouseEvent arg0) {
|
---|
253 |
|
---|
254 | // FileEquals
|
---|
255 | if (panelFileEquals.isVisible()) {
|
---|
256 | if (textFieldActualFile.getText().length() == 0)
|
---|
257 | JOptionPane.showMessageDialog(null,
|
---|
258 | "Please declare an actual file.",
|
---|
259 | "No actual file declared",
|
---|
260 | JOptionPane.OK_OPTION);
|
---|
261 | else if (!new File(textFieldExpectedFile.getText())
|
---|
262 | .exists()) {
|
---|
263 | if (textFieldExpectedFile.getText().length() == 0)
|
---|
264 | JOptionPane.showMessageDialog(null,
|
---|
265 | "Please choose an expected file.",
|
---|
266 | "No expected file chosen",
|
---|
267 | JOptionPane.OK_OPTION);
|
---|
268 | else
|
---|
269 | JOptionPane.showMessageDialog(
|
---|
270 | null,
|
---|
271 | "The expected file \""
|
---|
272 | + textFieldActualFile
|
---|
273 | .getText()
|
---|
274 | + "\" does not exist.",
|
---|
275 | "Expected file does not exist",
|
---|
276 | JOptionPane.OK_OPTION);
|
---|
277 | return;
|
---|
278 | } else {
|
---|
279 | FileEqualsReplay file = new FileEqualsReplay();
|
---|
280 | file.setActualFile(textFieldActualFile
|
---|
281 | .getText());
|
---|
282 | file.setExpectedFile(textFieldExpectedFile
|
---|
283 | .getText());
|
---|
284 |
|
---|
285 | AssertEvent<FileEqualsReplay> e = new AssertEvent<FileEqualsReplay>(
|
---|
286 | "FileEquals");
|
---|
287 | e.addReplayEvent(file);
|
---|
288 | e.setTarget(" ");
|
---|
289 | if (insertBefore)
|
---|
290 | sequences.add(selectedIndex, e);
|
---|
291 | else
|
---|
292 | sequences.add(selectedIndex + 1, e);
|
---|
293 | }
|
---|
294 | }
|
---|
295 | // TextEquals
|
---|
296 | else {
|
---|
297 | if (textFieldExpectedValue.getText().length() == 0) {
|
---|
298 | JOptionPane
|
---|
299 | .showMessageDialog(
|
---|
300 | null,
|
---|
301 | "\"Expected value\" is missing.",
|
---|
302 | "Expected value",
|
---|
303 | JOptionPane.OK_OPTION);
|
---|
304 | return;
|
---|
305 | } else if (listTargets.getSelectedIndex() == -1) {
|
---|
306 | JOptionPane.showMessageDialog(null,
|
---|
307 | "Please select a target.",
|
---|
308 | "No target selected",
|
---|
309 | JOptionPane.OK_OPTION);
|
---|
310 | return;
|
---|
311 | } else {
|
---|
312 | TextEqualsReplay text = new TextEqualsReplay();
|
---|
313 | text.setExpectedValue(textFieldExpectedValue
|
---|
314 | .getText());
|
---|
315 | text.setTarget(modelListTargets.get(
|
---|
316 | listTargets.getSelectedIndex())
|
---|
317 | .toString());
|
---|
318 |
|
---|
319 | AssertEvent<TextEqualsReplay> e = new AssertEvent<TextEqualsReplay>(
|
---|
320 | "TextEquals");
|
---|
321 | e.addReplayEvent(text);
|
---|
322 | e.setTarget(" ");
|
---|
323 | if (insertBefore)
|
---|
324 | sequences.add(selectedIndex, e);
|
---|
325 | else
|
---|
326 | sequences.add(selectedIndex + 1, e);
|
---|
327 | }
|
---|
328 | }
|
---|
329 |
|
---|
330 | dispose();
|
---|
331 | }
|
---|
332 | });
|
---|
333 | buttonPane.setLayout(null);
|
---|
334 | okButton.setActionCommand("OK");
|
---|
335 | buttonPane.add(okButton);
|
---|
336 | getRootPane().setDefaultButton(okButton);
|
---|
337 | // ***
|
---|
338 | }
|
---|
339 |
|
---|
340 | {
|
---|
341 | // JButton: cancelButton
|
---|
342 | JButton cancelButton = new JButton("Cancel");
|
---|
343 | cancelButton.setBounds(10, 11, 135, 31);
|
---|
344 | cancelButton.addMouseListener(new MouseAdapter() {
|
---|
345 |
|
---|
346 | public void mouseClicked(MouseEvent arg0) {
|
---|
347 | dispose();
|
---|
348 | }
|
---|
349 | });
|
---|
350 | cancelButton.setActionCommand("Cancel");
|
---|
351 | buttonPane.add(cancelButton);
|
---|
352 | // ***
|
---|
353 | }
|
---|
354 | }
|
---|
355 | // ***
|
---|
356 | }
|
---|
357 | }
|
---|