AjaxSwing Features API
AjaxSwing provides several features that you can use in your application code via AjaxSwing API, mose of these features are inside ClientAgent class.
Note, you need to add ajaxswing.jar and asboot.jar to your build classpath to get access to API, wrap your code inside AjaxSwingManager.isAjaxSwingRunning() to make sure it still works in Swing mode.
Available API index
Handling URL parameters
Custom submits from browser
Keep alive events
Running custom JavaScript code
Browser Locale support
Opening resources in browser
Browser time zone support
Handling URL parameters
Allows you to have a listener to act on URL parameter like "/ajaxswing/SwingSet2?someParam=someValue".
void removeURLParamHandler(URLParameterHandler handler, java.lang.String paramKey) void addURLParamHandler(URLParameterHandler handler, java.lang.String paramKey)Here is an examples, it would add listener when button is pressed, first listener would be called when "title" parameter is in URL, and it would set button text to than parameter value, second would be called when "exit" parameter is specified and would close application:
final JButton btn = new JButton("Click me"); btn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(AjaxSwingManager.isAjaxSwingRunning()) { ClientAgent.getCurrentInstance().addURLParamHandler(new URLParameterHandler() { public void handle(ClientAgent agent, HttpRequestData requestData, String paramKey, Object paramValue) { btn.setText(String.valueOf(paramValue)); } }, "title"); ClientAgent.getCurrentInstance().addURLParamHandler(new URLParameterHandler() { public void handle(ClientAgent agent, HttpRequestData requestData, String paramKey, Object paramValue) { System.exit(0); } }, "exit"); } } });You can specify multiply listeners on same parameter, you can remove listener with:
ClientAgent.getCurrentInstance().removeURLParamHandler(handler, paramKey);
Custom submits from browser
Allows you to implement FastSubmitHandler that would handle request from browser done by JavaScript call "doFastSubmit()".
void addFastSubmitHandler(FastSubmitHandler handler, java.lang.String paramKey) void removeFastSubmitHandler(FastSubmitHandler handler, java.lang.String paramKey)Here is an examples, it would get string text from input, send it with Ajax to server and put into text field:
$("#your_input").bind("keyup input paste", function () { doFastSubmit("customKey", {text:$(this).val()}); });And on Java side implement listener:
ClientAgent.getCurrentInstance().addFastSubmitHandler(new CustomKeyHandler(), "customKey"); public class CustomKeyHandler implements FastSubmitHandler { public CustomKeyHandler(ClientAgent agent) { } public void handle(ClientAgent agent, HttpRequestData requestData, String actionString) { String text = (String) requestData.getParams().get("text"); textField.setText(text); } }
Keep alive events
AjaxSwing would periodically "ping" server while application is opened in browser (including inactive tab), ping interval is configurable in properties file. Normal requests (like button click) would also generate KeepAliveEvent and reset interval time.
void addKeepAliveEventHandler(KeepAliveEventHandler handler) void removeKeepAliveEventHandler(KeepAliveEventHandler handler) KeepAliveEvent getLastKeepAliveEvent()AjaxSwing provides 2 ways to handle KeepAliveEvents:
1) Polling ClientAgent.
In any place in your code you can call "getLastKeepAliveEvent" method that would return last event at the moment. Code sample:
if(AjaxSwingManager.isAjaxSwingRunning()) { KeepAliveEvent event = ClientAgent.getCurrentInstance().getLastKeepAliveEvent(); System.out.println("Last keep alive event date: " + event.getEventDate()); }2) Callback. You can add a listener that would be called each time new KeepAliveEvent occurs, this listener would also get HttpRequestData that provides access to data submitted from browser. Code sample:
if(AjaxSwingManager.isAjaxSwingRunning()) { ClientAgent.getCurrentInstance().addKeepAliveEventHandler(new KeepAliveEventHandler() { @Override public void handle(ClientAgent agent, HttpRequestData requestData, KeepAliveEvent event) { System.out.println("Keep Alive Occurred"); System.out.println("Event date: " + event.getEventDate()); System.out.println("Previous event date: " + event.getPreviousEvent().getEventDate()); System.out.println("Event generated by user activity: " + event.isUserActivity()); System.out.println("Event action: " + requestData.getAction()); } }); }In both cases you get KeepAliveEvent object that contains:
Date eventDate - Date when event was received by server. KeepAliveEvent previousEvent - previous event (note that event.previousEvent.previousEvent would be null). boolean userActivity - indicates if event was created because of user activity. Would be "true" when user clicks on a button, resizes browser etc, would be "false" on "ping" and "update" requests from browser.KeepAlive configurations are stored in application properties:
# Enables keep alive mechanism when set to true
window.*.session.keepAlive = true
# Keep alive ping interval in seconds
window.*.session.keepAliveInterval = 60
Running custom JavaScript code
AjaxSwing allows you to run any JavaScript code statement during after processing.
void appendJavaScript(java.lang.String javaScript)For example if you want to show alert dialog on button press, you can do following:
final JButton btn = new JButton("Click me"); btn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(AjaxSwingManager.isAjaxSwingRunning()) { ClientAgent.getCurrentInstance().appendJavaScript("alert('test');"); } } });
Browser Locale support
AjaxSwing supports i18n and allows you to work with static on browser locale.
String getBrowserLocale() Locale getUserLocale()Locale for user is selected by:
1) Locale from URL (localhost:8040/ajaxswing/apps/SwingSet2?locale=fr_FR)
2) If URL locale not available - detect browser locale
3) If browser locale not available - use system locale
You can get user locale with:
ClientAgent.getCurrentInstance().getUserLocale();Using following in properties file would set user locale as JVM locale where application is running:
agent.forceJvmLocale=true
Opening resources in browser
AjaxSwing allows you to open any URL from your Java code in user browser. In would open in new tab or window based on browser settings.
void openInNewTab(java.lang.String url)Example that opens google on button click:
final JButton btn = new JButton("Click me"); btn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(AjaxSwingManager.isAjaxSwingRunning()) { ClientAgent.getCurrentInstance().openInNewTab("http://google.com/"); } } });
Browser time zone support
AjaxSwing supports getting Timezone from user browser.
TimeZone getTimeZone() int getTimeZoneCurrentOffset() int getTimeZoneRawOffset() boolean getTimeZoneUseDaylight()getTimeZoneCurrentOffset() - returns the time difference between GMT and current local time, in minutes, includes daylight saving time offset
getTimeZoneRawOffset() - returns the time difference between GMT and local time, in minutes, not affected by daylight saving time
getTimeZoneUseDaylight() - returns if timezone use daylight saving time
getTimeZone() - returns timezone for client browser
API would not always return exact client timezone, browser does not tell server which timezone it is using, only GMT offset, getTimeZone() method would return timezone that has same GMT offset as browser, same daylight offset and exact same dates when timezone switch to daylight time and from daylight time, so time in this timezone should always match to client local time. If no information is available from browser, server timezone would be returned. Example:
TimeZone timezone = null; if(AjaxSwingManager.isAjaxSwingRunning()) { timezone = ClientAgent.getCurrentInstance().getTimeZone(); } else { timezone = TimeZone.getDefault(); } Date date = new Date(); SimpleDateFormat format = new SimpleDateFormat(); format.setTimeZone(timezone); format.format(date);