Package org.zkoss.zkforge.clipboard
Class ClipboardHelper
- java.lang.Object
-
- org.zkoss.zkforge.clipboard.ClipboardHelper
-
public class ClipboardHelper extends Object
Static helper class that provides Java access to the browser's Clipboard API.This class wraps the native browser Clipboard API and provides static methods to read from and write to the system clipboard. All operations are asynchronous and require user interaction due to browser security restrictions.
Important: Clipboard operations must be triggered from user interactions (click, keypress, etc.) as they will fail if called outside of event handlers.
Security Note: When handling clipboard content, sanitize user input to prevent potential security vulnerabilities.
Example usage:
// Reading from clipboard (results delivered via desktop events) ClipboardHelper.readText(); // Writing to clipboard ClipboardHelper.writeText("Hello World");Results are delivered asynchronously via
ClipboardEventposted to the desktop. Components can listen for these events to handle clipboard results.Based on MDN Clipboard API
-
-
Field Summary
Fields Modifier and Type Field Description protected static ClipboardAuServiceauServiceprotected static StringCLIPBOARD_HELPER_JS_PATHprotected static StringCLIPBOARD_HELPER_KEY
-
Constructor Summary
Constructors Constructor Description ClipboardHelper()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description protected static voidaddHelperScript()static voiddispose()Dispose clipboard helper for the current desktop.protected static voidensureExecutionAvailable()static voidinit()Initialize clipboard helper for the current desktop if not already initialized.static voidreadImage()Read image from the system clipboard.static voidreadText()Read text from the system clipboard.static voidwriteText(String text)Write text to the system clipboard.
-
-
-
Field Detail
-
CLIPBOARD_HELPER_KEY
protected static final String CLIPBOARD_HELPER_KEY
- See Also:
- Constant Field Values
-
CLIPBOARD_HELPER_JS_PATH
protected static final String CLIPBOARD_HELPER_JS_PATH
- See Also:
- Constant Field Values
-
auService
protected static ClipboardAuService auService
-
-
Method Detail
-
writeText
public static void writeText(String text)
Write text to the system clipboard.- Parameters:
text- the text to write to the clipboard
-
readText
public static void readText()
Read text from the system clipboard. Results are delivered asynchronously viaClipboardEvent.- Throws:
IllegalStateException- if called outside an execution context
-
readImage
public static void readImage()
Read image from the system clipboard. Results are delivered asynchronously viaClipboardEvent.- Throws:
IllegalStateException- if called outside an execution context
-
init
public static void init()
Initialize clipboard helper for the current desktop if not already initialized. This method ensures the AU service and JavaScript are properly set up. You should call this method before you call any action likewriteText(String)orreadText(). You should call this method in a Composer's life cycle method e.g.Composer.doAfterCompose(Component).
-
addHelperScript
protected static void addHelperScript()
-
dispose
public static void dispose()
Dispose clipboard helper for the current desktop. Removes the AU service listener and JavaScript helper for this desktop.- Throws:
IllegalStateException- if called outside an execution context
-
ensureExecutionAvailable
protected static void ensureExecutionAvailable()
-
-