📄 tablerenderer.java
字号:
package com.esri.solutions.jitk.web.faces.renderkit.xml;
import java.io.IOException;
import java.util.Map;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.render.Renderer;
import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import com.esri.adf.web.util.XMLUtil;
import com.esri.solutions.jitk.web.faces.component.TableControl;
/**
* @author Carsten Piepel
*
*/
public class TableRenderer extends Renderer {
private static final String RESULT_ROW_KEY = "_resultRowKey";
private static final String RESULT_ROW_ACTION = "_resultRowAction";
private static final String RESULT_PAGING_ACTION = "_resultPageAction";
private static final String RESULT_COL_KEY = "_resultColKey";
private static final String RESULT_COL_ACTION = "_resultColAction";
public static final String FIRST_TIME = TableRenderer.class.getName() + ".first-time";
private Logger log = Logger.getLogger(TableRenderer.class.getName());
/**
* Determines if the associated table should handle this event.
* If yes, this method calls the control's setEventArgs method.
*
* @param facesContext the {@link FacesContext}
* @param component the JSF component
*
* @throws java.io.IOException
*/
@SuppressWarnings("unchecked")
@Override
public void decode(FacesContext facesContext, UIComponent component) {
Map rMap = facesContext.getExternalContext().getRequestParameterMap();
String id = component.getId();
if (rMap.get(id) == null || !(rMap.get(id).equals(id))) return;
String key = (String) rMap.get(id + RESULT_ROW_KEY);
if(key == null) {
key = (String) rMap.get(id + RESULT_COL_KEY);
}
String action = (String) rMap.get(id + RESULT_ROW_ACTION);
if(action == null)
action = (String)rMap.get(id + RESULT_COL_ACTION);
if(action == null)
action = (String)rMap.get(id + RESULT_PAGING_ACTION);
((TableControl) component).queueTableEvent(facesContext, key, action);
}
/**
* This method generates an XML representation of the {@link TableControl}
* and then transform it to the required markup using XSLT.
*
* @param facesContext the {@link FacesContext}
* @param component the JSF component
* @throws java.io.IOException
*/
@SuppressWarnings("unchecked")
@Override
public void encodeEnd(FacesContext facesContext, UIComponent component) throws IOException {
TableControl control = (TableControl) component;
if (!control.isVisible()) return;
Map requestMap = facesContext.getExternalContext().getRequestMap();
Document doc = XMLUtil.newDocument();
Element root = XMLUtil.createElement(doc, "table", null, null);
XMLUtil.createElement(doc, "id", control.getId(), root);
XMLUtil.createElement(doc, "map-id", control.getMapId(), root);
XMLUtil.createCommonElements(doc, root, facesContext, component);
XMLUtil.createElement(doc, "first-time", "false".equals(requestMap.get(FIRST_TIME)) ? "false" : "true", root);
TableRendererUtil.renderTableXml(control.getTableModel(), control.getId(), root);
if (log.isDebugEnabled()) {
log.debug(XMLUtil.transform(doc, null));
}
facesContext.getResponseWriter().write(XMLUtil.transform(doc, control.getXslUrl()));
requestMap.put(FIRST_TIME, "false");
}
/*
private static void encodeColumns(Object rowData, Element parent,
String clientIdPrefix) {
BeanInfo beanInfo = getBeanInfo(rowData);
MethodDescriptor[] methodDescriptors = beanInfo.getMethodDescriptors();
for (int i = 0; i < methodDescriptors.length; i++) {
MethodDescriptor methodDescriptor = methodDescriptors[i];
Method method = methodDescriptor.getMethod();
if (!method.getName().startsWith("get")
&& !method.getName().startsWith("is")) {
continue;
}
Element header = XMLUtil.createElement("header", null, parent);
header.setAttribute("id", clientIdPrefix + "_"
+ methodDescriptor.getName());
header.setAttribute("name",
removeGetterPrefix(methodDescriptor.getDisplayName()));
}
}
private static void encodeRow(Object rowData, Element parent,
String clientId) {
if (rowData == null) {
return;
}
Element row = XMLUtil.createElement("row", null, parent);
row.setAttribute("id", clientId);
BeanInfo beanInfo = getBeanInfo(rowData);
MethodDescriptor[] methodDescriptors = beanInfo.getMethodDescriptors();
int columnId = 0;
for (int i = 0; i < methodDescriptors.length; i++) {
MethodDescriptor methodDescriptor = methodDescriptors[i];
Method method = methodDescriptor.getMethod();
if (!method.getName().startsWith("get")
&& !method.getName().startsWith("is")) {
continue;
}
int modifiers = method.getModifiers();
if (!(Modifier.isPublic(modifiers)
|| Modifier.isStatic(modifiers))) {
continue;
}
Class[] parameterTypes = method.getParameterTypes();
if (parameterTypes.length > 0) {
continue;
}
if (method.getReturnType() == null) {
continue;
}
Object returnValue = null;
try {
returnValue = method.invoke(rowData, (Object[])null);
} catch (IllegalArgumentException ex) {
throw new ADFException("Failed to dynamically invoke row data "
+ "get-property.", ex);
} catch (IllegalAccessException ex) {
throw new ADFException("Failed to dynamically invoke row data "
+ "get-property.", ex);
} catch (InvocationTargetException ex) {
throw new ADFException("Failed to dynamically invoke row data "
+ "get-property.", ex);
}
Element column = XMLUtil.createElement("column", null, row);
column.setAttribute("id", clientId + "_" + columnId++);
if (returnValue != null) {
column.setAttribute("value", returnValue.toString());
} else {
column.setAttribute("value", "");
column.setAttribute("is-null", "true");
}
}
}
private static BeanInfo getBeanInfo(Object bean) {
BeanInfo beanInfo = null;
try {
beanInfo = Introspector.getBeanInfo(bean.getClass(),
Object.class);
} catch (IntrospectionException ex) {
throw new ADFException("An exception occurred during row data "
+ "introspection.", ex);
}
return beanInfo;
}
private static final String removeGetterPrefix(String str)
{
if (str.startsWith("get")) {
str = str.substring(3);
} else if (str.startsWith("is")) {
str = str.substring(2);
}
return str;
}
*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -