📄 multigeometrygraphicsresource.java
字号:
/**
*
*/
package com.esri.solutions.jitk.web.data.graphics;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.log4j.Logger;
import com.esri.adf.web.data.GraphicElement;
import com.esri.adf.web.data.WebContext;
import com.esri.adf.web.data.WebGraphics;
import com.esri.adf.web.data.WebLayerInfo;
import com.esri.adf.web.data.WebMap;
import com.esri.adf.web.data.geometry.WebExtent;
import com.esri.adf.web.data.geometry.WebGeometry;
import com.esri.adf.web.data.geometry.WebPoint;
import com.esri.adf.web.data.geometry.WebPointCollection;
import com.esri.adf.web.data.geometry.WebPolyline;
import com.esri.adf.web.data.graphics.GraphicFeature;
import com.esri.adf.web.data.graphics.GraphicsLayer;
import com.esri.adf.web.data.graphics.GraphicsResource;
import com.esri.adf.web.data.graphics.query.GraphicsQueryFunctionality;
import com.esri.adf.web.data.query.IdentifyCriteria;
import com.esri.adf.web.data.query.QueryResult;
import com.esri.adf.web.data.query.WebQuery;
import com.esri.adf.web.data.symbol.WebPictureMarkerSymbol;
import com.esri.adf.web.data.symbol.WebSimpleLineSymbol;
import com.esri.adf.web.data.symbol.WebSimpleMarkerSymbol;
import com.esri.adf.web.data.symbol.WebSimplePolygonSymbol;
import com.esri.adf.web.data.symbol.WebSymbol;
import com.esri.adf.web.data.symbol.WebTrueTypeMarkerSymbol;
import com.esri.solutions.jitk.common.geometry.ClosestVertexFinder;
import com.esri.solutions.jitk.common.geometry.WebGeometryOffsetter;
/**
* @author vlad2928
*
*/
public class MultiGeometryGraphicsResource implements IShapeMover, IShapeVertexMover {
private static final Logger _logger = Logger.getLogger(MultiGeometryGraphicsResource.class);
private static final String _fid = "FID";
private GraphicsResource gResource;
private final String graphicsLayerName = "Graphics";
private final String labelLayerName = "Labels";
private final String pointLayerName = "Points";
private final String lineLayerName = "Lines";
private final String polygonLayerName = "Polygons";
private GraphicsQueryFunctionality _queryFunc = null;
private List<WebLayerInfo> _layerInfos = new ArrayList<WebLayerInfo>();
private List<QueryResult> _selectedShapes = null;
private WebPoint _selectedVertex;
private GraphicElement _selectedVertexGraphicElement;
public boolean isEnabled() { return (gResource != null); }
public WebContext getContext() { return gResource.getWebContext(); }
public MultiGeometryGraphicsResource(GraphicsResource resource) {
if(resource != null) {
gResource = resource;
addLayer(labelLayerName);
addLayer(pointLayerName);
addLayer(graphicsLayerName);
addLayer(lineLayerName);
addLayer(polygonLayerName);
} else {
throw new NullPointerException("GraphicsResource is null.");
}
}
public List<WebLayerInfo> getLayerInfos() {
return _layerInfos;
}
private boolean isQueryable() {
if(gResource != null && _queryFunc == null) {
_queryFunc = (GraphicsQueryFunctionality) gResource.getFunctionality("query");
if(_queryFunc == null)
_logger.error("GraphicsResource has no QueryFunctionality.");
}
return (_queryFunc != null);
}
private void addLayerInfo(String layerName) {
if(isQueryable() && layerName != null && layerName.length() > 0)
for(WebLayerInfo layerInfo : _queryFunc.getQueryLayers()) {
if(layerInfo.getName().equals(layerName))
_layerInfos.add(layerInfo);
}
}
public void setLayerInfos(List<String> layerNames) {
if(isQueryable() && layerNames != null && layerNames.size() > 0) {
_layerInfos = new ArrayList<WebLayerInfo>();
for(WebLayerInfo layerInfo : _queryFunc.getQueryLayers()) {
if(layerNames.contains(layerInfo.getName()))
_layerInfos.add(layerInfo);
}
}
}
public void addGraphicFeature(WebGeometry webGeo, WebSymbol symbol) {
if (webGeo instanceof WebPoint) {
if(symbol instanceof WebPictureMarkerSymbol) {
_logger.debug("Inserting graphic into graphics layer.");
getGraphicsLayer().addGraphicFeature(webGeo, symbol, getHighlightPointSymbol());
} else if(symbol instanceof WebTrueTypeMarkerSymbol) {
_logger.debug("Inserting graphic into label layer.");
getLabelLayer().addGraphicFeature(webGeo, symbol, getHighlightPointSymbol());
} if (symbol instanceof WebSimpleMarkerSymbol) {
_logger.debug("Inserting graphic into point layer.");
getPointLayer().addGraphicFeature(webGeo, symbol, getHighlightPointSymbol());
}
} else if((webGeo instanceof WebPolyline) || (webGeo instanceof WebPointCollection)) {
_logger.debug("Inserting graphic into line layer");
getLineLayer().addGraphicFeature(webGeo, symbol, getHighlightLineSymbol());
} else {
_logger.debug("Inserting graphic into polygon layer");
getPolygonLayer().addGraphicFeature(webGeo, symbol, getHighlightPolygonSymbol());
}
}
public int getSelectedVertexCount() {
return (_selectedVertex == null) ? 0 : 1;
}
public void clearVertexSelection() {
clearVertexHightlight();
_selectedVertex = null;
}
public void moveSelectedVertex(WebMap webMap, WebPoint newLocation) throws Exception {
if (_selectedVertex != null) {
_selectedVertex.setX(newLocation.getX());
_selectedVertex.setY(newLocation.getY());
}
clearVertexHightlight();
}
protected void clearVertexHightlight() {
if (_selectedVertexGraphicElement != null) {
WebGraphics webGraphics = getContext().getWebGraphics();
webGraphics.removeGraphics(_selectedVertexGraphicElement);
_selectedVertexGraphicElement = null;
}
}
protected void highlightSelectedVertex(WebPoint vertex) {
WebGraphics webGraphics = getContext().getWebGraphics();
WebQuery webQuery = getContext().getWebQuery();
_selectedVertexGraphicElement = new GraphicElement();
_selectedVertexGraphicElement.setGeometry(vertex);
_selectedVertexGraphicElement.setSymbol(webQuery.getPointGraphicSymbol());
webGraphics.addGraphics(_selectedVertexGraphicElement);
}
public int selectShapeVertex(WebPoint point, int tolerance) throws Exception {
_selectedVertex = null;
clearVertexHightlight();
int selectedCount = 0;
WebQuery webQuery = getContext().getWebQuery();
IdentifyCriteria identifyCrit = new IdentifyCriteria();
identifyCrit.setWebGeometry(point);
List<QueryResult> selectedShapes = webQuery.query(identifyCrit, getLayerInfos());
if (selectedShapes != null) {
if (selectedShapes.size() >= 1) {
QueryResult res = selectedShapes.get(0);
selectedShapes = new ArrayList<QueryResult>();
selectedShapes.add(res);
GraphicFeature feature = getGraphicFeature((String)selectedShapes.get(0).getDetails().get(_fid));
ClosestVertexFinder vertexFinder = new ClosestVertexFinder();
_selectedVertex = vertexFinder.getClosestVertex(feature.getGeometry(), point);
if (_selectedVertex != null) {
selectedCount = 1;
_logger.debug("Vertex selected " + _selectedVertex);
highlightSelectedVertex(_selectedVertex);
} else _logger.debug("No vertex selected");
}
}
return selectedCount;
}
public int getSelectedCount() {
return (_selectedShapes != null) ? _selectedShapes.size() : 0;
}
public void clearSelections() {
clearHighlights();
_selectedShapes = null;
}
public int selectShapes(WebGeometry queryGeo, int tolerance, boolean isSelectOne) {
//Masks an ADF bug that can not do a point identify on a GraphicResource
if (queryGeo instanceof WebPoint)
queryGeo = createExtent((WebPoint) queryGeo, tolerance);
int selectedCount = 0;
WebQuery webQuery = getContext().getWebQuery();
IdentifyCriteria identifyCrit = new IdentifyCriteria();
identifyCrit.setWebGeometry(queryGeo);
_selectedShapes = webQuery.query(identifyCrit, getLayerInfos());
if (_selectedShapes != null) {
selectedCount = _selectedShapes.size();
_logger.debug(selectedCount + " total shapes were selected.");
if(isSelectOne && selectedCount > 1) {
QueryResult result = _selectedShapes.get(0);
_selectedShapes = new ArrayList<QueryResult>();
_selectedShapes.add(result);
selectedCount = 1;
}
highlightSelections();
} else _logger.debug("No results were selected.");
return selectedCount;
}
public void highlightSelections() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -