Author: slotia
Date: 2009-04-16 09:02:17 -0700 (Thu, 16 Apr 2009)
New Revision: 16600

Added:
   
core3/core-task-impl/trunk/src/main/java/org/cytoscape/task/internal/proxysettings/
   
core3/core-task-impl/trunk/src/main/java/org/cytoscape/task/internal/proxysettings/ProxySettings.java
   
core3/core-task-impl/trunk/src/main/java/org/cytoscape/task/internal/proxysettings/TestProxySettings.java
Log:
Added proxy settings dialog


Added: 
core3/core-task-impl/trunk/src/main/java/org/cytoscape/task/internal/proxysettings/ProxySettings.java
===================================================================
--- 
core3/core-task-impl/trunk/src/main/java/org/cytoscape/task/internal/proxysettings/ProxySettings.java
       2009-04-16 14:56:10 UTC (rev 16599)
+++ 
core3/core-task-impl/trunk/src/main/java/org/cytoscape/task/internal/proxysettings/ProxySettings.java
       2009-04-16 16:02:17 UTC (rev 16600)
@@ -0,0 +1,68 @@
+package org.cytoscape.task.internal.proxysettings;
+
+import java.util.Map;
+import java.util.HashMap;
+import java.util.ArrayList;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.CancellationException;
+
+import java.net.Proxy;
+
+import org.cytoscape.work.Tunable;
+import org.cytoscape.work.TunableValidator;
+import org.cytoscape.work.Tunable.Param;
+import org.cytoscape.work.util.ListSingleSelection;
+
+import org.cytoscape.work.TaskManager;
+import org.cytoscape.work.ValuedTaskExecutor;
+
+class ProxySettings implements TunableValidator
+{
+       static final Map<String, Proxy.Type> types = new HashMap<String, 
Proxy.Type>(4, 1.0f);
+       static
+       {
+               types.put("direct", Proxy.Type.DIRECT);
+               types.put("http", Proxy.Type.HTTP);
+               types.put("socks", Proxy.Type.SOCKS);
+       }
+
+       @Tunable(description="Type")
+       public ListSingleSelection<String> type = new 
ListSingleSelection<String>(new ArrayList<String>(types.keySet()));
+
+       @Tunable(description="Proxy 
Server",group={""},dependsOn="type!=direct",alignment={Param.horizontal})
+       public String hostname="";
+
+       
@Tunable(description="Port",group={""},dependsOn="type!=direct",alignment={Param.horizontal})
+       public int port = 0;
+
+       @Tunable(description="Check proxy settings now")
+       public boolean checkSettings = false;
+
+       final TaskManager taskManager;
+
+       public ProxySettings(final TaskManager taskManager)
+       {
+               this.taskManager = taskManager;
+       }
+
+       public String validate()
+       {
+               if (!checkSettings) return null;
+               ValuedTaskExecutor<Exception> executor = new 
ValuedTaskExecutor<Exception>(new TestProxySettings(types.get(type), hostname, 
port));
+               taskManager.execute(executor);
+               Exception exception = null;
+               try
+               {
+                       exception = executor.get();
+               }
+               catch (Exception ex)
+               {
+                       return null;
+               }
+
+               if (exception == null)
+                       return null;
+               else
+                       return String.format("Cytoscape was unable to connect 
to the Internet.\nPlease make sure the proxy settings are correct and try 
again.\n\n%s", exception.getMessage());
+       }
+}

Added: 
core3/core-task-impl/trunk/src/main/java/org/cytoscape/task/internal/proxysettings/TestProxySettings.java
===================================================================
--- 
core3/core-task-impl/trunk/src/main/java/org/cytoscape/task/internal/proxysettings/TestProxySettings.java
   2009-04-16 14:56:10 UTC (rev 16599)
+++ 
core3/core-task-impl/trunk/src/main/java/org/cytoscape/task/internal/proxysettings/TestProxySettings.java
   2009-04-16 16:02:17 UTC (rev 16600)
@@ -0,0 +1,78 @@
+package org.cytoscape.task.internal.proxysettings;
+
+import org.cytoscape.work.ValuedTask;
+import org.cytoscape.work.TaskMonitor;
+
+import java.net.Proxy;
+import java.net.SocketAddress;
+import java.net.InetSocketAddress;
+import java.net.URL;
+import java.net.URLConnection;
+
+class TestProxySettings implements ValuedTask<Exception>
+{
+       static String TEST_URL = "http://www.google.com";;
+       static int CONNECT_TIMEOUT_IN_MILLISECONDS = 5000;
+
+       final Proxy.Type proxytype;
+       final String hostname;
+       final int port;
+
+       boolean cancel = false;
+
+       public TestProxySettings(Proxy.Type proxytype, String hostname, int 
port)
+       {
+               this.proxytype = proxytype;
+               this.hostname = hostname;
+               this.port = port;
+       }
+
+       public Exception run(TaskMonitor taskMonitor)
+       {
+               taskMonitor.setTitle("Testing Proxy Settings");
+               try
+               {
+                       taskMonitor.setStatusMessage("Resolving proxy server 
address...");
+
+                       Proxy proxy = null;
+                       if (proxytype == Proxy.Type.DIRECT)
+                       {
+                               proxy = Proxy.NO_PROXY;
+                       }
+                       else
+                       {
+                               SocketAddress address = new 
InetSocketAddress(hostname, port);
+                               proxy = new Proxy(proxytype, address);
+                       }
+
+                       if (cancel) return null;
+                       taskMonitor.setStatusMessage("Attempting to open the 
URL connection...");
+                       
+                       URL url = new URL(TEST_URL);
+                       URLConnection urlConnection = url.openConnection(proxy);
+                       
urlConnection.setConnectTimeout(CONNECT_TIMEOUT_IN_MILLISECONDS);
+                       urlConnection.setUseCaches(false);
+
+                       if (cancel) return null;
+                       taskMonitor.setStatusMessage("Attempting to connect to 
the URL...");
+
+                       urlConnection.connect();
+
+                       if (cancel) return null;
+                       taskMonitor.setStatusMessage("Attempting to read from 
the URL...");
+
+                       urlConnection.getInputStream().close();
+               }
+               catch (Exception ex)
+               {
+                       return ex;
+               }
+
+               return null;
+       }
+
+       public void cancel()
+       {
+               cancel = true;
+       }
+}


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"cytoscape-cvs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/cytoscape-cvs?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to