if (isProjectNotLoaded(project, presentation)) {
Messages.showWarningDialog("Project not loaded.", "FindBugs"); // NON-NLS
return;
}
final FindBugsPreferences preferences = getPluginInterface(project).getPreferences();
String exportDir = preferences.getProperty(FindBugsPreferences.EXPORT_BASE_DIR, FindBugsPluginConstants.DEFAULT_EXPORT_DIR);
boolean exportXml = preferences.getBooleanProperty(FindBugsPreferences.EXPORT_AS_XML, true);
boolean exportHtml = preferences.getBooleanProperty(FindBugsPreferences.EXPORT_AS_HTML, true);
boolean exportBoth = exportXml && preferences.getBooleanProperty(FindBugsPreferences.EXPORT_AS_HTML, true);
if (exportDir.isEmpty() || !exportXml && !exportBoth && !exportHtml) {
//Ask the user for a export directory
final DialogBuilder dialogBuilder = new DialogBuilder(project);
dialogBuilder.addOkAction();
dialogBuilder.addCancelAction();
dialogBuilder.setTitle("Select directory to save the exported file");
final ExportFileDialog exportDialog = new ExportFileDialog(exportDir, dialogBuilder);
dialogBuilder.showModal(true);
if (dialogBuilder.getDialogWrapper().getExitCode() == DialogWrapper.CANCEL_EXIT_CODE) {
return;
}
final String path = exportDialog.getText();
if (path == null || path.trim().isEmpty()) {
return;
}
exportXml = exportDialog.isXml() != exportXml ? exportDialog.isXml() : exportXml;
exportHtml = exportDialog.isXml() == exportHtml ? !exportDialog.isXml() : exportHtml;
exportBoth = exportDialog.isBoth() != exportBoth ? exportDialog.isBoth() : exportBoth;
exportDir = path.trim();
}
//Create a unique file name by using time stamp
final Date currentDate = new Date();
final String timestamp = PATTERN.matcher(new SimpleDateFormat().format(currentDate)).replaceAll("_");
final String fileName = File.separatorChar + FINDBUGS_RESULT_PREFIX + timestamp;
final boolean finalExportXml = exportXml;
final boolean finalExportHtml = exportHtml;
final boolean finalExportBoth = exportBoth;
final String finalExportDir = exportDir + File.separatorChar + project.getName();
//Create a task to export the bug collection to html
final AtomicReference<Task> exportTask = new AtomicReference<Task>(new BackgroundableTask(project, "Exporting Findbugs Result", false) {
private ProgressIndicator _indicator;
@edu.umd.cs.findbugs.annotations.SuppressFBWarnings({"REC_CATCH_EXCEPTION"})
@SuppressWarnings({"IOResourceOpenedButNotSafelyClosed"})
@Override
public void run(@NotNull final ProgressIndicator indicator) {
indicator.setText2(finalExportDir + File.separatorChar + fileName);
setProgressIndicator(indicator);
Writer writer = null;
try {
createDirIfAbsent(project, finalExportDir);
String exportDir = finalExportDir;
final boolean createSubDir = preferences.getBooleanProperty(FindBugsPreferences.EXPORT_CREATE_ARCHIVE_DIR, true);
if(createSubDir) {
exportDir = finalExportDir + File.separatorChar + new SimpleDateFormat("yyyy_MM_dd", Locale.ENGLISH).format(currentDate);
createDirIfAbsent(project, exportDir);
}
if (_bugCollection != null) {
_bugCollection.setWithMessages(true);
final String exportDirAndFilenameWithoutSuffix = exportDir + fileName;
if (finalExportXml && !finalExportBoth) {
exportXml(exportDirAndFilenameWithoutSuffix + FINDBUGS_RESULT_RAW_SUFFIX);
} else if (finalExportBoth) {
exportXml(exportDirAndFilenameWithoutSuffix + FINDBUGS_RESULT_RAW_SUFFIX);
writer = exportHtml(exportDirAndFilenameWithoutSuffix + FINDBUGS_RESULT_HTML_SUFFIX);
} else if (finalExportHtml) {
writer = exportHtml(exportDirAndFilenameWithoutSuffix + FINDBUGS_RESULT_HTML_SUFFIX);
}
_bugCollection.setWithMessages(false);
showToolWindowNotifier(project, "Exported bug collection to " + exportDir + '.', MessageType.INFO);
if((!finalExportXml || finalExportBoth) && preferences.getBooleanProperty(FindBugsPreferences.EXPORT_OPEN_BROWSER, true)) {
BrowserUtil.launchBrowser(new File(exportDirAndFilenameWithoutSuffix + FINDBUGS_RESULT_HTML_SUFFIX).getAbsolutePath());
}
}
} catch (final IOException e1) {
final String message = "Export failed";