Event Listener Services
addBrowserActionListener
public void addBrowserActionListener(WebActionListener listener);
Description:
Adds a WebswingBrowserActionListener to listen to javascript browser initiated events.
Parameters:
listener The listener to add
Example:
private void addBrowserActionListenerExample() {
WebswingApi webswingApi = WebswingUtil.getWebswingApi();
if (webswingApi != null) {
if (browserActionListener == null) {
browserActionListener = actionEvent -> console.append("Action: " + actionEvent.getActionName() + "\n");
webswingApi.addBrowserActionListener(browserActionListener);
console.append("Browser action listener has been added\nCall this command from web browser console:\n");
console.append("webswingInstance0.performAction({actionName:'webswing-browser-action-test'})");
console.append("\n");
} else {
console.append("Browser action listener has already been added.\n");
}
}
}
removeBrowserActionListener
public void removeBrowserActionListener(WebActionListener listener);
Description:
Removed a WebswingBrowserActionListener.
Parameters:
listener The listener to remove
Example:
private void removeBrowserActionListenerExample() {
WebswingApi webswingApi = WebswingUtil.getWebswingApi();
if (webswingApi != null) {
if (browserActionListener != null) {
webswingApi.removeBrowserActionListener(browserActionListener);
browserActionListener = null;
console.append("Browser action listener has been removed\n");
} else {
console.append("Browser action listener was not added before, you cannot remove it\n");
}
}
}
setActionCallListener
public void setActionCallListener(WebActionCallListener actionCallListener);
Description:
Set a WebActionCallListener to listen to javascript browser initiated calls, using webswingInstance0.callAction.
Parameters:
actionCallListener The listener to set
Example:
private void setActionCallListenerExample() {
WebswingApi webswingApi = WebswingUtil.getWebswingApi();
if (webswingApi != null) {
webswingApi.setActionCallListener(wae -> {
if ("webswing-action-call-test".equals(wae.getActionName())) {
console.append("Action call received with data [" + wae.getData() + "]\n");
return new WebActionResponse("success", null);
}
return null;
});
console.append("Action call listener has been added\nCall this command from web browser console:\n");
console.append("webswingInstance0.callAction({actionName:'webswing-action-call-test', data: 'test123'}).then((res) => console.log(res))");
console.append("\n");
}
}
invokeWhenLaunched
public void invokeWhenLaunched(Runnable runnable);
Description:
Invoke the given runnable when Webswing is launched and user context is already available. When starting an IDLE instance with app main class configured in pre-launch, the runnable will be invoked when user is connecting to the IDLE instance. When starting a normal instance with app main class configured in standard launch, the runnable will be invoked immediately. The runnable is not guaranteed to be executed on EDT thread or on caller thread.
Parameters:
runnable runnable to invoke
Example:
private void invokeWhenLaunchedExample() {
console.append("No example.\n");
}
getWebswingStartupPhase
public WebswingStartupPhase getWebswingStartupPhase();
Description:
Get the current phase of Webswing startup. After main.Main call the phase is 'started'. After pre-launch main execution the phase is 'prelaunched'. After launch main execution the phase is 'launched'.
Returns:
current phase of Webswing startup
Example:
private void getWebswingStartupPhaseExample() {
WebswingApi webswingApi = WebswingUtil.getWebswingApi();
if (webswingApi != null) {
console.append("Current startup phase is '" + WebswingUtil.getWebswingApi().getWebswingStartupPhase() + "'.\n");
}
}
addShutdownListener
public void addShutdownListener(WebswingShutdownListener listener);
Description:
Add listener that is triggered when Webswing requests swing application to exit. If there is no explicit shutdown listener Webswing use default shutdown procedure (send window closing event to all windows). Otherwise listeners are fired. It is expected the result of listener execution will exit the swing process. Otherwise the swing process will be forcefully terminated after defined timeout (system property "webswing.waitForExit", default: 30000)
Parameters:
listener listener to add
Example:
private void addShutdownListenerExample() {
WebswingApi webswingApi = WebswingUtil.getWebswingApi();
if (webswingApi != null) {
if (shutdownListener == null) {
shutdownListener = new WebswingShutdownListener() {
@Override
public int onBeforeShutdown(OnBeforeShutdownEvent event) {
SwingUtilities.invokeLater(() -> {
int result;
switch (event.getReason()) {
case Inactivity:
result = JOptionPane.showConfirmDialog(null, "This application will close in 10s due to inactivity", "Application closing...", JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);
if (result == JOptionPane.CANCEL_OPTION && WebswingUtil.getWebswingApi() != null) {
webswingApi.resetInactivityTimeout();
}
break;
case Admin:
result = JOptionPane.showConfirmDialog(null, "Admin has requested shutdown of this application. Application will shutdown in 10s", "Application closing...", JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);
if (result == JOptionPane.CANCEL_OPTION && WebswingUtil.getWebswingApi() != null) {
webswingApi.resetInactivityTimeout();
}
break;
default:
JOptionPane.showConfirmDialog(null, "Application will exit in 10s: " + event.getReason(), "Application closing...", JOptionPane.OK_OPTION, JOptionPane.WARNING_MESSAGE);
break;
}
});
return 10;
}
@Override
public void onShutdown() {
System.out.println("ShutdownListener : onShutdown() : faking my death.");
System.exit(0);
}
};
webswingApi.addShutdownListener(shutdownListener);
console.append("Shutdown listener has been added\n");
} else {
console.append("Shutdown listener has already been added.\n");
}
}
}
removeShutdownListener
public void removeShutdownListener(WebswingShutdownListener listener);
Description:
Remove shutdown listener
Parameters:
listener listener to remove
Example:
private void removeShutdownListenerExample() {
WebswingApi webswingApi = WebswingUtil.getWebswingApi();
if (webswingApi != null) {
if (shutdownListener != null) {
webswingApi.removeShutdownListener(shutdownListener);
shutdownListener = null;
console.append("Shutdown listener has been removed\n");
} else {
console.append("Shutdown listener was not added before, you cannot remove it\n");
}
}
}
notifyShutdown
public void notifyShutdown(int forceKillTimeout);
Description:
When swing application exit and shutdown process takes longer time to process, invoking this method will notify user (web session) the application has finished, and disconnect the user from the session. Swing Application process is removed from running sessions list even though the process might still be running.
Parameters:
forceKillTimeout how long (in Ms) to wait for process to finish. After this time the process is forcefully terminated.
Example:
private void notifyShutdownExample() {
WebswingApi webswingApi = WebswingUtil.getWebswingApi();
if (webswingApi != null) {
JTextField forceKillTimeoutField = new JTextField(5) {{
setText("3000");
}};
JPanel myPanel = new JPanel() {{
add(new JLabel("Set ForceKillTimeout:"));
add(forceKillTimeoutField);
}};
int result = JOptionPane.showConfirmDialog(null, myPanel, "Set parameter [int forceKillTimeout]", JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION) {
String param = forceKillTimeoutField.getText();
try {
webswingApi.notifyShutdown(Integer.valueOf(param));
System.out.println("Notify shutdown in " + param + "ms");
// See webswing.log file
} catch (NumberFormatException e) {
console.append("NumberFormatException: " + e.getMessage());
}
}
}
}
addUrlStateChangeListener
public void addUrlStateChangeListener(WebswingUrlStateChangeListener listener);
Description:
Register a URL state change listener
Parameters:
listener the listener
Example:
private void addUrlStateChangeListenerExample() {
WebswingApi webswingApi = WebswingUtil.getWebswingApi();
if (webswingApi != null) {
if (urlStateChangeListener == null) {
urlStateChangeListener = event -> {
if (event.getState().isEmpty() && !event.getOldState().isEmpty()) { //if user reconnects or completely removes the #fragment from url
webswingApi.setUrlState(event.getOldState(), false); //the url is recreated using the old url state
}
if (webswingApi.getUrlState() != null) {
console.append("URL was changed to : " + webswingApi.getUrlState().getPath() + "\n");
}
};
webswingApi.addUrlStateChangeListener(urlStateChangeListener);
console.append("URL state change listener has been added\n");
}
} else {
console.append("URL state change listener has already been added.\n");
}
}
removeUrlStateChangeListener
public void removeUrlStateChangeListener(WebswingUrlStateChangeListener listener);
Description:
Remove URL state change listener
Parameters:
listener the listener
Example:
private void removeUrlStateChangeListenerExample() {
WebswingApi webswingApi = WebswingUtil.getWebswingApi();
if (webswingApi != null) {
if (urlStateChangeListener != null) {
webswingApi.removeUrlStateChangeListener(urlStateChangeListener);
urlStateChangeListener = null;
console.append("URL state change listener has been removed\n");
} else {
console.append("URL state change listener was not added before, you cannot remove it\n");
}
}
}
getUrlState
public WebswingUrlState getUrlState();
Description:
URL State
Returns:
current user's URL state (parsed hash fragment of URL) or null if no user is connected.
Example:
private void getUrlStateExample() {
WebswingApi webswingApi = WebswingUtil.getWebswingApi();
if (webswingApi != null) {
webswingApi.getUrlState();
console.append("Current URL state is :\n");
console.append(webswingApi.getUrlState().getPath() + "\n");
}
}
setUrlState (1)
public void setUrlState(WebswingUrlState state);
Description:
See {@link WebswingApi#setUrlState(WebswingUrlState, boolean)}. This method will not trigger url change event in {@link WebswingUrlStateChangeListener}
Parameters:
state state object url is generated from
Example:
private void setUrlStateExample1() {
WebswingApi webswingApi = WebswingUtil.getWebswingApi();
if (webswingApi != null) {
JTextField pageField = new JTextField(5) {{
setText("Files");
}};
JPanel myPanel = new JPanel() {{
add(new JLabel("Set URL:"));
add(pageField);
}};
int result = JOptionPane.showConfirmDialog(null, myPanel, "Set parameter [String page]", JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION) {
String page = pageField.getText();
WebswingUrlState s = webswingApi.getUrlState();
s.setPath(page);
webswingApi.setUrlState(s);
console.append("URL state has been set to:\n");
console.append(page + "\n");
console.append("Only URL in web browser has been changed\n");
}
}
}
setUrlState (2)
public void setUrlState(WebswingUrlState state, boolean fireChangeEvent);
Description:
Sets the hash Fragment of users browser URL to represent the current state of the swing application. Intended for use in combination with {@link WebswingUrlStateChangeListener} and/or {@link WebswingApi#getUrlState()}
Parameters:
fireChangeEvent if true, invoking this method will trigger url change event
Example:
private void setUrlStateExample2() {
WebswingApi webswingApi = WebswingUtil.getWebswingApi();
if (webswingApi != null) {
JTextField pageField = new JTextField(5) {{
setText("Fonts");
}};
JCheckBox fireChangeEventCheckBox = new JCheckBox("Set FireChangeEvent:", true);
fireChangeEventCheckBox.setHorizontalTextPosition(SwingConstants.LEFT);
JPanel myPanel = new JPanel() {{
add(new JLabel("Set URL:"));
add(pageField);
add(Box.createHorizontalStrut(15));
add(fireChangeEventCheckBox);
}};
int result = JOptionPane.showConfirmDialog(null, myPanel, "Set parameters [String page, boolean fireChangeEvent]", JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION) {
boolean fire = fireChangeEventCheckBox.isSelected();
String page = pageField.getText();
WebswingUrlState s = webswingApi.getUrlState();
s.setPath(page);
webswingApi.setUrlState(s, fire);
console.append("URL state has been set to:\n");
console.append(page + "\n");
console.append("fireChangeEvent: \n");
console.append(fire + "\n");
if (!fire) {
console.append("Only URL in web browser has been changed\n");
}
}
}
}
addUserConnectionListener
public void addUserConnectionListener(WebswingUserListener listener);
Description:
Add listener to receive notifications when user (primary/mirror view) connects or disconnects from session
Parameters:
listener listener to add
Example:
private void addUserConnectionListenerExample() {
WebswingApi webswingApi = WebswingUtil.getWebswingApi();
if (webswingApi != null) {
if (userConnectionListener == null) {
userConnectionListener = new WebswingUserListener() {
@Override
public void onPrimaryUserDisconnected(UserEvent evt) {
console.append("onUserDisconnected(" + evt.getUser() + ")\n");
}
@Override
public void onPrimaryUserConnected(UserEvent evt) {
console.append("onUserConnected(" + evt.getUser() + ")\n");
}
@Override
public void onMirrorViewDisconnected(UserEvent evt) {
console.append("onMirrorViewDisconnected(" + evt.getUser() + ")\n");
}
@Override
public void onMirrorViewConnected(UserEvent evt) {
console.append("onMirrorViewConnected(" + evt.getUser() + ")\n");
}
};
webswingApi.addUserConnectionListener(userConnectionListener);
console.append("User connection listener has been added\n");
} else {
console.append("User connection listener has already been added.\n");
}
}
}
removeUserConnectionListener
public void removeUserConnectionListener(WebswingUserListener listener);
Description:
Remove user listener
Parameters:
listener listener to remove
Example:
private void removeUserConnectionListenerExample() {
WebswingApi webswingApi = WebswingUtil.getWebswingApi();
if (webswingApi != null) {
if (userConnectionListener != null) {
webswingApi.removeUserConnectionListener(userConnectionListener);
userConnectionListener = null;
console.append("User connection listener has been removed\n");
} else {
console.append("User connection listener was not added before, you cannot remove it\n");
}
}
}
addLinkedViewListener
public void addLinkedViewListener(LinkedViewListener listener);
Description:
Add listener to receive notifications about Linked View.
Parameters:
listener listener to add
Example:
private void addLinkedViewListenerExample() {
WebswingApi webswingApi = WebswingUtil.getWebswingApi();
if (webswingApi != null) {
if (linkedViewListener == null) {
linkedViewListener = new LinkedViewListener() {
@Override
public void onLinkedViewInitialized(LinkedViewEvent evt) {
console.append("Linked View initialized with linkedViewName '" + evt.getLinkedViewName() + "' and tabId '" + evt.getTabId() + "'.\n");
}
};
webswingApi.addLinkedViewListener(linkedViewListener);
console.append("Linked View listener has been added.\n");
} else {
console.append("Linked View listener has already been added.\n");
}
}
}
removeLinkedViewListener
public void removeLinkedViewListener(LinkedViewListener listener);
Description:
Remove Linked View listener.
Parameters:
listener listener to remove
Example:
private void removeLinkedViewListenerExample() {
WebswingApi webswingApi = WebswingUtil.getWebswingApi();
if (webswingApi != null) {
if (linkedViewListener != null) {
webswingApi.removeLinkedViewListener(linkedViewListener);
linkedViewListener = null;
console.append("Linked View listener has been removed.\n");
} else {
console.append("Linked View listener has not been added yet, you cannot remove it.\n");
}
}
}
invokeWhenPrimaryUserConnected
public void invokeWhenPrimaryUserConnected(WebswingUserEventHandler handler, boolean oneTime);
Description:
Invoke the given handler when primary user connection from browser to the app is established. If a primary user is already connected at the time of calling this method, the handler will be invoked immediately. The handler is not guaranteed to be executed on EDT thread or on caller thread.
Parameters:
oneTime should the handler be invoked only on a single occurrence or should it be invoked every time a user is connected
Example:
private void invokeWhenPrimaryUserConnectedExample() {
WebswingApi webswingApi = WebswingUtil.getWebswingApi();
if (webswingApi != null) {
webswingApi.invokeWhenPrimaryUserConnected(evt -> {
console.append("user " + evt.getUser() + " is connected\n");
}, false);
}
}
invokeWhenPrimaryUserDisconnected
public void invokeWhenPrimaryUserDisconnected(WebswingUserEventHandler handler, boolean oneTime);
Description:
Invoke the given handler when primary user connection from browser to the app is lost. If there is no primary user connected at the time of calling this method, the handler will be invoked immediately. The handler is not guaranteed to be executed on EDT thread or on caller thread.
Parameters:
oneTime should the handler be invoked only on a single occurrence or should it be invoked every time a user is disconnected
Example:
private void invokeWhenPrimaryUserDisconnectedExample() {
WebswingApi webswingApi = WebswingUtil.getWebswingApi();
if (webswingApi != null) {
webswingApi.invokeWhenPrimaryUserDisconnected(evt -> {
console.append("user " + evt.getUser() + " is disconnected\n");
}, false);
console.append("<disconnect and connect this session again>\n");
}
}
sendActionEvent (1)
public void sendActionEvent(String actionName, String data, byte[] binaryData);
Description:
Sends an action event with optional data to the browser.
Parameters:
binaryData binary data
Example:
private void sendActionEventExample1() {
WebswingApi webswingApi = WebswingUtil.getWebswingApi();
if (webswingApi != null) {
JTextField dataField = new JTextField(25) {{
setText("Hello from Webswing Demo Swing application");
}};
JPanel myPanel = new JPanel() {{
add(new JLabel("Set Data:"));
add(dataField);
}};
int result = JOptionPane.showConfirmDialog(null, myPanel, "Set parameter [String data]", JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION) {
//
// handle incoming action events in javascript in Webswing-Home-Folder\apps\WebswingDemo\webroot\index.html
//
// ...
// customization: function(injector) {
// ...
// injector.services.base.handleActionEvent = function(actionName, data, binaryData) {
// if (actionName == "ApiDemoActionEventTest") {
// console.log("sendActionEvent - " + data);
// alert("sendActionEvent - " + data);
// }
// }
// ...
webswingApi.sendActionEvent("ApiDemoActionEventTest", dataField.getText(), null);
}
}
}
sendActionEvent (2)
public void sendActionEvent(WebWindow webWindow, String actionName, String data, byte[] binaryData);
Description:
Sends an action event to a WebWindow, with optional data to the browser.
Parameters:
binaryData binary data
Example:
private void sendActionEventExample2() {
WebswingApi webswingApi = WebswingUtil.getWebswingApi();
if (webswingApi != null) {
String htmlPanelName = "apiDemoHtmlPanelTest";
//
// components of html panel is created in Webswing-Home-Folder\apps\WebswingDemo\webroot\index.html
//
// ...
// windowOpened: function(win) {
// ...
// if (win.htmlWindow && win.name == "apiDemoHtmlPanelTest") {
// if (win.element.getAttribute("ws-initialized") == "true") {
// return;
// }
// win.element.setAttribute("ws-initialized", "true");
//
// let apiDemoButton = document.createElement("button");
// apiDemoButton.appendChild(document.createTextNode("This is HTML button. Click me!"));
// apiDemoButton.style.margin = "25px";
// apiDemoButton.addEventListener("click", function() {
// win.performAction({actionName: "openApiDemoConfirmDialog"});
// });
//
// let apiDemoResult = document.createElement("div");
// apiDemoResult.style.margin = "25px";
// win.element.appendChild(apiDemoButton);
// win.element.appendChild(apiDemoResult);
//
// win.handleActionEvent = function(name, data, binaryData) {
// ...
// } else if (name == "sendActionEvent") {
// console.log('Your input text: "' + data + '"');
// apiDemoResult.innerHTML = 'Your input text: "' + data + '"';
// }
// }
// }
// ...
JFrame window = new JFrame() {{
setIconImage(getToolkit().getImage(ApiDemoExamples.class.getResource("/logo.png")));
setTitle("createHtmlPanel");
setSize(new Dimension(750, 750));
setPreferredSize(new Dimension(750, 750));
setLocationRelativeTo(null);
setVisible(true);
setResizable(true);
}};
HtmlPanel htmlPanel = webswingApi.createHtmlPanel();
htmlPanel.setName(htmlPanelName);
htmlPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
htmlPanel.setVisible(true);
window.getContentPane().add(htmlPanel);
JTextField dataField = new JTextField(25) {{
setText("Hello from Webswing Demo Swing application");
}};
JPanel myPanel = new JPanel() {{
add(new JLabel("Set Data:"));
add(dataField);
}};
int result = JOptionPane.showConfirmDialog(null, myPanel, "Set parameter [String data]", JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION) {
webswingApi.sendActionEvent(htmlPanel, "sendActionEvent", dataField.getText(), null);
}
}
}
callAction (1)
public CompletableFuture<WebActionResponse> callAction(String actionName, String data, byte[] binaryData);
Description:
Call an action defined in javascript webswing options API. The result of the action will be returned as a result of the returned Future. If the result of the action is a Promise, the Future will complete once the Promise is resolved. The result of the javascript action can return either string or binary data.
Parameters:
binaryData binary data
Returns:
CompletableFuture to handle the async result
Example:
private void callActionExample1() {
WebswingApi webswingApi = WebswingUtil.getWebswingApi();
if (webswingApi != null) {
webswingApi.setActionCallListener(actionEvent -> {
switch (actionEvent.getActionName()) {
case "testActionStringJS": {
console.append("called testActionStringJS\n");
try {
Thread.currentThread().sleep(5000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
return new WebActionResponse(actionEvent.getData(), null);
}
case "testActionBinaryJS": {
console.append("called testActionBinaryJS\n");
return new WebActionResponse(null, actionEvent.getBinaryData());
}
case "testActionEmptyJS": {
console.append("called testActionEmptyJS\n");
return null;
}
}
return null;
});
console.append("calling testActionString defined in index.html, the result should be received in 5 seconds\n");
webswingApi.callAction("testActionString", "testData", null).thenAccept(webActionEvent -> {
console.append("result: " + webActionEvent.getData() + "\n");
console.append("calling testActionBinary defined in index.html, the result should be received immediately\n");
webswingApi.callAction("testActionBinary", null, new byte[] {1, 2, 3}).thenAccept(webActionEvent2 -> {
console.append("result: " + Arrays.toString(webActionEvent2.getBinaryData()) + "\n");
console.append("calling testActionEmpty defined in index.html, the result should be received immediately\n");
webswingApi.callAction("testActionEmpty", null, null).thenAccept(webActionEvent3 -> {
console.append("result: " + ((webActionEvent3.getData() == null && webActionEvent3.getBinaryData() == null) ? "<empty>" : "not empty!") + "\n");
webswingApi.callAction("testCallAction", null, null).thenAccept(wae -> {});
});
});
});
}
}
callAction (2)
public CompletableFuture<WebActionResponse> callAction(WebWindow webWindow, String actionName, String data, byte[] binaryData);
Description:
Call an action defined for a WebWindow in javascript's window 'handleActionCall' property. The result of the action will be returned as a result of the returned Future. If the result of the action is a Promise, the Future will complete once the Promise is resolved. The result of the javascript action can return either string or binary data.
Parameters:
binaryData binary data
Returns:
CompletableFuture to handle the async result
Example:
private void callActionExample2() {
WebswingApi webswingApi = WebswingUtil.getWebswingApi();
if (webswingApi != null) {
JLabel result = new JLabel();
class WebWindowFrame extends JFrame implements WebWindow {
@Override
public WebActionResponse handleWebActionCall(WebActionEvent webActionEvent) {
if ("clear".equals(webActionEvent.getActionName())) {
result.setText("");
return new WebActionResponse("done", null);
}
return null;
}
@Override
public void handleWebActionEvent(WebActionEvent webActionEvent) {
}
@Override
public void handleWindowInitialized() {
}
}
WebWindowFrame window = new WebWindowFrame() {{
setIconImage(getToolkit().getImage(ApiDemoExamples.class.getResource("/logo.png")));
setTitle("callActionExample2");
setName("callActionExample2");
setSize(new Dimension(400, 200));
setLocationRelativeTo(null);
setVisible(true);
setResizable(true);
}};
window.setVisible(true);
JPanel panel = new JPanel();
BoxLayout box = new BoxLayout(panel, BoxLayout.Y_AXIS);
panel.setLayout(box);
panel.setBorder(new EmptyBorder(10, 10, 10, 10));
JLabel label = new JLabel("<html>Click the button to show a javascript confirm. The result will show in this window. After 5 seconds, javascript calls Java to clear the result.</html>");
JButton button = new JButton("confirm");
button.addActionListener(e -> {
window.callAction("confirm", null, null).thenAccept(wae -> {
result.setText("Result: " + wae.getData());
});
});
panel.add(label);
panel.add(Box.createRigidArea(new Dimension(0, 10)));
panel.add(button);
panel.add(Box.createRigidArea(new Dimension(0, 10)));
panel.add(result);
window.getContentPane().add(panel, BorderLayout.CENTER);
}
}
setConsentListener
public void setConsentListener(ConsentListener consentListener);
Description:
Set consent listener for controlling the recording and mirroring consent flow.
Parameters:
consentListener the listener instance
Example:
private void setConsentListenerExample() {
WebswingApi webswingApi = WebswingUtil.getWebswingApi();
if (webswingApi != null) {
webswingApi.setConsentListener(new ConsentListener() {
private boolean showingRecordingDialog = false;
private boolean showingMirroringDialog = false;
@Override
public void recordingStarted() {
console.append("Recording started\n");
}
@Override
public void recordingFinished() {
console.append("Recording finished\n");
}
@Override
public void mirroringStarted() {
console.append("Mirroring started\n");
}
@Override
public void mirroringFinished() {
console.append("Mirroring finished\n");
}
@Override
public boolean beforeRecordingConsentDialogShowed(ConsentEvent event) {
//
// Require Recording Consent in Application web config must be true
//
if (showingRecordingDialog) {
return false;
}
SwingUtilities.invokeLater(() -> {
showingRecordingDialog = true;
int result = JOptionPane.showOptionDialog(null, "Custom message\nDo you want to allow?", "Custom Recording consent", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, new String[] { "Yes", "No" }, "Yes");
showingRecordingDialog = false;
webswingApi.resolveRecordingConsent(result == JOptionPane.YES_OPTION);
});
return false;
}
@Override
public boolean beforeMirroringConsentDialogShowed(ConsentEvent event) {
//
// Require Mirroring Consent in Application web config must be true
//
if (showingMirroringDialog) {
return false;
}
SwingUtilities.invokeLater(() -> {
showingMirroringDialog = true;
int result = JOptionPane.showOptionDialog(null, "Custom message\nDo you want to allow?", "Custom Mirroring consent", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, new String[] { "Yes", "No" }, "Yes");
showingMirroringDialog = false;
webswingApi.resolveMirroringConsent(result == JOptionPane.YES_OPTION);
});
return false;
}
});
console.append("Consent listener was set\n");
}
}