📄 addlayer.java
字号:
package com.esri.arcgis.webcontrols.test;
import com.esri.arcgis.webcontrols.ags.data.*;
import com.esri.arcgis.webcontrols.data.WebContext;
import com.esri.arcgis.webcontrols.ags.data.AGSWebMap;
import com.esri.arcgis.webcontrols.data.WebContextInitialize;
import com.esri.arcgis.webcontrols.data.WebLifecycle;
import com.esri.arcgis.carto.*;
import com.esri.arcgis.geodatabase.*;
import com.esri.arcgis.datasourcesfile.*;
import java.util.*;
import java.io.*;
import javax.faces.event.ActionEvent;
import javax.faces.context.FacesContext;
import javax.faces.application.FacesMessage;
import com.esri.arcgis.geometry.IEnvelope;
public class AddLayer implements WebContextInitialize, WebLifecycle{
private LayerCatalog catalog;
private AGSWebContext agsContext;
String addLayerName;
int addLayerIndex;
//layerName - index
private LinkedHashMap addedLayers = new LinkedHashMap();
private ArrayList removeLayers = new ArrayList();
public void init(WebContext webContext) {
agsContext = (AGSWebContext)webContext;
catalog = new LayerCatalog(agsContext);
}
public void activate() {
if(addedLayers == null || addedLayers.size() <= 0)
return;
for (Iterator iter = addedLayers.entrySet().iterator(); iter.hasNext(); ) {
java.util.Map.Entry item = (java.util.Map.Entry)iter.next();
addLayer((String)item.getKey(), ((Integer)item.getValue()).intValue(), false);
}
try {
new IMapServerObjectsProxy(agsContext.getServer()).refreshServerObjects();
}
catch (Exception _) {
throw new IllegalStateException("Cannot activate AddLayer.\n\t" + _);
}
}
public void passivate() {
try {
if(removeLayers.size() <= 0)
return;
IMapServerObjects mso = new IMapServerObjectsProxy(agsContext.getServer());
AGSWebMap agsMap = (AGSWebMap) agsContext.getWebMap();
IMap map = mso.getMap(agsMap.getFocusMapName());
for (Iterator iter = removeLayers.iterator(); iter.hasNext(); )
map.deleteLayer((ILayer)iter.next());
mso.refreshServerObjects();
}
catch (Exception _) {
throw new IllegalStateException("Cannot passivate AddLayer.\n\t" + _);
}
finally {
removeLayers.clear();
}
}
public void destroy() {
if(agsContext.getServerContext() == null)
return;
passivate();
}
public void processAddLayer(ActionEvent event) {
if(addedLayers.keySet().contains(addLayerName)) {
FacesContext fc = FacesContext.getCurrentInstance();
fc.addMessage(event.getComponent().getClientId(fc), new FacesMessage("Layer '" + addLayerName + "' has already been added to the map."));
return;
}
addLayer(addLayerName, addLayerIndex, true);
}
public LayerCatalog getCatalog() { return catalog; }
public void setAddLayerName(String addLayerName) { this.addLayerName = addLayerName; }
public String getAddLayerName() { return addLayerName; }
public void setAddLayerIndex(int addLayerIndex) { this.addLayerIndex = addLayerIndex; }
public int getAddLayerIndex() { return addLayerIndex; }
private void addLayer(String layerName, int index, boolean reloadContext) {
try {
File file = (File)catalog.availableLayers.get(layerName);
IMapServer server = agsContext.getServer();
IMapServerObjects mso = new IMapServerObjectsProxy(server);
AGSWebMap agsMap = (AGSWebMap) agsContext.getWebMap();
IMap map = mso.getMap(agsMap.getFocusMapName());
IFeatureLayer layer = new IFeatureLayerProxy(agsContext.createServerObject(FeatureLayer.getClsid()));
IWorkspaceFactory factory = new IWorkspaceFactoryProxy(agsContext.createServerObject(ShapefileWorkspaceFactory.getClsid()));
IWorkspace ws = factory.openFromFile(file.getParent(), 0);
layer.setFeatureClassByRef(new IFeatureWorkspaceProxy(ws).openFeatureClass(file.getName()));
layer.setName(layerName);
map.addLayer(layer);
map.moveLayer(layer, index);
removeLayers.add(layer);
if(reloadContext) {
catalog.addLayerIndices = null;
IEnvelope extent = agsMap.getFocusMapExtent();
mso.refreshServerObjects();
IMapDescription newDesc = server.getServerInfo(agsMap.getFocusMapName()).getDefaultMapDescription();
agsContext.getMapDescriptions().remove(0);
agsContext.getMapDescriptions().add(newDesc);
agsMap.setFocusMapExtent(extent);
agsContext.refresh(AGSRefreshId.CONTEXT_RELOADED);
addedLayers.put(layerName, new Integer(index));
}
}
catch(Exception _) {
throw new IllegalStateException("Unable to add layer: " + layerName + "\n\t" + _);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -