AGSSelection selection = (AGSSelection)
agsContext.getAttribute(
AGSSelection.WEB_CONTEXT_ATTRIBUTE_NAME);
selection.setSelectedLayerId(layerId);
Here are the steps to implement the selection tool in the MapViewer template:
1. Use arcgisant tool to create a web application named SelectionTest using the MapViewer template.
2. Create a new project as discussed here and copy over the newly created webapplication from ArcGIS build directory to this directory. Be sure to update the build file with the new app name.
3. Under the src folder, create a folder named test.
4. Create a file named AGSSelectionLayer.java in the test folder with this content:
package test;
import com.esri.arcgis.webcontrols.faces.event.*;
import com.esri.arcgis.webcontrols.data.*;
import com.esri.arcgis.webcontrols.ags.data.*;
import java.util.*;
import javax.faces.component.UIData;
import javax.faces.model.SelectItem;
import java.util.logging.*;
import com.esri.arcgis.geodatabase.*;
import com.esri.arcgis.carto.ILayerDescriptions;
public class AGSSelectionLayer implements
WebContextInitialize, WebContextObserver{
private static Logger logger =
Logger.getLogger(AGSSelectionLayer.class.getName());
private AGSWebContext agsContext;
private String mapName;
private int layerId = 0;
public void init(WebContext agsContext) {
if(agsContext == null ||
!(agsContext instanceof AGSWebContext))
throw new IllegalArgumentException
("WebContext null.");
this.agsContext = (AGSWebContext)agsContext;
setLayerId(((Integer)((SelectItem)
((AGSWebMap)agsContext.
getWebMap()).getFeatureLayers()
.get(0)).getValue()).intValue());
}
public void update(WebContext context, Object arg) {
if(arg == null ||
arg != AGSRefreshId.DATA_FRAME_CHANGED)
return;
AGSWebMap agsMap = ((AGSWebMap)
agsContext.getWebMap());
this.mapName = agsMap.getFocusMapName();
setLayerId(((Integer)((SelectItem)
agsMap.getFeatureLayers()
.get(0)).getValue()).intValue());
}
public int getLayerId() {
return layerId;
}
public void setLayerId(int layerId) {
this.layerId = layerId;
AGSSelection selection = (AGSSelection)agsContext
.getAttribute(
AGSSelection.WEB_CONTEXT_ATTRIBUTE_NAME
);
selection.setSelectedLayerId(layerId);
}
public void clearResults() {
try {
ILayerDescriptions layerDescs =
((AGSWebMap)agsContext.getWebMap()).
getFocusMapDescription().getLayerDescriptions();
for(int i = 0; i < layerDescs.getCount(); ++i)
layerDescs.getElement(i).setSelectionFeatures(null);
} catch(Exception _) {
logger.log(Level.WARNING,
"Unable to deselect features.",
_);
}
agsContext.refresh(AGSRefreshId.MAP_OPERATION);
}
}
Notice that on init method, we are calling setLayerId method and passing the first featurelayer from the collection in the wemap. setLayerId method sets the selection layer to the AGSSelection object. clearResults method clears out selection from all layers.
5. Go to webapp_name/WEB-INF/classes folder and open managed_context_attributes.xml file in a text editor. Add a new managed-context-attribute as shown below:
<managed-context-attribute>
<name>esriAGSSelectionLayer</name>
<attribute-class>test.AGSSelectionLayer</attribute-class>
<description>Selection Layer</description>
</managed-context-attribute>
6. Open mapviewer.jsp file from webapp folder and find the IMG tag for identify tool. Add the following code right after it to add a dropdown list of all FeatureLayers, a Selection tool and a button to clear selection.
<td>
Select From:
</td>
<td>
lt;jsfh:selectOneMenu onchange="this.form.submit();"
value="#{sessionScope['mapContext'].
attributes['esriAGSSelectionLayer'].
layerId}">
<jsfc:selectItems value="#{sessionScope['mapContext'].
webMap.featureLayers}
"/>
</jsfh:selectOneMenu>
</td>
<td>
<IMG id="imgSelection"
name="imgSelection"
src="images/polygon.gif"
alt="select"
title="Select"
onmouseover="this.src='images/polygonU.gif'"
onmousedown="this.src='images/polygonD.gif';
MapDragRectangle('Map0', 'Selection');
HighlightTool('Selection');"
onmouseout="ButtonOut('imgSelection', 'Map0',
'Selection', 'images/polygon.gif',
'images/polygonD.gif')">
</td>
<td>
<jsfh:commandButton id="cmdClear"
image="images/cancel.gif"
onmousedown="this.src='images/cancelD.gif'"
onmouseover="this.src='images/cancelU.gif'"
onmouseout="this.src='images/cancel.gif'"
title="Clear Selection"
alt="Clear Selection"
action="#{sessionScope['mapContext'].
attributes['esriAGSSelectionLayer'].clearResults}" />
</td>
7. Open templates.js file in a text editor from js folder and find HighlightTool function. Modify this function as shown below to include the Selection tool.
function HighlightTool(tool)
{
if ((tool!=null) && (tool!=""))
{
document.images["imgZoomIn"].src = "images/zoomin.gif";
document.images["imgZoomOut"].src = "images/zoomout.gif";
document.images["imgPan"].src = "images/pan.gif";
document.images["imgIdentify"].src = "images/identify.gif";
document.images["imgSelection"].src = "images/polygon.gif";
switch (tool)
{
case "ZoomIn":
document.images["imgZoomIn"].src = "images/zoominD.gif";
break;
case "ZoomOut":
document.images["imgZoomOut"].src = "images/zoomoutD.gif";
break;
case "Pan":
document.images["imgPan"].src = "images/panD.gif";
break;
case "Identify":
document.images["imgIdentify"].src = "images/identifyD.gif";
break;
case "Selection":
document.images["imgSelection"].src = "images/polygonD.gif";
break;
default:
}
}
}
8. Open command prompt and CD to ant folder. Run this command: arcgisant build
9. Verify that the SelectionTest.war file is created in build folder. Copy this file back to the ArcGIS Installation Directory\DeveloperKit\Templates\Java\build directory.
10. Copy the webapp folder back to the ArcGIS Installation Directory\DeveloperKit\Templates\Java\build directory.
11. Use arcgisant tool to deploy the web application.