Package com.onpositive.gae.tools.core

Source Code of com.onpositive.gae.tools.core.ConnectionOptionsManager

package com.onpositive.gae.tools.core;

import org.eclipse.equinox.security.storage.SecurePreferencesFactory;
import org.eclipse.equinox.security.storage.StorageException;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Display;

import com.google.appengine.tools.admin.AppAdminFactory.ConnectOptions;
import com.google.appengine.tools.admin.AppAdminFactory.PasswordPrompt;
import com.onpositive.commons.ui.dialogs.TitledDialog;
import com.onpositive.gae.tools.Activator;
import com.onpositive.semantic.model.api.property.java.annotations.Caption;
import com.onpositive.semantic.model.api.property.java.annotations.Required;
import com.onpositive.semantic.model.binding.Binding;
import com.onpositive.semantic.model.ui.property.editors.ButtonSelector;
import com.onpositive.semantic.model.ui.property.editors.CompositeEditor;
import com.onpositive.semantic.model.ui.property.editors.DisposeBindingListener;
import com.onpositive.semantic.model.ui.property.editors.OneLineTextElement;

public class ConnectionOptionsManager {

  public static class Options {
    @Caption("User Id:")
    @Required
    String usId;
    @Caption("Password:")
    @Required
    String password;
    @Caption("Always use this credentials:")
    boolean alwaysUse;
    boolean ok;
  }

  public static void clearAlwaysUse(final String appId) {
    Activator.getDefault().getPreferenceStore().setValue(
        "alwaysUseFor" + appId, true);
  }

  public static ConnectOptions getOptions(final String appId) {
    boolean alwaysUse = Activator.getDefault().getPreferenceStore()
        .getBoolean("alwaysUseFor" + appId);
    boolean storePass = alwaysUse;
    String userId = Activator.getDefault().getPreferenceStore().getString(
        "userIdFor" + appId);
    if (alwaysUse && storePass) {

      try {
        final String password = SecurePreferencesFactory.getDefault()
            .get(Activator.PLUGIN_ID + "." + appId, null);
        if (userId != null && userId.length() != 0 && password != null) {
          ConnectOptions connectOptions = new ConnectOptions();
          connectOptions.setUserId(userId);
          connectOptions.setPasswordPrompt(new PasswordPrompt() {

            public String getPassword() {
              return password;
            }
          });
          return connectOptions;
        }
      } catch (final StorageException e) {
        Display.getDefault().syncExec(new Runnable() {

          public void run() {
            MessageDialog.openError(Display.getDefault()
                .getActiveShell(), "Error", e.getMessage());
          }
        });

      }
    }
    final Options options = new Options();
    options.usId = userId;
    if (storePass) {
      try {
        options.password = SecurePreferencesFactory.getDefault().get(
            Activator.PLUGIN_ID + "." + appId, null);
      } catch (final StorageException e) {
        Display.getDefault().syncExec(new Runnable() {

          public void run() {
            MessageDialog.openError(Display.getDefault()
                .getActiveShell(), "Error", e.getMessage());
          }
        });
      }
    }

    options.alwaysUse = alwaysUse;
    Display.getDefault().syncExec(new Runnable() {

      public void run() {
        CompositeEditor compositeEditor = new CompositeEditor(options,
            true);

        Binding binding = (Binding) compositeEditor.getBinding();
        binding.setName("Credentials");
        binding
            .setDescription("Authentification to Google Account for application: "
                + appId);
        compositeEditor.add(new OneLineTextElement<String>(binding
            .getBinding("usId")));
        OneLineTextElement<String> element = new OneLineTextElement<String>(
            binding.getBinding("password"));
        element.setIsPassword(true);
        Binding binding2 = binding.getBinding("alwaysUse");
        binding2
            .setName("Remember password(Could trigger secure storage login)");
        ButtonSelector sl = new ButtonSelector(binding2);
        compositeEditor.add(element);
        compositeEditor.add(sl);

        TitledDialog t = new TitledDialog(compositeEditor);
        DisposeBindingListener.linkBindingLifeCycle(binding,
            compositeEditor);
        int open = t.open();
        options.ok = open == Dialog.OK;
      }
    });
    if (options.ok) {
      ConnectOptions cm = new ConnectOptions();
      cm.setUserId(options.usId);
      cm.setPasswordPrompt(new PasswordPrompt() {

        public String getPassword() {
          return options.password;
        }
      });
      Display.getDefault().syncExec(new Runnable() {

        public void run() {

        }
      });
      Activator.getDefault().getPreferenceStore().setValue(
          "alwaysUseFor" + appId, options.alwaysUse);
      Activator.getDefault().getPreferenceStore().setValue(
          "userIdFor" + appId, options.usId);
      if (options.alwaysUse) {
        try {
          SecurePreferencesFactory.getDefault().put(
              Activator.PLUGIN_ID + "." + appId,
              options.password, true);
        } catch (final StorageException e) {
          Display.getDefault().syncExec(new Runnable() {

            public void run() {
              MessageDialog.openError(Display.getDefault()
                  .getActiveShell(), "Error", e.getMessage());
            }
          });
        }
      }
      return cm;
    }
    return null;
  }
}
TOP

Related Classes of com.onpositive.gae.tools.core.ConnectionOptionsManager

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.