arcgissamples\trackinganalyst\ui\ServerInfoListener.java
/* Copyright 2010 ESRI * * All rights reserved under the copyright laws of the United States * and applicable international laws, treaties, and conventions. * * You may freely redistribute and use this sample code, with or * without modification, provided you include the original copyright * notice and use restrictions. * * See the use restrictions. * */ package arcgissamples.trackinganalyst.ui; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.IOException; import java.util.ArrayList; import javax.swing.*; import com.esri.arcgis.controls.IGlobeHookHelper; import com.esri.arcgis.controls.IToolbarControl2; import com.esri.arcgis.geodatabase.*; import com.esri.arcgis.globecore.esriGlobeLayerType; import com.esri.arcgis.interop.AutomationException; import com.esri.arcgis.system.ExtensionManager; import com.esri.arcgis.system.IExtensionManagerAdmin; import com.esri.arcgis.system.IPropertySet; import com.esri.arcgis.system.PropertySet; import com.esri.arcgis.system.UID; import com.esri.arcgis.trackinganalyst.AMSWorkspace; import com.esri.arcgis.trackinganalyst.AMSWorkspaceFactory; import com.esri.arcgis.trackinganalyst.ITrackingEnvironment; import com.esri.arcgis.trackinganalyst.TemporalFeatureLayer; import com.esri.arcgis.trackinganalyst.TrackingEnvironment; public class ServerInfoListener implements ActionListener { IGlobeHookHelper globeHookHelper = null; String serverName = null; String serviceName = null; private ServerInfoDialog serverInfoDialog = null; private JDialog progressDialog = null; boolean isConnected = false; public ServerInfoListener(ServerInfoDialog dlg, IGlobeHookHelper ghh) { super(); this.serverInfoDialog = dlg; this.globeHookHelper = ghh; } private String[] getServicesList(AMSWorkspace amsWorkspace) { ArrayList<String> list = null; try { list = new ArrayList<String>(10); IEnumDatasetName enumDSNames = amsWorkspace.getDatasetNames(esriDatasetType.esriDTFeatureClass); IDatasetName dsName = enumDSNames.next(); int numLayers = 0; while (dsName != null) { numLayers++; list.add(dsName.getName()); dsName = enumDSNames.next(); } list.trimToSize(); String[] nameList = new String[list.size()]; for (int i = 0; i < nameList.length; i++) { nameList[i] = list.get(i); } return nameList; } catch (Exception e) { e.printStackTrace(); } return null; } public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equalsIgnoreCase("connect")) { serverName = serverInfoDialog.getServerTextBox().getText(); serverInfoDialog.setVisible(false); getProgressDialog().setVisible(true); // connect to tracking server and retrieve tracking layer list AMSWorkspace amsWorkspace = openTrackingServerConnection(serverName); String[] servicesList = getServicesList(amsWorkspace); getProgressDialog().setVisible(false); try { // get the map container Object mapObj = null; if (globeHookHelper.getHook() instanceof IToolbarControl2) { IToolbarControl2 toolbarCtrl = (IToolbarControl2) globeHookHelper.getHook(); mapObj = toolbarCtrl.getBuddy(); } else { mapObj = globeHookHelper.getHook(); } // load the Tracking Analyst extension setupTrackingEnv(mapObj); ServiceInfoDialog dg = new ServiceInfoDialog("Select Tracking Service", globeHookHelper, servicesList, amsWorkspace); dg.setModal(true); dg.setVisible(true); } catch (AutomationException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } catch (Exception e1) { e1.printStackTrace(); } } if (e.getActionCommand().equalsIgnoreCase("cancel")) { serverName = null; serviceName = null; serverInfoDialog.setVisible(false); } } /* * Initialize the Tracking Environment, you only need to do this once */ private void setupTrackingEnv(Object mapObj) throws Exception { ExtensionManager extentionManager = new ExtensionManager(); UID uid = new UID(); uid.setValue("esriTrackingAnalyst.TrackingEngineUtil"); ((IExtensionManagerAdmin) extentionManager).addExtension(uid, mapObj); TrackingEnvironment trackingEnv = new TrackingEnvironment(); trackingEnv.initialize(mapObj); trackingEnv.setEnableTemporalDisplayManagement(true); } /* * Opens connection to the Tracking Server */ private AMSWorkspace openTrackingServerConnection(String serverName) { IWorkspaceFactory amsWorkspaceFactory = null; // Create connection property set for Tracking Server IPropertySet connectionProperties = null; try { amsWorkspaceFactory = new AMSWorkspaceFactory(); connectionProperties = new PropertySet(); connectionProperties.setProperty("SERVERNAME", serverName); connectionProperties.setProperty("AMS_CONNECTION_NAME", "Sample TS Connection"); // This is the standard AMS connection editor, this would only be // different if you wrote your own connector connectionProperties.setProperty("AMS_CONNECTOR_EDITOR", "{1C6BA545-2F59-11D5-B7E2-00010265ADC5}"); // This is the standard AMS connector, this would only be different if // you wrote your own connector connectionProperties.setProperty("AMS_CONNECTOR", "{F6FC70F5-5778-11D6-B841-00010265ADC5}"); connectionProperties.setProperty("AMS_USER_NAME", ""); connectionProperties.setProperty("TMS_USER_PWD", ""); } catch (Exception e1) { e1.printStackTrace(); } AMSWorkspace amsWorkspace = null; try { amsWorkspace = new AMSWorkspace(amsWorkspaceFactory.open(connectionProperties, 0)); amsWorkspace.connect(); isConnected = true; return amsWorkspace; } catch (Exception e) { getProgressDialog().setVisible(false); JOptionPane.showMessageDialog(serverInfoDialog, "An error occured when attempting connection to Tracking Server \"" + serverName + "\". Please check if this Tracking Server is available.\n"); e.printStackTrace(); return null; } } /* * Creates a Dialog box with a Progress bar in it. @return */ private JDialog getProgressDialog() { if (progressDialog == null) { progressDialog = new JDialog(); progressDialog.setTitle("Connecting to Tracking Server..."); JProgressBar bar = new JProgressBar(); bar.setBorderPainted(true); bar.setIndeterminate(true); progressDialog.setLocationRelativeTo(serverInfoDialog); progressDialog.setLocation(serverInfoDialog.getWidth() / 2, serverInfoDialog.getHeight() / 2); progressDialog.add(bar, BorderLayout.PAGE_START); progressDialog.pack(); progressDialog.setSize(275, 50); } return progressDialog; } }