// Add a test case to testBox and tests array. private void addTest() { // Set up the frame for the file chooser. final JFrame appframe = new JFrame("Select Application"); appframe.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); Container contentPane = appframe.getContentPane(); JFileChooser fileChooser = new JFileChooser("."); // Only let you select directories and add chooser to pane. fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); contentPane.add(fileChooser, BorderLayout.CENTER); // Make a new action listener for the file chooser. ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { // Get the information to check if the file chosen is valid. JFileChooser theFileChooser = (JFileChooser) actionEvent.getSource(); String command = actionEvent.getActionCommand(); // If the user cancels selecting a program. if (command.equals(JFileChooser.CANCEL_SELECTION)) { appframe.setVisible(false); appframe.dispose(); return; } // If the file chosen is valid. if (command.equals(JFileChooser.APPROVE_SELECTION)) { // Retrieve the selected file and ask for the main class name. File f = theFileChooser.getSelectedFile(); // Obtain the file URL. String fileURL = null; fileURL = f.getAbsolutePath(); // Add a checkbox to the testing check pane. JCheckBox newTest = new JCheckBox(fileURL, true); testBox.setEditable(true); testBox.add(newTest); testBox.repaint(); testBox.setEditable(false); // Add the test to the list of tests. tests.add(newTest); // Make the file chooser disappear. appframe.setVisible(false); appframe.dispose(); } } }; // Add the action listener created above to file chooser, display it. fileChooser.addActionListener(actionListener); appframe.pack(); appframe.setVisible(true); }
// Choose and run the app to test. private void displayAppFrame() { // Set up the frame for the file chooser. final JFrame appframe = new JFrame("Select Application"); appframe.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); Container contentPane = appframe.getContentPane(); JFileChooser fileChooser = new JFileChooser("."); // Ask user what type of executable the program is in, display chooser accordingly. int isJar = JOptionPane.showOptionDialog( null, "Is the program a .jar file?", "Question", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null); if (isJar == JOptionPane.YES_OPTION) { fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); } else if (isJar == JOptionPane.NO_OPTION) { fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); } else { fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); } contentPane.add(fileChooser, BorderLayout.CENTER); // Make a new action listener for the file chooser. ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { // Get the information to check if the file chosen is valid. JFileChooser theFileChooser = (JFileChooser) actionEvent.getSource(); String command = actionEvent.getActionCommand(); // If the user cancels selecting a program. if (command.equals(JFileChooser.CANCEL_SELECTION)) { appframe.setVisible(false); appframe.dispose(); return; } // If the file chosen is valid. if (command.equals(JFileChooser.APPROVE_SELECTION)) { // Retrieve the selected file and ask for the main class name. File f = theFileChooser.getSelectedFile(); String className = JOptionPane.showInputDialog("Please enter the main class name: "); mainClass = className; programPath = f.getAbsolutePath(); // Obtain the file URL. URL[] folderURL = new URL[1]; String fileURL = null; String programName = "/"; try { folderURL[0] = f.toURI().toURL(); fileURL = new String(folderURL[0].toString()); } catch (MalformedURLException e1) { System.err.println("File URL not correct."); } // check if OS is *nix, if so, add leading / String os = System.getProperty("os.name").toLowerCase(); if (os.indexOf("nix") >= 0 || os.indexOf("nux") >= 0) { fileURL = "/" + fileURL; } // Run the selected application. try { // Begin capturing the programs GUI. System.out.println("Before Capture: #Frames=" + Frame.getFrames().length); guiCapture.beginCapture(); // Load the program through the CaptureAsYouGo guiCapture.loadApplication(className, folderURL, new String[] {}); // Update the label that shows what program is running. programName = fileURL.substring(fileURL.lastIndexOf('/')); if (programName.length() == 1) { programName = "Main Class - " + className; } appName.setText(programName); } catch (IOException e) { e.printStackTrace(); System.out.println("TEST 1"); } catch (Exception e) { e.printStackTrace(); System.out.println("TEST 2"); } // Make the file chooser disappear. appframe.setVisible(false); appframe.dispose(); } } }; // Add the action listener created above to file chooser, display it. fileChooser.addActionListener(actionListener); appframe.pack(); appframe.setVisible(true); }