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.swt.widgets.Label;
import org.eclipse.ui.IWorkbench;

import rhogenwizard.Activator;
import rhogenwizard.rhohub.IRhoHubSetting;
import rhogenwizard.sdk.task.rhohub.TokenTask;

public class PreferencesPageRhoHub extends BasePreferencePage 
{
    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();
        
        addField(new StringFieldEditor(IRhoHubSetting.rhoHubUrl, 
                "&RhoHub API Endpoint (advanced):", getFieldEditorParent()));
                
        addField(new StringFieldEditor(IRhoHubSetting.rhoHubToken, 
                "&API Token:", getFieldEditorParent()));
        
        addField(new StringFieldEditor(IRhoHubSetting.rhoHubProxy, 
                "&HTTP proxy:", getFieldEditorParent()));
    }

    @Override
	protected Control createContents(Composite parent) 
    {
    	StringFieldEditor editor = null;
   		
    	Composite top = new Composite(parent, SWT.LEFT);
    	
    	editor = new StringFieldEditor(IRhoHubSetting.rhoHubUrl, "&RhoHub API Endpoint (advanced):", top);
    	editor.setPreferenceStore(getPreferenceStore());
    	editor.setPreferencePage(this);
    	editor.load();
    	
    	editor = new StringFieldEditor(IRhoHubSetting.rhoHubToken, "&API Token:", top);
    	editor.setPreferenceStore(getPreferenceStore());
    	editor.setPreferencePage(this);
    	editor.load();
    	
    	editor = new StringFieldEditor(IRhoHubSetting.rhoHubProxy, "&HTTP proxy:", top);
    	editor.setPreferenceStore(getPreferenceStore());
    	editor.setPreferencePage(this);
    	editor.load();
    	    	
    	top.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    	
        GridData checkBoxAligment = new GridData();
        checkBoxAligment.horizontalAlignment = GridData.FILL;
        checkBoxAligment.horizontalSpan = 3;
        
		Button clearButton = new Button(top, SWT.NONE);
		clearButton.setText("Clear token");
		clearButton.setLayoutData(checkBoxAligment);
    	clearButton.addSelectionListener(new SelectionListener() 
    	{
			@Override
			public void widgetSelected(SelectionEvent e) 
			{
				TokenTask.clearToken(m_pInit.getRhodesPath());
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				// TODO Auto-generated method stub				
			}			
		});
    	
		return top;
	}

	public void init(IWorkbench workbench) 
    {
        m_pInit = PreferenceInitializer.getInstance();
    }   
}
@@ -372,46 +372,8 @@
 ol;%0A
-import org.eclipse.swt.widgets.Label;%0A
 impo
@@ -402,16 +402,16 @@
 kbench;%0A
+
 %0Aimport 
@@ -594,16 +594,24 @@
  %0A%7B%0A    
+private 
 Preferen
@@ -640,16 +640,65 @@
 = null;%0A
+    private StringFieldEditor tokenField = null;%0A
     %0A   
@@ -977,32 +977,71 @@
  try %0A        %7B%0A
+        %09tokenField.store();%0A        %09%0A
             m_pI
@@ -1258,445 +1258,19 @@
 ();%0A
-        %0A        addField(new StringFieldEditor(IRhoHubSetting.rhoHubUrl, %0A                %22&RhoHub API Endpoint (advanced):%22, getFieldEditorParent()));%0A                %0A        addField(new StringFieldEditor(IRhoHubSetting.rhoHubToken, %0A                %22&API Token:%22, getFieldEditorParent()));%0A        %0A        addField(new StringFieldEditor(IRhoHubSetting.rhoHubProxy, %0A                %22&HTTP proxy:%22, getFieldEditorParent()));%0A
     %7D%0A
+    
 %0A
-
     
@@ -1569,32 +1569,55 @@
 PreferenceStore(
+Activator.getDefault().
 getPreferenceSto
@@ -1794,32 +1794,55 @@
 PreferenceStore(
+Activator.getDefault().
 getPreferenceSto
@@ -1903,32 +1903,64 @@
 r.load();%0A    %09%0A
+    %09tokenField = editor;%0A    %09%0A
     %09editor = ne
@@ -2060,16 +2060,39 @@
 ceStore(
+Activator.getDefault().
 getPrefe
@@ -2684,16 +2684,16 @@
 ) %0A%09%09%09%7B%0A
-
 %09%09%09%09Toke
@@ -2735,16 +2735,73 @@
 ath());%0A
+%09%09        m_pInit = PreferenceInitializer.getInstance();%0A
 %09%09%09%7D%0A%0A%09%09

fix set token