📄 esgcontroller.java
字号:
/*
Copyright (C) 2001, 2008 United States Government as represented by
the Administrator of the National Aeronautics and Space Administration.
All Rights Reserved.
*/
package gov.nasa.worldwind.applications.gio.esg;
import gov.nasa.worldwind.WorldWindow;
import gov.nasa.worldwind.applications.gio.ESGCatalogPanel;
import gov.nasa.worldwind.applications.gio.catalogui.*;
import gov.nasa.worldwind.applications.gio.csw.*;
import gov.nasa.worldwind.applications.gio.ows.*;
import gov.nasa.worldwind.avlist.*;
import gov.nasa.worldwind.examples.ApplicationTemplate;
import gov.nasa.worldwind.examples.util.BrowserOpener;
import gov.nasa.worldwind.layers.Layer;
import gov.nasa.worldwind.util.Logging;
import gov.nasa.worldwind.wms.*;
import javax.swing.*;
import java.beans.*;
import java.net.URL;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @author dcollins
* @version $Id: ESGController.java 9066 2009-02-27 16:42:58Z tgaskins $
*/
public class ESGController implements PropertyChangeListener
{
private ESGCatalogPanel esgPanel;
private CSWConnectionPool connectionPool;
private int threadPoolSize;
private ExecutorService executor;
private final AtomicInteger numResultActors = new AtomicInteger(0);
private final Map<Object, Layer> wwLayerMap = new HashMap<Object, Layer>();
private static final int DEFAULT_THREAD_POOL_SIZE = 3;
public ESGController(ESGCatalogPanel esgPanel)
{
if (esgPanel == null)
{
String message = "nullValue.ESGPanelIsNull";
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
this.esgPanel = esgPanel;
try
{
URL url = this.esgPanel.getServiceURL();
this.connectionPool = new HttpCSWConnectionPool(url);
}
catch (Exception e)
{
String message = "esg.InvalidServiceURL";
Logging.logger().severe(message);
throw new IllegalStateException(message);
}
this.threadPoolSize = DEFAULT_THREAD_POOL_SIZE;
this.executor = Executors.newFixedThreadPool(this.threadPoolSize);
}
public CSWConnectionPool getConnectionPool()
{
return this.connectionPool;
}
public int getThreadPoolSize()
{
return this.threadPoolSize;
}
public void setThreadPoolSize(int size)
{
if (this.executor != null)
{
this.executor.shutdown();
}
if (size < 1)
{
String message = Logging.getMessage("generic.ArgumentOutOfRange", "size=" + size);
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
this.threadPoolSize = size;
this.executor = Executors.newFixedThreadPool(this.threadPoolSize);
}
public void propertyChange(PropertyChangeEvent evt)
{
if (evt != null)
Logging.logger().fine(String.format("%s=%s", evt.getPropertyName(), evt.getNewValue()));
if (evt != null && evt.getPropertyName() != null)
{
if (evt.getPropertyName().equals(CatalogKey.ACTION_COMMAND_BROWSE))
{
Object value = evt.getNewValue();
if (value != null && value instanceof AVList)
{
onBrowse((AVList) value);
}
else if (value != null && value instanceof URL)
{
onBrowse(((URL) value).toExternalForm());
}
else if (value != null && value instanceof String)
{
onBrowse((String) value);
}
}
else if (evt.getPropertyName().equals(ESGKey.ACTION_COMMAND_GET_SERVICE_DATA))
{
Object value = evt.getNewValue();
if (value != null && value instanceof ESGResultModel)
{
onGetServiceData((ESGResultModel) evt.getNewValue());
}
}
else if (evt.getPropertyName().equals(ESGKey.ACTION_COMMAND_GET_SERVICE_INFO))
{
Object value = evt.getNewValue();
if (value != null && value instanceof ESGResultModel)
{
onGetServiceInfo((ESGResultModel) evt.getNewValue());
}
}
else if (evt.getPropertyName().equals(ESGKey.ACTION_COMMAND_GET_SERVICE_METADATA))
{
Object value = evt.getNewValue();
if (value != null && value instanceof ESGResultModel)
{
onGetServiceMetadata((ESGResultModel) evt.getNewValue());
}
}
else if (evt.getPropertyName().equals(ESGKey.ACTION_COMMAND_SERVICE_DATA_PRESSED))
{
Object source = evt.getSource();
Object value = evt.getNewValue();
if (source != null && source instanceof ESGResultModel &&
value != null && value instanceof ServiceData)
{
onUpdateServiceData((ESGResultModel) source, (ServiceData) value);
}
}
else if (evt.getPropertyName().equals(CatalogKey.ACTION_COMMAND_QUERY))
{
onGetServices();
}
else if (evt.getPropertyName().equals(ESGKey.ACTION_COMMAND_SHOW_SERVICE_DETAILS))
{
Object source = evt.getNewValue();
if (source != null && source instanceof ESGResultModel)
{
onShowServiceDetails((ESGResultModel) evt.getNewValue());
}
}
}
}
protected void onBrowse(AVList params)
{
if (params != null)
{
String path = params.getStringValue(CatalogKey.URI);
if (path != null)
onDoOpenInBrowser(path);
}
}
protected void onBrowse(String path)
{
if (path != null)
{
onDoOpenInBrowser(path);
}
}
protected void onGetServices()
{
AVList queryParams = this.esgPanel.getQueryModel();
if (queryParams != null)
{
queryParams = queryParams.copy();
GetServicesTask task = new GetServicesTask(queryParams, this);
this.executor.submit(task);
}
}
protected void onGetServiceData(ESGResultModel resultModel)
{
GetServiceDataTask task = new GetServiceDataTask(resultModel, this);
this.executor.submit(task);
}
protected void onGetServiceInfo(ESGResultModel resultModel)
{
GetServiceInfoTask task = new GetServiceInfoTask(resultModel, this);
this.executor.submit(task);
}
protected void onGetServiceMetadata(ESGResultModel resultModel)
{
GetServiceMetadataTask task = new GetServiceMetadataTask(resultModel, this);
this.executor.submit(task);
}
protected void onShowServiceDetails(ESGResultModel resultModel)
{
ShowServiceDetailsTask task = new ShowServiceDetailsTask(resultModel, this);
this.executor.submit(task);
}
protected void onUpdateServiceData(ESGResultModel resultModel, ServiceData serviceData)
{
UpdateServiceDataTask task = new UpdateServiceDataTask(resultModel, serviceData, this);
this.executor.submit(task);
}
private void onDoOpenInBrowser(String path)
{
if (path == null)
{
String message = "nullValue.PathIsNull";
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
// TODO: fixup path (if path is missing "http://", for example)
URL url = null;
try
{
url = new URL(path);
}
catch (Exception e)
{
String message = "esg.InvalidPath";
Logging.logger().log(java.util.logging.Level.SEVERE, message, e);
}
try
{
if (url != null)
BrowserOpener.browse(url);
}
catch (Exception e)
{
String message = "esg.CannotOpenBrowser";
Logging.logger().log(java.util.logging.Level.SEVERE, message, e);
}
}
protected void addResultActor()
{
this.numResultActors.incrementAndGet();
setResultsWaiting(true);
}
protected void removeResultActor()
{
int i = this.numResultActors.decrementAndGet();
if (i < 1)
setResultsWaiting(false);
}
private void setResultsWaiting(boolean waiting)
{
if (this.esgPanel != null)
{
if (this.esgPanel.getQueryPanel() != null)
this.esgPanel.getQueryPanel().setEnabled(!waiting);
if (this.esgPanel.getResultPanel() != null)
this.esgPanel.getResultPanel().setWaiting(waiting);
}
}
private void setResultsStatusText(String text)
{
if (this.esgPanel != null)
if (this.esgPanel.getResultPanel() != null)
this.esgPanel.getResultPanel().setStatusText(text);
}
private void setResultsStatusSearching()
{
setResultsStatusText("Searching...");
}
private void setResultsStatusFinished()
{
int numResults = 0;
if (this.esgPanel != null)
if (this.esgPanel.getResultModel() != null)
numResults = this.esgPanel.getResultModel().size();
StringBuilder sb = new StringBuilder();
sb.append(numResults);
sb.append(" result");
if (numResults > 1)
sb.append("s");
sb.append(" returned");
setResultsStatusText(sb.toString());
}
private void showExceptionDialog(ExceptionReport exceptionReport)
{
String title = "Problem communicating with Earth Science Gateway";
if (exceptionReport != null)
{
for (ExceptionType e : exceptionReport)
{
if (e != null)
{
String text = QueryUtils.getExceptionText(e);
if (text != null)
JOptionPane.showMessageDialog(null, text, title, JOptionPane.ERROR_MESSAGE);
}
}
}
}
private static class GetServicesTask implements Callable<Object>
{
private AVList params;
private ESGController controller;
public GetServicesTask(AVList queryParams, ESGController controller)
{
this.params = queryParams;
this.controller = controller;
}
public Object call() throws Exception
{
this.controller.doGetServices(this.params);
return null;
}
}
private void doGetServices(AVList queryParams)
{
if (queryParams == null)
{
String message = "nullValue.QueryParamsIsNull";
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
// TODO: capture request parameters in query history
addResultActor();
setResultsStatusSearching();
try
{
GetServices getServices = new GetServices(queryParams, this.esgPanel.getResultModel());
ExceptionReport exceptionReport = getServices.executeRequest(this.connectionPool);
if (exceptionReport != null)
{
showExceptionDialog(exceptionReport);
QueryUtils.logExceptionReport(exceptionReport);
}
}
catch (CSWConnectionException e)
{
String message = "esg.ExceptionWhileGettingServices";
Logging.logger().log(java.util.logging.Level.SEVERE, message, e);
JOptionPane.showMessageDialog(null,
"Cannot communicate with Earth Science Gateway at\n" + this.esgPanel.getService(),
"Problem communicating with Earth Science Gateway",
JOptionPane.ERROR_MESSAGE);
}
catch (Exception e)
{
String message = "esg.ExceptionWhileGettingServices";
Logging.logger().log(java.util.logging.Level.SEVERE, message, e);
}
finally
{
removeResultActor();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -