Shell shell = HandlerUtil.getActiveWorkbenchWindow(event).getShell();
// Get the view
IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
IWorkbenchPage page = window.getActivePage();
View view = (View) page.findView(View.ID);
// File standard dialog
FileDialog fileDialog = new FileDialog(shell, SWT.SAVE);
// Set the text
fileDialog.setText("Save as...");
// Open Dialog and save result of selection
String selected = fileDialog.open();
if (selected == null) {
logger.info("File selection aborted");
return null;
}
File file = new File(selected);
logger.debug("File selected " + file.getAbsolutePath());
// if the file exists, check for writeability
if (!file.exists() || (file.exists() && file.canWrite())) {
logger.debug("File is writable");
// enter password for the 1st time
PasswordInputDialog passwordDialog = new PasswordInputDialog(shell,
"Password for " + file.getName(), "Enter password ", "",
new DummyInputValidator());
passwordDialog.open();
String password1 = passwordDialog.getValue();
logger.debug("Entered password for the 1st time, length is "
+ password1.length());
// Re-enter password. Reusing the same dialog here is quicker to
// implement than using a dialog with two password fields.
passwordDialog = new PasswordInputDialog(shell, "Password for "
+ file.getName(), "Re-Enter password ", "",
new DummyInputValidator());
passwordDialog.open();
String password2 = passwordDialog.getValue();
logger.debug("Entered password for the 1st time, length is "
+ password1.length());
if (password1.equals(password2)) {
logger.info("Entered passwords match, OK");
String password = password1;
PWListContentProvider contentProvider = (PWListContentProvider) view
.getContentProvider();
try {
contentProvider.saveEncryptedFile(password, file);
MessageDialog.showInfoMessage("Successfully saved to "
+ file.getAbsolutePath(), "File saved", logger);