for (final Map.Entry<String, String> mapEntry : sourceElementMap.entrySet()) {
String unqualifiedGemName = mapEntry.getKey();
String gemSource = mapEntry.getValue();
QualifiedName qualifiedGemName = QualifiedName.make(getWorkingModuleName(), unqualifiedGemName);
Status saveStatus = getWorkspace().saveEntity(qualifiedGemName, gemSource, null, null);
lastGemName = unqualifiedGemName;
if (saveStatus.getSeverity() == Status.Severity.ERROR) {
String errTitle = getResourceString("CannotSaveDialogTitle");
String errMessage = getResourceString("ErrorSavingGemDefinition");
DetailsDialog dialog = new DetailsDialog(GemCutter.this, errTitle, errMessage,
saveStatus.getDebugMessage(), DetailsDialog.MessageType.ERROR);
dialog.doModal();
// Don't bother trying to save the other gems.
break;
}
}
}
Status generationStatus = new Status("Generation status.");
SourceModel.ModuleDefn moduleDefn = definitions.getModuleDefn();
if (moduleDefn != null) {
// There was a generated module defn.
ModuleName generatedModuleName = SourceModel.Name.Module.toModuleName(moduleDefn.getModuleName());
// Can't overwrite the prelude.
if (generatedModuleName.equals(CAL_Prelude.MODULE_NAME)) {
String errTitle = getResourceString("CannotSaveDialogTitle");
String errMessage = getResourceString("CannotClobberPrelude");
JOptionPane.showMessageDialog(GemCutter.this, errMessage, errTitle, JOptionPane.ERROR_MESSAGE);
return;
}
// Check whether the module name exists, and if so confirm that the user wished to clobber.
// This goes to the workspace source manager -- so it's a bit hacky.
// We can't just ask the workspace if it has the module though, since it might be the case where the workspace isn't
// using a module in the nullary case.
if (getWorkspace().getSourceManager(generatedModuleName).getResourceStore().hasFeature(
new ResourceName(CALFeatureName.getModuleFeatureName(generatedModuleName)))) {
// TODOEL: Ideally, if the user does not want to clobber the existing module, we should
// go back to the dialog with the existing inputs.
String warningTitle = getResourceString("WarningDialogTitle");
String errMessage = getResourceString("ModuleExistsWarning");
int continueChoice = JOptionPane.showConfirmDialog(GemCutter.this, errMessage, warningTitle, JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
if (continueChoice != JOptionPane.OK_OPTION) {
return;
}
}
// *** HACK ***
// *** Fix me when we can handle modules without vault info.
// Ok, now we have the generated module defn. Where do we put it?
// It should go into the workspace, but the workspace can't handle modules without any vault info.
// So for now we create a temp file first, and add from there.
// We could use NonExistentVault instead, but that requires some work to implement StoredVaultElement.Module.
String tmpDir = System.getProperty("java.io.tmpdir");
String fileName = generatedModuleName + ".cal";
File tempFile = new File(tmpDir, fileName);
String sourceText = moduleDefn.toSourceText();
Writer writer = null;
try {
writer = new BufferedWriter(new FileWriter(tempFile));
writer.write(sourceText);
} catch (IOException ioe) {
generationStatus.add(new Status(Status.Severity.ERROR, "Error writing file: " + tempFile, ioe));
} finally {
if (writer != null) {
try {
writer.flush();
writer.close();
} catch (IOException ioe) {
// Not much we can do about this.
}
}
}
// Add the module to the workspace.
// Note: this can result in errors in cases where the module is erroneous.
// For instance, if generating a foreign import module, this can result in errors if the module refers to
// foreign classes which are not on the classpath.
boolean addModuleAttemptSuccessful = false;
if (generationStatus.getSeverity().compareTo(Status.Severity.ERROR) < 0) {
SimpleCALFileVault simpleCALFileVault = SimpleCALFileVault.getSimpleCALFileVault(tempFile);
// This call also calls recompileWorkspace(true).
if (simpleCALFileVault != null) {
handleAddModuleAttempt(simpleCALFileVault, generatedModuleName, -1, false);
addModuleAttemptSuccessful = true;
} else {
String details = getResourceString("CannotCreateSimpleCALFileVault");
showProblemsGeneratingModuleDialog(DetailsDialog.MessageType.ERROR, details);
}
}
// Delete the tempFile. Don't worry too much about if this fails..
tempFile.delete();
// Select the module which was compiled, if any.
if (addModuleAttemptSuccessful && generationStatus.getSeverity().compareTo(Status.Severity.ERROR) < 0) {
getGemBrowser().getBrowserTree().selectDrawerNode(generatedModuleName);
// Also display a message.
String statusMessage = GemCutterMessages.getString("SM_ModuleGenerated", generatedModuleName);
statusMessageManager.displayMessage(this, statusMessage, StatusMessageDisplayer.MessageType.TRANSIENT, true);
}
if (generationStatus.getSeverity().compareTo(Status.Severity.WARNING) >= 0) {
String details = generationStatus.getDebugMessage();
DetailsDialog.MessageType messageType = generationStatus.getSeverity() == Status.Severity.WARNING ?
DetailsDialog.MessageType.WARNING : DetailsDialog.MessageType.ERROR;
showProblemsGeneratingModuleDialog(messageType, details);
}