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