📄 graphicsredliningtask.java
字号:
package com.esri.solutions.jitk.web.tasks.redlining.graphics;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletRequest;
import org.apache.log4j.Logger;
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.symbol.WebPictureMarkerSymbol;
import com.esri.adf.web.data.tasks.TaskInfo;
import com.esri.adf.web.faces.event.MapEvent;
import com.esri.adf.web.faces.event.TaskEvent;
import com.esri.adf.web.util.ADFDownloadServlet;
import com.esri.adf.web.util.WebUtil;
import com.esri.solutions.jitk.web.tasks.GraphicsDrawingTask;
public class GraphicsRedliningTask extends GraphicsDrawingTask {
private static final long serialVersionUID = -5795228215338347976L;
private static final Logger _logger = Logger.getLogger(GraphicsRedliningTask.class);
private static final String GRAPHICS_REDLINING_TASK = "Graphics Redlining Task";
private String footerNote = null;
private String _graphicsPreview = "";
private String _graphics = "";
private String _graphicsProperties = null;
private String _selectGraphicsFeatures = "0";
private Map<String, String> _graphicsList = null;
private Map<String, byte[]> _imageList = new LinkedHashMap<String, byte[]>();
private String _customList = null;
private String _graphicsFolder = "/images/redlining";
private String _widthParam = null;
private String _heightParam = null;
private String _sizeParam = null;
private String _imageFolderParam = null;
private String _uploadGraphicsTitle = null;
private String _deleteGraphicMessage = null;
private String _deleteSelectedGraphicMessage = null;
public GraphicsRedliningTask() {
setTaskInfo(new GraphicsRedliningTaskInfo());
setTaskName(GRAPHICS_REDLINING_TASK);
}
@SuppressWarnings("unchecked")
public TaskInfo getTaskInfo() {
WebUtil.getExternalContext().getSessionMap().put("jitkGraphicsHeightParam", this.getHeightParam());
WebUtil.getExternalContext().getSessionMap().put("jitkGraphicsWidthParam", this.getWidthParam());
WebUtil.getExternalContext().getSessionMap().put("jitkGraphicsSizeParam", this.getSizeParam());
return super.getTaskInfo();
}
/**
* Get number of graphics
*/
public int getNumGraphics() {
return mgResource.getGraphicsLayer().getGraphicsFeatures().size();
}
/**
* Get image path name parameter
*/
public String getImageFolderParam() {
return _imageFolderParam;
}
/**
* Set image path name parameter
*/
public void setImageFolderParam(String imageFolderParam) {
this._imageFolderParam = imageFolderParam;
}
/**
* Get width parameter
*/
public String getWidthParam(){
return this._widthParam;
}
/**
* Set width parameter
*/
public void setWidthParam(String widthParam) {
this._widthParam = widthParam;
}
/**
* Get height parameter
*/
public String getHeightParam() {
return this._heightParam;
}
/**
* Set height parameter
*/
public void setHeightParam(String heightParam) {
this._heightParam = heightParam;
}
/**
* Get size parameter
*/
public String getSizeParam() {
return this._sizeParam;
}
/**
* Set size parameter
*/
public void setSizeParam(String sizeParam) {
this._sizeParam = sizeParam;
}
/**
* Get footer note
*/
public String getFooterNote() {
return this.footerNote;
}
/**
* Set footer note
*/
public void setFooterNote(String footerNote) {
this.footerNote = footerNote;
}
/**
* Get upload graphics title
*/
public String getUploadGraphicsTitle() {
return _uploadGraphicsTitle;
}
/**
* Set upload graphics title
*/
public void setUploadGraphicsTitle(String uploadGraphicsTitle) {
this._uploadGraphicsTitle = uploadGraphicsTitle;
}
/**
* Get delete graphic message
*/
public String getDeleteGraphicMessage() {
return _deleteGraphicMessage;
}
/**
* Set delete graphic message
*/
public void setDeleteGraphicMessage(String deleteGraphicMessage) {
this._deleteGraphicMessage = deleteGraphicMessage;
}
/**
* Get delete selected graphic message
*/
public String getDeleteSelectedGraphicMessage() {
return _deleteSelectedGraphicMessage;
}
/**
* Set delete selected graphic message
*/
public void setDeleteSelectedGraphicMessage(String deleteSelectedGraphicMessage) {
this._deleteSelectedGraphicMessage = deleteSelectedGraphicMessage;
}
/**
* Get custom graphic list
*/
public String getCustomList() {
return this._customList;
}
/**
* Set custom graphic list
*/
public void setCustomList(String customList) {
//reset graphicsList
this.setGraphicsList(initGraphicsList());
String[] customArray = customList.split(",");
for (int i = 0; i < customArray.length; i++){
String val = customArray[i];
int pos = val.indexOf("_");
String filename = val.substring(pos + 1, val.length());
this._graphicsList.put(val, filename);
}
this._customList = customList;
}
/**
* Set graphics preview
*/
public void setGraphicsPreview(String graphicsPreview) {
this._graphicsPreview = graphicsPreview;
}
/**
* Get graphics preview
*/
public String getGraphicsPreview() {
return this._graphicsPreview;
}
/**
* Set graphics properties. Currently not being used.
*/
public void setGraphicsProperties(String graphicsProperties) {
this._graphicsProperties = graphicsProperties;
}
/**
* Get graphics properties. Currently not being used.
*/
public String getGraphicsProperties() {
return this._graphicsProperties;
}
/**
* Set graphics path
*/
public void setGraphics(String graphics) {
this._graphics = graphics;
}
/**
* Get graphics path
*/
public String getGraphics() {
return this._graphics;
}
/**
* set select graphics features
*/
public void setSelectGraphicsFeatures(String selectGraphicsFeatures) {
this._selectGraphicsFeatures = selectGraphicsFeatures;
}
/**
* Get select graphics features
*/
public String getSelectGraphicsFeatures() {
return this._selectGraphicsFeatures;
}
/**
* Delete all graphics
*/
public void deleteAllGraphics(TaskEvent event) {
try {
_logger.info("Deleting all graphic redlines from map.");
requestTaskRender();
mgResource.deleteAllGraphics();
setSelectGraphicsFeatures("0");
event.getWebContext().refresh();
} catch (Exception ex) {
renderExceptionMessage(ex);
_logger.error("Could not delete all graphic redlines from map.", ex);
}
}
/**
* Find element
*/
public void findElement(MapEvent event) {
try {
_logger.info("Selecting graphic redlines for deletion.");
requestTaskRender();
mgResource.clearVertexSelection();
mgResource.clearSelections();
// Init LayerInfos in case they haven't been initialized yet
if(mgResource.getLayerInfos().size() != 1) {
List<String> layerNames = new ArrayList<String>();
layerNames.add(mgResource.getGraphicsLayerName());
mgResource.setLayerInfos(layerNames);
}
setSelectGraphicsFeatures(Integer.toString(mgResource.selectShapes((WebExtent)event.getWebGeometry().toMapGeometry(event.getWebContext().getWebMap()), 1, false)));
event.getWebContext().refresh();
} catch (Exception ex) {
renderExceptionMessage(ex);
_logger.error("Could not select graphic redlines for deletion: ", ex);
}
}
/**
* Delete selected graphics
*/
public void deleteSelectGraphics(TaskEvent event) {
_logger.info("Deleting selected graphic elements");
requestTaskRender();
try {
mgResource.deleteSelectedShapes();
mgResource.clearSelections();
setSelectGraphicsFeatures("0");
event.getWebContext().getWebQuery().clearGraphics();
event.getWebContext().refresh();
}
catch (Exception ex) {
renderExceptionMessage(ex);
_logger.error("Could not delete selected graphics", ex);
}
}
/**
* Clear selected graphics
*/
public void clearSelectGraphics(TaskEvent event) {
_logger.info("Clearing selected graphic elements");
requestTaskRender();
try {
mgResource.clearSelections();
setSelectGraphicsFeatures("0");
event.getWebContext().refresh();
}
catch (Exception ex) {
renderExceptionMessage(ex);
_logger.error("Could not clear selections", ex);
}
}
/**
* Load image from file
*/
private byte[] loadImageFromFile(File imgFile) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream is = new FileInputStream(imgFile);
int r;
byte[] buffer = new byte[8000];
while((r = is.read(buffer)) >= 0) {
if(r == 0) continue;
baos.write(buffer, 0, r);
}
is.close();
return baos.toByteArray();
}
/**
* Place graphics event
*/
@SuppressWarnings("unchecked")
public void placeGraphics(MapEvent event) {
try {
_logger.info("Placing redline graphic on map");
requestTaskRender();
WebPoint webPoint = (WebPoint)event.getWebGeometry().toMapGeometry(event.getWebContext().getWebMap());
WebGeometry webGeometry = (WebGeometry) webPoint;
if (getGraphics().length() == 0) {
_logger.error("Selected graphics has length of 0, aborting adding redline...");
return;
}
byte[] data = null;
String fileName = getGraphics();
_logger.info("Adding redline graphics file: " + fileName);
if (fileName.indexOf("upload-") < 0)
data = (byte[]) this._imageList.get(fileName);
else
data = (byte[])WebUtil.getExternalContext().getSessionMap().get(fileName);
WebPictureMarkerSymbol pSymbol = new WebPictureMarkerSymbol();
pSymbol.setAntialiasing(true);
pSymbol.setPicture(data);
mgResource.getGraphicsLayer().addGraphicFeature(webGeometry, pSymbol, pSymbol);
event.getWebContext().refresh();
}
catch (Exception e) {
renderExceptionMessage(e);
_logger.error("Could not place redline graphic on map.", e);
}
}
@SuppressWarnings("deprecation")
private byte[] getImageByteFromFile(String fileName) {
try {
ServletRequest req = (ServletRequest)WebUtil.getExternalContext().getRequest();
if (fileName.toUpperCase().indexOf(".ICO") < 0) {
int pos = fileName.indexOf("/", 1);
fileName = fileName.substring(pos + 1, fileName.length());
fileName = req.getRealPath(fileName);
return loadImageFromFile(new File(fileName));
} else {
// filename for ico includes the session id for creating temp png file
String[] params = fileName.split("&");
String fullpath = params[0];
String[] idparams = params[1].split("=");
return GraphicsRedliningTaskUtil.getBytesFromFile(fullpath, idparams[1]);
}
}
catch(Exception e) {
_logger.error("Could not get image bytes from filename provided.", e);
}
return null;
}
/**
* Init graphics list (read the list of graphics from /images/redlining/graphics_list.xml)
*/
@SuppressWarnings("deprecation")
private Map<String, String> initGraphicsList() {
try {
if (_graphicsList != null) return _graphicsList;
_logger.debug("Initializing redlining graphic list");
Map<String, String> graphicsList = new LinkedHashMap<String, String>();
ServletRequest req = (ServletRequest)WebUtil.getExternalContext().getRequest();
String graphicsDirName = _imageFolderParam;
if (_imageFolderParam == null || _imageFolderParam.trim().length() == 0)
graphicsDirName = req.getRealPath(_graphicsFolder);
File f1 = new File(graphicsDirName);
File[] strFilesDirs = f1.listFiles();
for (int i = 0; i < strFilesDirs.length; i++) {
if (strFilesDirs[i].isFile()) {
String fullpath = strFilesDirs[i].toString();
int pos = fullpath.lastIndexOf(System.getProperty("file.separator"));
String filename = fullpath.substring(pos + 1);
pos = filename.indexOf(".");
String dispname = filename;
if (pos >= 0)
dispname = filename.substring(0, pos);
if(fullpath.toLowerCase().endsWith(".bmp") || fullpath.toLowerCase().endsWith(".ico") || fullpath.toLowerCase().endsWith(".jpg") ||
fullpath.toLowerCase().endsWith(".gif") || fullpath.toLowerCase().endsWith(".png")) {
if (fullpath.toLowerCase().endsWith(".ico")) {
fullpath = fullpath.replaceAll("\\\\", "/");
String fid = ADFDownloadServlet.generateId();
String fpath = fullpath + "&fid=" + fid;
graphicsList.put(fpath, dispname);
this._imageList.put(fpath, getImageByteFromFile(fpath));
} else {
String gpath = WebUtil.getRequestContextPath() + _graphicsFolder + "/" + filename;
graphicsList.put(gpath, dispname);
this._imageList.put(gpath, getImageByteFromFile(gpath));
}
}
}
}
return graphicsList;
}
catch (Exception e) {
_logger.error("Could not initialize graphics list", e);
}
return null;
}
public void setGraphicsList(Map<String, String> graphicsList) {
this._graphicsList = graphicsList;
}
public Map<String, String> getGraphicsList() {
if (_graphicsList == null) {
setGraphicsList(initGraphicsList());
}
return this._graphicsList;
}
/**
* upload. Currently not being used.
*/
public void upload(TaskEvent event) {
_logger.error("Upload task event called, this should never be called as it is a no-op event");
requestTaskRender();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -