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