Beispiel #1
0
  // 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);
  }