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

Last change on this file since 229 was 229, checked in by sherbold, 13 years ago
  • Property svn:mime-type set to text/plain
File size: 17.1 KB
Line 
1package de.ugoe.cs.eventbench.swing;
2
3import java.awt.BorderLayout;
4import java.util.Enumeration;
5import java.util.List;
6import java.util.SortedSet;
7import java.util.TreeSet;
8
9import de.ugoe.cs.eventbench.assertions.AssertEvent;
10import de.ugoe.cs.eventbench.assertions.FileEqualsReplay;
11import de.ugoe.cs.eventbench.assertions.TextEqualsReplay;
12import de.ugoe.cs.eventbench.data.Event;
13import de.ugoe.cs.eventbench.data.GlobalDataContainer;
14import de.ugoe.cs.util.console.Console;
15
16import javax.swing.JButton;
17import javax.swing.JDialog;
18import javax.swing.JPanel;
19import javax.swing.border.EmptyBorder;
20import javax.swing.JComboBox;
21import javax.swing.JLabel;
22import javax.swing.JTextField;
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.border.EtchedBorder;
32import javax.swing.JTree;
33import javax.swing.tree.DefaultTreeModel;
34import javax.swing.tree.DefaultMutableTreeNode;
35import javax.swing.tree.TreeNode;
36import javax.swing.tree.TreePath;
37import javax.swing.tree.TreeSelectionModel;
38
39/**
40 * <p>
41 * This class provides the dialog to insert one of the available assertion
42 * types.
43 * </p>
44 *
45 * @author Jeffrey Hall
46 * @version 1.0
47 * @deprecated Use SWT-GUI for modifying sequences.
48 */
49public class DlgInsert extends JDialog {
50
51        /**
52         * Id for object serialization.
53         */
54        private static final long serialVersionUID = 1L;
55
56        private final JPanel contentPanel = new JPanel();
57        private JTextField textFieldExpectedValue;
58        private JTextField textFieldActualFile;
59        private JTextField textFieldExpectedFile;
60
61        /**
62         * <p>
63         * Launch the dialog
64         * </p>
65         *
66         * @param sequences
67         *            A list of the events where an assertion will be inserted.
68         * @param selectedIndex
69         *            The position for inserting an assertion.
70         * @param insertBefore
71         *            To decide if the user clicked 'insert before' or 'insert
72         *            after'.
73         */
74        public static void showDialog(List<Event<?>> sequences, int selectedIndex,
75                        final boolean insertBefore) {
76                try {
77                        DlgInsert dialog = new DlgInsert(sequences, selectedIndex,
78                                        insertBefore);
79                        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
80                        dialog.setVisible(true);
81                } catch (Exception e) {
82                        e.printStackTrace();
83                }
84        }
85
86        /**
87         * <p>
88         * Create the dialog.
89         * </p>
90         *
91         * @param sequences
92         *            A list of the events where an assertion will be inserted.
93         * @param selectedIndex
94         *            The position for inserting an assertion.
95         * @param insertBefore
96         *            To decide if the user clicked 'insert before' or 'insert
97         *            after'.
98         */
99        public DlgInsert(final List<Event<?>> sequences, final int selectedIndex,
100                        final boolean insertBefore) {
101                initialize(sequences, selectedIndex, insertBefore);
102        }
103
104        /**
105         * <p>
106         * Initialize the contents of the frame.
107         * </p>
108         *
109         * @param sequences
110         *            A list of the events where an assertion will be inserted.
111         * @param selectedIndex
112         *            The position for inserting an assertion.
113         * @param insertBefore
114         *            To decide if the user clicked 'insert before' or 'insert
115         *            after'.
116         */
117        private void initialize(final List<Event<?>> sequences,
118                        final int selectedIndex, final boolean insertBefore) {
119
120                setResizable(false);
121
122                setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
123                setTitle("Insert testcase");
124
125                setModal(true);
126                setBounds(100, 100, 676, 673);
127                getContentPane().setLayout(new BorderLayout());
128                contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
129                getContentPane().add(contentPanel, BorderLayout.CENTER);
130                contentPanel.setLayout(null);
131                final JComboBox comboBoxAssertionType = new JComboBox();
132                final JPanel panelTextEquals = new JPanel();
133                panelTextEquals.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null,
134                                null));
135                final JPanel panelFileEquals = new JPanel();
136                panelFileEquals.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null,
137                                null));
138
139                // *****
140                // define your assertion types here
141                final int numberOfAssertionTypes = 2;
142                final JPanel[] panels = new JPanel[numberOfAssertionTypes];
143                String[] assertionTypes = new String[numberOfAssertionTypes];
144
145                panels[0] = panelTextEquals;
146                assertionTypes[0] = "TextEquals";
147                panels[1] = panelFileEquals;
148                assertionTypes[1] = "OutputFileEquals";
149                // *****
150
151                // add assertion types to comboBox
152                for (int i = 0; i < numberOfAssertionTypes; i++) {
153                        comboBoxAssertionType.addItem(assertionTypes[i]);
154                }
155
156                comboBoxAssertionType.setSelectedIndex(0);
157                comboBoxAssertionType.setBounds(90, 11, 180, 20);
158                contentPanel.add(comboBoxAssertionType);
159
160                final JPanel buttonPane = new JPanel();
161                final JButton okButton = new JButton("Insert");
162
163                // selecting of another assertion type
164                comboBoxAssertionType.addActionListener(new ActionListener() {
165                        public void actionPerformed(ActionEvent arg0) {
166                                if ("comboBoxChanged".equals(arg0.getActionCommand())) {
167
168                                        selectAssertionType(numberOfAssertionTypes, panels,
169                                                        comboBoxAssertionType.getSelectedIndex(),
170                                                        buttonPane, okButton);
171                                }
172                        }
173                });
174
175                JLabel label = new JLabel("Testcase:");
176                label.setBounds(12, 14, 86, 14);
177                contentPanel.add(label);
178
179                JLabel label_1 = new JLabel("Expected value:");
180                label_1.setBounds(10, 11, 96, 14);
181
182                JLabel label_2 = new JLabel("Target:");
183                label_2.setBounds(10, 38, 86, 14);
184
185                textFieldExpectedValue = new JTextField();
186                textFieldExpectedValue.setColumns(10);
187                textFieldExpectedValue.setBounds(116, 8, 524, 20);
188
189                panelTextEquals.setLayout(null);
190                panelTextEquals.setBounds(10, 40, 650, 401);
191                contentPanel.add(panelTextEquals);
192                panelTextEquals.add(label_1);
193                panelTextEquals.add(label_2);
194                panelTextEquals.add(textFieldExpectedValue);
195
196                JScrollPane scrollPane_1 = new JScrollPane();
197                scrollPane_1.setBounds(10, 58, 630, 291);
198                panelTextEquals.add(scrollPane_1);
199
200                final JTree tree = new JTree();
201                DefaultTreeModel treeModel = new DefaultTreeModel(null);
202                final DefaultMutableTreeNode root = new DefaultMutableTreeNode(
203                                "Targets");
204                treeModel.setRoot(root);
205                tree.setModel(treeModel);
206
207                tree.getSelectionModel().setSelectionMode(
208                                TreeSelectionModel.CONTIGUOUS_TREE_SELECTION);
209                buildTargetTree(root);
210
211                scrollPane_1.setViewportView(tree);
212
213                // expand all targets
214                JButton btnExpandAll = new JButton("Expand all");
215                btnExpandAll.addMouseListener(new MouseAdapter() {
216                        public void mouseClicked(MouseEvent arg0) {
217
218                                expandAll(tree, new TreePath(root), true);
219                        }
220                });
221                btnExpandAll.setBounds(10, 360, 112, 30);
222                panelTextEquals.add(btnExpandAll);
223
224                // collapse all targets
225                JButton btnCollapseAll = new JButton("Collapse all");
226                btnCollapseAll.addMouseListener(new MouseAdapter() {
227                        public void mouseClicked(MouseEvent arg0) {
228                                expandAll(tree, new TreePath(root), false);
229                        }
230                });
231                btnCollapseAll.setBounds(132, 360, 112, 30);
232                panelTextEquals.add(btnCollapseAll);
233
234                panelFileEquals.setBounds(34, 452, 607, 120);
235                contentPanel.add(panelFileEquals);
236                panelFileEquals.setLayout(null);
237                panelFileEquals.setVisible(false);
238
239                JLabel lblNewLabel = new JLabel("Actual file:");
240                lblNewLabel.setBounds(10, 11, 89, 14);
241                panelFileEquals.add(lblNewLabel);
242
243                textFieldActualFile = new JTextField();
244                textFieldActualFile.setBounds(10, 30, 587, 20);
245                panelFileEquals.add(textFieldActualFile);
246                textFieldActualFile.setColumns(10);
247
248                // open search file dialog
249                JButton btnSearchFile = new JButton("Search file");
250                btnSearchFile.addMouseListener(new MouseAdapter() {
251                        public void mouseClicked(MouseEvent arg0) {
252                                final JFileChooser fc = new JFileChooser();
253                                if (fc.showOpenDialog(contentPanel) == 0) {
254                                        textFieldExpectedFile.setText(fc.getSelectedFile()
255                                                        .getAbsolutePath());
256                                }
257                        }
258                });
259                btnSearchFile.setBounds(93, 61, 89, 23);
260                panelFileEquals.add(btnSearchFile);
261
262                JLabel lblNewLabel_1 = new JLabel("Expected file:");
263                lblNewLabel_1.setBounds(10, 70, 89, 14);
264                panelFileEquals.add(lblNewLabel_1);
265
266                textFieldExpectedFile = new JTextField();
267                textFieldExpectedFile.setColumns(10);
268                textFieldExpectedFile.setBounds(10, 88, 587, 20);
269                panelFileEquals.add(textFieldExpectedFile);
270
271                {
272                        buttonPane.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null,
273                                        null));
274                        buttonPane.setBounds(12, 583, 607, 51);
275                        contentPanel.add(buttonPane);
276                        {
277                                // clicking 'Insert'
278                                okButton.setBounds(462, 11, 135, 31);
279                                okButton.addMouseListener(new MouseAdapter() {
280                                        public void mouseClicked(MouseEvent arg0) {
281                                                if (insertAssertion(sequences, selectedIndex,
282                                                                insertBefore, tree, comboBoxAssertionType
283                                                                                .getSelectedItem().toString()) == false) {
284                                                        return;
285                                                }
286
287                                                dispose();
288                                        }
289                                });
290                                buttonPane.setLayout(null);
291                                okButton.setActionCommand("OK");
292                                buttonPane.add(okButton);
293                                getRootPane().setDefaultButton(okButton);
294                        }
295
296                        {
297                                // clicking 'Cancel'
298                                JButton cancelButton = new JButton("Cancel");
299                                cancelButton.setBounds(10, 11, 135, 31);
300                                cancelButton.addMouseListener(new MouseAdapter() {
301
302                                        public void mouseClicked(MouseEvent arg0) {
303                                                dispose();
304                                        }
305                                });
306                                cancelButton.setActionCommand("Cancel");
307                                buttonPane.add(cancelButton);
308                        }
309                }
310               
311                selectAssertionType(numberOfAssertionTypes, panels,
312                                comboBoxAssertionType.getSelectedIndex(),
313                                buttonPane, okButton);
314        }
315
316        /**
317         * Build up the tree containing all available targets.
318         *
319         * @param root
320         *            The tree root.
321         */
322        @SuppressWarnings("unchecked")
323        private void buildTargetTree(final DefaultMutableTreeNode root) {
324                // get targets from GlobalDataContainer
325                SortedSet<String> listTargets = new TreeSet<String>();
326                try {
327                        listTargets = (SortedSet<String>) GlobalDataContainer.getInstance()
328                                        .getData("ListTargets");
329                } catch (ClassCastException e) {
330                        Console.println("Not able to cast data in GlobalDataContainer to SortedSet of Strings");
331                }
332
333                // build the tree
334                for (String target : listTargets) {
335                        DefaultMutableTreeNode currentParent = root;
336
337                        String splitted[] = target.split("/>");
338
339                        for (String targetPart : splitted) {
340                                DefaultMutableTreeNode node = compareTargetWithNode(
341                                                currentParent, targetPart + "/>");
342
343                                if (node != null) {
344                                        currentParent = node;
345                                } else {
346                                        node = new DefaultMutableTreeNode(targetPart + "/>");
347                                        currentParent.add(node);
348                                        currentParent = node;
349                                }
350                        }
351                }
352        }
353
354        /**
355         * Check if there is a child equal to 'target'.
356         *
357         * @param node
358         *            The parent node which is browsed for a node equal to 'target'.
359         * @param target
360         *            'node' is browsed for this String.
361         * @return If there was no fitting node found, the return value is null.
362         *         Otherwise it is the node.
363         */
364        DefaultMutableTreeNode compareTargetWithNode(DefaultMutableTreeNode node,
365                        String target) {
366
367                if (node.isLeaf()) {
368                        if (target.contains(node.toString()))
369                                return node;
370                        else
371                                return null;
372                } else {
373                        for (@SuppressWarnings("unchecked")
374                        Enumeration<DefaultMutableTreeNode> e = node.children(); e
375                                        .hasMoreElements();) {
376                                DefaultMutableTreeNode nodeReturn = compareTargetWithNode(
377                                                e.nextElement(), target);
378                                if (nodeReturn == null) {
379                                        if (target.contains(node.toString())) {
380                                                return node;
381                                        }
382                                } else {
383                                        return nodeReturn;
384                                }
385                        }
386                }
387
388                return null;
389        }
390
391        /**
392         * Expand or collapse the target tree.
393         *
394         * @param tree
395         *            The tree itself.
396         * @param parent
397         *            The parent node which has to be expanded/collapsed.
398         * @param expand
399         *            To choose wether it is expanded or collapsed.
400         */
401        private void expandAll(JTree tree, TreePath parent, boolean expand) {
402                // Traverse children
403                TreeNode node = (TreeNode) parent.getLastPathComponent();
404                if (node.getChildCount() >= 0) {
405                        for (@SuppressWarnings("unchecked")
406                        Enumeration<DefaultMutableTreeNode> e = node.children(); e
407                                        .hasMoreElements();) {
408                                TreeNode n = e.nextElement();
409                                TreePath path = parent.pathByAddingChild(n);
410                                expandAll(tree, path, expand);
411                        }
412                }
413
414                // Expansion or collapse must be done bottom-up
415                if (expand) {
416                        tree.expandPath(parent);
417                } else {
418                        tree.collapsePath(parent);
419                }
420        }
421
422        /**
423         * Select another assertion type in the comboBox.
424         *
425         * @param numberOfAssertionTypes
426         *            Number of available assertion types.
427         * @param panels
428         *            The corresponding panels of the types.
429         * @param selectedIndex
430         *            The index of the selected type.
431         * @param buttonPane
432         *            The buttonPane of the dialog.
433         * @param okButton
434         *            The okButton of the buttonPane.
435         */
436        private void selectAssertionType(final int numberOfAssertionTypes,
437                        final JPanel[] panels, int selectedIndex, final JPanel buttonPane,
438                        final JButton okButton) {
439                for (int i = 0; i < numberOfAssertionTypes; i++) {
440                        panels[i].setVisible(false);
441                }
442
443                JPanel activePanel = panels[selectedIndex];
444                activePanel.setVisible(true);
445                activePanel.setLocation(10, 40);
446
447                buttonPane.setLocation(activePanel.getX(), activePanel.getY()
448                                + activePanel.getHeight() + 15);
449                buttonPane.setSize(activePanel.getWidth(), buttonPane.getHeight());
450                setSize(activePanel.getX() + activePanel.getSize().width + 15,
451                                buttonPane.getY() + buttonPane.getSize().height + 35);
452                okButton.setLocation(buttonPane.getWidth() - okButton.getWidth() - 10,
453                                okButton.getY());
454
455        }
456
457        /**
458         * To check if all the parameters needed where entered correctly.
459         *
460         * @param sequences
461         *            A list of the events where an assertion will be inserted.
462         * @param selectedIndex
463         *            The position for inserting an assertion.
464         * @param insertBefore
465         *            To decide if the user clicked 'insert before' or 'insert
466         *            after'.
467         * @param tree
468         *            The target tree.
469         * @param selectedItem
470         *            To identify the selected assertion type.
471         * @return If the assertion was inserted, the return value is true.
472         *         Otherwise it is false.
473         */
474        private boolean insertAssertion(final List<Event<?>> sequences,
475                        final int selectedIndex, final boolean insertBefore,
476                        final JTree tree, final String selectedItem) {
477
478                // FileEquals
479                if (selectedItem == "OutputFileEquals") {
480                        if (textFieldActualFile.getText().length() == 0) {
481                                JOptionPane.showMessageDialog(null,
482                                                "Please declare an actual file.",
483                                                "No actual file declared", JOptionPane.OK_OPTION);
484
485                                return false;
486                        } else if (!new File(textFieldExpectedFile.getText()).exists()) {
487                                if (textFieldExpectedFile.getText().length() == 0) {
488                                        JOptionPane.showMessageDialog(null,
489                                                        "Please choose an expected file.",
490                                                        "No expected file chosen", JOptionPane.OK_OPTION);
491                                } else {
492                                        JOptionPane.showMessageDialog(null, "The expected file \""
493                                                        + textFieldActualFile.getText()
494                                                        + "\" does not exist.",
495                                                        "Expected file does not exist",
496                                                        JOptionPane.OK_OPTION);
497                                }
498
499                                return false;
500                        } else {
501                                FileEqualsReplay file = new FileEqualsReplay(textFieldExpectedFile.getText(), textFieldActualFile.getText());
502
503                                AssertEvent<FileEqualsReplay> e = new AssertEvent<FileEqualsReplay>(
504                                                "FileEquals");
505                                e.addReplayEvent(file);
506                                e.setTarget(" ");
507                                if (insertBefore)
508                                        sequences.add(selectedIndex, e);
509                                else
510                                        sequences.add(selectedIndex + 1, e);
511                        }
512                }
513                // TextEquals
514                else if (selectedItem == "TextEquals") {
515                        if (textFieldExpectedValue.getText().length() == 0) {
516                                JOptionPane.showMessageDialog(null,
517                                                "\"Expected value\" is missing.", "Expected value",
518                                                JOptionPane.OK_OPTION);
519                                return false;
520                        } else if (tree.getSelectionCount() == 0
521                                        || tree.getSelectionPath().toString()
522                                                        .compareTo("[Targets]") == 0) {
523                                JOptionPane.showMessageDialog(null, "Please select a target.",
524                                                "No target selected", JOptionPane.OK_OPTION);
525                                return false;
526                        } else {
527
528                                // get target ***
529                                String selectionPath = tree.getSelectionPath().toString();
530
531                                // remove leading and ending brackets
532                                selectionPath = selectionPath.substring(1);
533                                selectionPath = selectionPath.substring(0,
534                                                selectionPath.length() - 1);
535                                // remove leading string "targets"
536                                selectionPath = selectionPath.substring(7);
537
538                                String splitted[] = selectionPath.toString().split(", ");
539
540                                // get all parents
541                                String target = "";
542                                for (int i = 0; i < splitted.length; i++) {
543                                        target += splitted[i];
544                                }
545                                // ***
546
547                                TextEqualsReplay text = new TextEqualsReplay(textFieldExpectedValue.getText(),target);
548
549                                AssertEvent<TextEqualsReplay> e = new AssertEvent<TextEqualsReplay>(
550                                                "TextEquals");
551                                e.addReplayEvent(text);
552                                e.setTarget(" ");
553                                if (insertBefore)
554                                        sequences.add(selectedIndex, e);
555                                else
556                                        sequences.add(selectedIndex + 1, e);
557                        }
558                }
559
560                return true;
561        }
562}
Note: See TracBrowser for help on using the repository browser.