source: trunk/EventBenchConsole/src/de/ugoe/cs/eventbench/swing/DlgInsert.java @ 164

Last change on this file since 164 was 164, checked in by jhall, 13 years ago

Implemented a new way to build the tree view - work in progress

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