zkツールクラスZkUtils 1.1バージョン
ZkUtilsはzkでよく使われるいくつかの機能を集め、開発中の使用を便利にしています.
このバージョンの更新リスト:
1、いくつかのバグを修正しました.
2,より詳細なコメントを追加
3、いくつかの一般的な方法を追加しました
添付ファイルにソースコードがあります
package org.sunflower.common.web.zk.util;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Collection;
import java.util.Map;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.Desktop;
import org.zkoss.zk.ui.Execution;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Session;
import org.zkoss.zk.ui.Sessions;
import org.zkoss.zk.ui.WebApp;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zk.ui.util.Clients;
import org.zkoss.zul.Messagebox;
/**
* ZkUtils zk ,
*
*
* @author sunflower
*
* :2010-7-9 09:40:26
*
* Email:zhangxuehuaemail # gmail com
*
*/
public class ZkUtils {
/**
* Execution
*
* @return
*/
public static Execution exec() {
return Executions.getCurrent();
}
/**
*
*
* @return
*/
public static Desktop desktop() {
return exec().getDesktop();
}
/**
* webapp
*
* @return
*/
public static WebApp webApp() {
return desktop().getWebApp();
}
/**
* key webapp
*
* @param key
* @return
*/
public static Object webAppAttr(String key) {
return webApp().getAttribute(key);
}
/**
* key webapp
*
* @param key
* @param value
*/
public static void setAppAttr(String key, Object value) {
webApp().setAttribute(key, value);
}
/**
* URL
*
* @param path
* @return
*/
public static URL getResource(String path) {
return webApp().getResource(path);
}
/**
*
*
* <p>
* Notice that, since 3.6.3, this method can retreive the resource starting
* with "~./". If the path contains the wildcard ('*'), you can use
* {@link Execution#locate} to convert it to a proper string first.
*/
public static InputStream getResourceAsStream(String path) {
return webApp().getResourceAsStream(path);
}
/**
* path . , the path "/index.html" returns the absolute file
* path on the server's filesystem would be served by a request for
* "http://host/contextPath/index.html", where contextPath is the context
* path of this {@link WebApp}.
*
* <p>
* Notice that ZK don't count on this method to retrieve resources. If you
* want to change the mapping of URI to different resources, override
* {@link org.zkoss.zk.ui.sys.UiFactory#getPageDefinition} instead.
*/
public static String getRealPath(String path) {
return webApp().getRealPath(path);
}
/**
* mime Returns the MIME type of the specified file, or null if the
* MIME type is not known. The MIME type is determined by the configuration
* of the Web container.
* <p>
* Common MIME types are "text/html" and "image/gif".
*/
public static String getMimeType(String file) {
return webApp().getMimeType(file);
}
/**
* session
*
* @return
*/
public static Session session() {
return Sessions.getCurrent();
}
/**
* key session
*
* @param name
* @param value
*/
public static void setSessionAttr(String key, Object value) {
session().setAttribute(key, value);
}
/**
* request session key
*
* @param name
* @return
*/
public static Object getSessionAttr(String key) {
return session().getAttribute(key);
}
/**
* session
*
* @return
*/
@SuppressWarnings("unchecked")
public static Map getSessionAttrs() {
return session().getAttributes();
}
/**
*
*
* @param name
*
* @return
*/
public static Object getParameter(String name) {
return exec().getParameter(name);
}
/**
*
*
* @return map
*/
public static Object getParameterMap() {
return exec().getParameterMap();
}
/**
*
*
* @param name
*
* @return
*/
public static Object getRequestAttr(String name) {
return exec().getAttribute(name);
}
/**
* key request scope
*
* @param name
* @param value
*/
public static void setRequestAttr(String name, Object value) {
exec().setAttribute(name, value);
}
/**
*
*
* @param name
*
* @return
*/
@SuppressWarnings("unchecked")
public static Map getRequestAttrs() {
return exec().getAttributes();
}
/**
*
*
* After calling this method, the caller shall end the processing
* immediately (by returning). All pending requests and events will be
* dropped.
*
* Parameters: uri the URI to redirect to, or null to reload the same page
*/
public static void sendRedirect(String uri) {
exec().sendRedirect(uri);
}
/**
*
* <p>
*
*
* @param uri
* uri
* @param target
* uri , target=null,
*/
public static void sendRedirect(String uri, String target) {
exec().sendRedirect(uri, target);
}
/**
*
* <p>
* : ,
*
* : url ,
*
* @param uri
*/
public static void forward(String uri) {
try {
exec().forward(uri);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Sends the event to the specified component and process it
* immediately. This method can only be called when processing an event. It
* is OK to send event to component from another page as long as they are in
* the same desktop.
* <p>
* <a href="http://zh.zkoss.org/doc/devguide/ch05s03.html"> </a>
*/
public static void sendEvent(Component comp, Event event) {
Events.sendEvent(comp, event);
}
/**
* event Sends the event the target specified in the event. *
* <p>
* <a href="http://zh.zkoss.org/doc/devguide/ch05s03.html"> </a>
* <p>
* Note: {@link Event#getTarget} cannot be null.
*/
public static void sendEvent(Event event) {
Events.sendEvent(event);
}
/**
* execution Posts an event to the current execution.
* <p>
* The priority of the event is assumed to be 0. Refer to
* {@link #postEvent(int, Event)}.
*
* <p>
* On the other hand, the event sent by {@link #sendEvent} is processed
* immediately without posting it to the queue.
*
* <p>
* Note: if the target of an event is not attached to the page yet, the
* event is ignored silently.
* <p>
* <a href="http://zh.zkoss.org/doc/devguide/ch05s03.html"> </a>
*
* @see #sendEvent
* @see #echoEvent
* @see #postEvent(int, Event)
*/
public static final void postEvent(Event event) {
exec().postEvent(event);
}
/**
* target Posts an instance of {@link Event} to the current
* execution.
* <p>
* The priority of the event is assumed to be 0. Refer to
* {@link #postEvent(int, String, Component, Object)}. <a
* href="http://zh.zkoss.org/doc/devguide/ch05s03.html"> </a>
*
* @see #postEvent(Event)
* @see #postEvent(int, String, Component, Object)
*/
public static final void postEvent(String name, Component target,
Object data) {
postEvent(0, name, target, data);
}
/**
* Posts an event to the current execution with the specified priority.
*
* <p>
* The posted events are processed from the higher priority to the lower
* one. If two events are posted with the same priority, the earlier the
* event being posted is processed earlier (first-in-first-out).
*
* <p>
* The priority posted by posted by {@link #postEvent(Event)} is 0.
* Applications shall not use the priority higher than 10,000 and lower than
* -10,000 since they are reserved for component development.
*
* @param priority
* the priority of the event.
* @since 3.0.7
*/
public static final void postEvent(int priority, Event event) {
exec().postEvent(priority, event);
}
/**
* Posts an instance of {@link Event} to the current execution with the
* specified priority.
*
* <p>
* The posted events are processed from the higher priority to the lower
* one. If two events are posted with the same priority, the earlier the
* event being posted is processed earlier (first-in-first-out).
*
* <p>
* The priority posted by posted by {@link #postEvent(Event)} is 0.
* Applications shall not use the priority higher than 10,000 and lower than
* -10,000 since they are reserved for component development.
*
* @param priority
* the priority of the event.
* @since 3.0.7
*/
public static final void postEvent(int priority, String name,
Component target, Object data) {
Events.postEvent(priority, name, target, data);
}
/**
*
* . By echo we mean the event is fired after the client receives the AU
* responses and then echoes back. In others, the event won't be execute in
* the current execution. Rather, it executes after the client receives the
* AU responses and then echoes back the event back.
*
* <p>
* It is usually if you want to prompt the user before doing a long
* operartion. A typical case is to open a hightlighted window to prevent
* the user from clicking any button before the operation gets done.
*
* @since 3.0.2
* @see #sendEvent
* @see #echoEvent
* @param name
* the event name, such as onSomething
* @param target
* the component to receive the event (never null).
* @param data
* the extra information, or null if not available. It will
* become {@link Event#getData}.
*/
public static final void echoEvent(String name, Component target,
String data) {
Events.echoEvent(name, target, data);
}
/**
*
* <p>
* <b style="color:red;"/> </b>: zk5.0 , , zk.xml
* <disable-event-thread>true</disable-event-thread> ,
* true false
* , , <disable-event-thread>false</disable
* -event-thread
* > , messagebox , if 。 <a
* href="http://sunflowers.iteye.com/blog/686243"> <a>
*
* @param message
*
* @param title
*
*/
public static void showInformationbox(String message, String title) {
show(message, title, Messagebox.INFORMATION);
}
/**
*
* <p>
* <b style="color:red;"/> </b>: zk5.0 , , zk.xml
* <disable-event-thread>true</disable-event-thread> ,
* true false
* , , <disable-event-thread>false</disable
* -event-thread
* > , messagebox , if 。 <a
* href="http://sunflowers.iteye.com/blog/686243"> <a>
*
* @param message
*
* @param title
*
* @return boolean ,true ,false
*/
public static boolean showQuestionbox(String message, String title) {
try {
int flag = Messagebox
.show(message, title, Messagebox.OK | Messagebox.CANCEL,
Messagebox.QUESTION, Messagebox.CANCEL);
return flag == Messagebox.OK;
} catch (InterruptedException e) {
}
return false;
}
/**
*
* <p>
* <b style="color:red;"/> </b>: zk5.0 , , zk.xml
* <disable-event-thread>true</disable-event-thread> ,
* true false
* , , <disable-event-thread>false</disable
* -event-thread
* > , messagebox , if 。 <a
* href="http://sunflowers.iteye.com/blog/686243"> <a>
*
* @param message
*
* @param title
*
*/
public static void showExclamationbox(String message, String title) {
show(message, title, Messagebox.EXCLAMATION);
}
/**
*
* <p>
* <b style="color:red;"/> </b>: zk5.0 , , zk.xml
* <disable-event-thread>true</disable-event-thread> ,
* true false
* , , <disable-event-thread>false</disable
* -event-thread
* > , messagebox , if 。 <a
* href="http://sunflowers.iteye.com/blog/686243"> <a>
*
* @param message
*
* @param title
*
*/
public static void showErrorbox(String message, String title) {
show(message, title, Messagebox.ERROR);
}
/**
* Information
*
* @param message
*
* @param title
*
* @param icon
* :Messagebox.INFORMATION,Messagebox.QUESTION,Messagebox.EXCLAMATION
* ,Messagebox.ERROR
*/
private static void show(String message, String title, String icon) {
try {
Messagebox.show(message, title, Messagebox.OK, icon);
} catch (InterruptedException e) {
// do nothing
}
}
/**
* ,
*/
public static String getRemoteHost() {
return exec().getRemoteHost();
}
/**
*
* IP
*/
public String getRemoteAddr() {
return exec().getRemoteAddr();
}
/**
* ( ServletRequest), null
*
* <p>
* The returned object depends on the Web container. If it is based Java
* servlet container, an instance of javax.servlet.ServletRequest is
* returned.
*/
public static Object getNativeRequest() {
return exec().getNativeRequest();
}
/**
* ( ServletResponse), null
*
* <p>
* The returned object depends on the Web container. If it is based Java
* servlet container, an instance of javax.servlet.ServletResponse is
* returned.
*/
public static Object getNativeResponse() {
return exec().getNativeResponse();
}
/**
*
* <p>
* , " , ..." <br />
* ,
*
* @since 5.0
*/
public static void startProcessing() {
Clients.evalJavaScript("zk.startProcessing(1);");
}
/**
*
* <p>
* " , ..." ,<br />
* ,
*
* @since 5.0
*/
public static void endProcessing() {
Clients.evalJavaScript("zk.endProcessing();");
}
/**
*
*
* @param oldc
*
* @param newc
*
* @exception IllegalArgumentException
* page
* @since 3.5.2
*/
public static void replaceComponent(Component oldc, Component newc) {
Components.replace(oldc, newc);
}
/**
* .
*
* <pre>
* <code>parent.getChildren().clear();
* parent.getChildren().addAll(newChildren);
* </code>
* </pre>
*
* @since 3.5.2
*/
@SuppressWarnings("unchecked")
public static void replaceChildren(Component parent, Collection newChildren) {
Components.replaceChildren(parent, newChildren);
}
/**
* , , , null ,
*
* @since 3.6.3
*/
public static Component getRoot(Component comp) {
return Components.getRoot(comp);
}
/**
* node1 node2 , true
*/
public static boolean isAncestor(Component node1, Component node2) {
return Components.isAncestor(node1, node2);
}
/**
* , <code>comp.getChildren().clear()</code>.
*/
public static void removeAllChildren(Component comp) {
if (comp == null || comp.getChildren().size() < 1)
return;
comp.getChildren().clear();
}
/**
* ( ).
* <p>
* : comp , true. , ,
* <code>Components.isRealVisible(getParent())</code>.
*
* @see Component#isVisible
*/
public static boolean isRealVisible(Component comp) {
return Components.isRealVisible(comp);
}
/**
* id
*
* @since 5.0.3
*/
public static final boolean isAutoUuid(String id) {
return org.zkoss.zk.ui.sys.ComponentsCtrl.isAutoUuid(id);
}
/**
* Configuration
*/
public static final Object getConfiguration(String key) {
return webApp().getConfiguration().getAttribute(key);
}
/**
*
*
* @param event
* @return
*/
public static Event getRealOrigin(ForwardEvent event) {
return Events.getRealOrigin(event);
}
}