User Related Services
getDimensionOnUserScreen
public Dimension getDimensionOnUserScreen(Window window);
Description:
Get the outer dimensions of browser window on user's screen where the window is being rendered. Returns null if window parameter is not provided, if the window is not being rendered or if Webswing has not yet been initialized on client side. Multi screen support must be enabled for this method to return correct information.
Parameters:
window get user screen dimension of browser window where this window is rendered
Returns:
outer dimension of browser window
Example:
private void getDimensionOnUserScreenExample() {
WebswingApi webswingApi = WebswingUtil.getWebswingApi();
if (webswingApi != null) {
JFrame window = new JFrame() {{
setIconImage(getToolkit().getImage(ApiDemoExamples.class.getResource("/logo.png")));
setTitle("getDimensionOnUserScreen");
setSize(new Dimension(750, 750));
setLocationRelativeTo(null);
setVisible(true);
setResizable(true);
}};
window.setVisible(true);
JTextArea info = new JTextArea();
info.setEditable(false);
window.getContentPane().add(new WJPanel() {{
setBorder(new EmptyBorder(10, 10, 10, 10));
setLayout(new BorderLayout(10, 10));
add(info, BorderLayout.CENTER);
}});
Dimension dim = webswingApi.getDimensionOnUserScreen(window);
if (dim != null) {
info.setText("Dimension on user screen is : [" + dim.width + "x" + dim.height + "]");
} else {
info.setText("No data");
}
}
}
getLocationOnUserScreen
public Point getLocationOnUserScreen(Window window);
Description:
Returns the position of browser window on user's screen where the window is being rendered. Returns null if window parameter is not provided, if the window is not being rendered or if Webswing has not yet been initialized on client side.
Parameters:
window get location of browser window on user's screen for this window
Returns:
location of the browser window
Example:
private void getLocationOnUserScreenExample() {
WebswingApi webswingApi = WebswingUtil.getWebswingApi();
if (webswingApi != null) {
JFrame window = new JFrame() {{
setIconImage(getToolkit().getImage(ApiDemoExamples.class.getResource("/logo.png")));
setTitle("getLocationOnUserScreen");
setSize(new Dimension(750, 750));
setLocationRelativeTo(null);
setVisible(true);
setResizable(true);
}};
window.setVisible(true);
JTextArea info = new JTextArea();
info.setEditable(false);
window.getContentPane().add(new WJPanel() {{
setBorder(new EmptyBorder(10, 10, 10, 10));
setLayout(new BorderLayout(10, 10));
add(info, BorderLayout.CENTER);
}});
Point p = webswingApi.getLocationOnUserScreen(window);
if (p != null) {
info.setText("Location on user screen is : [" + p.x + "," + p.y + "]");
} else {
info.setText("No data");
}
}
}
setLocationOnUserScreen
public void setLocationOnUserScreen(Window window, Point location);
Description:
Sets the position of browser window on user's screen where the window is being rendered. This only changes the location of browser window and not the real location of Swing window. This only works for undocked windows.
Parameters:
location location on user's screen where the window should move to
Example:
private void setLocationOnUserScreenExample() {
WebswingApi webswingApi = WebswingUtil.getWebswingApi();
if (webswingApi != null) {
int xPoint;
int yPoint;
JTextField xField = new JTextField(5) {{
setText("250");
}};
JTextField yField = new JTextField(5) {{
setText("250");
}};
JPanel myPanel = new JPanel();
myPanel.add(new JLabel("x:"));
myPanel.add(xField);
myPanel.add(Box.createHorizontalStrut(15)); // a spacer
myPanel.add(new JLabel("y:"));
myPanel.add(yField);
int result = JOptionPane.showConfirmDialog(null, myPanel, "Set parameter [Point location]", JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION) {
try {
xPoint = Integer.valueOf(xField.getText());
yPoint = Integer.valueOf(yField.getText());
JFrame window = new JFrame() {{
setIconImage(getToolkit().getImage(ApiDemoExamples.class.getResource("/logo.png")));
setTitle("setLocationOnUserScreen");
setSize(new Dimension(500, 250));
setLocation(new Point(500, 500));
setResizable(false);
}};
if (webswingApi.isDockingEnabled(window)) {
window.setVisible(true);
JTextArea info = new JTextArea();
info.setEditable(false);
window.getContentPane().add(new WJPanel() {{
setBorder(new EmptyBorder(10, 10, 10, 10));
setLayout(new BorderLayout(10, 10));
add(info, BorderLayout.CENTER);
add(new JButton("Move this window to new location [" + xPoint + ", " + yPoint + "]") {{
addActionListener(event -> {
info.setText("");
if (webswingApi.isUndocked(window)) {
webswingApi.setLocationOnUserScreen(window, new Point(xPoint, yPoint));
} else {
info.append("Window is docked\nThis only works for undocked windows\nPlease undock this window (right corner)\n");
}
});
setPreferredSize(new Dimension(250, 35));
}}, BorderLayout.PAGE_START);
}});
webswingApi.toggleWindowDock(window);
} else {
window.setVisible(false);
console.append("Window undocking is disabled\nYou can change the Docking Mode settings in the application configuration\n");
}
} catch (NumberFormatException e) {
console.append("NumberFormatException: " + e.getMessage());
}
}
}
}
getMirrorViewUser
public WebswingUser getMirrorViewUser();
Description:
Return the user of connected mirror view web session. Mirroring App is located in - Admin console - Session - Current App - Mirror view. Note: if admin disconnects/closes browser, this method will return null.
Returns:
user or null if mirror view session is disconnected.
Example:
private void getMirrorViewUserExample() {
WebswingApi webswingApi = WebswingUtil.getWebswingApi();
if (webswingApi != null) {
console.append("Mirror View User is :\n");
if (webswingApi.getMirrorViewUser() == null) {
console.append("null");
} else {
console.append(webswingApi.getMirrorViewUser().getUserId());
}
console.append("\n");
}
}
getPrimaryUser
public WebswingUser getPrimaryUser();
Description:
Return the user of connected web session. Note: if user disconnects/closes browser, this method will return null.
Returns:
user or null if session is disconnected.
Example:
private void getPrimaryUserExample() {
WebswingApi webswingApi = WebswingUtil.getWebswingApi();
if (webswingApi != null) {
WebswingUser wu = webswingApi.getPrimaryUser();
console.append("Primary User is :\n");
console.append("User ID: (Unique user id)\n");
console.append(wu.getUserId() + "\n");
console.append("Attributes: (map of user attributes specific to configured security module implementation.)\n");
console.append(wu.getUserAttributes().toString() + "\n");
console.append("Roles: (list user roles)\n");
for (String role : wu.getUserRoles()) {
console.append(role + "\n");
}
WebswingUserInfoDialog.open();
}
}
getPrimaryUserIPAddress
public String getPrimaryUserIPAddress();
Description:
Returns the IP address of the user of connected web session.
Returns:
IP address
Example:
private void getPrimaryUserIPAddressExample() {
WebswingApi webswingApi = WebswingUtil.getWebswingApi();
if (webswingApi != null) {
console.append("Primary User IP Address is :\n");
console.append(webswingApi.getPrimaryUserIPAddress() + "\n");
}
}
primaryUserHasRole
public Boolean primaryUserHasRole(String role) throws WebswingApiException;
Description:
Check if connected web session user has role defined. If no user is connected null is returned.
Parameters:
role name of role
Returns:
True if user has the role, false if not. Null if no user is connected
Throws:
WebswingApiException if communication with server fails.
Example:
private void primaryUserHasRoleExample() {
WebswingApi webswingApi = WebswingUtil.getWebswingApi();
if (webswingApi != null) {
String user = webswingApi.getPrimaryUser().getUserId();
String[] roles = new String[] { "test", "admin" };
Boolean hasRole = null;
console.append("Webswing User Role Check\n");
for (int i = 0; i < roles.length; i++) {
String role = roles[i];
console.append("Example " + (i + 1) + "\n");
console.append("User : " + user + "\n");
console.append("Role to check : " + role + "\n");
try {
hasRole = webswingApi.primaryUserHasRole(role);
} catch (WebswingApiException error) {
displayErrorMessage("Failed to resolve role:", error);
}
console.append(hasRole == null ? "No user is connected.\n" : hasRole ? "User " + user + " has role " + role + "\n" : "User " + user + " does not have role " + role + "\n");
}
WebswingHasUserRoleDialog.open(JOptionPane.showInputDialog("Role:"));
}
}
primaryUserIsPermitted
public Boolean primaryUserIsPermitted(String permission) throws WebswingApiException;
Description:
Check if connected web session user has permission defined. If no user is connected null is returned.
Parameters:
permission name of permission
Returns:
True if user has the permission, false if not. Null if no user is connected
Throws:
WebswingApiException if communication with server fails.
Example:
private void primaryUserIsPermittedExample() {
WebswingApi webswingApi = WebswingUtil.getWebswingApi();
if (webswingApi != null) {
String user = webswingApi.getPrimaryUser().getUserId();
String[] permissions = new String[] { "test", "admin" };
Boolean permitted = null;
console.append("Webswing User Permission Check\n");
for (int i = 0; i < permissions.length; i++) {
String permission = permissions[i];
console.append("Example " + (i + 1) + "\n");
console.append("User : " + user + "\n");
console.append("Permission to check : " + permission + "\n");
try {
permitted = webswingApi.primaryUserIsPermitted(permission);
} catch (WebswingApiException error) {
displayErrorMessage("Failed to resolve permission:", error);
}
console.append(permitted == null ? "No user is connected.\n" : permitted ? "User " + user + " is permitted to do " + permission + "\n" : "User " + user + " is not permitted to do " + permission + "\n");
}
WebswingIsUserPermittedDialog.open(JOptionPane.showInputDialog("Permission:"));
}
}
getUserScreenInfo (1)
public List<UserScreen> getUserScreenInfo();
Description:
Get the information about user's screens. In case user has multiple screens, returns information about all screens, if supported by browser. This may return empty list if Webswing has not yet been initialized on client side. UserScreen values are based on data provided by browser according to W3C specification. Multi screen support must be enabled for this method to return correct information.
Returns:
list of information about user's screens or empty list if Webswing is not initialized
Example:
private void getUserScreenInfoExample1() {
WebswingApi webswingApi = WebswingUtil.getWebswingApi();
if (webswingApi != null) {
if (WebswingUtil.getWebswingApi() != null) {
console.append("User Screen Info is :\n");
List<UserScreen> list = WebswingUtil.getWebswingApi().getUserScreenInfo();
if (list.isEmpty()) {
console.append("No data\n");
} else {
for (UserScreen userScreen : WebswingUtil.getWebswingApi().getUserScreenInfo()) {
console.append(userScreen.getLabel() + "\n");
}
}
}
}
}
getUserScreenInfo (2)
public WindowUserScreenInfo getUserScreenInfo(Window window);
Description:
Get the information about browser window on user's screens where the particular Swing Window is rendered. This may return null if Webswing has not yet been initialized on client side. Multi screen support must be enabled for this method to return correct information.
Parameters:
window get the user screen information where this window is rendered
Returns:
information about browser window on user's screens or null if Webswing is not initialized
Example:
private void getUserScreenInfoExample2() {
WebswingApi webswingApi = WebswingUtil.getWebswingApi();
if (webswingApi != null) {
JFrame window = new JFrame() {{
setIconImage(getToolkit().getImage(ApiDemoExamples.class.getResource("/logo.png")));
setTitle("getUserScreenInfo");
setSize(new Dimension(750, 750));
setLocationRelativeTo(null);
setVisible(true);
setResizable(true);
}};
window.setVisible(true);
JTextArea info = new JTextArea();
info.setEditable(false);
window.getContentPane().add(new WJPanel() {{
setBorder(new EmptyBorder(10, 10, 10, 10));
setLayout(new BorderLayout(10, 10));
add(info, BorderLayout.CENTER);
}});
WindowUserScreenInfo windowUserScreenInfo = webswingApi.getUserScreenInfo(window);
if (windowUserScreenInfo != null) {
info.setText("THIS screen: " + windowUserScreenInfo.getUserScreen().getLabel() + ", [" + windowUserScreenInfo.getUserScreen().getAvailLeft() + ", " + windowUserScreenInfo.getUserScreen().getAvailTop() + ", " + windowUserScreenInfo.getUserScreen().getAvailWidth() + ", " + windowUserScreenInfo.getUserScreen().getAvailHeight() + "], "
+ "angle: " + windowUserScreenInfo.getUserScreen().getAngle() + ", " + windowUserScreenInfo.getUserScreen().getOrientation());
} else {
info.setText("No data");
}
}
}
getUserScreenSize
public Dimension getUserScreenSize(Window window);
Description:
Get the client's screen size. In case user has multiple screens, returns size of screen where the window is currently being rendered. When window parameter is not provided, the default user screen size is returned. This may return null if Webswing has not yet been initialized on client side. Multi screen support must be enabled for this method to return correct information.
Parameters:
window get the user screen size where this window is rendered
Returns:
dimension of user screen where the window is rendered or default user screen dimension if window is not provided or null if Webswing is not initialized
Example:
private void getUserScreenSizeExample() {
WebswingApi webswingApi = WebswingUtil.getWebswingApi();
if (webswingApi != null) {
JFrame window = new JFrame() {{
setIconImage(getToolkit().getImage(ApiDemoExamples.class.getResource("/logo.png")));
setTitle("getUserScreenSize");
setSize(new Dimension(750, 750));
setLocationRelativeTo(null);
setVisible(true);
setResizable(true);
}};
window.setVisible(true);
JTextArea info = new JTextArea();
info.setEditable(false);
window.getContentPane().add(new WJPanel() {{
setBorder(new EmptyBorder(10, 10, 10, 10));
setLayout(new BorderLayout(10, 10));
add(info, BorderLayout.CENTER);
}});
Dimension dimension = webswingApi.getUserScreenSize(window);
if (dimension != null) {
info.setText("User screen size is : [" + dimension.width + "x" + dimension.height + "]");
} else {
info.setText("No data");
}
}
}