/**
  * Loads the username and password from the preferences or shows the dialog where user can enter
  * his credentials.
  *
  * @param previous version of credentials that did not work
  * @return credentials if confirmed or an empty object if cancelled
  */
 public static Credentials getCredentials(Optional<Credentials> previous) {
   final Credentials[] provider = new Credentials[1];
   Subject subj = SecuritySupport.getSubject();
   final String currentUser =
       previous.isPresent()
           ? previous.get().getUsername()
           : subj == null ? null : SecuritySupport.getSubjectName(subj);
   org.eclipse.swt.widgets.Display.getDefault()
       .syncExec(
           () -> {
             String username = null;
             char[] password = null;
             boolean remember = false;
             if (previous.isPresent()) {
               username = previous.get().getUsername();
               password = previous.get().getPassword();
               remember = previous.get().isRemember();
             } else {
               username = getUsername(Optional.empty());
               password = getPassword(Optional.empty(), username).orElse(null);
             }
             if (username == null || password == null || previous.isPresent()) {
               new UsernameAndPasswordDialog(
                       PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
                       username,
                       remember,
                       "Please, provide the username and password to access Save and Restore git repository")
                   .openAndWat()
                   .ifPresent(
                       e -> {
                         provider[0] = e;
                         if (e.isRemember()) {
                           storeCredentials(
                               Optional.ofNullable(currentUser), e.getUsername(), e.getPassword());
                         }
                       });
             } else {
               provider[0] = new Credentials(username, password, false);
             }
           });
   return provider[0];
 }
  @Override
  protected void okPressed() {
    final Cursor originalCursor = getShell().getCursor();

    getButton(IDialogConstants.OK_ID).setEnabled(false);

    try {
      // get logbook client
      final LogbookClient logbookClient =
          LogbookClientManager.getLogbookClientFactory()
              .getClient(username.getText(), password.getText());
      getShell().setCursor(Display.getDefault().getSystemCursor(SWT.CURSOR_WAIT));

      String signature = SecuritySupport.getSubjectName(SecuritySupport.getSubject());
      PropertyBuilder SignOffProperty = property("SignOff").attribute("signature", signature);

      StringBuffer sb = new StringBuffer();
      sb.append("signature:" + signature + System.getProperty("line.separator"));

      final Collection<LogEntry> logEntires = new ArrayList<LogEntry>();
      for (LogEntryBuilder logEntryBuilder : data) {
        logEntryBuilder.addProperty(SignOffProperty);
        sb.append("logEntry: " + logEntryBuilder + System.getProperty("line.separator"));
        logEntires.add(logEntryBuilder.build());
      }
      Job job =
          new Job("Review and Sign Log Entries") {

            @Override
            protected IStatus run(IProgressMonitor monitor) {
              try {
                logbookClient.updateLogEntries(logEntires);
                return Status.OK_STATUS;
              } catch (final Exception e) {
                getShell()
                    .getDisplay()
                    .asyncExec(
                        new Runnable() {

                          @Override
                          public void run() {
                            getShell().setCursor(originalCursor);
                            getButton(IDialogConstants.OK_ID).setEnabled(true);
                            errorBar.setException(e);
                          }
                        });
                return Status.CANCEL_STATUS;
              }
            }
          };
      job.addJobChangeListener(
          new JobChangeAdapter() {
            public void done(IJobChangeEvent event) {
              if (event.getResult().isOK()) {
                getShell()
                    .getDisplay()
                    .asyncExec(
                        new Runnable() {

                          @Override
                          public void run() {
                            getShell().setCursor(originalCursor);
                            getButton(IDialogConstants.OK_ID).setEnabled(true);
                            setReturnCode(OK);
                            close();
                          }
                        });
              }
            }
          });
      job.schedule();
    } catch (Exception ex) {
      getShell().setCursor(originalCursor);
      getButton(IDialogConstants.OK_ID).setEnabled(true);
      errorBar.setException(ex);
    }
  }