package rhogenwizard.preferences;

import org.eclipse.jface.preference.StringFieldEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.IWorkbench;

import rhogenwizard.Activator;
import rhogenwizard.rhohub.IRhoHubSetting;
import rhogenwizard.rhohub.RhoHubCommands;
import rhogenwizard.rhohub.TokenChecker;

public class PreferencesPageRhoHub extends BasePreferencePage
{
    private PreferenceInitializer m_pInit = null;

    public PreferencesPageRhoHub()
    {
        super(GRID);
        setPreferenceStore(Activator.getDefault().getPreferenceStore());
        setDescription("");
    }

    @Override
    public boolean performOk()
    {
        boolean ret = super.performOk();

        try
        {
            m_pInit.savePreferences();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return ret;
    }

    public void createFieldEditors()
    {
        checkRhodesSdk();
    }

    @Override
    protected Control createContents(Composite parent)
    {
        Composite top = new Composite(parent, SWT.LEFT);

        StringFieldEditor editor =
            new StringFieldEditor(IRhoHubSetting.rhoHubProxy, "&HTTP proxy:", top);
        editor.setPreferenceStore(Activator.getDefault().getPreferenceStore());
        editor.setPage(this);
        editor.load();

        top.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

        GridData checkBoxAligment = new GridData();
        checkBoxAligment.horizontalAlignment = GridData.FILL;
        checkBoxAligment.horizontalSpan = 3;

        Button loginButton = new Button(top, SWT.NONE);
        loginButton.setText("Login rhomobile.com");
        loginButton.setLayoutData(checkBoxAligment);
        loginButton.addSelectionListener(new SelectionListener()
        {
            @Override
            public void widgetSelected(SelectionEvent e)
            {
                String rhodesPath = m_pInit.getRhodesPath();
                RhoHubCommands.logout(rhodesPath);
                TokenChecker.login(rhodesPath);
            }

            @Override
            public void widgetDefaultSelected(SelectionEvent e)
            {
            }
        });

        Button logoutButton = new Button(top, SWT.NONE);
        logoutButton.setText("Logout rhomobile.com");
        logoutButton.setLayoutData(checkBoxAligment);
        logoutButton.addSelectionListener(new SelectionListener()
        {
            @Override
            public void widgetSelected(SelectionEvent e)
            {
                RhoHubCommands.logout(m_pInit.getRhodesPath());
            }

            @Override
            public void widgetDefaultSelected(SelectionEvent e)
            {
            }
        });

        return top;
    }

    public void init(IWorkbench workbench)
    {
        m_pInit = PreferenceInitializer.getInstance();
    }
}
@@ -206,16 +206,55 @@
 stener;%0A
+import org.eclipse.swt.graphics.Point;%0A
 import o
@@ -285,16 +285,16 @@
 idData;%0A
-
 import o
@@ -406,16 +406,55 @@
 ontrol;%0A
+import org.eclipse.swt.widgets.Layout;%0A
 import o
@@ -1360,322 +1360,14 @@
     
-Composite top = new Composite(parent, SWT.LEFT);%0A%0A        StringFieldEditor editor =%0A            new StringFieldEditor(IRhoHubSetting.rhoHubProxy, %22&HTTP proxy:%22, top);%0A        editor.setPreferenceStore(Activator.getDefault().getPreferenceStore());%0A        editor.setPage(this);%0A        editor.load();%0A%0A        top
+parent
 .set
@@ -1611,35 +1611,38 @@
 on = new Button(
-top
+parent
 , SWT.NONE);%0A   
@@ -2254,19 +2254,22 @@
  Button(
-top
+parent
 , SWT.NO
@@ -2768,18 +2768,71 @@
     
+//
 return 
-top
+parent;%0A        return new Composite(parent, SWT.NULL)
 ;%0A  

fix create controls without proxy edit field