Search in sources :

Example 1 with DuplicateNameException

use of com.qualcomm.robotcore.exception.DuplicateNameException in project robotcode by OutoftheBoxFTC.

the class WriteXMLFileHandler method writeToFile.

public void writeToFile(String data, File folder, String filenameWithExt) throws RobotCoreException, IOException {
    if (duplicates.size() > 0) {
        throw new DuplicateNameException("Duplicate names: " + duplicates);
    }
    boolean success = true;
    if (!folder.exists()) {
        success = folder.mkdir();
    }
    if (success) {
        File file = new File(folder, filenameWithExt);
        FileOutputStream stream = null;
        try {
            stream = new FileOutputStream(file);
            stream.write(data.getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                stream.close();
            } catch (IOException e) {
                // Auto-generated catch block
                e.printStackTrace();
            }
        }
    } else {
        throw new RobotCoreException("Unable to create directory");
    }
}
Also used : DuplicateNameException(com.qualcomm.robotcore.exception.DuplicateNameException) FileOutputStream(java.io.FileOutputStream) RobotCoreException(com.qualcomm.robotcore.exception.RobotCoreException) IOException(java.io.IOException) File(java.io.File) DuplicateNameException(com.qualcomm.robotcore.exception.DuplicateNameException) IOException(java.io.IOException) RobotCoreException(com.qualcomm.robotcore.exception.RobotCoreException)

Example 2 with DuplicateNameException

use of com.qualcomm.robotcore.exception.DuplicateNameException in project robotcode by OutoftheBoxFTC.

the class FtcConfigurationActivity method onDoneButtonPressed.

/**
 * A button-specific method, this gets called when you click the "Done" button.
 * This writes the current objects into an XML file located in the Configuration File Directory.
 * The user is prompted for the name of the file.
 *
 * @param v the View from which this was called
 */
public void onDoneButtonPressed(View v) {
    RobotLog.vv(TAG, "onDoneButtonPressed()");
    // Generate the XML. If that failed, we will already have complained to the user.
    final String data = robotConfigFileManager.toXml(getRobotConfigMap());
    if (data == null) {
        return;
    }
    String message = getString(R.string.configNamePromptBanter);
    final EditText input = new EditText(this);
    input.setText(currentCfgFile.isNoConfig() ? "" : currentCfgFile.getName());
    AlertDialog.Builder builder = utility.buildBuilder(getString(R.string.configNamePromptTitle), message);
    builder.setView(input);
    DialogInterface.OnClickListener okListener = new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int button) {
            String newConfigurationName = input.getText().toString();
            RobotConfigFileManager.ConfigNameCheckResult checkResult = robotConfigFileManager.isPlausibleConfigName(currentCfgFile, newConfigurationName, extantRobotConfigurations);
            if (!checkResult.success) {
                // User hasn't given us file name that works
                String message = String.format(checkResult.errorFormat, newConfigurationName);
                appUtil.showToast(UILocation.ONLY_LOCAL, context, String.format("%s %s", message, getString(R.string.configurationNotSaved)));
                return;
            }
            try {
                /*
                     * If the user changed the name then we create a new set of metadata for the
                     * new name and set the active config to be the new metadata
                     */
                if (!currentCfgFile.getName().equals(newConfigurationName)) {
                    currentCfgFile = new RobotConfigFile(robotConfigFileManager, newConfigurationName);
                }
                robotConfigFileManager.writeToFile(currentCfgFile, remoteConfigure, data);
                robotConfigFileManager.setActiveConfigAndUpdateUI(remoteConfigure, currentCfgFile);
            } catch (DuplicateNameException e) {
                warnDuplicateNames(e.getMessage());
                RobotLog.ee(TAG, e.getMessage());
                return;
            } catch (RobotCoreException | IOException e) {
                appUtil.showToast(UILocation.ONLY_LOCAL, context, e.getMessage());
                RobotLog.ee(TAG, e.getMessage());
                return;
            }
            clearDuplicateWarning();
            confirmSave();
            pauseAfterSave();
            finishOk();
        }
    };
    builder.setPositiveButton(getString(R.string.buttonNameOK), okListener);
    builder.setNegativeButton(getString(R.string.buttonNameCancel), doNothingAndCloseListener);
    builder.show();
}
Also used : EditText(android.widget.EditText) AlertDialog(android.app.AlertDialog) DialogInterface(android.content.DialogInterface) IOException(java.io.IOException) DuplicateNameException(com.qualcomm.robotcore.exception.DuplicateNameException) RobotCoreException(com.qualcomm.robotcore.exception.RobotCoreException)

Aggregations

DuplicateNameException (com.qualcomm.robotcore.exception.DuplicateNameException)2 RobotCoreException (com.qualcomm.robotcore.exception.RobotCoreException)2 IOException (java.io.IOException)2 AlertDialog (android.app.AlertDialog)1 DialogInterface (android.content.DialogInterface)1 EditText (android.widget.EditText)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1