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

Last change on this file since 359 was 287, checked in by sherbold, 13 years ago
  • fixed some code smells
  • Property svn:mime-type set to text/plain
File size: 17.1 KB
RevLine 
[132]1package de.ugoe.cs.eventbench.swing;
2
3import java.awt.BorderLayout;
[164]4import java.util.Enumeration;
[132]5import java.util.List;
[213]6import java.util.SortedSet;
7import java.util.TreeSet;
[134]8
9import de.ugoe.cs.eventbench.assertions.AssertEvent;
10import de.ugoe.cs.eventbench.assertions.FileEqualsReplay;
11import de.ugoe.cs.eventbench.assertions.TextEqualsReplay;
[132]12import de.ugoe.cs.eventbench.data.Event;
[144]13import de.ugoe.cs.eventbench.data.GlobalDataContainer;
14import de.ugoe.cs.util.console.Console;
[132]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;
[155]31import javax.swing.border.EtchedBorder;
32import javax.swing.JTree;
33import javax.swing.tree.DefaultTreeModel;
34import javax.swing.tree.DefaultMutableTreeNode;
[213]35import javax.swing.tree.TreeNode;
[164]36import javax.swing.tree.TreePath;
[213]37import javax.swing.tree.TreeSelectionModel;
[132]38
[213]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
[229]47 * @deprecated Use SWT-GUI for modifying sequences.
[213]48 */
[132]49public class DlgInsert extends JDialog {
[139]50
[134]51        /**
52         * Id for object serialization.
53         */
54        private static final long serialVersionUID = 1L;
[132]55
56        private final JPanel contentPanel = new JPanel();
57        private JTextField textFieldExpectedValue;
[134]58        private JTextField textFieldActualFile;
59        private JTextField textFieldExpectedFile;
[132]60
61        /**
[213]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'.
[132]73         */
[139]74        public static void showDialog(List<Event<?>> sequences, int selectedIndex,
75                        final boolean insertBefore) {
[132]76                try {
[139]77                        DlgInsert dialog = new DlgInsert(sequences, selectedIndex,
78                                        insertBefore);
[132]79                        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
80                        dialog.setVisible(true);
81                } catch (Exception e) {
82                        e.printStackTrace();
83                }
84        }
85
86        /**
[213]87         * <p>
[132]88         * Create the dialog.
[213]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'.
[132]98         */
[139]99        public DlgInsert(final List<Event<?>> sequences, final int selectedIndex,
100                        final boolean insertBefore) {
[213]101                initialize(sequences, selectedIndex, insertBefore);
102        }
[139]103
[213]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) {
[139]119
[213]120                setResizable(false);
121
[132]122                setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
123                setTitle("Insert testcase");
[139]124
[132]125                setModal(true);
[213]126                setBounds(100, 100, 676, 673);
[132]127                getContentPane().setLayout(new BorderLayout());
128                contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
129                getContentPane().add(contentPanel, BorderLayout.CENTER);
130                contentPanel.setLayout(null);
[213]131                final JComboBox comboBoxAssertionType = new JComboBox();
[132]132                final JPanel panelTextEquals = new JPanel();
[164]133                panelTextEquals.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null,
134                                null));
[132]135                final JPanel panelFileEquals = new JPanel();
[164]136                panelFileEquals.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null,
137                                null));
[139]138
[213]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() {
[132]165                        public void actionPerformed(ActionEvent arg0) {
166                                if ("comboBoxChanged".equals(arg0.getActionCommand())) {
[139]167
[213]168                                        selectAssertionType(numberOfAssertionTypes, panels,
169                                                        comboBoxAssertionType.getSelectedIndex(),
170                                                        buttonPane, okButton);
[132]171                                }
172                        }
173                });
[139]174
[132]175                JLabel label = new JLabel("Testcase:");
[155]176                label.setBounds(12, 14, 86, 14);
[132]177                contentPanel.add(label);
[139]178
[132]179                JLabel label_1 = new JLabel("Expected value:");
[155]180                label_1.setBounds(10, 11, 96, 14);
[139]181
[132]182                JLabel label_2 = new JLabel("Target:");
183                label_2.setBounds(10, 38, 86, 14);
[139]184
[132]185                textFieldExpectedValue = new JTextField();
186                textFieldExpectedValue.setColumns(10);
[213]187                textFieldExpectedValue.setBounds(116, 8, 524, 20);
[139]188
[132]189                panelTextEquals.setLayout(null);
[213]190                panelTextEquals.setBounds(10, 40, 650, 401);
[132]191                contentPanel.add(panelTextEquals);
192                panelTextEquals.add(label_1);
193                panelTextEquals.add(label_2);
194                panelTextEquals.add(textFieldExpectedValue);
[139]195
[155]196                JScrollPane scrollPane_1 = new JScrollPane();
[213]197                scrollPane_1.setBounds(10, 58, 630, 291);
[155]198                panelTextEquals.add(scrollPane_1);
[139]199
[164]200                final JTree tree = new JTree();
201                DefaultTreeModel treeModel = new DefaultTreeModel(null);
[213]202                final DefaultMutableTreeNode root = new DefaultMutableTreeNode(
203                                "Targets");
[164]204                treeModel.setRoot(root);
205                tree.setModel(treeModel);
206
[213]207                tree.getSelectionModel().setSelectionMode(
208                                TreeSelectionModel.CONTIGUOUS_TREE_SELECTION);
209                buildTargetTree(root);
[164]210
[213]211                scrollPane_1.setViewportView(tree);
[164]212
[213]213                // expand all targets
214                JButton btnExpandAll = new JButton("Expand all");
215                btnExpandAll.addMouseListener(new MouseAdapter() {
216                        public void mouseClicked(MouseEvent arg0) {
[164]217
[213]218                                expandAll(tree, new TreePath(root), true);
219                        }
220                });
221                btnExpandAll.setBounds(10, 360, 112, 30);
222                panelTextEquals.add(btnExpandAll);
[164]223
[213]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);
[144]229                        }
[213]230                });
231                btnCollapseAll.setBounds(132, 360, 112, 30);
232                panelTextEquals.add(btnCollapseAll);
[144]233
[213]234                panelFileEquals.setBounds(34, 452, 607, 120);
[132]235                contentPanel.add(panelFileEquals);
236                panelFileEquals.setLayout(null);
237                panelFileEquals.setVisible(false);
[139]238
[134]239                JLabel lblNewLabel = new JLabel("Actual file:");
240                lblNewLabel.setBounds(10, 11, 89, 14);
[132]241                panelFileEquals.add(lblNewLabel);
[139]242
[134]243                textFieldActualFile = new JTextField();
[213]244                textFieldActualFile.setBounds(10, 30, 587, 20);
[134]245                panelFileEquals.add(textFieldActualFile);
246                textFieldActualFile.setColumns(10);
[139]247
[213]248                // open search file dialog
249                JButton btnSearchFile = new JButton("Search file");
250                btnSearchFile.addMouseListener(new MouseAdapter() {
[132]251                        public void mouseClicked(MouseEvent arg0) {
252                                final JFileChooser fc = new JFileChooser();
[139]253                                if (fc.showOpenDialog(contentPanel) == 0) {
254                                        textFieldExpectedFile.setText(fc.getSelectedFile()
255                                                        .getAbsolutePath());
256                                }
[132]257                        }
258                });
[213]259                btnSearchFile.setBounds(93, 61, 89, 23);
260                panelFileEquals.add(btnSearchFile);
[139]261
[134]262                JLabel lblNewLabel_1 = new JLabel("Expected file:");
[155]263                lblNewLabel_1.setBounds(10, 70, 89, 14);
[132]264                panelFileEquals.add(lblNewLabel_1);
[139]265
[134]266                textFieldExpectedFile = new JTextField();
267                textFieldExpectedFile.setColumns(10);
[213]268                textFieldExpectedFile.setBounds(10, 88, 587, 20);
[134]269                panelFileEquals.add(textFieldExpectedFile);
[213]270
[132]271                {
[164]272                        buttonPane.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null,
273                                        null));
[213]274                        buttonPane.setBounds(12, 583, 607, 51);
[155]275                        contentPanel.add(buttonPane);
[132]276                        {
[213]277                                // clicking 'Insert'
278                                okButton.setBounds(462, 11, 135, 31);
[132]279                                okButton.addMouseListener(new MouseAdapter() {
280                                        public void mouseClicked(MouseEvent arg0) {
[213]281                                                if (insertAssertion(sequences, selectedIndex,
282                                                                insertBefore, tree, comboBoxAssertionType
283                                                                                .getSelectedItem().toString()) == false) {
284                                                        return;
[132]285                                                }
[139]286
[132]287                                                dispose();
288                                        }
289                                });
[155]290                                buttonPane.setLayout(null);
[132]291                                okButton.setActionCommand("OK");
292                                buttonPane.add(okButton);
293                                getRootPane().setDefaultButton(okButton);
294                        }
[139]295
[132]296                        {
[213]297                                // clicking 'Cancel'
[132]298                                JButton cancelButton = new JButton("Cancel");
[155]299                                cancelButton.setBounds(10, 11, 135, 31);
[132]300                                cancelButton.addMouseListener(new MouseAdapter() {
[139]301
302                                        public void mouseClicked(MouseEvent arg0) {
303                                                dispose();
304                                        }
[132]305                                });
306                                cancelButton.setActionCommand("Cancel");
307                                buttonPane.add(cancelButton);
308                        }
309                }
[213]310               
311                selectAssertionType(numberOfAssertionTypes, panels,
312                                comboBoxAssertionType.getSelectedIndex(),
313                                buttonPane, okButton);
[132]314        }
[164]315
[213]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         */
[164]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) {
[213]379                                        if (target.contains(node.toString())) {
[164]380                                                return node;
[213]381                                        }
382                                } else {
[164]383                                        return nodeReturn;
[213]384                                }
[164]385                        }
386                }
387
388                return null;
389        }
[213]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();) {
[226]408                                TreeNode n = e.nextElement();
[213]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
[287]479                if (selectedItem.equals("OutputFileEquals")) {
[213]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 {
[229]501                                FileEqualsReplay file = new FileEqualsReplay(textFieldExpectedFile.getText(), textFieldActualFile.getText());
[213]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
[284]514                else if (selectedItem.equals("TextEquals")) {
[213]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
[284]541                                StringBuilder target = new StringBuilder();
[213]542                                for (int i = 0; i < splitted.length; i++) {
[284]543                                        target.append(splitted[i]);
[213]544                                }
545                                // ***
546
[284]547                                TextEqualsReplay text = new TextEqualsReplay(textFieldExpectedValue.getText(),target.toString());
[213]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        }
[164]562}
Note: See TracBrowser for help on using the repository browser.