// 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); }
// Save a test case. private void saveTest() { JFileChooser fileChooser = new JFileChooser("."); // Only let you select directories and add chooser to pane. fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); // 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)) { return; } // If the file chosen is valid. if (command.equals(JFileChooser.APPROVE_SELECTION)) { // Retrieve the requested file name and location. File f = theFileChooser.getSelectedFile(); // Obtain the requested URL. String fileURL = f.getAbsolutePath(); // This all makes a folder named by user with three files with the same name in it. // Make a folder named as user requested. f.mkdir(); // Concatenate first part of .gui, .efg, .tst file names. fileURL = fileURL.concat(fileURL.substring(fileURL.lastIndexOf('/'))); // Create the three files in the folder. File guiFile = new File(fileURL + ".GUI"); File efgFile = new File(fileURL + ".EFG"); File tstFile = new File(fileURL + ".TST"); // Write a file to save the program details String programDetails = "path=" + programPath + "\n"; programDetails += "main=" + mainClass; try { FileWriter fstream = new FileWriter(fileURL + ".PRG"); BufferedWriter out = new BufferedWriter(fstream); out.write(programDetails); out.close(); } catch (Exception e) { // Catch exception if any System.err.println(e.getMessage()); } try { guiFile.createNewFile(); efgFile.createNewFile(); tstFile.createNewFile(); } catch (IOException e) { JOptionPane.showMessageDialog(null, "Could not create files."); } // Write the three files in the created directory. try { efgtstCapture.writeFiles(guiCapture.getStructure(), fileURL); } catch (InstantiationException e) { JOptionPane.showMessageDialog(null, "Could not save to files."); } updateGUI("Saved Test: " + fileURL); } } }; // Add the action listener created above to file chooser, display it. fileChooser.addActionListener(actionListener); fileChooser.showSaveDialog(this); }
// 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); }