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