package rhogenwizard.rhohub;

import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jgit.errors.UnsupportedCredentialItem;
import org.eclipse.jgit.transport.CredentialItem;
import org.eclipse.jgit.transport.CredentialsProvider;
import org.eclipse.jgit.transport.URIish;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;

import rhogenwizard.DialogUtils;

public class GitCredentialsProvider extends CredentialsProvider
{
    @Override
    public boolean get(URIish arg0, final CredentialItem... arg1) throws UnsupportedCredentialItem
    {
    	if (arg1[0] instanceof CredentialItem.YesNoType)
    	{
    		CredentialItem.YesNoType yesnoCred = (CredentialItem.YesNoType)arg1[0];
    		
    		yesnoCred.setValue(DialogUtils.quetsion("Git", arg1[0].getPromptText()));
    	}
    	else if (arg1[0] instanceof CredentialItem.StringType)
    	{
	    	Display.getDefault().syncExec(new Runnable() 
	    	{
	    		public void run() 
	    		{
	    			try 
	    			{
				        InputDialog dlg = new InputDialog(Display.getCurrent().getActiveShell(), 
				            "Git password", "Please enter you git password.", "", null) 
				        {
				            @Override
				            protected int getInputTextStyle() 
				            {
				                return SWT.SINGLE | SWT.BORDER | SWT.PASSWORD;
				            }
				        };
				        dlg.open();
				        
				        CredentialItem.StringType pwdCred = (CredentialItem.StringType)arg1[0];
				        pwdCred.setValue(dlg.getValue());
	    			} 
	    			catch (Exception ex) 
	    			{
	    				ex.printStackTrace();
	    			}
	    		}
	    	});
    	}
    	
        return true;
    }

    @Override
    public boolean isInteractive()
    {
        return false;
    }

    @Override
    public boolean supports(CredentialItem... arg0)
    {
        return false;
    }
}

@@ -69,16 +69,56 @@
 Dialog;%0A
+import org.eclipse.jface.window.Window;%0A
 import o
@@ -159,32 +159,32 @@
 CredentialItem;%0A
-
 import org.eclip
@@ -342,16 +342,53 @@
 wt.SWT;%0A
+import org.eclipse.swt.SWTException;%0A
 import o
@@ -1038,32 +1038,8 @@
 %09%09%7B%0A
-%09    %09%09%09try %0A%09    %09%09%09%7B%0A%09
 %09%09%09 
@@ -1118,25 +1118,24 @@
 hell(), %0A%09%09%09
-%09
             
@@ -1180,17 +1180,17 @@
 password
-.
+:
 %22, %22%22, n
@@ -1195,17 +1195,16 @@
  null) %0A
-%09
 %09%09%09     
@@ -1203,33 +1203,32 @@
 %09%09%09        %7B%0A%09%09%09
-%09
             @Ove
@@ -1233,17 +1233,16 @@
 verride%0A
-%09
 %09%09%09     
@@ -1282,25 +1282,24 @@
 Style() %0A%09%09%09
-%09
             
@@ -1300,17 +1300,16 @@
       %7B%0A
-%09
 %09%09%09     
@@ -1365,25 +1365,24 @@
 ASSWORD;%0A%09%09%09
-%09
             
@@ -1383,17 +1383,16 @@
       %7D%0A
-%09
 %09%09%09     
@@ -1398,16 +1398,27 @@
     %7D;%0A%09
+%09%09        %0A
 %09%09%09     
@@ -1420,16 +1420,20 @@
         
+if (
 dlg.open
@@ -1434,30 +1434,59 @@
 g.open()
-;%0A%09%09
+ == Window.CANCEL)%0A%09%09%09        %7B%0A
 %09%09
+%09
         
 %0A%09%09%09%09   
@@ -1477,18 +1477,89 @@
         
-%0A
 %09
+throw new SWTException(SWT.ERROR_FAILED_EXEC);%0A%09%09%09        %7D%0A%09%09%09        %0A
 %09%09%09     
@@ -1636,17 +1636,16 @@
 %5B0%5D;%0A%09%09%09
-%09
         
@@ -1679,102 +1679,40 @@
 ue()
-);%0A%09    %09%09%09%7D %0A%09    %09%09%09catch (Exception ex) %0A%09    %09%09%09%7B%0A%09    %09%09%09%09ex.printStackTrace();%0A%09    %09%09%09%7D
+ == null ? %22%22 : dlg.getValue());
 %0A%09  

fix cancelling git pwd dialog