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");
}
}
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();
}
Aggregations