public void checkForChange() {
    if (autoChooser.getSelected() instanceof MarsRock) {
      CommandGroup cg = new CommandGroup();
      cg.addSequential(new MarsRock());
      currentCommandGroup = cg;
      return;
    }

    if (!selectedAuto.equalsIgnoreCase((String) autoChooser.getSelected())) {
      selectedAuto = (String) autoChooser.getSelected();
      currentCommandGroup = buildScript(selectedAuto);
      System.out.println("Selected Auto: " + selectedAuto);
    }
  }
  public CommandGroup buildScript(String scriptToRead) {
    CommandGroup cg = null;
    try {
      reader = new BufferedReader(new FileReader(filename));

      String line;
      String scriptName = null;
      int comma1, comma2;
      boolean scriptStarted = false;
      String commandName = null;
      double timeout, param1, param2, param3;
      boolean stop;
      boolean sequential;

      Command command = null;

      // search for specified script name
      while ((line = reader.readLine()) != null) {
        //                System.out.println(line);
        if (line.toUpperCase().startsWith("SCRIPT_NAME,")) {
          comma1 = line.indexOf(",");
          comma2 = line.substring(comma1 + 1).indexOf(",") + comma1 + 1;
          scriptName = line.substring(comma1 + 1, comma2).toUpperCase().trim();
          System.out.println("Found Script: " + scriptName);
          scriptStarted = true;
          if (scriptName.equals(scriptToRead)) {
            cg = new CommandGroup(scriptName);
            break;
          }
        }
      }

      while ((line = reader.readLine()) != null) {
        if (line.toUpperCase().startsWith("END_OF_SPREAD_SHEET,")) {
          if (scriptStarted)
            System.err.println("!!!End of Spreadsheet found while " + scriptName + " still open");
          else {
            //                        System.out.println("End of Spreadsheet");
            break;
          }
        } else if (line.toUpperCase().startsWith("END,")) {
          //                    System.out.println("End of Script: " + scriptName);
          scriptStarted = false;
          cg.addSequential(new Wait(15));
          break;
        } else if (line.startsWith(",") || line.length() <= 2) {
          // empty line
        } else {
          if (!scriptStarted) System.err.println("!!!Command found while Script not open");
          else {
            commandName = line.substring(0, line.indexOf(","));

            comma1 = line.indexOf(",");
            comma2 = line.substring(comma1 + 1).indexOf(",") + comma1 + 1;
            if (line.substring(comma1 + 1).startsWith("S")) sequential = true;
            else sequential = false;

            line = line.substring(comma1 + 1);
            comma1 = line.indexOf(",");
            comma2 = line.substring(comma1 + 1).indexOf(",") + comma1 + 1;
            try {
              timeout = Double.parseDouble(line.substring(comma1 + 1, comma2).toUpperCase().trim());
            } catch (NumberFormatException ex) {
              timeout = 0;
            }

            line = line.substring(comma2 + 1);
            if (line.toUpperCase().startsWith("CONTINUE")) stop = false;
            else stop = true;
            //                        System.out.println("stop: " + stop + " " +
            // line.toUpperCase());

            comma1 = line.indexOf(",");
            comma2 = line.substring(comma1 + 1).indexOf(",") + comma1 + 1;
            try {
              param1 = Double.parseDouble(line.substring(comma1 + 1, comma2).trim());
            } catch (NumberFormatException ex) {
              param1 = 0;
            }

            comma1 = comma2;
            comma2 = line.substring(comma1 + 1).indexOf(",") + comma1 + 1;
            try {
              param2 = Double.parseDouble(line.substring(comma1 + 1, comma2).trim());
            } catch (NumberFormatException ex) {
              param2 = 0;
            }

            comma1 = comma2;
            comma2 = line.substring(comma1 + 1).indexOf(",") + comma1 + 1;
            try {
              param3 = Double.parseDouble(line.substring(comma1 + 1, comma2).trim());
            } catch (NumberFormatException ex) {
              param3 = 0;
            }

            try {
              Object commandClass = Class.forName(commandPackage + "." + commandName).newInstance();
              if ((commandClass instanceof AutoSpreadsheetCommand)
                  || (commandClass instanceof CommandGroup)) {
                command = (Command) commandClass;
                if (command instanceof AutoSpreadsheetCommand) {
                  System.out.println("Found Command: " + command.getName());
                  //                                    command =
                  // ((AutoSpreadsheetCommand)command).copy();
                  ((AutoSpreadsheetCommand) command).setStopAtEnd(stop);
                  ((AutoSpreadsheetCommand) command).setParam1(param1);
                  ((AutoSpreadsheetCommand) command).setParam2(param2);
                  ((AutoSpreadsheetCommand) command).setParam3(param3);
                  (command).setTimeout(timeout);
                  if (sequential) cg.addSequential(command);
                  else cg.addParallel(command);

                } else if (command instanceof CommandGroup) {
                  System.out.println("Found Command Group: " + command.getName());
                  //                                    command =
                  // ((AutoSpreadsheetCommandGroup)command).copy();
                  if (sequential) cg.addSequential(command);
                  else cg.addParallel(command);
                }
              } else {
                System.err.println(
                    commandClass.toString()
                        + " is not an instance of AutospreadsheetCommand or AutoSpreadsheetCommandGroup");
              }
            } catch (ClassNotFoundException e) {
              System.err.println(
                  "Can not find command: " + commandName + " in package: " + commandPackage);
              e.printStackTrace();
            } catch (InstantiationException e) {
              System.err.println(
                  "Could not instantiate command: "
                      + commandName
                      + " in package: "
                      + commandPackage);
              e.printStackTrace();
            } catch (IllegalAccessException e) {
              System.err.println(
                  "Could not access command: " + commandName + " in package: " + commandPackage);
              e.printStackTrace();
            }

            //                        command =
            // (Command)commandTable.get(commandName.toUpperCase());

            //                        if (command == null)
            //                        {
            //                            System.err.println("Could not find command: " +
            // commandName);
            //                        }
            //                        else if (command instanceof AutoSpreadsheetCommand)
            //                        {
            //                            System.out.println("Found Command: " + command.getName());
            //                            command = ((AutoSpreadsheetCommand)command).copy();
            //                            ((AutoSpreadsheetCommand)command).setStopAtEnd(stop);
            //                            ((AutoSpreadsheetCommand)command).setParam1(param1);
            //                            ((AutoSpreadsheetCommand)command).setParam2(param2);
            //                            ((AutoSpreadsheetCommand)command).setParam3(param3);
            //                            (command).setTimeout(timeout);
            //                            if (sequential)
            //                                cg.addSequential(command);
            //                            else
            //                                cg.addParallel(command);
            //
            //                        }
            //                        else if (command instanceof AutoSpreadsheetCommandGroup)
            //                        {
            //                            System.out.println("Found Command Group: " +
            // command.getName());
            //                            command = ((AutoSpreadsheetCommandGroup)command).copy();
            //                            if (sequential)
            //                                cg.addSequential(command);
            //                            else
            //                                cg.addParallel(command);
            //                        }
            //                        else
            //                        {
            //                            System.err.println(commandName + " (" + command.getName()
            // + ") is not instance of AutoSpreadsheetCommand");
            //                        }

            //                        System.out.println("Command: " + commandName + " Timeout: " +
            // timeout + " Continue: " + !stop + " Param1: " + param1 + " Param2: " + param2 + "
            // Param3: " + param3);
          }
        }
      }

    } catch (IOException ex) {
      ex.printStackTrace();
    } finally {
      try {
        reader.close();
      } catch (IOException ex) {
        ex.printStackTrace();
      }
    }
    return cg;
  }