📄 exportimagetask.java
字号:
package com.esri.solutions.jitk.web.tasks.export.image;
import com.esri.adf.web.data.WebContext;
import com.esri.adf.web.data.WebMap;
import com.esri.adf.web.data.tasks.TaskInfo;
import com.esri.adf.web.faces.event.TaskEvent;
import com.esri.adf.web.util.WebUtil;
import com.esri.solutions.jitk.common.resources.TextResources;
import com.esri.solutions.jitk.web.tasks.RenderControlledTask;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import java.io.File;
import java.util.Map;
/**
* This task enables user to export map view as
* an image to the following export formats:
* - BMP
* - GIF
* - JPEG
* - PNG24
* - PNG8
* - PNM
* - TIF
*/
public class ExportImageTask extends RenderControlledTask {
private static final long serialVersionUID = 5444622501952700065L;
private static Logger _logger = LogManager.getLogger(ExportImageTask.class.getName());
private static final String EXPORT_IMAGE_TASK = "EXPORT_IMAGE_TASK";
private ExportImageTaskInfo taskInfo = null;
private String taskName = null;
private String exportType = ImageTypesMap.PNG8;
private String width = "";
private String message = "";
private String resultUrl = "";
private WebContext context = null;
private String footerNote = null;
private boolean exportNorthArrow = true;
private boolean exportScalebar = true;
private boolean exportGraphicResources = true;
public ExportImageTask() {
setTaskInfo(new ExportImageTaskInfo());
this.taskName = EXPORT_IMAGE_TASK;
cleanTempDir(System.getProperty("java.io.tmpdir") +
System.getProperty("file.separator"));
}
public void init(WebContext context) {
setContext(context);
}
public TaskInfo getTaskInfo() {
if (this.context == null) {
_logger.error("TaskInfo null, disabling");
taskInfo.getTaskDescriptor().setDisabled(true);
}
return this.taskInfo;
}
public void setTaskInfo(TaskInfo taskInfo) {
this.taskInfo = (ExportImageTaskInfo) taskInfo;
super.setTaskInfo(this.taskInfo);
}
public String getResultUrl() {
return this.resultUrl;
}
public void setResultUrl(String resultUrl) {
this.resultUrl = resultUrl;
}
public WebContext getContext() {
return context;
}
private static void cleanTempDir(String tmp) {
try {
String[] flist = new File(tmp).list();
for (int i = 0; i < flist.length; i++) {
String fname = flist[i];
if (fname.startsWith("jitk")) {
String fd = tmp + fname;
File fl = new File(fd);
if (fl.isDirectory()) {
String[] fdlist = new File(fd).list();
for (int j = 0; j < fdlist.length; j++) {
String fdname = fdlist[j];
File fdl = new File(fd + "/" + fdname);
fdl.deleteOnExit();
}
}
fl.deleteOnExit();
}
}
} catch (Exception e) {
_logger.warn("Unable to clean temp directory", e);
}
}
public void setContext(WebContext context) {
if (context != null) {
this.context = context;
} else {
taskInfo.getTaskDescriptor().setDisabled(true);
}
}
/**
* Get footer note
*/
public String getFooterNote() {
return this.footerNote;
}
/**
* Set footer note
*/
public void setFooterNote(String footerNote) {
this.footerNote = footerNote;
}
/**
* Get task name
*/
public String getTaskName() {
return taskName;
}
/**
* Set task name
*/
public void setTaskName(String taskName) {
this.taskName = taskName;
}
/**
* Get message
*/
public String getMessage() {
return this.message;
}
/**
* Set message
*/
public void setMessage(String message) {
this.message = message;
}
/**
* Get export type
*/
public String getExportType() {
return this.exportType;
}
/**
* Set export type
*/
public void setExportType(String exportType) {
this.exportType = exportType;
}
/**
* Set image width
*/
public void setWidth(String width) {
this.width = width;
}
/**
* Get image width
*/
public String getWidth() {
return this.width;
}
/**
* Get export type list
*/
public Map<String, String> getExportTypeList() {
return new ImageTypesMap();
}
/**
* Get image width list
*/
public Map<String, String> getWidthList() {
return new ImageWidthMap(TextResources.getResourceString(
ResourceProps.DEFAULT));
}
/**
* Export map view as an image to the selected export format.
*/
public void exportImage(TaskEvent event) {
WebMap webMap = null;
int width;
int height;
int mwidth;
int mheight;
try {
_logger.info("Exporting map image");
requestTaskRender();
webMap = event.getWebContext().getWebMap();
width = webMap.getWidth();
height = webMap.getHeight();
mwidth = width;
mheight = height;
if (!this.getWidth().equalsIgnoreCase("default")) {
mwidth = webMap.getDpi() * Integer.parseInt(this.getWidth());
mheight = (int) ((height * mwidth) / width);
}
this.setResultUrl(WebUtil.getRequestContextPath() +
"/exportImageServlet?width=" + mwidth + "&height=" + mheight +
"&format=" + this.getExportType() + "&northArrow=" +
exportNorthArrow + "&graphicResources=" +
exportGraphicResources + "&scaleBar=" + exportScalebar);
} catch (Exception ex) {
renderExceptionMessage(ex);
_logger.error("Unable to export image", ex);
}
}
public void setExportNorthArrow(boolean export) {
exportNorthArrow = export;
}
public void setExportScalebar(boolean export) {
exportScalebar = export;
}
public void setExportGraphicResources(boolean export) {
exportGraphicResources = export;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -